0

I'm currently experiencing an issue in wrapping some Fortran subroutines for use in a python3 script. This issue has only come up since I have attempted to use OpenMP in the subroutines.

For example, if I compile a module 'test.pyd' using f2py -c -m --fcompiler=gfortran --compiler=mingw32 --f90flags='-fopenmp' test test.f90 -lgomp, in which 'test.f90' is a Fortran subroutine which contains a parallelized loop, upon attempting to import this module into my script, I encounter ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed..

Removing the -fopenmp flag in compiling, or the !$omp comments in the Fortran subroutine remove this error.

Changing the subroutine to a roughly equivalent Fortran program, the program compiles to a .exe and runs correctly in parallel.

I'm on a Windows 10 platform, with an AMD64 processor, using GNU Fortran and C compilers from TDM-GCC

1 Answers1

0

I just tried your build command, and it looks prefectly fine. I am myself able to run a parallel subroutine from a python module compiled just the way you are doing.

How are you executing the python code that is using your module? I think the problem is that you don't have the openmp dll (which is named libgomp-1.dll) in your path

I would advise you to run (from a bash shell) :

where libgomp-1.dll

If the command can't find it, then you should probably add the path to your openmp dll (which is usually "C:\tools\mingw64\bin\") to your PATH.

In order to do this, you can use:

export PATH=$PATH:C:\tools\mingw64\bin\ && python script_using_module.py

There is a good chance the way you are executing your python code doesn't account properly for the path, since you can run the parallel executable without a problem.

Thundzz
  • 675
  • 1
  • 11
  • 15
  • Thank you for your reply! I have included `C:\TDM-GCC-64\bin` in my PATH, which includes both C and Fortran compilers, as well as `libgomp-1.dll`. Initially I was running the script from an iPython console (working with Anaconda3), however to simplify matters I then simply tried opening a Python3 console, and typing `import test`, to the same effect (after adding the .pyd file to correct directory of course) – Connor Stephens Jul 06 '17 at 21:17
  • What if you open a regular `cmd` console in the directory where your script is, and run `python your_script.py` ? Does it yield the same error? (not with an anaconda python console). Anaconda may be messing with your PATH. – Thundzz Jul 06 '17 at 21:27
  • Unfortunately yes, I get the same error in Python 3.6.0. I have removed Anaconda3 from the system PATH, and the module successfully imports when compiled without OpenMP flags, and I get the original error when I reintroduce the flag. I have just gone into a `cmd` console, and after adding both .pyd files to my Python36 folder typed: `import numpy` `import test_mod` `import omp_test_mod` The error appears after the last line as before. Typing `where libgomp-1.dll` shows that it is indeed in PATH. – Connor Stephens Jul 06 '17 at 22:17
  • I have now tested this on a Ubuntu workstation, and it imports without issue. This will be my workaround for now - thanks for your help – Connor Stephens Jul 07 '17 at 01:28