Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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?

like image 323
Riko Jacob Avatar asked Jan 06 '11 00:01

Riko Jacob


People also ask

Are variables shared by default OpenMP?

The data-sharing attribute of variables, which are declared outside the parallel region, is usually shared. Therefore, n and a are shared variables. The loop iteration variables, however, are private by default.

What is the difference between private and shared clauses 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. Example.

How does OpenMP provide a shared memory programming environment?

OpenMP Programming Model A shared memory process consists of multiple threads. OpenMP is an explicit, none-automatic, programming model which offers the programmer full control over parallelization. When a thread reaches a PARALLEL directive, it creates a team of threads and becomes the master of the team.

Which variables are shared variables in Java?

Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.


1 Answers

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 (i.e., 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.

like image 57
ejd Avatar answered Sep 28 '22 18:09

ejd