我是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
函数 . 这些过程非常重要,MPI计算每次调用时可能需要几分钟才能完成 .
threading
模块是否可以用于此目的,或者 multiprocessing
模块是更好的选择吗?我可能需要在线程/进程之间传递上述示例中的 event
boolean之类的简单消息 .
另外,我如何确保python脚本没有使用我分配给MPI运行的处理器?
1 回答
我首先尝试多线程处理I / O密集型程序,假设有足够的带宽来实际并行化I / O.
如果不使用多处理,则脚本将仅使用单个CPU,因为Global Interpreter Lock .