本文从本人简书博客同步过来

在上一篇中我们介绍了用 Boost.Python 包装 C++ 语言 MPI 程序以供 mpi4py 调用的方法,下面我们将介绍使用 f2py 包装 Fortran MPI 程序的方法。

f2py (Fortran to Python interface generator) 是 numpy 中自带的一个 Fortran 到 Python 的接口生成工具,支持 Fortran 77/90/95,可以使用它将 Fortran 程序包装成可供 Python 调用的扩展模块。感兴趣的读者可以参考其文档,这里不多作介绍。我们只会以简单的例子展示如何使用 f2py 包装 Fortran MPI 程序以供 mpi4py 调用。

假设我们有以下 Fortran 程序文件 helloworld.f90,其中定义了子例程 sayhello,其接受一个 MPI 通信子作为参数。

! helloworld.f90
!
! $ f2py --f90exec=mpif90 -m helloworld -c helloworld.f90
!subroutine sayhello(comm)use mpiimplicit noneinteger :: comminteger :: rank, size, nlen, ierrcharacter (len=MPI_MAX_PROCESSOR_NAME) :: pnameif (comm == MPI_COMM_NULL) thenprint *, 'You passed MPI_COMM_NULL !!!'returnend ifcall MPI_Comm_rank(comm, rank, ierr)call MPI_Comm_size(comm, size, ierr)call MPI_Get_processor_name(pname, nlen, ierr)print *, 'Hello, World!', &' I am process ', rank, &' of ', size, &' on ', pname(1:nlen), '.'
end subroutine sayhello! program main
!   use mpi
!   implicit none
!   integer ierr
!   call MPI_Init(ierr)
!   call sayhello(MPI_COMM_WORLD)
!   call MPI_Finalize(ierr)
! end program main

要将其编译成扩展模块 helloworld.so 以供 mpi4py 程序调用,我们可以使用类似于以下的命令一步到位:

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

其中 -m 指定模块名,-c 指明编译和重建扩展模块。

但是 f2py 文档中推荐使用以下两步走的方案:

  • 首先通过以下命令产生一个 signature 文件 helloworld.pyf:

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

生成的 signature 文件如下:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.python module helloworld ! in interface  ! in :helloworldsubroutine sayhello(comm) ! in :helloworld:helloworld.f90use mpiinteger :: commend subroutine sayhelloend interface
end python module helloworld! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

可以根据需要手动编辑所产生的 signature 文件(一般情况下不用作任何编辑也能正常工作)。 作为一个简单的例子,我们在 integer :: comm 这一句中加上 intent(in),使 helloworld.pyf 变成如下:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.python module helloworld ! in interface  ! in :helloworldsubroutine sayhello(comm) ! in :helloworld:helloworld.f90use mpiinteger intent(in) :: commend subroutine sayhelloend interface
end python module helloworld! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/
  • 然后使用以下命令生成扩展模块:

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

编译成功后会生成扩展模块 helloworld.so,然后就可以在我们的 mpi4py 程序中像使用其它 Python 模块一样导入该模块并调用该模块中定义的 sayhello 函数,可以向此函数传递一个 mpi4py 中定义的通信子,如 MPI.COMM_WORLD 或者其它通信子对象。例如使用以下的 test 例程:

# test.pyfrom mpi4py import MPI
import helloworld as hwnull = MPI.COMM_NULL
fnull = null.py2f()
hw.sayhello(fnull)comm = MPI.COMM_WORLD
fcomm = comm.py2f()
hw.sayhello(fcomm)try:hw.sayhello(list())
except:pass
else:assert 0, "exception not raised"

执行结果如下:

$ mpiexec -n 4 python test.py
You passed MPI_COMM_NULL !!!
Hello, World! I am process            0  of            4  on node2.
You passed MPI_COMM_NULL !!!
Hello, World! I am process            1  of            4  on node2.
You passed MPI_COMM_NULL !!!
Hello, World! I am process            2  of            4  on node2.
You passed MPI_COMM_NULL !!!
Hello, World! I am process            3  of            4  on node2.

以上的过程直接编译出了扩展模块,如果想得到或看看扩展模块的源文件,可以使用如下命令:

$ f2py -m helloworld helloworld.f90

该命令会生成扩展模块源码文件 helloworldmodule.c,感兴趣的读者可以打开看看。当然如果你愿意的话也可以用一个 C 编译器手动地将此源文件编译成一个扩展模块。

为了方便,我们也可以编写如下 Makefile 以简化上述操作(注意其中使用了在上一篇中介绍的 python-config 文件):

# Makefile.PHONY: default
default: build test cleanPYTHON  = python
PYTHON_CONFIG = ${PYTHON} ./python-configMPIF90 = mpif90
F2PY = f2py
SO = ${shell ${PYTHON_CONFIG} --extension-suffix}
.PHONY: build
build: helloworld${SO}
helloworld${SO}: helloworld.f90${F2PY} --f90exec=${MPIF90} -m helloworld -c $<MPIEXEC = mpiexec
NP_FLAG = -n
NP = 5
.PHONY: test
test: build${MPIEXEC} ${NP_FLAG} ${NP} ${PYTHON} test.py.PHONY: clean
clean:${RM} helloworld${SO}

编译扩展库,执行程序及清理可以分别使用如下命令:

$ make build
$ make test
$ make clean

以上我们介绍了用 f2py 包装 Fortran 语言 MPI 程序以供 mpi4py 调用的方法,可以看到包装 C, C++,Fortran 等其它计算机语言的 MPI 程序供 mpi4py 调用是比较容易的,其实反过来将 mpi4py 程序嵌入其它计算机语言中也不难,在下一篇中我们将介绍在 C 语言程序中嵌入 mpi4py 程序的方法。

使用 f2py 包装 Fortran MPI 程序相关推荐

  1. windows下python利用f2py调用Fortran

    目录 1.运行环境 2.测试实例 3.参考文章 Fortran(Formula Translation)是世界上第一个被正式推广使用的高级语言,其目前仍然是数值计算领域最重要的编程语言之一,虽然有ju ...

  2. 使用VS2019+Intel OneAPI (ifort)+Intel MPI编译和运行MPI程序与Coarray程序

    使用VS2019+Intel OneAPI (ifort)+Intel MPI编译和运行MPI程序与Coarray程序 一.安装环境 安装vs2019 安装Intel OneAPI Base Tool ...

  3. 用gdb调试mpi程序的一些心得

    Linux下MPI (Message Passage Interface) 的程序不太好调试,在windows下vs2005以上的IDE有集成的简便MPI调试工具,没有用过,有兴趣的可以试验一下.下面 ...

  4. MPI程序例子 test_8_1_2.c -- 对等模式的MPI程序,Jacobi迭代 (MPI_Send、MPI_Recv)

    NOTE: 这里首先需要弄明白 Jacobi迭代是做什么的,怎么操作. 网上找到的一篇讲解使用 MPI解决Jacobi迭代并行化的文章,这个与都志辉 并行程序一书的例子有相似之处.链接http://w ...

  5. 使用性能测试工具TAU测试MPI程序记录

    前言 最近又重新把性能测试给捡起来了.所以决定再重新使用TAU(Tuning and Analysis Utilities )来做一些性能测试.测试的程序主要是组里一个师兄开发的基因组装程序,使用MP ...

  6. 在C语言上如何编写并运行MPI程序

    一.安装MPI 第一步:下载MPI软件包 得到文件: mpich.nt. 1.2.5.zip 第二步:安装 1.解压缩文件mpich.nt.1.2.5.zip到目录mpich.nt.1.2.5 2.进 ...

  7. MPI程序的运行命令

    本博客已迁往http://coredumper.cn 在运行MPI程序之前,首先用命令mpdtrace查看一下当前集群的运行状况,如果集群没有启动,需要用如下命令启动集群: mpdboot -n 4 ...

  8. gdb调试mpi程序

    一.命令行下共终端的调试方法 1. 首先,在用mpi的编译器编译程序的时候,象平常一样,需要加入调试选项 "-g", 2. 其次,运行的时候,可用以下命令: $ mpirun -g ...

  9. 【MPI程序】向量乘法,向量点积(高性能计算)

    简述 假设,调用的节点数量整除向量的秩. 高性能算法 让0节点来读取文件数据 所有的节点都负责计算,然后,这里使用的是块分配法. 其他的都是接受到数据之后,再进行计算. 而0节点由于需要负责传输和调度 ...

  10. 我的第一个MPI程序:利用矩形规则计算pi

    利用矩形规则计算pi #include<mpi.h> #include<iostream> using namespace std;   const int INTERVALS ...

最新文章

  1. 马库斯开喷GPT-3:演员而已,它根本不知道自己在说什么
  2. Android 如何做一次内存泄漏大排查
  3. GoJS v1.8.27 去水印方法
  4. 30jquery-qrcode生成二维码
  5. 使用rpm包升级ntpd服务_服务器准备升级,小程序将暂停使用
  6. Docker 安装常用软件记录
  7. 标定_基于全景基础设施的多摄像机和3D激光雷达校准
  8. Cplex求解线性规划
  9. matlab中度数化成度分秒,度分秒转换:32.68度用度、分、秒表示=? 求解释!?请帮忙...
  10. 华为VRRP-基于交换机的VRRP配置
  11. 除雾霾去朦胧增强色彩对比清晰画面调色插件 ClearPlus v2.1 Win/Mac AE/PR插件中文汉化版安装与使用
  12. a标签download属性无效_html常用标签大全
  13. rpm软件安装冲突:conflicts with
  14. FP-Growth关联分析算法在网络监控领域的应用
  15. Android .9图片使用报错...报错:AAPT: error: file failed to compile.
  16. 更高更快更强!“游戏引擎”发展漫谈
  17. 如何用 Minio SDK 访问滴滴云对象存储
  18. 面试挂在JVM?别慌,图文讲解JVM工作原理,看完还不懂我跪键盘
  19. C++风格的类型转换操作符与C风格的强制类型转换
  20. c全日制聋校实验教材语言训练教参小学1册,聋哑学校课程设置建议(2)

热门文章

  1. 非线性最小二乘求解方法总结
  2. ffmpeg MP3转wav
  3. 分析开关电源中电容和电感的几条原则
  4. 项目之间Cookie的共享
  5. 【知识兔】自学Excel之4:窗口视图控制
  6. Azure云平台学习之路(一)——Azure简介
  7. 3500字专家访谈,探访汽车零部件企业争相迈步数字化背后的故事
  8. 电脑出现无法访问您试图使用的功能所在的网络位置怎么办?(清除软件卸载残留)
  9. MSF利用宏病毒感染word文档获取shell复现
  10. 百度关键词地区排名查询php源码,百度关键词地区排名查询