In the following example, we'll see how to distribute data to different processes using the scatter functionality:
- Import the mpi4py library:
from mpi4py import MPI
- Next, we define the comm and rank parameters in the usual way:
comm = MPI.COMM_WORLD rank = comm.Get_rank()
- For the process of rank equal to 0, the following array will be scattered:
if rank == 0: array_to_share = [1, 2, 3, 4 ,5 ,6 ,7, 8 ,9 ,10]
else:
array_to_share = None
- Then, recvbuf is set. The root process is the process of rank equal to 0:
recvbuf = comm.scatter(array_to_share, root=0) print("process = %d" %rank + " recvbuf = %d " %recvbuf)