-1

在以下代码中:

if (rank==0) master();
else slave();

...

void master()
{
int i=0;
}

...

void slave()
{
int i=1;
MPI_BCAST(&i,1,MPI_INT,0,COMM);
}

从节点是否会在主节点中广播“i(==0)”并将所有从节点中的“i”值设置为0?

4

1 回答 1

2

从您发布的内容中有点不清楚您是否拥有正确的语义 - 通信器中的所有进程都必须调用MPI_BCAST,这是 MPI 的集体操作之一。然后,您的程序将表现得好像在调用中指定根的进程MPI_BCAST将消息发送到指定通信器中的所有其他进程,这些进程反过来接收消息。

您的代码段表明您认为MPI_BCAST如果仅在您所谓的“从属”进程上调用 to 就会成功,这是不正确的。

但是,您的调用语法是正确的。

编辑回应评论

我相信所有进程都必须执行调用MPI_BCAST. 如果按照您的建议,伪代码是这样的:

if (myrank == master) then
   do_master_stuff ...
   call mpi_bcast(...)
end if

if (myrank /= master) then
   call mpi_bcast(...)
   do_worker_stuff ...
end if

那么调用将失败;在作业管理或操作系统注意到并将其丢弃之前,您的程序可能会停止。MPI_BCASTMPI 中没有跨范围“匹配”对(或任何其他集体通信例程)的调用的机制。

你的伪代码应该是这样的

if (myrank == master) then
   do_master_stuff ...
end if

if (myrank /= master) then
   do_worker_stuff ...
end if

! all together now
call mpi_bcast(...)

或您的程序需要的任何变体

于 2012-10-16T21:11:08.063 回答