简体   繁体   English

导入几个特定包后,mpirun 无法正常工作

[英]mpirun not working after importing few specific packages

I have two python scripts, one is being executed with mpi within another script using os module.我有两个 python 脚本,一个正在使用 os 模块在另一个脚本中使用 mpi 执行。 Contents of both the scripts are shown below.两个脚本的内容如下所示。 Let package x be the one which is causing trouble and package abc be a random package.让 package x 成为造成麻烦的那个,而 package abc 是随机的 package。 I have added commands "type mpirun" and "mpirun hostname" to show what is changing.我添加了命令“type mpirun”和“mpirun hostname”来显示正在发生的变化。

runscript.py:运行脚本.py:

import os

# importing a normal package abc
import abc
os.system("type mpirun")
os.system("mpirun hostname")
os.system("mpirun -n 5 python hello.py")

# importing package x
import x
os.system("type mpirun")
os.system("mpirun hostname")
os.system("mpirun -n 5 python hello.py")

hello.py:你好.py:

from mpi4py import MPI
import x
comm = MPI.COMM_WORLD
print(comm.rank)

Output of the command "python runscript.py":命令“python runscript.py”的Output:

mpirun is /home/pavan/packages/openmpi-4.0.7/opt-gfortran/bin/mpirun
skynet
skynet
skynet
skynet
skynet
skynet
skynet
skynet
skynet
skynet
2
3
4
1
0
mpirun is /home/pavan/packages/openmpi-4.0.7/opt-gfortran/bin/mpirun

As can be seen in output, if I import a specific package, the "mpirun hostname" doesn't print anything and hello.py script is also not executed but "type mpirun" is printing the same location for openmpi.从 output 中可以看出,如果我导入特定的 package,“mpirun 主机名”不会打印任何内容,并且 hello.py 脚本也不会执行,但“type mpirun”正在为 openmpi 打印相同的位置。 I don't know why importing that specific package causes this problem.我不知道为什么导入特定的 package 会导致这个问题。 This doesn't happen when I import a random package abc.当我导入随机 package abc 时,不会发生这种情况。

Also, note that script that is being executed by mpirun is also importing that specific package x, but the script is executed properly.另外,请注意,mpirun 正在执行的脚本也在导入特定的 package x,但脚本执行正确。 The issue only happens in the runscript.py该问题仅发生在 runscript.py

Any help on this strange error will be appreciated,?对此奇怪错误的任何帮助将不胜感激,? Also, what does it mean when "mpirun hostname" command doesn't output anything?另外,当“mpirun 主机名”命令没有 output 什么时,这是什么意思?

I suspect the root cause is package x imports MPI (from mpi4py ).我怀疑根本原因是 package x进口MPI (来自mpi4py )。

Under the hood, that means that your program first start as a sequential program, and it is hence valid to system("mpirun...") .在引擎盖下,这意味着您的程序首先作为顺序程序启动,因此它对system("mpirun...")有效。

Once MPI is imported (indirectly via package x ), runscript.py becomes a MPI singleton and is no more allowed to `system("mpirun...").一旦MPI被导入(间接通过 package x ), runscript.py就变成了 MPI singleton 并且不再允许`system("mpirun...")。

Before import x , you can insert the following linesimport x之前,您可以插入以下行

import mpi4py
mpi4py.rc.initialize=False
mpi4py.rc.finalize=False

and see if it helps.看看它是否有帮助。

Note that this works for me with a simple test, but that will "broke" package x if it (indirectly) issues MPI calls such as accessing MPI.COMM_WORLD.rank.请注意,这适用于我的简单测试,但如果它(间接)发出 MPI 调用(例如访问 MPI.COMM_WORLD.rank),则会“破坏”package x

The program below illustrates this.下面的程序说明了这一点。

import os
os.system("mpirun -n 1 hostname")

print('after mpi4py')
# make sure `MPI_Init()` is not invoked under the hood
import mpi4py
mpi4py.rc.initialize=False
mpi4py.rc.finalize=False

from mpi4py import MPI

# since `mpi4py` was not initialized, the following call will crash the program if uncommented
#print(MPI.COMM_WORLD.rank)

os.system("mpirun -n 1 hostname")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM