mpi reduce example

3,138 views
Skip to first unread message

Oliver

unread,
Mar 5, 2015, 1:44:29 AM3/5/15
to mpi...@googlegroups.com
hi -

I am new to mpi4py, and trying out a simple reduce example:

#!/usr/bin/env python
from mpi4py import MPI
comm
= MPI.COMM_WORLD
x
= comm.rank
y
= 0
comm
.reduce(x, y, MPI.SUM)
print "rank %s, x= %s, y=%s" % (comm.rank, x, y


I was expecting x is being sent, y is being sumed, and the result should be 0+1+2+3, am I missing something?

mpirun -np 4 test/t_reduce.py
rank 3, x= 3, y=0
rank 1, x= 1, y=0
rank 0, x= 0, y=0
rank 2, x= 2, y=0

thanks

Oliver

Lisandro Dalcin

unread,
Mar 5, 2015, 1:50:01 AM3/5/15
to mpi4py
On 5 March 2015 at 04:44, Oliver <pyth...@gmail.com> wrote:
> hi -
>
> I am new to mpi4py, and trying out a simple reduce example:
>
> #!/usr/bin/env python
> from mpi4py import MPI
> comm = MPI.COMM_WORLD
> x = comm.rank
> y = 0
> comm.reduce(x, y, MPI.SUM)
> print "rank %s, x= %s, y=%s" % (comm.rank, x, y
>
>
> I was expecting x is being sent, y is being sumed, and the result should be
> 0+1+2+3, am I missing something?
>

y = comm.reduce(x, op=MPI.SUM)

I had to make it work this way simply because some Python types (int,
float, tuple, etc.) are immutable, so you cannot just pass y as an
argument and get the reduction by modifying "y" internally.

Otherwise, you have to use NumPy arrays

x = np.asarray(x)
y = np.asarray(y)
comm.Reduce(x, y, op=MPI.SUM) # note uppercase "Reduce"


--
Lisandro Dalcin
============
Research Scientist
Computer, Electrical and Mathematical Sciences & Engineering (CEMSE)
Numerical Porous Media Center (NumPor)
King Abdullah University of Science and Technology (KAUST)
http://numpor.kaust.edu.sa/

4700 King Abdullah University of Science and Technology
al-Khawarizmi Bldg (Bldg 1), Office # 4332
Thuwal 23955-6900, Kingdom of Saudi Arabia
http://www.kaust.edu.sa

Office Phone: +966 12 808-0459

yusufc...@gmail.com

unread,
Apr 16, 2019, 4:12:39 PM4/16/19
to mpi4py

Thanks @Lisandro Dalcin
5 Mart 2015 Perşembe 08:44:29 UTC+2 tarihinde Oliver yazdı:

alaca...@gmail.com

unread,
Jun 6, 2019, 2:02:29 PM6/6/19
to mpi4py
Hi Lisandro: does your explanation in this context is still correct in the most recent version of mpi4py? We've being trying to reduce lists of int (not numpy arrays but simple lists) but so far we get union of them when using MPI.SUM and "reduce" not "Reduce"(with this it doesn't work at all). Is that what we should expect? Is it because of the explanation you gave in this context? Thank you in advance!!!

Lisandro Dalcin

unread,
Jun 6, 2019, 2:19:09 PM6/6/19
to mpi...@googlegroups.com
On Thu, 6 Jun 2019 at 14:02, <alaca...@gmail.com> wrote:
Hi Lisandro: does your explanation in this context is still correct in the most recent version of mpi4py?

Yes, I think it is, nothing has changed.
 
We've being trying to reduce lists of int (not numpy arrays but simple lists) but so far we get union of them when using MPI.SUM and "reduce" not "Reduce"(with this it doesn't work at all). Is that what we should expect?

Yes, because in Python if you do [1,2] + [2,3] you get the concatenation [1,2,3,4], that is the way "+" works for lists. If you had NumPy arrays, then "+" does something different (pointwise addition).  Lowercase "reduce" in mpi4py works akin of as Python 2  builtin "reduce" function (or "functools.reduce" in Python 3) using "operator.add" as the reduction function.
 
Is it because of the explanation you gave in this context? Thank you in advance!!!


Well, I'm not sure the contexts are the same. But I can confirm that if you try to lowercase "reduce" Python lists using op=MPI.SUM, then you will get list concatenation, basically because that's the way "+" operates on Python lists.

If you want pointwise addition of list elements instead, then it is pretty easy to do:


$ cat mpi-sum.py
from mpi4py import MPI

def mysum(x, y):
    return [a+b for a, b in zip(x, y)]

res = MPI.COMM_WORLD.allreduce([1,2,3], op=mysum)
print(res)

$ mpiexec -n 3 python2 mpi-sum.py
[3, 6, 9]
[3, 6, 9]
[3, 6, 9]

--
Lisandro Dalcin
============
Research Scientist
Extreme Computing Research Center (ECRC)

King Abdullah University of Science and Technology (KAUST)

alaca...@gmail.com

unread,
Jun 6, 2019, 3:28:42 PM6/6/19
to mpi4py
Thank you Lisandro!!!
Reply all
Reply to author
Forward
0 new messages