Am brand new to mpi4py. The calculate pi example from the Tutorial goes like so:
Master (or parent, or client) side:
#!/usr/bin/env python
from mpi4py import MPI
import numpy
import sys
comm = MPI.COMM_SELF.Spawn(sys.executable,
args=['cpi.py'],
maxprocs=5)
N = numpy.array(100, 'i')
comm.Bcast([N, MPI.INT], root=MPI.ROOT)
PI = numpy.array(0.0, 'd')
comm.Reduce(None, [PI, MPI.DOUBLE],
op=MPI.SUM, root=MPI.ROOT)
print(PI)
comm.Disconnect()
Worker (or child, or server) side:
#!/usr/bin/env python
from mpi4py import MPI
import numpy
comm = MPI.Comm.Get_parent()
size = comm.Get_size()
rank = comm.Get_rank()
N = numpy.array(0, dtype='i')
comm.Bcast([N, MPI.INT], root=0)
h = 1.0 / N; s = 0.0
for i in range(rank, N, size):
x = h * (i + 0.5)
s += 4.0 / (1.0 + x**2)
PI = numpy.array(s * h, dtype='d')
comm.Reduce([PI, MPI.DOUBLE], None,
op=MPI.SUM, root=0)
comm.Disconnect()
Sorry for the stupid question but: How do I run this? If I do mpirun from the command line, it looks like it is creating 4 instances of the parent code, not just the child code. (I get 4 outputs to STDOUT.) If I try to run within python via an import or an execfile, it won't run. Gives an error "error parsing parameters". Also, I presume the child code is named "cpi,py"? Thanks. A horribly basic question, I know. If I could have found the answer, I wouldn't be bothering you guys.
Well, it was one of those things so simple that it was hard. I run with mpirun from the command line, with only one process. The parent code itself spawns the others:
mpirun -np 1 python parent_code.py