MPI屏障无法循环工作 [英] MPI Barrier not working in loops

查看:100
本文介绍了MPI屏障无法循环工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用MPI C库,但是对c ++进行编码,我知道MPI_Barrier(MPI_COMM_WORLD)函数会阻塞调用方,直到通信器中的所有进程都调用了它,如文档.这是我的代码,在4个进程上运行.

I am currently using the MPI C library, but coding c++, I know that MPI_Barrier(MPI_COMM_WORLD) function blocks the caller until all processes in the communicator have called it, as in the documentation. Here is my code, running on 4 processes.

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

由于障碍,我希望在以下地方进行

Where I am expecting the following due to the barrier.

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

任何帮助表示赞赏.如果需要,我可以发布更多代码.

Any help appreciated. I can post more code if needed.

当前程序的执行顺序是否与当前std输出相同?如果没有,我该如何告知实际执行顺序?如果是,那我该如何正确使用屏障?

Is the current program execution order really the same as the current std output? If no how do I tell the real execution order? If yes then how do I correctly use barrier?

推荐答案

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

MPI does not order your outputs correctly per default. The Barrier statement is probably working correctly, the prints are just ordered for each process but not for all processes.

这篇关于MPI屏障无法循环工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆