【问题标题】:f2py: using openMP parallel in fortran failsf2py:在 fortran 中使用 openMP 并行失败
【发布时间】:2018-03-25 09:00:15
【问题描述】:

我正在尝试为使用f2py 的python 编译一个使用openMP 的fortran 例程。

这是文件bsp.f90

module OTmod
  !$ use omp_lib
  implicit none

  public :: get_threads

contains

  function get_threads() result(nt)
    integer :: nt

    nt = 0
    !$ nt = omp_get_max_threads()

    !$omp parallel num_threads(nt)
        write( *, * ) 'hello world!'
    !$omp end parallel

  end function get_threads

end module OTmod

如果我用它编译它

f2py -m testmod --fcompiler=gfortran --f90flags='-fopenmp' -lgomp -c bsp.f90

编译有效,但将其导入 python 失败并出现错误

ImportError: dlopen(/Users/USER/omp_py/testmod/testmod.cpython-36m-darwin.so, 2): Symbol not found: _GOMP_parallel
  Referenced from: /Users/USER/omp_py/testmod/testmod.cpython-36m-darwin.so
  Expected in: flat namespace
 in /Users/USER/omp_py/testmod/testmod.cpython-36m-darwin.so

但是,如果我只删除构成并行循环的两条线

!$omp parallel num_threads(nt)

!$omp end parallel

然后导入python并执行它可以工作,所以omp_get_max_threads()可以工作,但是并行循环会导致它失败。这是 Anaconda 和我的 brew install gcc 之间的一些不匹配吗?我该如何解决这个问题?


系统设置信息如下


OS

macOS 10.13

python

Python 3.6.2 |Anaconda custom (x86_64)| (default, Sep 21 2017, 18:29:43) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin

gfortran -v

Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin17.0.0/7.2.0/lto-wrapper
Target: x86_64-apple-darwin17.0.0
Configured with: ../configure --build=x86_64-apple-darwin17.0.0 --prefix=/usr/local/Cellar/gcc/7.2.0 --libdir=/usr/local/Cellar/gcc/7.2.0/lib/gcc/7 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-7 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-checking=release --with-pkgversion='Homebrew GCC 7.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-nls
Thread model: posix
gcc version 7.2.0 (Homebrew GCC 7.2.0) 

f2py -c --help-fcompiler

Gnu95FCompiler instance properties:
  archiver        = ['/usr/local/bin/gfortran', '-cr']
  compile_switch  = '-c'
  compiler_f77    = ['/usr/local/bin/gfortran', '-Wall', '-g', '-ffixed-
                    form', '-fno-second-underscore', '-m64', '-fPIC', '-O3', '
                    -funroll-loops']
  compiler_f90    = ['/usr/local/bin/gfortran', '-Wall', '-g', '-fno-second-
                    underscore', '-m64', '-fPIC', '-O3', '-funroll-loops']
  compiler_fix    = ['/usr/local/bin/gfortran', '-Wall', '-g', '-ffixed-
                    form', '-fno-second-underscore', '-Wall', '-g', '-fno-
                    second-underscore', '-m64', '-fPIC', '-O3', '-funroll-
                    loops']
  libraries       = ['gfortran']
  library_dirs    = ['/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple
                    -darwin17.0.0/7.2.0']
  linker_exe      = ['/usr/local/bin/gfortran', '-Wall', '-Wall']
  linker_so       = ['/usr/local/bin/gfortran', '-Wall', '-g', '-m64', '-
                    Wall', '-g', '-undefined', 'dynamic_lookup', '-bundle']
  object_switch   = '-o '
  ranlib          = ['/usr/local/bin/gfortran']
  version         = LooseVersion ('7.2.0')
  version_cmd     = ['/usr/local/bin/gfortran', '-dumpversion']
Fortran compilers found:
  --fcompiler=gnu95  GNU Fortran 95 compiler (7.2.0)

otool -L testmod.cpython-36m-darwin.so

/Users/USER/omp_py/testmod/testmod.cpython-36m-darwin.so:
    @rpath/libgomp.1.dylib (compatibility version 2.0.0, current version 2.0.0)
    @rpath/libgfortran.3.dylib (compatibility version 4.0.0, current version 4.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.0.0)
    /usr/local/lib/gcc/7/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    @rpath/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)

【问题讨论】:

  • 运行 otool -L /Users/USER/omp_py/testmod/testmod.cpython-36m-darwin.so 看看你是否找到 gomp 参考
  • 我将输出添加到问题的末尾。
  • 你能找到libgomp.1.dylib在你系统上的位置吗?
  • 我唯一能找到它的地方是 homebrew 和 anaconda 安装:1) /usr/local/Cellar/gcc/7.2.0/lib/gcc/7/i386/libgomp.1.dylib 2) /usr/local/Cellar/gcc/7.2.0/lib/gcc/7/libgomp.1.dylib 3) /Users/USER/anaconda3/pkgs/libgfortran-3.0.1-h93005f0_2/lib/libgomp.1.dylib 4) /Users/USER/anaconda3/lib/libgomp.1.dylib
  • 尝试设置LD_LIBRARY_PATH="/Users/USER/anaconda3/pkgs/libgfortran-3.0.1-h93005f0_2/lib‌​/:/Users/USER/anaconda3/lib/",然后启动您的程序,看看是否有帮助?

标签: python macos openmp f2py macos-high-sierra


【解决方案1】:

我不确定如何解决这个动态链接问题。它主要与 LD_LIBRARY_PATH 或其他东西有关。

但作为一种解决方法,您可以对库进行 static 链接

-static 选项添加到setup.py 中的链接器选项

extra_link_args=['-lgomp -static']

这将消除在运行时定位依赖项的需要

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2021-09-11
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    相关资源
    最近更新 更多