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:
- why does
i
private and not shared? - about this answer 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 ?
How do threads achieve this work? I need your help.
The variables
i
andj
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. Ifi
orj
were shared among threads, then each thread would update them and mess with the execution of other threads.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 thefor
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.
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