Consider the use of the FORALL statement in the following code.
FTN95 accepts this a valid code, and succeeds in arriving at the array x = (-5., -4. -3 …… 5) as implied by the coding.
Both gFortran and iFort also accept it as valid code, and both return an identical set of results, x = (-5, -4, 1, 1, 1, 1, …. 1), i.e. different from FTN95.
Should not all the compilers (including FTN95) not reject this line of code?
My simple understanding is that the steps within the FORALL structure can be theoretically executed in any order i.e. when evaluating x(i) = x(i-1) + dx, the value of x(i-1) may not necessarily have been previously calculated, so the compliers should reject the situation where x(i) and x(j) appear on either side of the = sign? Is my understanding correct?
Can anybody explain what I am observing here? Very confused by this, especially when the three compiler test for any code returns different answers.
program forall_puzzle
implicit none
integer, parameter :: n = 11
integer, parameter :: dp = kind(1.d0)
real(kind=dp) xmin, xmax, dx, x(n)
integer i
xmin = -5.d0
xmax = +5.d0
dx = (xmax - xmin)/dble(n-1)
print*, 'dx = ', dx
x(1) = xmin
forall (i=2:n:1) x(i) = x(i-1) + dx
!$$$$$$ ! Do loop alternative to forall
!$$$$$$ do i = 2, n, 1
!$$$$$$ x(i) = x(i-1) + dx
!$$$$$$ end do
do i = 1, n
print*, i, x(i)
end do
end program forall_puzzle