Home > other >  Default Variable Scope in OpenMP
Default Variable Scope in OpenMP

Time:01-14

I am learning OpenMP in C programming and trying to understand the default scope of variables in different cases.

Given this code, I am a bit confused about the scope of pointers and their respective variable values. For example *x and x, and *y and y. From what I understand, variables of functions called from parallel regions are "Private" except for static variable which become "shared".

Following this rule, the variables *x, *y and z, should be "Private" since those are variables of the function 'fvg2' which is called from a parallel region. Additionally, the variables x and y should be be also private.

Is my reasoning correct?

Also in parallel programming is it best practice to explicitly specify every variable scope instead of relying on defaults?

Thank you.

void fvg1 ( int *a, int n)
{
    int i, j, m = 3;
    #pragma omp parallel for
        for (i = 0; i < n; i   )
        {
            int k = m;
            for (j = 0; j < 5; j   )
            fvg2 (&( a[i]), &k, j);
        }
}

    extern int c;

void fvg2 ( int *x, int *y, int z)
{
    int ii;
    static int cnt ;
    cnt   
        for (ii = 0; ii < z; ii   )
        {
        *x = *y   c;
        }
}

CodePudding user response:

Variables declared inside a function will indeed be private, but your a array comes from a shared array variable, so variable x, being a function parameter is shared: every call of the function gets a pointer into the same original array a.

  •  Tags:  
  • Related