【问题标题】:How to link with FFTW3 in f2py?如何在 f2py 中与 FFTW3 链接?
【发布时间】:2017-03-20 11:02:58
【问题描述】:

我正在尝试为我前段时间编写的 Python 实现获得一些加速,并决定为此使用 f2py。我目前仍在测试一些东西,并认为我需要一些帮助才能让 FFTW3 正常工作。对于第一次测试,我编写了以下子例程:

subroutine initFFT(planF, a, res, n)
    implicit none
    include "fftw3.f"
    integer, INTENT(IN) :: n
    complex(KIND=8), dimension(n) :: a, res
    integer(KIND=8), intent(OUT) :: planF

    call dfftw_plan_dft(planF, 1, n, a, res, FFTW_FORWARD, FFTW_MEASURE)
end subroutine initFFT

subroutine operation(a, res, n, planF)
    implicit none
    INCLUDE 'fftw3.f'
    integer :: n
    integer(KIND=8) :: planF
    complex(KIND=8), dimension(n), intent(IN) :: a
    complex(KIND=8), dimension(n), intent(OUT) :: res

    call dfftw_execute_dft(planF, a, res)
end subroutine operation

我用一个简单的主程序对此进行了测试

program test
    implicit none
    integer, parameter :: n = 3
    integer(KIND=8) :: planF
    complex(KIND=8), dimension(n) :: res, a

    call initFFT(planF, a, res, n)

    a = (/ 1., 1., 1./)
    call operation(a, res, n, planF)
end program test

并编译

gfortran -o ftest myFun.f90 -L/usr/lib -lfftw3 -I/usr/include

一切正常并返回了正确的结果。现在我尝试通过以下方式将其与 f2py 一起使用:

f2py -c myFun.f90 -m modf --f90flags="-L/usr/lib -lfftw3 -I/usr/include"

问题是,当我尝试在 Python 中导入创建的模块(使用 import modf)时,我收到以下错误消息:

导入错误:modf.so 未定义符号:dfftw_execute_dft_

我已经在 Google 上度过了一段时间,但不知何故,到目前为止我还没有发现任何有用的东西。有谁知道如何解决这个问题?

【问题讨论】:

  • 顺便说一句,dfftw* 过程是为double precision 参数定义的。丑陋的kind=8 不保证兼容。
  • 谢谢,是的,在生产代码中我将其命名为double precision - 不过仍然习惯于“旧”格式..

标签: python fortran wrapper fftw f2py


【解决方案1】:

您可以尝试直接使用 f2py 的标志而不是通过--f90flags 吗?

目前,您告诉 fortran 编译器如何构建模块,但 f2py 不知道链接步骤。你需要的是最终的 Python 可调用模块知道 fftw 的存在。

f2py -c -L/usr/lib -lfftw3 -I/usr/include -m modf myFun.f90

根据https://docs.scipy.org/doc/numpy/f2py/usage.html

,这些选项在 Fortran 文件名之前给出

【讨论】:

  • 如此简单,却又如此有效。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多