Fabio
Fabio

Reputation: 336

Implicit private control loop variable

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?

Upvotes: 2

Views: 172

Answers (1)

dreamcrash
dreamcrash

Reputation: 51393

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.

Upvotes: 3

Related Questions