0
votes

I am currently learning about OpenMP. As default variables declared outside of the parallel region are public, where as variables inside of a parallel region are private. Also stack variables from inside the parallel regions are private.

double A[10];
int index[10];
#pragma omp parallel
{
 work(index);
}
printf(%d\n”,index[0]);

But why is "index" on the above example public for each thread? Shouldn't it be private, since its put on the stack, and stack variables are private?

Thanks in advance

1
Why would you think that "stack variables are private"? Do you mean "shared" if you write "public"? Are you, perhaps, suffering from the same misunderstandings as discussed in this question? - Zulan
Well I am learning for my exam, and on the slides there is a line "But not everything is shared - Stack variables in C functions called from parallel regions are PRIVATE". Sorry, yes I mean shared when I say public - User20139023

1 Answers

1
votes

The statement

Stack variables in C functions called from parallel regions are private

is true, but you need to differentiate in your case. First,

int index[10];
#pragma omp parallel
{
    // index is a shared variable here
    work(index);
}

But when it comes to the function you call, imagine:

void work(int* passed_index)
{
    ...
}

passed_index - the pointer - is in fact a private variable within work. You can change the pointer, and no other thread will notice.

But the data pointed to by *passed_index is still shared.