Simple MPI Program
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 statu开发者_如何学运维s;
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;
}
You are sending the message to yourself:
MPI_Send(message, 23, MPI_BYTE, 0, 7, MPI_COMM_WORLD);
I believe this should be
MPI_Send(message, 23, MPI_BYTE, i, 7, MPI_COMM_WORLD);
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.
精彩评论