问题
I want to launch parallel processes from a python script (and, for testing, interactively, but not from ipython), across two different versions of python, and have started out with mpi4py. The two versions are (for 2 and 8 cores respectively):
Python 2.7.2 |EPD 7.2-2 (64-bit)| (default, Sep 7 2011, 16:31:15) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
and
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2
On the first one (to learn the ropes), interactively I get:
from mpi4py import MPI
import sys
size = MPI.COMM_WORLD.Get_size()
print size
1
rank = MPI.COMM_WORLD.Get_rank()
print rank
0
which is not what I want (and doing mpirun/mpiexec python just seems to hang/do nothing). But if I do:
mpiexec -n 5 python helloworld.py
on
#!/usr/bin/env python
from mpi4py import MPI
import sys
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
name = MPI.Get_processor_name()
sys.stdout.write(
"Hello, World! I am process %d of %d on %s.\n"
% (rank, size, name))
I get
Hello, World! I am process 0 of 5 on localhost.
Hello, World! I am process 1 of 5 on localhost.
Hello, World! I am process 2 of 5 on localhost.
Hello, World! I am process 3 of 5 on localhost.
Hello, World! I am process 4 of 5 on localhost.
How can I get size > 0 when launching python interactively?
Incidentally, doing ./helloworld.py rather than python helloworld.py doesn't work:
localhost:demo jtlz2$ mpiexec -n 5 ./helloworld.py
Failed to find or execute the following executable:
Host: localhost Executable: ./helloworld.py
Cannot continue.
Any ideas why? Thanks!
回答1:
If not launched from mpirun
/mpiexec
, MPI executables form singletons and that's why MPI_COMM_WORLD
is always of size 1.
As for mpiexec
failing to find the executable, the latter must have its "executable" bit set, e.g. via
$ chmod +x helloworld.py
Interactively running MPI jobs is tricky. Most MPI implementations perform output redirection for all processes and that's why you can see the combined output, but this is not true for input redirection. Only rank 0 is able to receive interactive input. There are several things that you can do though:
Run rank 0 interactively and let other ranks execute scripts. This would allow you to explore communication with other ranks:
$ mpiexec -np 1 python : -np 4 python script.py
This will start one copy of the interpreter in interactive mode as rank 0 and then four copies ofscript.py
as ranks 1 to 4.Run interactively with each rank in its own graphical terminal emulator, e.g.
xterm
:$ mpiexec -np 5 xterm -e python
This will start 5 xterms and a separate copy of the Python interpreter in all of them. This approach requires that eachxterm
is able to talk to your X11 display server, which means that you might have to explicitly pass the value of theDISPLAY
environment variable, e.g. using-x DISPLAY
for Open MPI or-genv DISPLAY
for MPICH-derived implementations.
来源:https://stackoverflow.com/questions/15705859/mpi4py-returning-rank-differences