I was wondering if there is any reason for preferring the private(var)
clause in OpenMP over the local definition of (private) variables, i.e.
int var;
#pragma omp parallel private(var)
{
...
}
vs.
#pragma omp parallel
{
int var;
...
}
Also, I'm wondering what's the point of private clauses then. This question was partially explained in OpenMP: are local variables automatically private?, but I don't like the answer since even C89 doesn't bar you from defining variables in the middle of functions as long as they are in the beginning of a scope (which is automatically the case when you enter a parallel region). So even for old-fashion C-programmers this shouldn't make any difference. Should I consider this as syntactic sugar that allows for a "define-variables-in-the-beginning-of-your-function" style as used in the good old days?
By the way: In my opinion the second version also prevents programmers from using private variables after the parallel region in the hope that it may contain something useful so another -1 for the private clause.
But since I'm quite new to OpenMP I don't want to question anything without having a good explanation for it. Thanks in advance for your answers!
It's not just syntactic sugar. One of the features of OpenMP strives for is to not change the serial code if the code is not compiled with OpenMP. Any construct you use as part of a pragma is ignored if you don't compile with OpenMP. Doing this you can use things like private
, firstprivaate
, collapse
, and parallel for
without changing your code. Changing the code can affect for example how the code is optimized by the compiler.
If you have code like
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 defining j
inside the parallel section e.g:
int i,j;
#pragma omp parallel
{
int j;
#pragma omp for
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
}
}
}
Here's a C++ example with firstprivate
. Let's say you have a vector which you want to be private. If you use firstprivate
you don't have to change your code but if you declare a private copy inside the parallel region you do change your code. If you compile that without OpenMP it makes a unnecessary copy.
vector<int> a;
#pragma omp parallel {
vector<int> a_private = a;
}
This logic applies to many other constructs. For example collapse
. You could manually fuse a loop which changes your code or you can use collapse
and only fuse it when compiled with OpenMP.
However, having said all that, in practice I find often that I need to change the code anyway to get the best parallel result so I usually define everything in parallel sections anyway and don't use features such as private
, firstprivate
, or collapse
(not to mention that OpenMP implementations in C++ often struggle with non-POD anyway so it's often better to do it yourself).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With