Search code examples
cmpiopenmpimessage-passing

Calling MPI_Barrier twice in same function is this okay to do?


Using OpenMPI in C;

Say I'm have

main()
{
MPI_Init();

//// Important Program Region

MPI_Barrier(MPI_COMM_WORLD);

// do something here

MPI_Barrier(MPI_COMM_WORLD);

////
MPI_Finalize();
}

Is this bad practice? Can I force synchronisation in forcing barriers twice like this? Any disadvantages of doing this?


Solution

  • The above program should work as is. It is perfectly acceptable to force synchronization via MPI_Barrier not just once or twice, but for as many times as you want. With that said, the major disadvantage of MPI_Barrier is that the scalability of your program will be significantly lesser the more times you call it.

    Note: If you call it on the "same line" as suggested in the title (but not the question itself), then the second barrier is effectively a no-op - you've already hit a synchronization point; what exactly is the second barrier going to do?