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

本文介绍了如何在Python项目中集成Fortran代码,通过F2Py和gfortran两种方式将Fortran编译为Python可调用的模块或DLL。F2Py提供便捷的编译和调用方式,而gfortran需要手动生成exe或dll。在使用过程中注意接口函数参数的定义和Fortran代码版本的统一。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

于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] …


以上,欢迎补充&交流。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值