MPI Barrier not working in loops

2019-07-27 11:58发布

问题:

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.



标签: c mpi