Reputation: 43
I'm beginning in openMP and I want parallelize this portion of code :
for (i=0 ;i<n ;i++)
for (j=1 ;j<n ;j++)
A[i][j]+=A[i][j-1];
and I find this answer:
#pragma omp parallel for private(i, j) shared(A, n)
for (i = 0; i < n; ++i)
for (j = 1; j < n; ++j)
A[i][j] += A[i][j-1];
I have some questions:
i
private and not shared? How do threads achieve this work? I need your help.
Upvotes: 2
Views: 545
Reputation: 157
why is
i
private and not shared?
The variables i
and j
are private to each thread because they are loop counters. Each thread has to keep track of what it is doing, separately from other threads. If i
or j
were shared among threads, then each thread would update them and mess with the execution of other threads.
if i have 4 threads so each thread executes the same code, I don't understand how can I obtain a same result as sequential code ?
Each thread executes the same lines of code, but using different variables. OpenMP's 'for' directive helps to automagically distribute the iterations among threads, so that each thread gets a different starting value of i
and a different ending value in the for
loop. So it's more like each thread gets the same code inside the loop, but different loop initialization and end condition. All of them together will (if programmed correctly) give the same end result as a sequential implementation.
Of course, for this to work it is necessary for the loop to be parallelizable. If each iteration of the loop depends on results calculated by previous iterations, then it can't be run in parallel. In that case you'll need to rewrite the loop so that each iteration can be run independently.
Upvotes: 1
Reputation: 108
I've never use omp, but your question made me curious. I hope this documentation will help you. It was helpful for me. https://computing.llnl.gov/tutorials/openMP/#Exercise1
Upvotes: 0