5

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?

6
  • Can you show where SIZE is declared?
    – Mr Fooz
    Commented May 5, 2011 at 1:22
  • 1
    Only variables can be specified in data sharing clauses. Don't put it into a clause and it should be shared by default and should work.
    – ejd
    Commented May 5, 2011 at 1:31
  • @Mr Fooz - SIZE is declared as const in a included .h file.
    – ryguy
    Commented May 5, 2011 at 1:53
  • @ejd - so "#pragma omp parallel private(row, col)" would leave SIZE,cArray and data as shared by default?
    – ryguy
    Commented May 5, 2011 at 1:58
  • There is inconsistency between the declaration and the definition of L::foo: you use vector<bool> in the former and vector<int> in the latter. Commented May 5, 2011 at 12:34

2 Answers 2

9

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.

0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.