Search code examples
fortranmpisend

Having trouble with simple Send/Recv using MPI in Fortran


I am trying to send a single integer from one process to another. However, I am getting a segmentation fault/invalid memory reference. Apparently I have misunderstood some basic notion of MPI. Can anyone tell me what I am doing wrong?

program read_data
use mpi
implicit none

integer :: ierr, my_id, max_id, test

call MPI_INIT(ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, my_id, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, max_id, ierr)

if (my_id .eq. 0) then
    test = 1
    call MPI_Send(test, 1, MPI_Integer, 1, 6, MPI_COMM_WORLD, ierr)
endif

if (my_id .eq. 1) then
    call MPI_Recv(test, 1, MPI_Integer, 0, 6, MPI_COMM_WORLD, ierr)
    write(*,*) test
endif

call MPI_FINALIZE(ierr)

end program read_data

Solution

  • You lack the status array argument in MPI_Recv. See the manual

    MPI_RECV(BUF, COUNT, DATATYPE, SOURCE, TAG, COMM, STATUS, IERROR)
    

    https://www.open-mpi.org/doc/v1.8/man3/MPI_Recv.3.php