Is there a difference between variables in a private sentence and variables defined in a parallel region in OpenMP?

I was wondering if there is any reason to prefer the private(var) clause in OpenMP over the local definition of (private) variables, e.g.

 int var; #pragma omp parallel private(var) { ... } 

against

 #pragma omp parallel { int var; ... } 

Also, I wonder what the meaning of private reservations is then. This question was partially explained in OpenMP: are local variables automatically closed? , but I donโ€™t like the answer, since even C89 does not forbid you to define variables in the middle of functions if they are at the beginning of the region (which automatically happens when you enter the parallel region). So even for old-fashioned C programmers, this shouldn't matter. Should I consider this syntactic sugar, which allows the style of "defining variables at the beginning of your function", as it was in the good old days?

By the way: in my opinion, the second version also prohibits programmers from using private variables after a parallel area in the hope that it can contain something useful, for example, another -1 for a private sentence.

But since I'm completely new to OpenMP, I don't want to doubt without a good explanation for this. Thanks in advance for your answers!

+5
c multithreading parallel-processing openmp
May 27 '14 at 13:18
source share
1 answer

It is not just syntactic sugar. One of the features of OpenMP is to not change the serial code if the code is not compiled using OpenMP. Any construct that you use as part of a pragma is ignored unless you compile it using OpenMP. You can use things like private , firstprivaate , collapse and parallel for without changing the code. Changing the code can affect, for example, how the code is optimized by the compiler.

If you have a type code

 int i,j; #pragma omp parallel for private(j) for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { } } 

The only way to do this without private in C89 is to change the code by specifying j inside the parallel section, for example:

 int i,j; #pragma omp parallel { int j; #pragma omp for for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { } } } 

Here is a C ++ example with firstprivate . Say you have a vector that you want to be private. If you use firstprivate , you do not need to change the code, but if you declare a personal copy within the parallel region, you will change your code. If you compile this without OpenMP, it will make an unnecessary copy.

 vector<int> a; #pragma omp parallel { vector<int> a_private = a; } 

This logic applies to many other designs. For example collapse . You can manually plan a loop that modifies your code, or you can use collapse and only compile it when compiling with OpenMP.

However, having said all this, in practice I often find that I need to change the code in any case to get the best parallel result, so I usually define everything in parallel sections anyway and donโ€™t use functions like private , firstprivate or collapse (not to mention that the OpenMP implementation in C ++ often works with non-POD anyway , so it is often better to do it yourself).

+8
May 27 '14 at 14:25
source share



All Articles