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

编程入门 行业动态 更新时间:2024-10-25 17:25:29

fortran与python跨<a href=https://www.elefans.com/category/jswz/34/1770116.html style=语言集成(f2py/gfortran)"/>

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

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

目录

  • 一、引言
    • 运行环境
    • 问题描述
  • 二、解决方案
    • 示例代码
    • 方案一:F2Py
      • 编译方式
      • 调用方式
    • 方案二:gfortran
      • 1. 生成exe文件:
      • 2. 生成dll文件:
      • 调用方式
  • 三、遇到问题及建议
    • 遇到的问题
    • 建议
  • 参考文献

一、引言

运行环境

系统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 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文件:

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


以上,欢迎补充&交流。

更多推荐

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

本文发布于:2024-02-10 13:29:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1675705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言   python   fortran   f2py   gfortran

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!