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

Jonathan Dursi
  • 50,107
  • 9
  • 127
  • 158
Eagle
  • 3,362
  • 5
  • 34
  • 46
  • 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. – Konrad Rudolph Jun 29 '11 at 14:31

1 Answers1

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.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • 1
    assuming i have an array `boost::multi_array 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? – Eagle Jun 29 '11 at 15:07
  • 1
    Of course. This is a schoolbook example of the use of an atomic operation or the like. – Constantinius Jun 29 '11 at 15:18
  • 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... – Eagle Jul 01 '11 at 09:56