Fortran Array Temporary debugging
Fortran array temporaries can be implicitly created when non-contiguous array slices are passed to procedures. This copy of data can impact performance. Older compilers like GCC 8.x (including 8.5), which may be the only installed choice on some HPC systems, have occasional bugs related to array temporaries that can lead to incorrect results or crashes.
It is possible that disabling optimization can change the bug behavior, but this can make the program run vastly slower and it’s just for debugging. Use git bisect or similar techniques to find the commit that introduced the bug. If the bug is introduced by a new / changed function call using an actual array argument that indexes into a non-contiguous array, that is a hint that the problem may be related to array temporaries. Enable compiler flags to help identify array temporaries in GFortran:
- -Warray-temporaries (compile-time warning)
- -fcheck=array-temps (runtime check)
Suppose the following code snippet:
x = myfun(A(:, 4:16:3))Here, A(:, 4:16:3) creates a non-contiguous array slice, leading to the creation of an array temporary when passed to myfun.
Manually allocate an contiguous auxiliary array variable and copy the data into it before passing it to the function:
real, allocatable :: temp(:,:)
allocate(temp(size(A, 1), 5))
! Adjust size as needed
temp = A(:, 4:16:3)
! Copy data into contiguous array
x = myfun(temp)
! Pass the contiguous array
deallocate(temp)
! Clean upObviously this process is tedious, so we only employ it when needed to workaround a compiler bug.