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).
Z boson May 27 '14 at 14:25 2014-05-27 14:25
source share