简单的MPI程序
我正在尝试做一些 MPI,这是一个使用 MPI_Send、MPI_Recv (是的,阻塞)的简单程序。 Rank 0 向所有其他进程发送消息,其他进程接收该消息。但是,我的 MPI_Send 永远不会返回。我在这里错过了什么吗?谢谢!
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include"mpi.h"
int main(int argc, char **argv)
{
int rank, size, i;
MPI_Status status;
char message[20];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
strcpy(message, "Hello, world");
if (rank==0){
for(i=1;i<size;i++){
printf("I am sending %s\n", message);
MPI_Send(message, 23, MPI_BYTE, 0, 7, MPI_COMM_WORLD);
printf("Sending node=%d, message=%s\n", rank, message);
}
}
else{
MPI_Recv(message, 23, MPI_BYTE, MPI_ANY_SOURCE, 7, MPI_COMM_WORLD,&status);
printf("Receiving node=%d, message=%s\n", rank, message);
}
MPI_Finalize();
return 0;
}
I am trying to do some MPI, and here is a simple program using MPI_Send, MPI_Recv (yes, blocking). Rank 0 sends messages to all other processes and the others receive it. However, my MPI_Send never returns. Am I missing something here? Thanks!
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include"mpi.h"
int main(int argc, char **argv)
{
int rank, size, i;
MPI_Status status;
char message[20];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
strcpy(message, "Hello, world");
if (rank==0){
for(i=1;i<size;i++){
printf("I am sending %s\n", message);
MPI_Send(message, 23, MPI_BYTE, 0, 7, MPI_COMM_WORLD);
printf("Sending node=%d, message=%s\n", rank, message);
}
}
else{
MPI_Recv(message, 23, MPI_BYTE, MPI_ANY_SOURCE, 7, MPI_COMM_WORLD,&status);
printf("Receiving node=%d, message=%s\n", rank, message);
}
MPI_Finalize();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你正在向自己发送信息:
我相信这应该是
You are sending the message to yourself:
I believe this should be
正如 @cnicutar 指出的,进程 0 正在向自身发送消息。然而,在我看来,MPI_Bcast 是更好的选择。它肯定会简单得多,因为它既充当“发送”又充当“接收”方法。
As @cnicutar points out, process 0 is sending messages to itself. It appears to me, however, that MPI_Bcast would be a better choice here. It would certainly be much simpler, since it acts as both a "send" and "receive" method.