【问题标题】:calling MPI of Fortran from python从 python 调用 Fortran 的 MPI
【发布时间】:2020-03-26 16:11:06
【问题描述】:

我尝试从 python 调用 Fortran 的 MPI。

在 helloworld.f90 中,我写道:

subroutine sayhello(comm)
use mpi
!include 'mpif.h'
implicit none
integer :: comm, rank, size, ierr
call MPI_Comm_size(comm, size, ierr)
call MPI_Comm_rank(comm, rank, ierr)
print *, ’Hello, World! I am process ’,rank,’ of ’,size,’.’
end subroutine sayhello

还有一个叫hello.py

from mpi4py import MPI
import helloworld
fcomm = MPI.COMM_WORLD.py2f()
helloworld.sayhello(fcomm)

但是在ubuntu 18.04,我用的是Python 3.7.4,无法使用命令创建file.so:

f2py -m helloworld -h helloworld.pyf helloworld.f90
f2py -c helloworld.pyf hello.py

当我运行命令时:

mpirun.openmpi was unable to find the specified executable file, and 
therefore
    did not launch the job.  This error was first reported for process
    rank 0; it may have occurred for other processes as well.

你能帮我解决这个错误或提供其他命令来运行

我的电脑可以从 Python 中打开 Fortran 的 MP。另外,我可以在 Fortran 中运行 MPI。但是,我不能从 Python 调用 Fortran 的 MPI。

任何帮助将不胜感激

【问题讨论】:

  • 看起来f2py 找不到 MPI 包含文件。确保已安装 mpi devel 包,并在 f2py 命令行上添加包含文件的路径。
  • 我曾经用fortran成功运行MPI。运行 MPI。我为 C/C++/Fortran 安装了 openMPI,为 PYTHON 安装了 mpi4pi。你能给我举个例子,包括你提到的路径。
  • f2py -I/path/to/include ...
  • 如果您的 MPI 代码仅在 Fortran 中(也就是说,在您的 Python 中没有进行 MPI 通信),那么您根本不需要在 Python 中使用 mpi4py。只需从 Python 内部调用您的 Fortran 代码,就好像它是串行代码一样,但在运行 Python 脚本时在命令行上调用 mpiexec。不要忘记在 Fortran 代码中初始化 MPI。这样一来,您就不需要 Python 识别 MPI 库,这应该可以解决您的问题。

标签: parallel-processing fortran mpi ubuntu-18.04 f2py


【解决方案1】:

我只是想就这个问题做一些cmet。我在 Ubuntu 18.04 中创建了 Python 模块。实际上,它是一个 WSL(Windows 子系统)。我使用 openMPI 1.10.7(太旧)运行,但它是我拥有的版本。我的测试是使用 numpy 1.16.5 和 Python 2.7.15 进行的。

1) 我通过mpi4py doc 页面中的以下命令使用您的源代码创建了一个 Python 模块

f2py --f90exec=mpif90 -c helloworld.f90 -m helloworld

正如 Gilles 所说,您需要链接 mpi,但您可以使用标志 --f90exec,如上所示。这个标志告诉 f2py 你想使用哪个 F90 编译器。 f2pydoc 有一个完整的 f2py 标志列表,可帮助您从 Fortran 代码创建 Python 模块。在 Python 模块生成之后,您可以运行您的脚本。

另一种方法就像你做的那样。但是,我认为您的第二个命令是错误的。您不要在第二个命令中使用标志 --f90exec。此外,您已使用带有 Python 脚本名称的签名文件来创建模块。您应该用 Fortran 文件名替换 Python 脚本。

2) 我测试了你所做的。然后,当我尝试在 Python 解释器中导入模块时,引发了一个异常。综上所述,为了创建一个使用.pyf文件类型的Python模块

f2py -m helloworld -h helloworld.pyf helloworld.f90

然后

f2py --f90exec=mpif90 -c helloworld.pyf helloworld.f90  

3) King 说你不需要mpi4py。但是,我在没有 mpi4py 的情况下进行了测试,并且没有工作,甚至在 FORTRAN 子例程中执行了所有 MPI 初始化过程。下面是我用来做这个测试的代码。

helloworld.f90

subroutine sayhello
use mpi
implicit none

! include 'mpif.h'

    integer :: comm, rank, size, ierr, namelength
    character(len=15) :: processorname

call MPI_INIT(ierr)
call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
call MPI_GET_PROCESSOR_NAME(processorName, namelength, ierr)
print *, 'Hello, World! I am process ',rank,' of ',size,'.'

end subroutine sayhello

你好.py

from mpi4py import MPI
import helloworld

helloworld.sayhello()

4) 如果您想为 Python 3.x 脚本创建一个模块,您可以使用类似的命令,您只需在上面的命令中将 f2py 替换为 f2py3python3 -m numpy.f2py

【讨论】:

    猜你喜欢
    • 2014-12-31
    • 2015-04-14
    • 2021-02-05
    • 2019-08-29
    • 2016-12-06
    • 2016-03-25
    • 2019-03-06
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多