How to do it...

In the following example, we'll represent the condition shown in the preceding diagram, in which each process builds its own data, which is to be sent to the root processes that are identified with the rank zero:

  1. Type the necessary import:
from mpi4py import MPI 
  1. Next, we define the following three parameters. The comm parameter is the communicator, rank provides the rank of the process, and size is the total number of processes:
comm = MPI.COMM_WORLD 
size = comm.Get_size() 
rank = comm.Get_rank() 
  1. Here, we define the data to be gathered from the process of rank zero:
data = (rank+1)**2 

  1. Finally, the gathering is provided through the comm.gather function. Also, note that the root process (the process that will gather the data from the other ones) is the zero rank process:
data = comm.gather(data, root=0) 
  1. For the rank equal to the 0 process, the data gathered and the sending process are printed out:
if rank == 0: 
    print ("rank = %s " %rank + 
          "...receiving data to other process") 
   for i in range(1,size): 
       value = data[i] 
       print(" process %s receiving %s from process %s" 
            %(rank , value , i)) 
..................Content has been hidden....................

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