我的代码有效:
program main
implicit none
include "mpif.h"
integer :: root, rank, err, status(MPI_STATUS_SIZE)
integer :: n, m
call MPI_INIT(err)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, err)
root = 0
if (rank == root) then
m = 1
n = 2
end if
call MPI_BCAST(m, 1, MPI_INTEGER, root, MPI_COMM_WORLD, err)
call MPI_BCAST(n, 1, MPI_INTEGER, root, MPI_COMM_WORLD, err)
print*, 'Rank ', rank, ' received m=', m,' and n=', n
call MPI_FINALIZE(err)
end program
我得到预期的输出:
Rank 0 received m= 1 and n= 2
Rank 1 received m= 1 and n= 2
Rank 2 received m= 1 and n= 2
Rank 3 received m= 1 and n= 2
但是现在,如果我将MPI_BCAST
中的一个放在if语句中:
program main
implicit none
include "mpif.h"
integer :: root, rank, err, status(MPI_STATUS_SIZE)
integer :: n, m
call MPI_INIT(err)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, err)
root = 0
if (rank == root) then
m = 1
n = 2
call MPI_BCAST(m, 1, MPI_INTEGER, root, MPI_COMM_WORLD, err)
end if
call MPI_BCAST(n, 1, MPI_INTEGER, root, MPI_COMM_WORLD, err)
print*, 'Rank ', rank, ' received m=', m,' and n=', n
call MPI_FINALIZE(err)
end program
输出不是我所期望的:
Rank 0 received m= 1 and n= 2
Rank 1 received m= 0 and n= 1
Rank 2 received m= 0 and n= 1
Rank 3 received m= 0 and n= 1
据我所知,通过将第一个MPI_BCAST
置于if语句中,其余的不等待并且没有收到消息。但为什么这也打破了第二个MPI_BCAST
呢?其他流程不应该在第二个root
MPI_BCAST