在MPI编译的Fortran可执行文件上从python调用mpirun

问题描述

我有一个fortran仿真代码,该代码只能并行运行,并且必须在至少4个内核上用mpi(make mpi=yes)进行编译。我可以运行可执行文件,将其命名为“ test”,当我调用mpirun -n 4 ./test时没有问题。

现在,我可以生成不同的输入文件,并处理python的输出。因此,我想从python执行上述命令以运行多个模拟。主要问题似乎是,无论我是否使用os.system,subprocess.call,.run,.Popen等,对于MPI来说,只有一个进程可用(当python启动一个新的子进程时,这才有意义): / p>

例如使用os.system('mpirun -n 4 ./test') or subprocess.run(['mpirun','-n','4','./test'])时,我得到以下输出

starting MPI-3.1 code.
 using    1 nodes with total    1 processors and    1 threads.
 node    0: procs=   1 threads=   1 name=my-pcname

>>>>>>  General @R_406_4045@ion  <<<<<<<
 -------------------------------------------------------------------
 Started              : 22-OCT-2020 18:38:55
 Name of host machine : 
 Current directory    : /home/test
 Compiled on          : Linux (Intel Fortran)
 Compiled             : without OpenMP
 Compiled             : with MPI
 Linked FFT package   : cvecfft_acc         
 Compiled for         :           4  MPI processors
                                  1  OMP threads
 Running on           :           1  MPI processors
                                  1  OMP threads
                                  1  OMP processors per node

>>>> some more @R_406_4045@ion about simulation parameters...

par.f: Mismatch nproc in par.f and MPI nodes:
 Compiled for :            4  MPI processors
 Running on   :            1  MPI processors
 *** STOP *** at location (node            0 ):           3

有趣的是,我得到了4次输出,这使我更加困惑...

关于如何使它起作用的任何想法?抱歉,如果某个地方已经有人问过类似的问题,我搜索了至少一个小时并问了同事,然后才决定在此处发布此问题...

我将Intel fortran编译器ifort 19.0.5.281 20190815与OpenMPI 4.0.5一起使用

解决方法

看起来很奇怪。所有这些消息都与执行环境有关。这些是由您的代码产生的,还是您的自定义mpirun脚本的一部分?

如果我基于简单的MPI案例

program main

  use mpi

  integer error,id,p

  call MPI_Init ( error )
  call MPI_Comm_size ( MPI_COMM_WORLD,p,error )
  call MPI_Comm_rank ( MPI_COMM_WORLD,error )

  write (*,*) 'Hello: ','/',p

  call MPI_Finalize ( error )

end

使用基于GNU的链进行编译

gnuchain/openmpi-4.0.2-gcc-9.2.0

以下方法:mpif90 -o hello hello.f90有效。

然后,如果我使用简单的Python脚本

import os

os.system('mpirun -np 4 ./hello')

它只是按预期工作:

> python ./mpi_run.py
 Hello:            1 /           4
 Hello:            3 /           4
 Hello:            0 /           4
 Hello:            2 /           4

您是否有机会将自己的Python代码本身作为MPI代码运行?