使用MPI4PY从python调用使用MPI的外部程序

2024-06-01 12:11:13 发布

您现在位置:Python中文网/ 问答频道 /正文

在一个项目中,我正在开发一个python代码,它通过python接口和MPI4PY并行模块将LAMMPS作为库运行。 我的代码就像一个傀儡大师,驱动LAMMP:

from mpi4py import MPI
from lammps import lammps  # ', PyLammps
import numpy as np
def init_opls(datafile, lmp_opls='', cnt_log_opls=0,communicator=''):
    if not lmp_opls:
       cnt_log_opls += 1
       # lammps can create an instance with a custom comminicator
       lmp_opls = lammps(comm=communicator) 
     return lmp_opls, cnt_log_opls
def run_sim(lmp, simtype, tempi=100, tempf=100, steps=100, current_step=0):
    """ lmp : lammps instance, simtype : nvt or nve , temp
    """
    lmp.command("reset_timestep %d" % current_step)
    lmp.command("dump opls all custom 100 dump.lammpstrj id mass q x y z")
    lmp.command("dump_modify opls sort id append yes")
    lmp.command('dump_modify opls format line "%5d %12.3f %12.8f %12.4f %12.4f %12.4f"')
    lmp.command(
         'fix run_sim all %s temp %f %f 100.0 ' % (simtype, tempi, tempf))
    lmp.command('run %d ' % steps)
    lmp.command('unfix run_sim')
    lmp.command("undump opls")
    current_step = lmp.get_thermo("step")
    return lmp, current_step
def main():
    import os
    import sys
    me = MPI.COMM_WORLD.Get_rank()
    datafileopls = 'MEDIUM2_opls.lmp'
    comm = MPI.COMM_WORLD
    current_step = 0
    lmp_opls,cnt = init_opls(datafileopls,communicator=comm)
    lmp_opls, current_step = run_sim(lmp_opls,"nvt",tempi=100., tempf=900.0,
                    steps=500, current_step=current_step)
if me == 0:
    comm2 =  MPI.COMM_SELF.Spawn('reaxff',args=[],maxprocs=1) 
    comm2.Disconnect()

在完成LAMMPS后的最后一部分中,我想运行另一个名为reaxff的md代码reaxff'是一个可执行文件,我没有任何控制权,但它与MPI并行运行。使用此设置时,会出现以下错误:

MPID_Init(1976).............: spawn process group was unable to obtain parent 
port name from the channelenter code here

我尝试从rank=0作为系统调用调用reaxff,但这次它只使用1个cpu,它应该使用所有可用的cpu,但在同一个cpu上创建16个线程。我想做的是调用reaxff,它应该使用所有可用的处理器。 老实说,我在这方面没有太多经验,任何解决方案都应该受到欢迎


Tags: run代码fromimportstepsimcurrentdump