Technical data

Debugging Parallel Fortran
111
Example: Erroneous C$DOACROSS
In this example, the bug is that the two references to a have the indexes in
reverse order. If the indexes were in the same order (if both were a(i,j) or
both were a(j,i)), the loop could be multiprocessed. As written, there is a data
dependency, so the C$DOACROSS is a mistake.
c$doacross local(i,j)
do i = 1, n
do j = 1, n
a(i,j) = a(j,i) + x*b(i)
end do
end do
Because a (correct) multiprocessed loop can execute its iterations in any
order, you could rewrite this as:
c$doacross local(i,j)
do i = n, 1, 1
do j = 1, n
a(i,j) = a(j,i) + x*b(i)
end do
end do
This loop no longer gives the same answer as the original even when
compiled without the mp option. This reduces the problem to a normal
debugging problem consiting of the following checks:
Check the LOCAL variables when the code runs correctly as a single
process but fails when multiprocessed. Carefully check any scalar
variables that appear in the left-hand side of an assignment statement
in the loop to be sure they are all declared LOCAL. Be sure to include
the index of any loop nested inside the parallel loop.
A related problem occurs when you need the nal value of a variable
but the variable is declared LOCAL rather than LASTLOCAL. If the
use of the nal value happens several hundred lines farther down, or if
the variable is in a COMMON block and the nal value is used in a
completely separate routine, a variable can look as if it is LOCAL when
in fact it should be LASTLOCAL. To combat this problem, simply
declare all the LOCAL variables LASTLOCAL when debugging a loop.