In the following example, we'll see an mpi4py implementation of comm.Alltoall. We'll consider a communicator a group of processes, where each process sends and receives an array of numerical data from the other processes defined in the group:
- For this example, the relevant mpi4py and numpy libraries must be imported:
from mpi4py import MPI import numpy
- As in the previous example, we need to set the same parameters, comm, size, and rank:
comm = MPI.COMM_WORLD size = comm.Get_size() rank = comm.Get_rank()
- Hence, we must define the data that each process will send (senddata) and, at the same time, receive (recvdata) from the other processes:
senddata = (rank+1)*numpy.arange(size,dtype=int) recvdata = numpy.empty(size,dtype=int)
- Finally, the Alltoall function is executed:
comm.Alltoall(senddata,recvdata)
- The data that is sent and received for each process is displayed:
print(" process %s sending %s receiving %s" %(rank , senddata , recvdata))