References:

Barrier (Comm.barrier | Comm.Barrier):

It works the same way as standard MPI.

Function prototype:

def barrier() -> None #Python objects
#or
def Barrier() -> None #Buffer-like objects

Usage example:

Program without barrier:

from mpi4py import MPI

communicator = MPI.COMM_WORLD
size = communicator.size
rank = communicator.Get_rank()

print(rank)

Output:

Untitled

As you can see, the output order is unpredictable.

Program with barrier:

from mpi4py import MPI

communicator = MPI.COMM_WORLD
size = communicator.size
rank = communicator.Get_rank()

for ind in range(size):
  if(ind == rank): print(rank)
  communicator.barrier();

Output:

Untitled

Using a barrier it is possible to ensure that the output is in order.

Send (Comm.send) and Receive (Comm.recv):

Firstly lets see the basic communication, using Python objects.

Functions prototypes: