(初学者问题)我正在尝试使用MPI_Comm_Spawn动态生成进程,然后向子进程广播消息,但程序在从根进程到子进程的广播中停止.我正在关注http://www.mpi-forum.org/docs/docs.html上的文档,但我无法使其正常运行.有人能帮帮我吗?
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
MPI_Comm parentcomm;
MPI_Comm_get_parent( &parentcomm );
if (parentcomm == MPI_COMM_NULL) {
MPI_Comm intercomm;
MPI_Status status;
char msg_rec[1024];
char msg_send[1024];
int size, i;
int np = (argc > 0) ? atoi(argv[1]) : 3;
printf("Spawner will spawn %d processes\n", np);
MPI_Comm_spawn( argv[0], MPI_ARGV_NULL, np, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm, MPI_ERRCODES_IGNORE );
MPI_Comm_size(intercomm, &size);
sprintf(msg_send, "Hello!");
printf("Spawner will broadcast '%s'\n", msg_send);
MPI_Bcast( (void*)msg_send, 1024, MPI_CHAR, 0, intercomm);
printf("Spawner will receive answers\n");
for (i=0; i < size; i++) {
MPI_Recv( (void*)msg_rec, 1024, MPI_CHAR, i, MPI_ANY_TAG, intercomm, &status);
printf("Spawner received '%s' from rank %d\n", msg_rec, i);
};
} else {
int rank, size;
char msg_rec[1024];
char msg_send[1024];
MPI_Comm_rank(parentcomm, &rank);
MPI_Comm_size(parentcomm, &size);
printf(" Rank %d ready\n", rank);
MPI_Bcast( (void*)msg_rec, 1024, MPI_CHAR, 0, parentcomm);
printf(" Rank %d received '%s' from broadcast!\n", rank, msg_rec);
sprintf(msg_send, "Hi there from rank %d!\n", rank);
MPI_Send( (void*)msg_send, 1024, MPI_CHAR, 0, rank, parentcomm);
};
MPI_Finalize();
return 0;
};
Run Code Online (Sandbox Code Playgroud)
我不知道它是否重要,但我正在使用ubuntu 11.10和Hidra Process Manager.
sus*_*att -1
集体通信呼叫(例如Bcast()需要内部通信器):您正在尝试使用内部通信器( 和intercomm)parentcomm。您必须使用组创建方法来定义一个包含父进程和所有子进程的组,然后在该组上创建一个新的内部通信器。