C语言 使用 MPI_Bcast 进行 MPI 通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7864075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using MPI_Bcast for MPI communication
提问by David
I'm trying to broadcast a message from the root node to all other nodes using MPI_Bcast. However, whenever I run this program it always hangs at the beginning. Does anybody know what's wrong with it?
我正在尝试使用 MPI_Bcast 将消息从根节点广播到所有其他节点。然而,每当我运行这个程序时,它总是在开始时挂起。有人知道它有什么问题吗?
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
int rank;
int buf;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0) {
buf = 777;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
}
else {
MPI_Recv(&buf, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
printf("rank %d receiving received %d\n", rank, buf);
}
MPI_Finalize();
return 0;
}
回答by Jonathan Dursi
This is a common source of confusion for people new to MPI. You don't use MPI_Recv()to receive data sent by a broadcast; you use MPI_Bcast().
这是 MPI 新手的常见困惑来源。你不用MPI_Recv()来接收广播发送的数据;你用MPI_Bcast().
Eg, what you want is this:
例如,你想要的是这样的:
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
int rank;
int buf;
const int root=0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == root) {
buf = 777;
}
printf("[%d]: Before Bcast, buf is %d\n", rank, buf);
/* everyone calls bcast, data is taken from root and ends up in everyone's buf */
MPI_Bcast(&buf, 1, MPI_INT, root, MPI_COMM_WORLD);
printf("[%d]: After Bcast, buf is %d\n", rank, buf);
MPI_Finalize();
return 0;
}
For MPI collective communications, everyonehas to particpate; everyone has to call the Bcast, or the Allreduce, or what have you. (That's why the Bcast routine has a parameter that specifies the "root", or who is doing the sending; if only the sender called bcast, you wouldn't need this.) Everyone calls the broadcast, including the receivers; the receviers don't just post a receive.
对于 MPI 集体交流,每个人都必须参与;每个人都必须调用 Bcast,或 Allreduce,或者你有什么。(这就是为什么 Bcast 例程有一个参数指定“根”,或者谁在做发送;如果只有发送者调用 bcast,你就不需要这个。)每个人都调用广播,包括接收者;接收者不只是发布接收。
The reason for this is that the collective operations can involve everyone in the communication, so that you state what you want to happen (everyone gets one processes' data) rather than howit happens (eg, root processor loops over all other ranks and does a send), so that there is scope for optimizing the communication patterns (eg, a tree-based hierarchical communication that takes log(P)steps rather than Psteps for P processes).
这样做的原因是集体操作可以让每个人都参与通信,因此您可以说明您想要发生的事情(每个人都获得一个进程的数据)而不是它是如何发生的(例如,根处理器在所有其他级别上循环并执行发送),以便有优化通信模式的空间(例如,基于树的分层通信采取log(P)步骤而不是PP 进程的步骤)。
回答by GoingMyWay
MPI_Bcastis a collective operation and it must be called by all processes in order to complete.
MPI_Bcast是一个集体操作,必须被所有进程调用才能完成。
And there is no need to call MPI_Recvwhen using MPI_Bcast. There is a post that may be helpful for you, click here
并且MPI_Recv使用时无需调用MPI_Bcast。有一个帖子可能对你有帮助,点击这里

