简体   繁体   English

带有Openmp的F2py在Python中给出了导入错误

[英]F2py with Openmp gives import error in Python

I am able to compile the minimal working example below in Fortran which uses Openmp and run to give the expected result (prints 1). 我能够在使用Openmp的Fortran中编译下面的最小工作示例,并运行以给出预期的结果(打印1)。

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

However using this in python with f2py gives the error: 但是在f2py中在python中使用它会产生错误:

ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed. ImportError:DLL加载失败:动态链接库(DLL)初始化例程失败。

I have used the dependency walker to test if the issue is in linking to the openmp dll, however the following .dll is linked in both the fortran executable and the .pyd compiled from f2py: 我已经使用依赖关系遍历程序测试了问题是否在于链接到openmp dll,但是,以下.dll在fortran可执行文件和从f2py编译的.pyd中都链接:

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

Therefore I am confused as to why python would fail to load the .dll, as it appears to be linked correctly from f2py. 因此,我对为什么python无法加载.dll感到困惑,因为它似乎是从f2py正确链接的。 The f2py command that I'm using to generate the .pyd is 我用来生成.pyd的f2py命令是

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

Any help would be greatly appreciated, thanks. 任何帮助将不胜感激,谢谢。

EDIT : I have tested this with another windows PC with the same installation setup, and get the same error message. 编辑 :我已经用另一台具有相同安装设置的Windows PC对它进行了测试,并得到了相同的错误消息。 Am I missing something? 我想念什么吗?

EDIT 2 : Apparently this program wouldn't actually work in f2py, so is bad example. 编辑2 :显然,该程序实际上无法在f2py中工作,因此是一个不好的例子。 My apologies. 我很抱歉。 I'm actually working with subroutines, which are able to work with f2py correctly so long as no openmp commands are present. 我实际上正在使用子例程,只要没有openmp命令,它们就可以正确地与f2py一起使用。

EDIT 3 : I have replaced the example code to be a subroutine instead of program due to feedback from Pierre de Buyl, though this makes no difference to my question. 编辑3 :由于来自Pierre de Buyl的反馈,我已将示例代码替换为子例程而不是程序,尽管这对我的问题没有影响。

This works for me: 这对我有用:

tesf.f95 tesf.f95

subroutine nthreads
    !$ use omp_lib
    integer :: nt

    nt = 0
    !$ nt = omp_get_max_threads()

    write(*,*) 'Nthreads'
    write(*,*) nt

end subroutine

Compile with: 编译:

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

If I run: 如果我运行:

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

The result is: 结果是:

 Nthreads
           8

看来问题出在使用tdm-gcc-64作为编译器,所以我改为使用可以按预期工作的mingw64安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM