Gokul
Gokul

Reputation: 139

OpenMP, calling global variables, through functions

While implementing the following code:

#pragma omp parallel for default(shared) private(rx,ry,a, Xmax,Ymax)
for (a=0;a<30000;a++)
{int mn,mnn;
mn=part.i;
mnn=part.j;
setup(mn,mnn,a);
}

The function setup uses global variable rx, ry, Xmax, and Ymax. Does Open MP notice these to be declared as private? Or what happens if global variables are called in a function which is present in a loop?

Upvotes: 0

Views: 4926

Answers (1)

Z boson
Z boson

Reputation: 33679

Global variables used in a function will be shared unless you use threadprivate. So rx,ry, Xmax,Ymax will not be private if you call them in a function unless you use threadprivate.

Try the following code with and without threadprivate and see the results.

#include <stdio.h>
int x;
//#pragma omp threadprivate(x)

void foo() {
    printf("%p\n", &x);
}

int main() {
    //#pragma omp parallel
    #pragma omp parallel private(x)
    {
        printf("%p\n", &x);
        foo();
    }
}

See the OpenMP specificiation under "Data-sharing Attribute Rules for Variables Referenced in a Region but not in a Construct"

File-scope or namespace-scope variables referenced in called routines in the region are shared unless they appear in a threadprivate directive.

Upvotes: 2

Related Questions