I am using a fortran code called EPW and I would like to extract a matrix from a loop. The structure of the loop is as follows:
Open file to write to
Allocate(A(nbnd,modes,nkf)) ! A is the matrix to be printed
Loop iq ! iq runs from 1 to nqf
Loop ik ! ik runs from 1 to nkf
Loop imode ! imode runs from 1 to nmodes
Loop ibnd ! ibnd runs from 1 to nbnd
A is defined here
ENDDO ! ibnd loop
ENDDO ! imode loop
ENDDO ! ik loop
WRITE A(1,1,1) to file ! Writing the first element, one for each iq
ENDDO ! iq loop
I compiled with intel fortran and am running this code serially but get a segmentation fault. The code runs fine if I do not ALLOCATE and WRITE.
I would appreciate any suggestions you may have.
I just realized that I had declared A as real allocatable outside the ik loop and inside the ik loop. The ik loop is done in a subroutine. So I declared it REAL inside the subroutine and real allocatable outside the subroutine and now I don't get the segmentation fault.
I will comment that snippets and paraphrases of code are not useful in helping resolve problems. These almost always fail to show the actual cause, as you found. In the future, please make sure you post actual source code that can be used to demonstrate the problem. Often users find that simply reducing the test case into something they can share reveals a coding error that was the cause.