Reputation: 71
I'm new in MPI using Python and I'm having some issues here. This is my code:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
a = 1
comm.bcast(a, root=0)
s = comm.reduce(a, op=MPI.SUM)
print 'From process 0, sum =', s
elif rank == 1:
b = 2
comm.bcast(b, root=1)
x = comm.reduce(b, op=MPI.SUM)
print 'From process 1, sum =', x
I want to print: From process PROCESS_NUMBER, sum = 3
Process 0 prints correctly, but process 1 prints None.
I can't understand why. Could anyone help me?
Upvotes: 5
Views: 358
Reputation: 421
Bcast
, Reduce
) should be called on all
processes, so it is incorrect to place it inside if rank == N
statement.root=1
.a = comm.bcast(a, root=0)
Corrected code:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
a = 1
else:
a = None
a = comm.bcast(a, root=0)
s = comm.reduce(a, op=MPI.SUM)
if rank == 0:
print 'From process 0, sum =', s
if rank == 1:
b = 2
else:
b = None
b = comm.bcast(b, root=1)
x = comm.reduce(b, op=MPI.SUM, root=1)
if rank == 1:
print 'From process 1, sum =', x
Result of running on 3 processes:
From process 0, sum = 3
From process 1, sum = 6
Upvotes: 4
Reputation: 9817
comm.reduce(a, op=MPI.SUM)
corresponds to MPI_Reduce()
: the sum is only available on the root process.
If you wish the sum to be available on every process of the communicator, you can use comm.allreduce(a, op=MPI.SUM)
. It corresponds to MPI_Allreduce()
. See this page to know more about the difference between MPI_Reduce()
and MPI_Allreduce()
.
Upvotes: 1