OpenMP minimum value array

跟風遠走 提交于 2019-12-12 03:03:51

问题


I have the original code:

min = INT_MAX;
for (i=0;i<N;i++)
  if (A[i]<min) 
    min = A[i];
for (i=0;i<N;i++)
  A[i]=A[i]-min;

I want to get the parallel version of this and I did this:

min = INT_MAX;
#pragma omp parallel private(i){
minl = INT_MAX;
#pragma omp for
for (i=0;i<N;i++)
  if (A[i]<minl)
    minl=A[i];
#pragma omp critical{
if (minl<min)
  min=minl;
}
#pragma omp for
for (i=0;i<N;i++)
  A[i]=A[i]-min;
}

Is the parallel code right? I was wondering if it is necessary to write #pragma omp barrier before #pragma omp critical so that I make sure that all the minimums are calculated before calculating the global minimum.


回答1:


The code is correct. There is no necessity to add a #pragma omp barrier because there is no need for all min_l to be computed when one thread enters the critical section. There is also an implicit barrier at the end of the loop region.

Further, you do not necessarily need to explicitly declare the loop iteration variable i private.

You can improve the code by using a reduction instead of your manual merging of minl:

#pragma omp for reduction(min:min)
for (i=0;i<N;i++)
  if (A[i]<min)
    min=A[i];

Note: The min operator for reduction is available since OpenMP 3.1.



来源:https://stackoverflow.com/questions/41060031/openmp-minimum-value-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!