4

我想从 python 脚本启动并行进程(并且,为了测试,交互式,但不是从 ipython),跨两个不同版本的 python,并从mpi4py. 这两个版本是(分别适用于 2 核和 8 核):

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, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2

在第一个(学习绳索)上,交互式地我得到:

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只是似乎挂起/什么都不做)。但如果我这样做:

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))

我明白了

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.

size交互式启动python时如何获得> 0?

顺便说一句,做./helloworld.py而不是python helloworld.py不起作用:

localhost:demo jtlz2$ mpiexec -n 5 ./helloworld.py
--------------------------------------------------------------------------
Failed to find or execute the following executable:

Host:       localhost
Executable: ./helloworld.py

Cannot continue.
--------------------------------------------------------------------------

任何想法为什么?谢谢!

4

1 回答 1

7

如果不是从mpirun/启动mpiexec,MPI 可执行文件会形成单例,这就是为什么MPI_COMM_WORLD总是大小为 1。

至于mpiexec找不到可执行文件,后者必须设置其“可执行”位,例如通过

$ chmod +x helloworld.py

交互式运行 MPI 作业很棘手。大多数 MPI 实现对所有进程执行输出重定向,这就是您可以看到组合输出的原因,但输入重定向并非如此。只有等级 0 能够接收交互式输入。不过,您可以做几件事:

  • 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 of script.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 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.

于 2013-03-30T11:00:43.183 回答