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++;
}