gpt4 book ai didi

c - MPI Barrier 不在循环中工作

转载 作者:太空宇宙 更新时间:2023-11-04 04:17:20 25 4
gpt4 key购买 nike

我目前正在使用 MPI C 库,但是使用 C++ 编码,我知道 MPI_Barrier(MPI_COMM_WORLD) 函数会阻止调用者,直到通信器中的所有进程都调用它 , 如 documentation .这是我的代码,在 4 个进程上运行。

int WORLD_SIZE = 0;
int WORLD_RANK = 0;
{
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &WORLD_SIZE);
MPI_Comm_rank(MPI_COMM_WORLD, &WORLD_RANK);
MPI_Barrier(MPI_COMM_WORLD);
}
// everything works up till here
// WORLD_SIZE is 4, WORLD_RANK is the current process rank
{
int j = 1;
while (j <= log2(WORLD_SIZE)) {
printf("rank%d at iteration %d\n", WORLD_RANK, j);
MPI_Barrier(MPI_COMM_WORLD);
j++;
}
}
{
MPI_Finalize();
}

程序给了我输出。

rank0 at iteration 1
rank0 at iteration 2
rank1 at iteration 1
rank1 at iteration 2
rank2 at iteration 1
rank2 at iteration 2
rank3 at iteration 1
rank3 at iteration 2

由于障碍,我期待以下内容。

rank0 at iteration 1
rank1 at iteration 1
rank2 at iteration 1
rank3 at iteration 1
rank0 at iteration 2
rank1 at iteration 2
rank2 at iteration 2
rank3 at iteration 2

感谢任何帮助。如果需要,我可以发布更多代码。

当前的程序执行顺序真的和当前的std输出一样吗?如果没有,我怎么知道真正的执行顺序?如果是,那么我该如何正确使用屏障?

最佳答案

默认情况下,MPI 不会正确排序您的输出。 Barrier 语句可能工作正常,只是为每个进程而不是所有进程排序打印。

关于c - MPI Barrier 不在循环中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50332772/

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