下面的一个简单脚本将显示,在最简单的情况下(仅在循环中发送“ECHO”),Popen 仅在第一次迭代中有效(在带有 RHEL 5 的 IBM iDataplex x86 系统上运行)。第一次迭代它可以毫无问题地多次调用 Popen,但之后只有 4 个进程可以访问 Popen。因此,如果有一个调用需要将信息传递给所有进程(例如,allgather,因此需要屏障),则不会传递任何内容,因为除了 4 个进程外,所有进程的 stdo=''(空字符串)。如果使用 while 循环,那么它永远不会退出,因为除了 4 个进程外,它会陷入循环中。这是这个系统特有的吗?或者这里有人知道这是怎么回事吗?
如果在 4 个或更少的进程上运行,它完全可以正常工作。
from mpi4py import MPI
from subprocess import Popen, PIPE
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
num_proc = comm.Get_size()
if rank == 0:
start_time = MPI.Wtime()
for i in range(10):
stdo = ''
cmd = ['echo','HELLO']
# while stdo == '':
a = Popen(cmd, shell=False, stdout=PIPE, stdin=PIPE)
stdo, stder = a.communicate("Input")
a.wait()
if stdo != "HELLO\n":
print "Rank", rank, "ITER: ", i, "OUT: ", stdo
# comm.barrier()
# r = comm.allgather(stdo) #Causes infinite loop because any number above 4 processors after the first iteration will never exit the while loop
#if any collective operation or barrier is removed, it will work properly
if rank == 0:
print "Num Proc is", num_proc, "Time is", MPI.Wtime() - start_time
最佳答案
Popen 的第一个参数(传递给 bash 的参数)需要是一个字符串。我也总是这样做 shell=True 。如果您是编写脚本的人,您知道代码应该做什么,因此可以安全地运行。
cmd = 'echo HELLO'
a = Popen(cmd, shell=True, stdout=PIPE, stdin=PIPE)
关于python - 在使用 mpi4py 的并行环境中第一次迭代后,在 for 循环中使用 Popen 的系统调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16067220/