0

I am just starting playing around with OpenMp and got very confused about this problem. Say if I declare an array int a[10] and use it inside some loop. What is the difference between #pragma omp parallel for private(a) and #pragma omp parallel for shared(a)? To me a is just a pointer to the first element of the array and according to what I have read the private construct will make a copy of the variable for each thread, and also very strangely initialize it with a random value if I do not use firstprivate construct. So my question is if I use private construct, what will happen semantically? Will it make a set of pointers and assign them to every thread created or will it make a set of copies of the whole array and let each thread to use them separately?

Thanks a lot.

1 Answer 1

2

There is a subtle semantic difference between arrays and pointers in C and C++. An array definition like int a[10] provides the compiler with information both about the type and the size of the array and it (the compiler) can use that information in order to properly allocate a private copy of the same size and possibly initialise it (in the case of firstprivate).

int *a only tells the compiler the type but not the size and therefore in OpenMP such variable is treated simply as a pointer and the pointer itself is made private, not the data that it points to. If you put a pointer in a firstprivate clause then each thread would get a separate copy of the pointer variable but initially all copies would be pointing at the same place in memory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.