I have the following fortran code modified from https://computing.llnl.gov/tutorials/openMP/exercise.html
PROGRAM REDUCTION
INTEGER I, J, N
REAL A(100), B(100), SUM
REAL SUM2(2)
! Some initializations
N = 100
DO I = 1, N
A(I) = I *1.0
B(I) = A(I)
ENDDO
numthreads = 2
call omp_set_num_threads(numthreads)
!$omp parallel
SUM = 0.0
!$OMP DO private(I,J) !!REDUCTION(+:SUM2)
DO J=1,2
SUM2(J) = 0.0
DO I = 1, N
!!SUM = SUM + (A(I) * B(I))
SUM2(J) = SUM2(J) + (A(I) * B(I))
ENDDO
ENDDO
!$omp end do
!$omp end parallel
PRINT *, ' Sum(1) = ', SUM2(1)
END
Once I remove the comment symbols !!
before REDUCTION(+:SUM2)
, then by gfortran -fopenmp
to compile it, it leads to random results, e.g.,
Sum(1) = 338364.812
, Sum(1) = -1.32860411E+15
, though sometimes right one Sum(1) = 338350.000
.
In other words, if I keep the comment symbols !!
of reduction
, i.e., no reduction
, the result will be the same as no openmp.
Why reduction
does not do a good job here? Do I need reduction
in this example?