Reputation: 249
I have the following class:
Class L{
public:
bool foo(vector<bool> & data);
private:
C** cArray;
}
and would like to parallelize the for loop in the function foo which is called somtime after an object of L is created and all the elements in cArray are initialized.
bool L::foo(vector<int> & data){
int row, col;
#pragma omp parallel shared(SIZE, cArray, data) private(row, col)
for (row=0, row<SIZE; ++row)
{
for (col=0; col<SIZE; ++col)
{
cArray[row][col].computeScore(data);
}
}
}
But this gives an error: error C3028: 'L::cArray' : only a variable or static data member can be used in a data-sharing clause.
Is there anything that can be done about this assuming I don't want to make cArray static?
Upvotes: 5
Views: 3526
Reputation: 33
You can use C++ 11 thread_local like following. Then it should all work as intended.
// .h
Class L{
public:
bool foo(vector<bool> & data);
private:
static thread_local C** cArray;
}
// .cpp
thread_local C** cArray;
Upvotes: 0
Reputation: 1757
This question has come up several times before. The problem is, that class data members may not be instantiated at compile time. If they are being shared, then there is no problem, because variables are shared by default in OpenMP (unless you change the default to private - which you can't do in C - or none). However, if they are defined as private, then the compiler needs to know how to make private copies and this information is not always available at compile time.
Unfortunately, if you want to scope all your data (using explicit data scoping clauses), which you should, then you have a problem. The scoping clauses can only handle variables - which class data members aren't. Leaving them off any data scoping clause works as long as the default remains shared. If you want them to be private, then you are out of luck and need to define the class data members as variables. Unfortunately, since OpenMP is not part of the base language, I don't see this changing anytime soon.
Upvotes: 9