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 mpi
It seems that comm.scatter
can not take count
as an argument and expects a list of exactly comm.size
elements as data to be scattered; so you need to distribute your data between processes yourself. Something like this will do:
if rank == 0:
data = [i for i in range(8)]
# dividing data into chunks
chunks = [[] for _ in range(size)]
for i, chunk in enumerate(data):
chunks[i % size].append(chunk)
else:
data = None
chunks = None
data = comm.scatter(chunks, root=0)
print str(rank) + ': ' + str(data)
[physics@tornado] ~/utils> mpirun -np 3 ./mpi.py
2: [2, 5]
0: [0, 3, 6]
1: [1, 4, 7]
Hope this helps.