【问题标题】:F2py with Openmp gives import error in Python带有 Openmp 的 F2py 在 Python 中出现导入错误
【发布时间】:2018-03-12 08:37:56
【问题描述】:

我能够在 Fortran 中编译下面的最小工作示例,它使用 Openmp 并运行以给出预期结果(打印 1)。

subroutine test
use omp_lib
write(*,*) omp_get_num_threads()
end subroutine 

但是在带有 f2py 的 python 中使用它会产生错误:

ImportError: DLL load failed: 动态链接库 (DLL) 初始化例程失败。

我已经使用依赖walker 来测试问题是否在于链接到openmp dll,但是以下.dll 在fortran 可执行文件和从f2py 编译的.pyd 中都链接:

c:\tdm-gcc-64\bin\LIBGOMP_64-1.DLL

因此,我对为什么 python 无法加载 .dll 感到困惑,因为它似乎是从 f2py 正确链接的。我用来生成 .pyd 的 f2py 命令是

python -m numpy.f2py -c -m %output% %input%.f90 --fcompiler=gnu95 --compiler=mingw32 --f90flags="-fopenmp " -lgomp

任何帮助将不胜感激,谢谢。

编辑:我已经在另一台具有相同安装设置的 Windows PC 上对此进行了测试,并得到了相同的错误消息。我错过了什么吗?

编辑 2:显然这个程序实际上不能在 f2py 中运行,所以是一个不好的例子。我很抱歉。我实际上正在使用子程序,只要不存在 openmp 命令,它们就能够正确使用 f2py。

编辑 3:由于 Pierre de Buyl 的反馈,我已将示例代码替换为子例程而不是程序,但这对我的问题没有影响。

【问题讨论】:

  • 您是否尝试过使用program test 或将代码编辑到模块中(module test)? f2py 只会包装子程序、函数或模块。
  • 感谢您的评论,我不知道。但是,只要不存在 openmp 命令,我一直在使用的子例程就能够正确编译并导入到 python 中。
  • 你有没有看过stackoverflow.com/questions/44957887/…(PATH 中包含dll 路径的问题)?
  • 我有,因此我查看了依赖项步行器以查看是否链接了 dll。似乎来自 f2py 的 .pyd 正确链接到 dll,但由于某种原因,当导入 python 时,它再也找不到它了。我确保目录 c:\tdm-gcc-64\bin 在 PATH 中。你知道是否有任何方法可以让 python 明确说明缺少什么 dll 以及它试图在哪里寻找它?或者是否可以以某种方式显式地将 dll 路径传递给 python?感谢您的帮助
  • 我自己不使用 Windows,因此无法调试或进一步提供帮助。希望有人提出解决方案。

标签: python fortran openmp f2py


【解决方案1】:

看来问题在于使用 tdm-gcc-64 作为编译器,因此我改用了 mingw64 安装,它按预期工作。

【讨论】:

    【解决方案2】:

    这对我有用:

    tesf.f95

    subroutine nthreads
        !$ use omp_lib
        integer :: nt
    
        nt = 0
        !$ nt = omp_get_max_threads()
    
        write(*,*) 'Nthreads'
        write(*,*) nt
    
    end subroutine
    

    编译:

    f2py -c test.f95 -m test --f90flags='-fopenmp' -lgomp -lpthread --compiler=mingw32 --fcompiler=gfortran
    

    如果我跑步:

    python -c "import test; test.nthreads()"
    

    结果是:

     Nthreads
               8
    

    【讨论】:

    • 非常感谢您的回答,但它似乎是我的安装问题,我现在已经解决了。
    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2018-12-07
    • 2014-07-16
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多