Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Segmentation fault with write statement

Status
Not open for further replies.

Vahid9

Technical User
Jul 28, 2016
23
CA
Hello,

I am using a fortran code called EPW and would like to extract a matrix A(ibnd,imode,ik) from the code. This is how I have tried to extract the matrix:

Open file to write to
Allocate(A(nbnd,nmodes,nkf))
Loop iq ! from 1 to nqf
Loop ik ! from 1 to nkf
Loop ibnd ! from 1 to nbnd
Loop imode ! from 1 to nmodes
A(ibnd,imode,ik) is calculated here
ENDDO ! loop imode
ENDDO ! loop ibnd
ENDDO ! loop ik
WRITE A(1,1,1) to file ! to get the first element for each iq
ENDDO ! loop iq
Deallocate(A)
Close file

Without the WRITE and Allocation statements, the code runs fine. With them, it segfaults.

I would really appreciate any suggestions you may have.

Thank you,

Vahid
 
Are nbnd, nmodes and nkf always greater than 0
 
Yes. They have been declared as integers and are always greater than zero. nbnd is the number of bands in the system, nmodes is the number of vibrational modes and nkf is the number of points used for integration. The code adds up the contributions to matrix A from all points, modes and bands. I am attempting to print individual A before it gets overwritten and added up.

I should also add that whatever happens within the ik loop happens in a subroutine. Once the ik loop is done, we are back to the main file and I should be able to print A for that particular iq.
 
I finally realized the problem. I had declared A as real allocatable outside the subroutine and inside the subroutine. This time I declared it as REAL inside the subroutine and real allocatable outside the subroutine. Now I don't get the segfault.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top