Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

openmp

I've looked at the official definitions, but I'm still quite confused.

firstprivate: Specifies that each thread should have its own instance of a variable, and that the variable should be initialized with the value of the variable, because it exists before the parallel construct.

To me, that sounds a lot like private. I've looked for examples, but I don't seem to understand how it's special or how it can be used.

lastprivate: Specifies that the enclosing context's version of the variable is set equal to the private version of whichever thread executes the final iteration (for-loop construct) or last section (#pragma sections).

I feel like I understand this one a bit better because of 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 for i to be returned outside of the loop as the last value it was.

I just started learning OpenMP today.

like image 243
SaiyanGirl Avatar asked Mar 08 '13 22:03

SaiyanGirl


People also ask

What does Firstprivate do in OpenMP?

firstprivate. Specifies that each thread should have its own instance of a variable, and that the variable should be initialized with the value of the variable, because it exists before the parallel construct.

What is shared and private in OpenMP?

A variable in an OpenMP parallel region can be either shared or private. If a variable is shared, then there exists one instance of this variable which is shared among all threads. If a variable is private, then each thread in a team of threads has its own local copy of the private variable.

What are the scoping clauses in OpenMP?

Declaring the scope attributes of variables in an OpenMP parallel region is called scoping . In general, if a variable is scoped as SHARED , all threads share a single copy of the variable. If a variable is scoped as PRIVATE , each thread has its own copy of the variable. OpenMP has a rich data environment.


1 Answers

private variables are not initialised, i.e. they start with random values like any other local automatic variable (and they are often implemented using automatic variables on 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 initialised) inside the parallel region and that any modifications to it are not visible after the parallel region (i.e. the variable keeps its value from before entering the region).

If i is made firstprivate, then it is initialised with the value that it has before the parallel region:

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

Still modifications to the value of i inside the parallel region are not visible after it.

You already know about lastprivate (and it is not applicable to the simple demonstration program as it lacks worksharing constructs).

So yes, firstprivate and lastprivate are just special cases of private. The first one results in bringing in values from the outside context into the parallel region while the second one transfers values from the parallel region to the outside context. The rationale behind these data-sharing classes is that inside the parallel region all private variables shadow the ones from the outside context, i.e. it is not possible to use an assignment operation to modify the outside value of i from inside the parallel region.

like image 143
Hristo Iliev Avatar answered Oct 13 '22 23:10

Hristo Iliev