语言集成(f2py/gfortran)"/>
fortran与python跨语言集成(f2py/gfortran)
于2021年9月4日进行第一次更新,优化了文章结构和解决方案内容。
目录
- 一、引言
- 运行环境
- 问题描述
- 二、解决方案
- 示例代码
- 方案一:F2Py
- 编译方式
- 调用方式
- 方案二:gfortran
- 1. 生成exe文件:
- 2. 生成dll文件:
- 调用方式
- 三、遇到问题及建议
- 遇到的问题
- 建议
- 参考文献
一、引言
运行环境
系统 | Windows10 |
---|---|
Python | Python3.7.0 64位 |
编译工具 | gfortran |
Fortran编译环境 | minGW64 |
问题描述
软件开发项目,界面使用Python开发,基于性能和资源丰富程度考虑,数值计算部分用Fotran语言实现,然后通过Python调用,其中Fortran代码版本较杂,f77
、f90
、f95
同时共存。
二、解决方案
目前有如下两种解决方案,由于项目时间较为紧张,最终暂采用了F2Py
方案:
- 利用
F2Py
将Fortran代码编译为pyd
格式,在Python中可以直接作为模块import
。针对简单代码很容易搞定,Fortran代码内容较多较复杂时,需遵守一定编写规范(见建议); - 利用
gfortran
或intel
的编译工具,将Fortran代码编译为dll
文件,在Python中借用ctypes
进行加载并调用。针对简单代码较为有效,面对复杂代码时,报错较多。
示例代码
hello_subroutine.f90
subroutine helloprint *,'hello world'
end subroutine hello
hello_program.f90
program mainimplicit noneprint *, '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文件:
gfortran -shared -fPIC -o hello_subroutine.dll hello_subroutine.f90
—— 编译后,引用其中函数时,需在函数名称后添加_,eq:hello_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)
三、遇到问题及建议
遇到的问题
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
已在上面说明OSError: exception: access violation reading 0x0000000000007530
暂未解决,待补充。
建议
- 接口函数中的参数,不得使用allocate定义,若输入参数确实需为变长,可同时将参数大小作为单独的参数传入,然后在Fortran代码中创建该参数;
- 接口函数中的参数,建议明确使用
intent(in)
、intent(out)
、intent(iinout)
指定,其中in
表示只作为输入参数,不可对其进行修改;out
表示作为返回参数,将数据传递回Python;inout
表示同时作为输入参数和返回参数,可以对其进行修改; - 不建议使用
module
进行传参; - 建议统一
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] …
以上,欢迎补充&交流。
更多推荐
fortran与python跨语言集成(f2py/gfortran)
发布评论