MPI_Send/Recv 不传输初始条目

计算科学 正则 mpi
2021-12-21 16:57:03

问题

我正在 Fortran 中构建一个简单的 MPI Send/Recv hello world,但遇到了一个奇怪的错误。在指责 OpenMPI 或 gfortran 之前,我想确保我没有遗漏任何琐碎的事情。

在下面的代码中,我在两个 MPI 进程之间发送了一个简单的数组。数据的前几个条目未正确发送。尾巴没问题。

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

我希望sent相等received,并且在前几个条目之后它会这样做。

环境

这些结果来自gfortran-4.6.3openmpi-1.6.4一位同事使用ifort 13.0.1并获得了正确的结果。

代码

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个回答

您似乎对 stat 的声明有误。它必须声明为大小为 MPI_STATUS_SIZE 的数组。

整数统计(MPI_STATUS_SIZE)

您的dat整数大小和MPI_INTEGER. 我会明确声明dat使用大小规范(4 字节或 8 字节)您使用的 MPI 数据类型。例如:

...
整数(种类=4)数据(8)
...
调用 MPI_RECV(dat, 8, MPI_INTEGER4, 0, 2001, MPI_COMM_WORLD, stat, ierr)
...

或者kind=8MPI_INTEGER8如果您需要 64 位类型。

我无法完全按照写的方式重现您的错误,因为 Fortraninteger并且MPI_INTEGER在我的体系结构上大小相同,但是通过显式不匹配它们,我能够在接收到的数据末尾获得虚假内容。

我怀疑你的架构有类似的问题。