f2py with omp: can't import module, undefined symbol gomp_*

  • Last Update :
  • Techknowledgy :

I was able to reproduce the error on Mac OS X (10.9.5), with gfortran installed using homebrew, and I was able to fix it by adding -lgomp to the command:

f2py - m SOmod--fcompiler = gnu95--f90flags = '-march=native -O3 -fopenmp' - lgomp - c SOtest.f95

Suggestion : 2

I was hoping to use openmp to speed up my Fortran code that I run through f2py. However, after compiling succesfully, I can't import the module in Python., 6 days ago Feb 01, 2015  · I was hoping to use openmp to speed up my Fortran code that I run through f2py. However, after compiling succesfully, I can't import the module in Python. ... f2py with OMP: can't import module, undefined symbol GOMP_* Ask Question ... Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: ./SOmod.so: undefined ... , Installation via source code will require a Fortran and C compiler in order to run f2py. You can get them here. To install, if you do not need OpenMP support, change your working directory to the wrf-python source directory and run: , Also, make sure you are running in the gnu/4.8.2 compiler environment or you will get import errors for a missing libquadmath library when you go to import wrf-python. Installation via source code will require a Fortran and C compiler in order to run f2py.


module test implicit none contains subroutine readygo() real(kind = 8), dimension(10000)::q!$OMP WORKSHARE q = 7!$OMP END WORKSHARE end subroutine end module

f2py - m SOmod--fcompiler = gnu95--f90flags = '-march=native -O3 -fopenmp' - lgomp - c SOtest.f95
module test implicit none contains subroutine readygo() real(kind = 8), dimension(10000)::q!$OMP WORKSHARE q = 7!$OMP END WORKSHARE end subroutine end module
f2py - m SOmod--fcompiler = gnu95--f90flags = '-march=native -O3 -fopenmp' - c SOtest.f95 python2 - c "import SOmod"
Traceback (most recent call last): File "<string>", line 1, in <module>ImportError: ./SOmod.so: undefined symbol: GOMP_barrier
f2py - m SOmod--fcompiler = gnu95--f90flags = '-march=native -O3 -fopenmp' - lgomp - c SOtest.f95

Suggestion : 3

My approach is as follows: 1) Compile a pure fortran module, 2) Write a wrapper fortran/f2py module which will be used from python, and 3) Compile and link the wrapper to the pure fortran module. This approach avoids changing the fortran code, and lets you re-use the fortran code in a pure fortran program later. ,The (pure) fortran95 module I wish to wrap contains the following code: ,Tested OS-es and gfortran versions: Mac OS X 10.6 with gfortran 4.4.4. Fedora 12 with gfortran 4.4.4. (Suggestions and/or test results from other OS-es, compilers, and versions are of course welcome!) ,I like to have a Makefile to remember the terminal commands and flags for later use.

The (pure) fortran95 module I wish to wrap contains the following code:

!fortranmodule.f95
module fortranmodule
use omp_lib

contains
subroutine test(x, y)
real(8), dimension(: ), intent(in)::x
real(8), dimension(: ), intent(out)::y!Code begins
integer::i, n
integer::num_threads
n = size(x, 1)

   !$omp parallel do private(i) firstprivate(n) shared(x, y)
   do i = 1, n
   if (i == 1) then!The
if clause can be removed
for serious use.!It is here
for debugging only.
num_threads = OMP_get_num_threads()
print * , 'num_threads running:', num_threads
end
if
y(i) = sin(x(i)) + cos(x(i) + exp(x(i))) + log(x(i))
end do
   !$omp end parallel do
      end subroutine test
   end module fortranmodule

The wrapper module looks like this:

module w_fortranmodule
use fortranmodule, only: test
implicit none

contains
subroutine w_test(x, y, ysize)
real(8), intent(in), dimension(ysize)::x
real(8), intent(out), dimension(ysize)::y
integer::ysize!f2py intent(hide)::ysize = shape(x, 0) !f2py depend(ysize) y
call test(x, y)
end subroutine w_test
end module w_fortranmodule

I use a setup.py script, it is rather simple:

import os
from numpy.distutils.misc_util
import Configuration
from numpy.distutils.core
import setup

def configuration(parent_package = '', top_path = None):
   config = Configuration(None, parent_package, top_path)
libraries = [
   # 'gomp',
   # 'blas',
]
config.add_extension('wrapper',
   ['fortranmodule.f95', 'wrapper.f95'],
   libraries = libraries,
   f2py_options = [],
   define_macros = [('F2PY_REPORT_ON_ARRAY_COPY', '1')],
   # this is the flag gfortran needs to process OpenMP directives extra_compile_args = ['-fopenmp'],
   extra_link_args = [],
)
return config

if __name__ == "__main__":
   setup(configuration = configuration)

All you need to do now, is basically to run "make test". Hopefully, your output should look something like mine:

Running with 1 thread...
   env OMP_NUM_THREADS = 1 python script.py
num_threads running: 1
Time elapsed: 2.17034 s
Running with 2 thread...
   env OMP_NUM_THREADS = 2 python script.py
num_threads running: 2
Time elapsed: 1.13914 s
Running with 4 thread...
   env OMP_NUM_THREADS = 4 python script.py
num_threads running: 4
Time elapsed: 0.60494 s
Running with 8 thread...
   env OMP_NUM_THREADS = 8 python script.py
num_threads running: 8
Time elapsed: 0.64650 s

My test program script.py reads as follows:

import time
import numpy
from wrapper
import w_fortranmodule

N = 10 ** 7
def main():
   xs = numpy.linspace(1, 10, N)
tstart = time.time()
ys = w_fortranmodule.w_test(xs)
dt = time.time() - tstart
print 'Time elapsed: %.5fs' % dt

if __name__ == '__main__':
   main()

Suggestion : 4

If you use conda to install wrf-python on a supercomputer like Yellowstone or Cheyenne, we recommend that you do not load any python related modules via the ‘module load’ command. The packages installed by the ‘module load’ system will not play nicely with packages installed via conda.,Further, some systems will install python packages to a ~/.local directory, which will be found by the miniconda python interpreter and cause various import problems. If you have a ~/.local directory, we strongly suggest renaming it (mv ~/.local ~/.local_backup).,Installation Required Dependencies Highly Recommended Packages Plotting Packages Installing via Conda Installing on Yellowstone Installing via Source Code ,On Yellowstone, wrf-python can also be installed using the module load system, if this is preferred over using conda.

conda install - c conda - forge wrf - python
module load gnu / 4.8 .2 or module swap intel gnu / 4.8 .2
module load python / 2.7 .7
module load numpy / 1.11 .0 wrapt / 1.10 .10 scipy / 0.17 .1 bottleneck / 1.1 .0 numexpr / 2.6 .0 pyside / 1.1 .2 matplotlib / 1.5 .1 pandas / 0.18 .1 netcdf4python / 1.2 .4 xarray / 0.8 .2
module load wrf - python / 1.0 .1
$ pip install.
cd.. / fortran / build_help

gfortran - o sizes - fopenmp omp_sizes.f90

python sub_sizes.py

cd..

gfortran - E ompgen.F90 - fopenmp - cpp - o omp.f90

f2py * .f90 - m _wrffortran - h wrffortran.pyf--overwrite - signature

cd..

python setup.py clean--all

python setup.py config_fc--f90flags = "-mtune=generic -fopenmp"
build_ext--libraries = "gomp"
build

pip install.
$ unset LDFLAGS python setup.py config_fc--f90flags = "-mtune=generic -fopenmp"
build_ext--libraries = "gomp"
build