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.
Hristo Iliev Mar 09 '13 at 10:13 2013-03-09 10:13
source share