How to do it...

In the following example, we'll see how to distribute data to different processes using the scatter functionality:

  1. Import the mpi4py library:
from mpi4py import MPI 
  1. Next, we define the comm and rank parameters in the usual way:
comm = MPI.COMM_WORLD 
rank = comm.Get_rank() 
  1. 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
  1. 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) 
..................Content has been hidden....................

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