I am trying to print the dictionary using Mpi4py:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.rank
if rank == 0:
data = {'a':1,'b':2,'c':3}
else:
data = None
data = comm.bcast(data, root=0)
print 'rank',rank,data
as soon as I run it for more processors using the command below:
mpiexec -n 10 python code.py
the results get mixed with other processor results as shown below:
rank 0 {'arank 2 {'a': 1, 'c': 3, 'b': 2}
rank' 3 {'a': 1, 'c': 3, 'b': 2}
: 1, 'c': 3, 'b': 2}
rank 8 {'a': 1, 'c': 3, 'b': 2}rank
1 {'a': 1, 'c': 3, 'b': 2}
rank 4 {'a': 1, 'c': 3, 'b': 2}
rank 5 {'a': 1, 'c': 3, 'b': 2}
rank 9 {'a': 1, 'c': 3, 'b': 2}
rankrank 7 {'a': 1, 'c': 3, 'b': 2}
6 {'a': 1, 'c': 3, 'b': 2}
I assume this is happening because as and when the processors complete their tasks they print the results, leading to mix-matches. I tried using OrderedDict
but even that didn't work. The code is as
from mpi4py import MPI
import collections
comm = MPI.COMM_WORLD
rank = comm.rank
if rank == 0:
data = collections.OrderedDict({'a':1,'b':2,'c':3})
else:
data = None
data = comm.bcast(data, root=0)
print 'rank',rank,data
Is there a way to get the results to be consistent and not mixed with the the results of other processors?