I am trying to understand the reduction in nested loops.
Consider a very simple program which calculates the number of iterations of inner loop.
PROGRAM main
integer N, i,j
integer*8 ans1
N = 10000
ans1 = 0
!$acc parallel copyin(N) reduction(+:ans1)
do i = 1, N
do j = 1, N
ans1 = ans1 + 1
enddo
enddo
!$acc end parallel
write(*,*) 'ans1 = ', ans1
END PROGRAM main
(Q-1) WHY is this giving CORRECT answer (100000000), when the OpenACC manual says:“If a variable is involved in a reduction that spans multiple nested loops where two or more of those loops have associated loop directives, a reduction clause containing that variable must appear on each of those loop directives.”
Now, consider a very small change to also count the number of outer-loop iterations (by introducing ans2 variable).
PROGRAM main
integer N, i,j
integer*8 ans1,ans2
N = 10000
ans1 = 0
ans2 = 0
!$acc parallel copyin(N) reduction(+:ans1,ans2)
do i = 1, N
ans2 = ans2 + 1
do j = 1, N
ans1 = ans1 + 1
enddo
enddo
!$acc end parallel
write(*,*) 'ans1 = ', ans1
write(*,*) 'ans2 = ', ans2
END PROGRAM main
(Q-2) WHY is this giving wrong answer (780000, 10000)? What is surprising is: ans1 which was earlier correct has gone wrong now!!!
If I add a loop construct before j-loop with reduction on ans1, it works. Okay fine, as the manual also says the same.
But then Q-1 remains.
Request someone to please clarify.
Thanks,
Arun