fortran与python跨语言集成(f2py/gfortran)

于2021年9月4日进行第一次更新,优化了文章结构和解决方案内容。

一、引言

运行环境

系统Windows10
PythonPython3.7.0 64位
编译工具gfortran
Fortran编译环境minGW64

问题描述

软件开发项目,界面使用Python开发,基于性能和资源丰富程度考虑,数值计算部分用Fotran语言实现,然后通过Python调用,其中Fortran代码版本较杂,f77f90f95同时共存。

二、解决方案

目前有如下两种解决方案,由于项目时间较为紧张,最终暂采用了F2Py方案:

  1. 利用F2Py将Fortran代码编译为pyd格式,在Python中可以直接作为模块import。针对简单代码很容易搞定,Fortran代码内容较多较复杂时,需遵守一定编写规范(见建议);
  2. 利用gfortranintel的编译工具,将Fortran代码编译为dll文件,在Python中借用ctypes进行加载并调用。针对简单代码较为有效,面对复杂代码时,报错较多。

示例代码

hello_subroutine.f90

subroutine hello
    print *,'hello world'
end subroutine hello

hello_program.f90

program main
  implicit none
  print *, 'Hello World'
end program main

方案一:F2Py

具体信息见官网:F2PY - Calling Fortran routines from Python

编译方式

python2下:
python -m numpy.f2py -c hello_subroutine.f90 -m hello_subroutine 建议这种方式
or f2py -c hello_subroutine.f90 -m hello_subroutine
python3下
python3 -m numpy.f2py -c hello_subroutine.f90 -m hello_subroutine 建议这种方式
or f2py3 -c hello_subroutine.f90 -m hello_subroutine
编译后会生成hello_subroutine.pyd文件

调用方式

from hello_subroutine import hello  # 需注意路径问题
print(hello())

方案二:gfortran

首先需自行配置minGW64,网上资料较多,按照说明配置好环境变量,在dos窗口中输入gfortran --version能够输出其版本信息则说明安装成功。

1. 生成exe文件:

gfortran -o hello_program hello_program.f90
运行:./hello_program.exe
需要注意的是,若用这种方式编译hello_subroutine.f90,则会报错

D:/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

2. 生成dll文件:

  1. gfortran -shared -fPIC -o hello_subroutine.dll hello_subroutine.f90 —— 编译后,引用其中函数时,需在函数名称后添加_,eq:hello_
  2. gfortran -shared -fPIC -g -o hello_subroutine.dll hello_subroutine.f90 -fno-underscoring —— 可直接引用原函数名

调用方式

from ctypes import cdll, windll, CDLL, WinDLL
dllpath = 'hello_subroutine.dll'
dll1 = cdll.LoadLibrary(dllpath)  # cdll是CDLL类的对象
dll2 = windll.LoadLibrary(dllpath)  # windll是WinDLL类的对象
dll3 = CDLL(dllpath)
dll4 = WinDLL(dllpath)
dll1.hello_()  # 针对于方式1)
dll1.hello()  # 针对于方式2)

三、遇到问题及建议

遇到的问题

  1. D:/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status
    已在上面说明
  2. OSError: exception: access violation reading 0x0000000000007530
    暂未解决,待补充。

建议

  1. 接口函数中的参数,不得使用allocate定义,若输入参数确实需为变长,可同时将参数大小作为单独的参数传入,然后在Fortran代码中创建该参数;
  2. 接口函数中的参数,建议明确使用intent(in)intent(out)intent(iinout)指定,其中in表示只作为输入参数,不可对其进行修改;out表示作为返回参数,将数据传递回Python;inout表示同时作为输入参数和返回参数,可以对其进行修改;
  3. 不建议使用module进行传参;
  4. 建议统一Fortran代码版本,最好是f90以上版本。

参考文献

[1] F2PY - Calling Fortran routines from Python
[2] call functions from a shared fortran library in python
[3] creating DLL with gfortran on Windows
[4] …


以上,欢迎补充&交流。

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
PythonFortran的混合编程可以通过使用F2PYFortran to Python)工具来实现。F2PY是一个用于将Fortran代码转换为Python调用模块的工具,它可以将Fortran代码编译成Python模块,使得Python代码可以调用Fortran代码。 以下是Python封装Fortran的步骤: 1. 编写Fortran代码,并创建一个Fortran模块文件。例如,假设我们有一个Fortran程序文件`test.f90`,其中包含一个名为`add`的程序,它可以计算两个整数的和。我们可以使用以下命令编译Fortran代码,生成一个名为`test_module.so`的共享库文件: ``` f2py -c -m test_module test.f90 ``` 2. 在Python中导入创建的Fortran模块。可以使用以下代码: ```python import numpy as np from test_module import add a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = add(a, b) print(c) ``` 在上述代码中,我们首先导入了Numpy库和我们创建的Fortran模块`test_module`中的`add`程序。然后,我们使用Numpy创建了两个数组`a`和`b`,并将它们作为参数传递给`add`程序。最后,我们打印输出结果。 注意,在使用F2PY封装Fortran代码时,需要将Fortran代码中的变量类型和Python中的变量类型进行匹配。同时,还需要考虑Fortran代码中的数组是按列存储还是按行存储的,以及是否使用了Fortran特有的数组下标从1开始的特性。 除了使用F2PY进行Python封装Fortran外,还可以使用Cython、SWIG等工具进行混合编程。这些工具都可以将Fortran代码编译成Python调用模块,使得Python代码可以调用Fortran代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值