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?
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With