How are firstprivate and lastprivate different from private clauses in OpenMP?

I looked through the official definitions, but I'm still pretty confused.

firstprivate : indicates that each stream should have its own instance of the variable and that the variable should be initialized with the value of the variable since it exists before the parallel construction.

For me it is very similar to personal. I was looking for examples, but I don't seem to understand how this is special or how it can be used.

lastprivate : Indicates that the nested version of the variable context is set to the private version of any thread that performs the final iteration (for-loop construct) or the last section (#pragma sections).

I think I understand this a little better due to the following example:

 #pragma omp parallel { #pragma omp for lastprivate(i) for (i=0; i<n-1; i++) a[i] = b[i] + b[i+1]; } a[i]=b[i]; 

So, in this example, I understand that lastprivate allows i to be returned outside the loop as the last value.

I just started learning OpenMP today.

+44
openmp
Mar 08 '13 at 22:59
source share
2 answers

private variables are not initialized, i.e. they start with random values, like any other local automatic variable (and they are often implemented using automatic variables in the stack of each thread). Take this simple program as an example:

 #include <stdio.h> #include <omp.h> int main (void) { int i = 10; #pragma omp parallel private(i) { printf("thread %d: i = %d\n", omp_get_thread_num(), i); i = 1000 + omp_get_thread_num(); } printf("i = %d\n", i); return 0; } 

With four threads, it outputs something like:

 thread 0: i = 0 thread 3: i = 32717 thread 1: i = 32717 thread 2: i = 1 i = 10 (another run of the same program) thread 2: i = 1 thread 1: i = 1 thread 0: i = 0 thread 3: i = 32657 i = 10 

This clearly demonstrates that the value of i is random (not initialized) inside the parallel region and that any modifications of it are not visible after the parallel region (i.e., the variable retains its value before entering the region).

If i done firstprivate , then it is initialized with the value that it has before the parallel area:

 thread 2: i = 10 thread 0: i = 10 thread 3: i = 10 thread 1: i = 10 i = 10 

After it, changes in the value of i inside the parallel region are still not visible.

You already know about lastprivate (and it is not applicable to a simple demo program, since it does not have any constructions for collaboration).

So yes, firstprivate and lastprivate are only special cases of private . The first result leads to the fact that the values ​​from the external context are brought to the parallel region, and the second transfers the values ​​from the parallel region to the external context. The rationale for these data sharing classes is that within a parallel region, all private variables obscure those from external contexts, that is, it is impossible to use the assignment operation to change the external value of i from within the parallel region.

+98
Mar 09 '13 at 10:13
source share

firstprivate and lastprivate are only special cases of private .

The first leads to the conversion of values ​​from the external context to the parallel region, while the second transfers the values ​​from the parallel region to the external context.

+1
Jan 20 '17 at 10:10
source share



All Articles