I need help understanding this openMP example

2019-07-21 15:12发布

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.

2条回答
神经病院院长
2楼-- · 2019-07-21 15:33

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.

查看更多
不美不萌又怎样
3楼-- · 2019-07-21 15:45

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

查看更多
登录 后发表回答