gpt4 book ai didi

c - MPI 将多个内部通信合并为一个内部通信

转载 作者:行者123 更新时间:2023-12-04 11:05:58 32 4
gpt4 key购买 nike

我试图将一堆产生的进程设置成一个单一的内部通信。我需要将单独的进程生成到唯一的工作目录中,因为这些子进程会写出一堆文件。在生成所有进程之后,希望将它们合并到一个单一的内部通信器中。为了尝试这一点,我设置了一个简单的测试程序。

int main(int argc, const char * argv[]) {
int rank, size;

const int num_children = 5;
int error_codes;

MPI_Init(&argc, (char ***)&argv);

MPI_Comm parentcomm;
MPI_Comm childcomm;
MPI_Comm intracomm;
MPI_Comm_get_parent(&parentcomm);

if (parentcomm == MPI_COMM_NULL) {
printf("Current Command %s\n", argv[0]);

for (size_t i = 0; i < num_children; i++) {
MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 1, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &childcomm, &error_codes);
MPI_Intercomm_merge(childcomm, 0, &intracomm);
MPI_Barrier(childcomm);
}
} else {
MPI_Intercomm_merge(parentcomm, 1, &intracomm);
MPI_Barrier(parentcomm);
}

printf("Test\n");

MPI_Barrier(intracomm);

printf("Test2\n");

MPI_Comm_rank(intracomm, &rank);
MPI_Comm_size(intracomm, &size);

printf("Rank %d of %d\n", rank + 1, size);

MPI_Barrier(intracomm);
MPI_Finalize();
return 0;
}

当我运行它时,我得到了所有 6 个进程,但我的内部通信只在父级和最后一个生成的子级之间说话。结果输出是
Test
Test
Test
Test
Test
Test
Test2
Rank 1 of 2
Test2
Rank 2 of 2

有没有办法将多个通信器合并为一个通信器?另请注意,我一次生成这些,因为我需要每个子进程在唯一的工作目录中执行。

最佳答案

我意识到我已经过时了一年,但我想也许其他人可能想看到这个的实现。正如原受访者所说,没有办法合并三个(或更多)传播者。您必须一次建立一个新的内部通信。这是我使用的代码。此版本删除了原有的内部通信;根据您的特定应用程序,您可能想要也可能不想这样做:

#include <mpi.h>


// The Borg routine: given
// (1) a (quiesced) intra-communicator with one or more members, and
// (2) a (quiesced) inter-communicator with exactly two members, one
// of which is rank zero of the intra-communicator, and
// the other of which is an unrelated spawned rank,
// return a new intra-communicator which is the union of both inputs.
//
// This is a collective operation. All ranks of the intra-
// communicator, and the remote rank of the inter-communicator, must
// call this routine. Ranks that are members of the intra-comm must
// supply the proper value for the "intra" argument, and MPI_COMM_NULL
// for the "inter" argument. The remote inter-comm rank must
// supply MPI_COMM_NULL for the "intra" argument, and the proper value
// for the "inter" argument. Rank zero (only) of the intra-comm must
// supply proper values for both arguments.
//
// N.B. It would make a certain amount of sense to split this into
// separate routines for the intra-communicator processes and the
// remote inter-communicator process. The reason we don't do that is
// that, despite the relatively few lines of code, what's going on here
// is really pretty complicated, and requires close coordination of the
// participating processes. Putting all the code for all the processes
// into this one routine makes it easier to be sure everything "lines up"
// properly.
MPI_Comm
assimilateComm(MPI_Comm intra, MPI_Comm inter)
{
MPI_Comm peer = MPI_COMM_NULL;
MPI_Comm newInterComm = MPI_COMM_NULL;
MPI_Comm newIntraComm = MPI_COMM_NULL;

// The spawned rank will be the "high" rank in the new intra-comm
int high = (MPI_COMM_NULL == intra) ? 1 : 0;

// If this is one of the (two) ranks in the inter-comm,
// create a new intra-comm from the inter-comm
if (MPI_COMM_NULL != inter) {
MPI_Intercomm_merge(inter, high, &peer);
} else {
peer = MPI_COMM_NULL;
}

// Create a new inter-comm between the pre-existing intra-comm
// (all of it, not only rank zero), and the remote (spawned) rank,
// using the just-created intra-comm as the peer communicator.
int tag = 12345;
if (MPI_COMM_NULL != intra) {
// This task is a member of the pre-existing intra-comm
MPI_Intercomm_create(intra, 0, peer, 1, tag, &newInterComm);
}
else {
// This is the remote (spawned) task
MPI_Intercomm_create(MPI_COMM_SELF, 0, peer, 0, tag, &newInterComm);
}

// Now convert this inter-comm into an intra-comm
MPI_Intercomm_merge(newInterComm, high, &newIntraComm);


// Clean up the intermediaries
if (MPI_COMM_NULL != peer) MPI_Comm_free(&peer);
MPI_Comm_free(&newInterComm);

// Delete the original intra-comm
if (MPI_COMM_NULL != intra) MPI_Comm_free(&intra);

// Return the new intra-comm
return newIntraComm;
}

关于c - MPI 将多个内部通信合并为一个内部通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24806782/

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