0

I am able to compile the minimal working example below in Fortran which uses Openmp and run to give the expected result (prints 1).

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

However using this in python with f2py gives the error:

ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.

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:

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. The f2py command that I'm using to generate the .pyd is

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. Am I missing something?

EDIT 2: Apparently this program wouldn't actually work in f2py, so is bad example. My apologies. I'm actually working with subroutines, which are able to work with f2py correctly so long as no openmp commands are present.

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.

Chris
  • 37
  • 7
  • Did you try this with `program test` or did you edit the code into a module (`module test`)? `f2py` will only wrap subroutine, functions or modules. – Pierre de Buyl Oct 01 '17 at 17:07
  • Thanks for your comment, I didn't know that. However the subroutines which I've been working with are able to compile and import correctly into python so long as there are no openmp commands present. – Chris Oct 01 '17 at 17:12
  • Have you looked at https://stackoverflow.com/questions/44957887/python-import-error-for-f2py-modules-compiled-with-openmp (where the issue of the PATH containing the path to the dll)? – Pierre de Buyl Oct 01 '17 at 18:13
  • I have, hence why I took a look at dependency walker to see if the dll was being linked. It seems the .pyd from f2py links to the dll correctly, but for some reason when imported into python it can't find it anymore. I have ensured that the directory c:\tdm-gcc-64\bin is in the PATH. Do you know if there any way of getting python to explicitly say what dll is missing and where it's attempting to look for it? Or if it's possible to explicitly pass in the dll path to python somehow? Thanks for your help – Chris Oct 01 '17 at 18:22
  • I don't use Windows myself, so I can't debug or help further. Hope someone comes along with a solution. – Pierre de Buyl Oct 01 '17 at 18:46

2 Answers2

0

This works for me:

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
yellowhat
  • 429
  • 6
  • 17
  • Many thanks for your answer, however it appears it was instead a problem with my installation which I've now resolved. – Chris Oct 12 '17 at 17:09
0

It appears that the problem was in using tdm-gcc-64 as a compiler, and so I am instead using a mingw64 installation which works as expected.

Chris
  • 37
  • 7