我对 python 很陌生,我不确定在分布式集群上实现多线程/多进程代码的最佳方法是什么。
我正在尝试使用 Python 编写一个包装脚本,该脚本调用使用 PBS 排队系统在大型集群上运行的外部 MPI 程序。下面给出了我一直在处理的一种(非常)简化的脚本类型,其中代码移动到特定目录,运行外部 MPI 程序并检查结果以查看是否有任何大的变化。
#!/local/python-2.7.1/bin/python2.7
import os
import subprocess as sp
import coordinate_functions as coord_funcs
os.chdir('/usr/work/cmurray/SeachTest/')
print os.getcwd()
# Gets nodefile and num procs (NP)
cat_np = sp.Popen('cat $PBS_NODEFILE | wc -l', shell = True, stdout=sp.PIPE)
NP = int(cat_np.communicate()[0])
sp.call('cat $PBS_NODEFILE > nodefile', shell = True)
def run_mpi(np, nodefile):
mpi_cmd = 'mpirun -machinefile %s -np %d mpipg > calc.out' % (nodefile, np)
sp.call(vasp_cmd, shell = True)
def search_loop(calc_dir, t_total, nodefile, num_procs):
os.chdir(calc_dir)
no_events = True
while no_events or t < t_total:
run_mpi(mynodefile, NP)
num_events = coord_funcs.change_test('OUTFILE', 'INFILE', 0.01)
if num_events > 0:
event = True
else:
t += 1
search_loop('/usr/work/cmurray/SeachTest/calc_1/', 10, mynodefile, NP)
然后使用以下命令将其提交到队列:
qsub -l nodes=4 -N SeachTest ./SearchTest
我想要做的是search_loop
在从列表中读取的不同目录(例如包含不同的起始位置)中并行运行该函数的多个版本。这些进程的 IO 非常繁重,每次调用 MPI 计算可能需要几分钟才能运行。
该threading
模块可以用于此目的还是该multiprocessing
模块是更好的选择?我可能需要event
在线程/进程之间传递简单的消息,例如上面示例中的布尔值。
另外,如何确保 python 脚本没有使用我分配给 MPI 运行的处理器?