Hi all,
I am trying to understand whether my observation below is as expected. Here is a sample code, where I did bcast, then barrier:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
if rank == 0:
message = "SENT_FROM_RANK_0"
else:
message = "Empty"
print("Content of message in rank ", rank, " before broadcast: ", message)
message = comm.bcast(message, root=0)
comm.barrier()
print("Content of message in rank ", rank, " after broadcast: ", message)
Without comm.barrier() up there, the order of "before" and "after" outputs are mixed: -- for 4 processes below:
Content of message in rank 2 before broadcast: Empty
Content of message in rank 3 before broadcast: Empty
Content of message in rank 0 before broadcast: SENT_FROM_RANK_0
Content of message in rank 0 after broadcast: SENT_FROM_RANK_0
Content of message in rank 2 after broadcast: SENT_FROM_RANK_0
Content of message in rank 1 before broadcast: Empty
Content of message in rank 3 after broadcast: SENT_FROM_RANK_0
Content of message in rank 1 after broadcast: SENT_FROM_RANK_0
But without the extra comm.barrier(), the program always prints all the "before" lines before all the "after":
Content of message in rank 0 before broadcast: SENT_FROM_RANK_0
Content of message in rank 2 before broadcast: Empty
Content of message in rank 3 before broadcast: Empty
Content of message in rank 1 before broadcast: Empty
Content of message in rank 3 after broadcast: SENT_FROM_RANK_0
Content of message in rank 2 after broadcast: SENT_FROM_RANK_0
Content of message in rank 0 after broadcast: SENT_FROM_RANK_0
Content of message in rank 1 after broadcast: SENT_FROM_RANK_0
Out of curiosity I remade the program in C and ran it...and it did exactly the same thing.
Can somebody explain what happened? Why does the bcast followed by barrier lead to synchronization of output?
Thanks,
Wirawan