【问题标题】:MPI Barrier not working in loopsMPI屏障不在循环中工作
【发布时间】: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输出一样吗?如果没有,我怎么知道真正的执行顺序?如果是,那么我该如何正确使用屏障?

【问题讨论】:

标签: c mpi


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-01-04
    • 2016-06-10
    • 2015-11-08
    • 1970-01-01
    • 2013-10-20
    • 2023-04-03
    • 2013-03-31
    • 2017-03-16
    • 2014-04-04
    相关资源
    最近更新 更多