Collective communication using scatter
The scatter functionality is very similar to a scatter broadcast but has one major difference, while comm.bcast
sends the same data to all listening processes, comm.scatter
can send the chunks of data in an array to different processes. The following figure illustrates the functionality of scatter:
The comm.scatter
function takes the elements of the array and distributes them to the processes according to their rank, for which the first element will be sent to the process zero, the second element to the process 1, and so on. The function implemented in mpi4py
is as follows:
recvbuf = comm.scatter(sendbuf, rank_of_root_process)
How to do it…
In the next example, we see how to distribute data to different processes using the scatter
functionality:
from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() if rank == 0: array_to_share = [1, 2, 3, 4 ,5 ,6 ,7, 8 ,9 ,10] else: ...