0
votes

I'm using mpi to do some simulation calculations. Then I found a segmentation fault error caused by MPI_SEND and RECV. And I don't know why. I write a short code to show the error as follows:

program mpishift
implicit none
include 'mpif.h'
integer :: nrow,mcol
integer,parameter :: ndim = 2
integer :: dims(ndim)
integer :: thisid(ndim)
integer,dimension(1:ndim,1:2) :: neigid
integer,dimension(1:ndim,1:2) :: neigid2
logical :: period(ndim)
integer myid,ierr,rc,comm2

integer :: recv1,recv2

nrow = 3
mcol = 3
dims(1) = nrow
dims(2) = mcol
period  = .false.
call MPI_INIT(ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD,myid,ierr)
call MPI_CART_CREATE(MPI_COMM_WORLD,ndim,dims,period,.true.,comm2,ierr)
call MPI_CART_COORDS(comm2,myid,ndim,thisid,ierr)
call MPI_CART_SHIFT(comm2,0,1,neigid(1,1),neigid(1,2),ierr)
call MPI_CART_SHIFT(comm2,1,1,neigid(2,1),neigid(2,2),ierr)

call MPI_CART_SHIFT(comm2,0,2,neigid2(1,1),neigid2(1,2),ierr)
call MPI_CART_SHIFT(comm2,1,2,neigid2(2,1),neigid2(2,2),ierr)

!send
if (neigid(2,2) == MPI_PROC_NULL) then
   call MPI_SEND(myid,1,MPI_INT,neigid (2,1),1,comm2,ierr)
   call MPI_SEND(myid,1,MPI_INT,neigid2(2,1),1,comm2,ierr)
end if
if ((neigid(2,2) /= MPI_PROC_NULL) .and. (neigid(2,1) /= MPI_PROC_NULL)) then
   call MPI_SEND(myid,1,MPI_INT,neigid (2,1),1,comm2,ierr)
   call MPI_SEND(myid,1,MPI_INT,neigid (2,2),1,comm2,ierr)
end if
if (neigid(2,1) == MPI_PROC_NULL) then
   call MPI_SEND(myid,1,MPI_INT,neigid (2,2),1,comm2,ierr)
   call MPI_SEND(myid,1,MPI_INT,neigid2(2,2),1,comm2,ierr)
end if
!recv
if (neigid(2,2) == MPI_PROC_NULL) then
   call MPI_RECV(recv1,1,MPI_INT,neigid (2,1),1,comm2,ierr)
   call MPI_RECV(recv2,1,MPI_INT,neigid2(2,1),1,comm2,ierr)
end if
if ((neigid(2,2) /= MPI_PROC_NULL) .and. (neigid(2,1) /= MPI_PROC_NULL)) then
   call MPI_RECV(recv1,1,MPI_INT,neigid (2,1),1,comm2,ierr)
   call MPI_RECV(recv2,1,MPI_INT,neigid (2,2),1,comm2,ierr)
end if
if (neigid(2,1) == MPI_PROC_NULL) then
   call MPI_RECV(recv1,1,MPI_INT,neigid (2,2),1,comm2,ierr)
   call MPI_RECV(recv2,1,MPI_INT,neigid2(2,2),1,comm2,ierr)
end if
if (myid==0) write(*,'(a5,a5,a5)') 'myid','recv1','recv2'
write(*,'(I5,I5,I5)') myid, recv1,recv2
call MPI_FINALIZE(rc)
end program

The out file shows error with "forrtl: severe(174): SIGSEGV, segementation fault occured"

This is a textbook example of why you should use mpi (or even better, use mpi_f08) instead of include 'mpif.h'Gilles Gouaillardet
I tried use mpi and the result is the same as include 'mpif.h'Reboost
I was unable to compile the program when I used use mpiGilles Gouaillardet
use mpi should write in front of implicit none.Reboost
I got that. mpi_status is forgotten in mpi_recv.Reboost