How to do it...

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:

  1. For this example, the relevant mpi4py and numpy libraries must be imported:
from mpi4py import MPI 
import numpy 
  1. 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() 
  1. 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) 
  1. Finally, the Alltoall function is executed:
comm.Alltoall(senddata,recvdata) 
  1. The data that is sent and received for each process is displayed:
print(" process %s sending %s receiving %s" 
      %(rank , senddata , recvdata)) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.113