gpt4 book ai didi

c - MPI 生成 : root process does not communicate to child processes

转载 作者:太空狗 更新时间:2023-10-29 17:03:44 25 4
gpt4 key购买 nike

(初学者问题)我尝试使用 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;
};

我不知道这是否重要,但我使用的是 ubuntu 11.10 和 Hidra Process Manager。

最佳答案

正如@suszterpatt 所指出的,您正在使用“Intercommunicator”(而不是“Intracommunicator”)。了解这一点并查看MPI_Bcast ,我们看到:

If comm is an intercommunicator, then the call involves all processes in the intercommunicator, but with one group (group A) defining the root process. All processes in the other group (group B) pass the same value in argument root, which is the rank of the root in group A. The root passes the value MPI_ROOT in root. All other processes in group A pass the value MPI_PROC_NULL in root. Data is broadcast from the root to all processes in group B. The receive buffer arguments of the processes in group B must be consistent with the send buffer argument of the root.

这意味着您只需将父级中的广播调用替换为:

MPI_Bcast( (void*)msg_send, 1024, MPI_CHAR, MPI_ROOT, intercomm);

其他一些错误:

  • 检查参数数量应该是argc > 1
  • MPI_Comm_size(intercomm, &size) 将返回 1。您需要改用 MPI_Comm_remote_size(intercomm, &size)

关于c - MPI 生成 : root process does not communicate to child processes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9970409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com