【发布时间】:2018-10-24 06:12:25
【问题描述】:
我目前正在使用 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输出一样吗?如果没有,我怎么知道真正的执行顺序?如果是,那么我该如何正确使用屏障?
【问题讨论】: