1

I am learning OpenACC and came across the code bellow for the Jacobi iteration, provided by NVidia. From my understanding, reduction(max:err) creates a private err variable for each loop iteration, and returns the max value from all of them.

My question is why use err = max(err, abs(Anew[j][i] - A[j][i]);, specifically, the max() function, when reduction seems to do this by itself?

while ( err > tol && iter < iter_max ) 
{ 
err=0.0;
#pragma acc kernels reduction(max:err) 
for( int j = 1; j < n-1; j++) 
{ 
    for(int i = 1; i < m-1; i++) 
    { 
        Anew[j][i] = 0.25 * (A[j][i+1] + A[j][i-1] + A[j-1][i] + A[j+1][i]); 
        err = max(err, abs(Anew[j][i] - A[j][i]); 
    } 
} 
        
#pragma acc kernels
for( int j = 1; j < n-1; j++)
{ 
    for( int i = 1; i < m-1; i++ ) 
    { 
        A[j][i] = Anew[j][i]; 
    } 
} 
iter++; 

}

  • Do you know (or is it documented) what the valid range of values for `err` is? If typical, `err` will be either `0` for normal execution, or negative for some range of possible errors. If that is the case, then `max(err, 'some expression >= 0');` will only ever return some `value >= 0`. – ryyker Dec 01 '20 at 22:27
  • In this case, `err` is measuring the difference between a value of `A` and the average between the four surrounding values. Since it is an absolute value, it can go from 0 to the range of A. – Ricardo Matias Dec 01 '20 at 22:49

1 Answers1

2

The "max" function takes the maximum value from the arguments for a single iteration. The max reduction finds the max across all the iterations.

Mat Colgrove
  • 5,441
  • 1
  • 10
  • 11