Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data members in an OpenMP loop

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?

like image 390
ryguy Avatar asked May 05 '11 01:05

ryguy


People also ask

Are variables shared by default OpenMP?

A variable in an OpenMP parallel region can be either shared or private. If a variable is shared, then there exists one instance of this variable which is shared among all threads. If a variable is private, then each thread in a team of threads has its own local copy of the private variable.

What is Firstprivate OpenMP?

firstprivate. Specifies that each thread should have its own instance of a variable, and that the variable should be initialized with the value of the variable, because it exists before the parallel construct. lastprivate.

How many iterations are executed if four threads execute the above program?

How many iterations are executed if four threads execute the above program? 25, as the loop is split among the four threads.

What is shared variable and private variable used in OpenMP?

A shared variable has the same address in the execution context of every thread. All threads have access to shared variables. A private variable has a different address in the execution context of every thread.


1 Answers

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.

like image 186
ejd Avatar answered Sep 30 '22 06:09

ejd