I was wondering if there is any reason for preferring the private(var)
clause in OpenMP over the local definition of (private) variables, e.g.
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 already explained in OpenMP: are local variables automatically private?, but I'm convinced that the answer is wrong 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 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 doubt things without having good explanations for it. Thanks in advance for 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
, andparallel 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
The only way to do this without
private
inC89
is to change the code by definingj
inside the parallel section e.g:Here's a C++ example with
firstprivate
. Let's say you have a vector which you want to be private. If you usefirstprivate
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.This logic applies to many other constructs. For example
collapse
. You could manually fuse a loop which changes your code or you can usecollapse
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
, orcollapse
(not to mention that OpenMP implementations in C++ often struggle with non-POD anyway so it's often better to do it yourself).