2

I have a doubt and I did not find the correct response in the OpenMP documentation. If I have a loop like this:

int i;

#pragma omp parallel for
for(i=0;i<10;i++)
    //do some stuff

Is the variable i implicit private, am I right? Or i have to define as private , like #pragma omp parallel for private(i)?

Instead if I have a loop like this:

int i,j;

#pragma omp parallel for
for(i=0;i<10;i++)
    for(j=i+1;j<10;j++)
        //do some stuff

Only the variable i will be implicit private, and the variable j will be shared because by default OpenMP "forces" as private only the control loop variable of the underlying loop, am I right?

Can I have some references to confirm these assumptions?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Fabio
  • 336
  • 3
  • 17
  • 1
    If you were to move into the 1990s in your coding style, you could place these variable declarations in their minimum required scope, then there is no need to worry about this, since the inner loop control variables will be declared inside the parallel region... – Jim Cownie Apr 14 '21 at 07:44

1 Answers1

3

Is the variable i implicit private, am I right? Or i have to define as private , like #pragma omp parallel for private(i)?

Yes, the OpenMP standard ensures that such variable is implicitly private.

int i,j;

#pragma omp parallel for
for(i=0;i<10;i++)
    for(j=i+1;j<10;j++)

Only the variable i will be implicit private, and the variable j will be shared because by default OpenMP "forces" as private only the control loop variable of the underlying loop, am I right?

Yes, you are right. However, if you were using #pragma omp parallel for collapse(2), both variables i and j would be private.

Can I have some references to confirm these assumptions?

Yes from the OpenMP 5.1 standard section 2.11.9 Loop Transformation Constructs:

Loop iteration variables of generated loops are always private in the enclosing teams, parallel, simd, or task generating construct.

and also in 2.21.1.1 Variables Referenced in a Construct

The loop iteration variable in any associated loop of a for, parallel for, taskloop, or distribute construct is private.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117