On 13 April 2013 10:43, yi lu <
zhiwudazh...@gmail.com> wrote:
> from mpi4py import MPI
> comm = MPI.COMM_WORLD
> rank = comm.Get_rank()
> rsum = 0
> lsum = 0
>
> if rank != 0:
> for i in range((rank-1)*10**6+1,(rank+1)*10**6+1):
> lsum = lsum + i
> print lsum
>
> comm.Reduce([rsum,
MPI.INT], [lsum,
MPI.INT], op=MPI.SUM, root=0)
>
> if rank == 0:
> print rsum
>
> The code is all above.
> I am new to mpi, and I am learning how to use mpi Reduce.
> I learned it from
>
http://code.google.com/p/mpi4py/source/browse/demo/compute-pi/cpi-cco.py,
> and it works well.
> But I wonder if there is some documentation for all the methods for mpi4py
> in detailed, 'Reduce' for example.
>
Unfortunately, there is no additional documentation. If you have
knowledge about using MPI in C/C++ or Fortran, then understanding
mpi4py is easy.
> I tried the code above.
> It is supposed to find the sum of 1 to 10^7. And I divided it into five
> parts.
> But I got some errors.
> Can you tell me what is the correct way to use this method?
> The error information is listed below:
> mpirun -np 6 python compute.py
> Traceback (most recent call last):
> File "compute.py", line 13, in <module>
> comm.Reduce([rsum,
MPI.INT], [lsum,
MPI.INT], op=MPI.SUM, root=0)
> File "Comm.pyx", line 535, in mpi4py.MPI.Comm.Reduce
> (src/mpi4py.MPI.c:63642)
> mpi4py.MPI.Exception: MPI_ERR_ARG: invalid argument of some other kind
> 2000001000000
> 4000001000000
> 6000001000000
> 10000001000000
> 8000001000000
>
Here you have a version of the code that works on arbitrary number of
processes. Please note I'm using "long" datatype in a 64 bits Linux
box, otherwise the 32bit "int" integers overflow. The most important
thing to note is that is you want to use the initial-upercase method
calls, you really need to pass buffer-like objects like NumPy arrays.
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
def partition(rank, size, N):
n = N//size + ((N % size) > rank)
s = rank * (N//size)
if (N % size) > rank:
s += rank
else:
s += N % size
return s, s+n
N = 10**5
start, end = partition(rank, size, N)
lsum = np.array(0, dtype='l')
for i in xrange(start, end):
lsum += i
gsum = np.array(0, dtype='l')
comm.Reduce([lsum, MPI.LONG], [gsum, MPI.LONG], op=MPI.SUM, root=0)
if rank == 0:
print gsum, np.arange(N).sum()
--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
3000 Santa Fe, Argentina
Tel:
+54-342-4511594 (ext 1011)
Tel/Fax:
+54-342-4511169