Reputation: 33
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;
}
}
Upvotes: 2
Views: 1096
Reputation:
private(j,m)
This int k = m;
is not really at it's default place, inside the loop. By using a private
clause it is not needed.
But what is needed is the same for j
: has to be "private". i
also, but that already is by definition.
So the simplified function could be:
void fvg1 (int *a, int n) {
int i, j, m = 3;
#pragma omp parallel for private(m,j)
for (i = 0; i < n; i ++)
for (j = 0; j < 2; j ++)
printf("_%d_ %p %p %d %p\n", omp_get_thread_num(), &i, &j, j, &m);
}
producing:
_4_ 0x7fc82be22d4c 0x7fc82be22d50 0 0x7fc82be22d54
_4_ 0x7fc82be22d4c 0x7fc82be22d50 1 0x7fc82be22d54
_0_ 0x7fffe840b0ac 0x7fffe840b0b0 0 0x7fffe840b0b4
_0_ 0x7fffe840b0ac 0x7fffe840b0b0 1 0x7fffe840b0b4
_3_ 0x7fc82c623d4c 0x7fc82c623d50 0 0x7fc82c623d54
_3_ 0x7fc82c623d4c 0x7fc82c623d50 1 0x7fc82c623d54
_5_ 0x7fc82b621d4c 0x7fc82b621d50 0 0x7fc82b621d54
_5_ 0x7fc82b621d4c 0x7fc82b621d50 1 0x7fc82b621d54
_1_ 0x7fc82d625d4c 0x7fc82d625d50 0 0x7fc82d625d54
_1_ 0x7fc82d625d4c 0x7fc82d625d50 1 0x7fc82d625d54
_2_ 0x7fc82ce24d4c 0x7fc82ce24d50 0 0x7fc82ce24d54
_2_ 0x7fc82ce24d4c 0x7fc82ce24d50 1 0x7fc82ce24d54
Each thread has it's own variables i, j and m. (the middle digits differ, 0 is special).
Without a private(j,m)
clause lines are missing:
_4_ 0x7fec97c4bd54 0x7fffb280a80c 0 0x7fffb280a808
_4_ 0x7fec97c4bd54 0x7fffb280a80c 1 0x7fffb280a808
_0_ 0x7fffb280a784 0x7fffb280a80c 0 0x7fffb280a808
_3_ 0x7fec9844cd54 0x7fffb280a80c 0 0x7fffb280a808
_2_ 0x7fec98c4dd54 0x7fffb280a80c 0 0x7fffb280a808
_1_ 0x7fec9944ed54 0x7fffb280a80c 0 0x7fffb280a808
_5_ 0x7fec9744ad54 0x7fffb280a80c 0 0x7fffb280a808
Now &j
is the same for all threads, which is confusing the next threads.
Upvotes: 0
Reputation: 5794
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
.
Upvotes: 2