简体   繁体   中英

Why is class member variable x not allowed to be shared(x) in OpenMP?

In a member function, I can parallelize using the shared member variable int *x like this

#pragma omp parallel for default(shared)
for(int i=0;i<size;i++) {
  x[i]=i;
}

But if I try

#pragma omp parallel for default(none) shared(x,size)
for(int i=0;i<size;i++) {
  x[i]=i;
}

I get the error: 'obj::x' is not a variable in clause 'shared' . I would prefer the second version because it announces the shared variables it is working with, reminding me to make sure there are no race conditions or similar problems.

What is going on that OpenMP claims that obj::x is not a variable?

Most implementations of OpenMP outline the parallel region. That is, they make it a function. Private variables are generally passed to this function and shared variables may be passed or be within the function's scope. The problem with class data members is that they are not the same as variables.

When the compiler outlines a parallel region, variables have storage locations defined that the compiler can set up to pass to the function. Data members may not be instantiated (ie, storage allocated) until the class is called during execution of the program. This means that the compiler cannot privatize data members by itself. It would also have to be done in the runtime and this would cause a lot more work and would affect performance of both serial and parallel programs. So far no implementation has tried to do this work and since the OpenMP spec is written by consensus the decision was made to disallow data members in all clauses. Otherwise, it seemed too confusing to say that they are allowed in shared clauses, but no other clause.

不知道 - 但是请看看32个OpenMP陷阱,以便C ++开发人员解决大多数OpenMP问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM