Multithreading with C++ API
Asked Answered
S

1

6

i am trying to parallel my program using OpenMP and sometimes i feels that i am reaching a dead end.

I would like to share variables in a function member that i defined (and initialized) in the class. If i understood correctly, it is not possible doing #pragma omp parallel shared(foo) of data members (e.g. int, boost::multi_array and std::vector) of a class. e.g.: using push_back() on a vector data member in the class. updating values of a boost::multi_array.

My question is if OpenMP is the right tools for it, or should i use boost::thread or tbb? or something else... what support C++ API

Reagrds

Schnurr answered 29/6, 2011 at 9:35 Comment(1)
Just a FYI, the shared clause is actually redundant – any variable declared outside the parallel block that is not declared as private (or any of the other options) is automatically shared.Elwaine
C
2

As the documentation states, shared defines that an object is placed only once in the memory. For example if your foo object contains a std::vector of some type, it should be perfectly ok to push_back items within the loop. But you should make sure, that your code is thread safe, either by atomic instructions or with mutex sections.

Ci answered 29/6, 2011 at 14:28 Comment(3)
assuming i have an array boost::multi_array<int,1> foo(boost::extents[10]) and then: #pragma omp parallel for for(int i = 0;i<10;i++) foo[i] = i; will i need before accessing foo atomic or mutex as well?Schnurr
Of course. This is a schoolbook example of the use of an atomic operation or the like.Ci
i dont agree with your claim that it is clear. Maybe i havent explain my self clearly. Assuming you have an array with 10 elements. The 1st thread will work only on the first 5 elements and the 2nd thread on the last 5 elements. Do i then need mutex? since each element stand for it self...Schnurr

© 2022 - 2024 — McMap. All rights reserved.