I am teaching myself the Python mpi4py module for programming in multiple processes. I have written the following piece of code to practice scatter.
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
data = [i for i in range(8)]
else:
data = None
data = comm.scatter(data, root=0)
print str(rank) + ': ' + str(data)
Running the above code with 8 processes works great. However, when I run it with 4 processes, I get an error:
Traceback (most recent call last):
File "scatter.py", line 11, in <module>
data = comm.scatter(data, root=0)
File "Comm.pyx", line 874, in mpi4py.MPI.Comm.scatter (src/mpi4py.MPI.c:68023)
File "pickled.pxi", line 656, in mpi4py.MPI.PyMPI_scatter (src/mpi4py.MPI.c:32402)
File "pickled.pxi", line 127, in mpi4py.MPI._p_Pickle.dumpv (src/mpi4py.MPI.c:26813)
ValueError: expecting 4 items, got 8
What does this error mean? My intention is to break up my large array of 8 items into small arrays of 8 / 4 = 2 items and send each process one such subarray. How do I do that? I would also like to generalize if possible to numbers of processes that do not divide evenly into 8 such as 3.