我在使用 Fortran,我正在学习 MPI。具体来说,我正在使用 4 个进程,我想通过创建一个子通信器只向等级 0、等级 0、等级 0、等级 1、等级 2 发送消息,以练习它们的使用。无论我尝试什么,我都没有得到共享消息的预期结果。
这是我试过的。
SUBROUTINE FOO(IER)
USE MPI
INTEGER :: IER
integer :: ierr, rank, size, color
integer :: subcomm
integer :: msg
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)
color = MPI_UNDEFINED
msg = -1
! Split the original communicator into two subcommunicators based on rank
if (rank < 3) then
color = 0 ! First subcomm has color 0
end if
if (rank == 0) then
msg = 100
endif
! Create the subcommunicator
call MPI_Comm_split(MPI_COMM_WORLD, color, rank, subcomm, ierr)
if (rank < 3) then
! Broadcast the message within each subcomm
call MPI_Bcast(msg, 1, MPI_INTEGER, 0, subcomm, ierr)
! Print the result on each process
print *, 'Process ', rank, ' of ', size, ' in subcomm received msg = ', msg
endif
! Free the subcommunicator and finalize MPI
call MPI_Comm_free(subcomm, ierr)
call MPI_Finalize(ierr)
END SUBROUTINE
函数FOO被主程序调用:
PROGRAM DUMMY
IMPLICIT NONE
INTEGER IER
CALL FOO(IER)
END PROGRAM DUMMY
错误信息发生在运行时:
Process 0 of 4 in subcomm received msg = 100
Process 2 of 4 in subcomm received msg = -1
Process 1 of 4 in subcomm received msg = -1
subcomm 顺便说一句,总是等于 0。
这条线:
call MPI_Comm_free(subcomm, ierr)
在 subcommunicator 为空的进程上给出错误。将相同的条件放在该行上,就像您放在 split call 和 bcast 上一样。下次请附上错误信息。