# MPI_Send/Recv not transmitting initial entries

### Problem

I'm building a simple MPI Send/Recv hello world in Fortran and am running into an odd error. I want to make sure I'm not missing anything trivial before blaming OpenMPI or gfortran.

In the code below I send a simple array between two MPI processes. The first few entries of the data aren't sent correctly. The tail is fine.

Sent      1   2   3   4   5   6   7   8
Received  0   0  32   0   5   6   7   8


I expect sent to equal received and it does so after the first few entries.

### Environment

These results come from gfortran-4.6.3 and openmpi-1.6.4. A colleague ran this using ifort 13.0.1 and obtained correct results.

### Code

program main
implicit none
include 'mpif.h'

integer rank, size, ierr

call MPI_INIT( ierr )
call MPI_COMM_RANK( MPI_COMM_WORLD, rank, ierr )
call MPI_COMM_SIZE( MPI_COMM_WORLD, size, ierr )
! print *, 'Process ', rank, ' of ', size, ' is alive'

if (rank .eq. 0) then
call sender()
end if
if (rank .eq. 1) then
call recver()
end if
call MPI_FINALIZE( ierr )
end program

subroutine sender()
implicit none
include 'mpif.h'

integer i, ierr
integer dat(8)
do i=1, 8
dat(i) = i
enddo
call MPI_SEND( dat, 8, MPI_INTEGER, 1, 2001, MPI_COMM_WORLD, ierr )
print *, "Sent     ", dat

end subroutine

subroutine recver()
implicit none
include 'mpif.h'

integer i, ierr
integer stat
integer dat(8)

dat = -1
print *, "Empty    ", dat
call MPI_RECV( dat, 8, MPI_INTEGER, 0, 2001, MPI_COMM_WORLD, stat, ierr )
print *, "Received ", dat

end subroutine


## 2 Answers

You seem to have the wrong declaration for stat. It must be declared as an array of size MPI_STATUS_SIZE.

integer stat(MPI_STATUS_SIZE)

• That fixes it for me. – Max Hutchinson Jun 5 '13 at 19:41
• I've run into this same problem before too. You can use MPI_STATUS_IGNORE if you're not concerned about the status. – Daniel Shapero Jun 6 '13 at 11:42

You probably have a mismatch between your dat integer's size and MPI_INTEGER. I would be explicit about both declaring dat with an size specification (4 bytes or 8 bytes) and which MPI data type you are using. E.g. something like:

...
integer(kind=4) dat(8)
...
call MPI_RECV( dat, 8, MPI_INTEGER4, 0, 2001, MPI_COMM_WORLD, stat, ierr )
...


or kind=8 and MPI_INTEGER8 if you need 64-bit types.

I couldn't reproduce your error exactly as written because Fortran integer and MPI_INTEGER are the same size on my architecture, but by explicitly mismatching them, I was able to get spurious content at the end of the received data.

I suspect that you have similar issue on your architecture.

• I am able to reproduce the error, and this doesn't fix it. kind=4 seems to be the default, while kind=8 results in a clear size mismatch, with the back half of the result buffer filled with the value -1 from the initialization. – Max Hutchinson Jun 5 '13 at 18:51
• OK, that's what I saw too. Does the status array fix above work for you? – Bill Barth Jun 5 '13 at 19:14