在 Fortran 中使用 MPI 时遇到简单 Send/Recv 的问题

Having trouble with simple Send/Recv using MPI in Fortran

我正在尝试将单个整数从一个进程发送到另一个进程。但是,我得到了分段 fault/invalid 内存引用。显然我误解了 MPI 的一些基本概念。谁能告诉我我做错了什么?

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

您在 MPI_Recv 中缺少 status 数组参数。参见手册

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

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