问题描述
我想在两个不同版本的python中从python脚本(为了进行测试,以交互方式进行测试,但不是从ipython来)进行并行处理,并且已经从mpi4py开始.这两个版本分别是(分别用于2个和8个内核):
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位)| (默认值,2011年9月7日,16:31:15)达尔文[GCC 4.0.1(Apple Inc.内部版本5493)]
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
和
Python 2.6.5(r265:79063,2010年4月16日,13:57:41)linux2上的[GCC 4.4.3]
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
这不是我想要的(并且执行mpirun/mpiexec python似乎挂起/不执行任何操作).但是,如果我这样做:
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
mpiexec -n 5 python helloworld.py
上
#!/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))
我知道
你好,世界!我正在本地主机上处理5个进程中的0个.
Hello, World! I am process 0 of 5 on localhost.
你好,世界!我是本地主机上的进程1,共5个.
Hello, World! I am process 1 of 5 on localhost.
你好,世界!我是本地主机上的进程2,共5个.
Hello, World! I am process 2 of 5 on localhost.
你好,世界!我是本地主机上的进程3,共5.
Hello, World! I am process 3 of 5 on localhost.
你好,世界!我正在本地主机上处理5之4.
Hello, World! I am process 4 of 5 on localhost.
以交互方式启动python时,如何获得size> 0?
How can I get size > 0 when launching python interactively?
偶然地,执行./helloworld.py而不是python helloworld.py无效:
Incidentally, doing ./helloworld.py rather than python helloworld.py doesn't work:
无法找到或执行以下可执行文件:
Failed to find or execute the following executable:
主机:本地主机可执行文件:./helloworld.py
Host: localhostExecutable: ./helloworld.py
任何想法为何?谢谢!
推荐答案
如果未从mpirun
/mpiexec
启动,则MPI可执行文件的形式为,这就是MPI_COMM_WORLD
总是大小为1的原因.
If not launched from mpirun
/mpiexec
, MPI executables form singletons and that's why MPI_COMM_WORLD
is always of size 1.
至于mpiexec
找不到可执行文件,后者必须设置其可执行"位,例如通过
As for mpiexec
failing to find the executable, the latter must have its "executable" bit set, e.g. via
$ chmod +x helloworld.py
交互式运行MPI作业很棘手.大多数MPI实现都对所有进程执行输出重定向,这就是为什么您可以看到合并的输出的原因,但是对于输入重定向却不是这样.只有等级0能够接收交互式输入.您可以执行以下几项操作:
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:
-
以交互方式运行等级0,并让其他等级执行脚本.这将允许您探索与其他等级的通信:
$ mpiexec -np 1 python : -np 4 python script.py
这将以交互方式从1开始以1等级开始翻译的一个副本,然后是script.py
排名1到4.
在其自己的图形终端仿真器中与每个等级交互运行,例如xterm
:
$ mpiexec -np 5 xterm -e python
这将启动5个xterm,并在所有xterm中提供一个单独的Python解释器副本.这种方法要求每个xterm
都能够与X11显示服务器通信,这意味着您可能必须显式传递DISPLAY
环境变量的值,例如对于Open MPI使用-x DISPLAY
或对于MPICH派生的实现使用-genv DISPLAY
.
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 each xterm
is able to talk to your X11 display server, which means that you might have to explicitly pass the value of the DISPLAY
environment variable, e.g. using -x DISPLAY
for Open MPI or -genv DISPLAY
for MPICH-derived implementations.
这篇关于mpi4py返回排名差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!