MPI Barrier not working in loops

懵懂的女人 提交于 2020-01-15 12:11:21

问题


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();
}

The program gives me the output.

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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/50332772/mpi-barrier-not-working-in-loops

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!