References:

Buffer-like objects with Numpy:

Functions prototypes:

def Send(
    buf: BufSpec, #Buffer data (to send)
    dest: int, #Destination rank
    tag: int = 0 #Message tag
) -> None
def Recv(
    buf: BufSpec, #Buffer data (to receive)
    source: int = ANY_SOURCE, #Source rank
    tag: int = ANY_TAG, #Message tag
    status: Status | None = None #Message status
) -> None

The primary difference is that the receive method does not return the data; it is received through a parameter.

The buffer data can simply be the data to send/receive, but you can also specify the data type. If you do not do this, it will be automatically discovered.

Usage examples:

Automatic data type discovery:

from mpi4py import MPI
import numpy

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if(rank == 0):
  data = numpy.arange(10, dtype='i')
  comm.Send(data, dest=1, tag=0)
elif (rank == 1):
  data = numpy.empty(10, dtype='i')
  comm.Recv(data, source=0, tag=0)
  print(data)

Output:

Untitled

Manual data type specification:

from mpi4py import MPI
import numpy

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if(rank == 0):
  data = numpy.arange(10, dtype='i')
  comm.Send([data, MPI.INT], dest=1, tag=0)
elif (rank == 1):
  data = numpy.empty(10, dtype='i')
  comm.Recv([data, MPI.INT], source=0, tag=0)
  print(data)

Output:

Untitled

Handling status:

Both for Python objects and for those in buffer format, it is possible to pass the status; below, we will show how to handle it.