The gather
function performs the inverse of the scatter
functionality. In this case, all processes send data to a root process that collects the data received. The gather
function implemented in mpi4py
is, as follows:
recvbuf = comm.gather(sendbuf, rank_of_root_process)
Here, sendbuf
is the data that is sent and rank_of_root_process
represents the process receiver of all the data:
In the following example, we wanted to represent just the condition shown in the preceding figure. Each process builds its own data that is to be sent to the root processes that are identified with the rank zero:
from mpi4py import MPI comm = MPI.COMM_WORLD size = comm.Get_size() rank = comm.Get_rank() data = (rank+1)**2 data = comm.gather(data, root=0) if rank == 0: print ("rank = %s " %rank +\ "...receiving data to other process") for i in range(1,size): data[i] = (i+1)**2 value = data[i] ...