# L-13 MCS 507 Wed 20 Sep 2023 : mpi4py_scatter.py

"""
A scatter is a collective communication in which
data is distributed among the processes
which belong to the same intracommunicator.

At the command prompt, type
mpiexec -n 10 python3 mpi4py_scatter.py
where '10' is the number of processes.
"""

from mpi4py import MPI

COMM = MPI.COMM_WORLD
RANK = COMM.Get_rank()
SIZE = COMM.Get_size()

if(RANK == 0):
    DATA = [i**2 for i in range(1, SIZE+1)]
else:
    DATA = None # DATA must be defined

DATA = COMM.scatter(DATA, root=0)
print(RANK, 'has data', DATA)
