Minimum array of OpenMP values

I have the source 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 a 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 parallel code correct? I was wondering if I needed to write the #pragma omp barrier before the critical #pragma omp so that I was sure that all the lows are calculated before calculating the global minimum.

0
source share
1 answer

The code is correct. There is no need to add #pragma omp barrier, because there is no need to call everything min_lwhen one thread enters a critical section. There is also an implicit barrier at the end of the loop area.

In addition, you do not have to explicitly specify a iprivate loop iteration loop variable .

, minl:

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

. min OpenMP 3.1.

+1

All Articles