【问题标题】:Compiling cython with openMP support on OSX在 OSX 上使用 openMP 支持编译 cython
【发布时间】:2016-12-22 21:40:30
【问题描述】:

我正在使用 OSX v10.11.6 并安装了最新版本的 xcode。所以我的默认编译器是gcc,实际上是clang。我已经使用 homebrew 安装 gcc5 以便我可以使用 openMP,并且通过在我的 Makefiles 中为我的 C 源代码设置 CC := g++-5,我可以成功编译 C 源代码,并使用非平凡的 -fopenmp。

我想做的是让 Cython 使用 gcc5 进行编译,这样我就可以使用 Cython 的原生 prange 功能,如最小示例 here 中所示。我在this gist 中写了一个最小的例子,从 Neal Hughes 页面借来的。当我尝试使用 setup.py 编译 omp_testing.pyx 时,我收到一个(可能不相关的)警告和致命错误:

cc1plus: warning: command line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++
omp_testing.cpp:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
 #error Do not use this file, it is the result of a failed Cython compilation.
  ^
error: command 'g++-5' failed with exit status 1

阅读How to tell distutils to use gcc? 后,我尝试在setup.py 中设置CC 环境变量,但这不起作用。我应该如何修改我的 Cython setup.py 文件以使用 g++-5 进行编译?

【问题讨论】:

  • 该错误与 GCC 无关。 Cython 处理步骤失败,留下一个旨在导致编译错误的文件。
  • 你能详细说明@DavidW吗?
  • setup.py 做了(至少)两件事。它使用 Cython 将您的 .pyx 文件处理为 .c 文件,然后它使用您的 C 编译器编译 C 文件。如果 Cython 无法处理 .pyx 它会产生一些有用的错误输出,告诉你它为什么不开心,它会产生一个 omp_testing.c 文件,其中包含一个 #error 行,告诉任何 C 编译器停止。当您运行setup.py 时,您应该会看到一些额外的错误消息(前几条通常最有帮助)。如果失败,您可以自己从命令行运行 cython omp_testing.pyx 以查看问题所在。

标签: python c++ macos gcc cython


【解决方案1】:

显然,Apple 前一段时间放弃了对 OpenMP 的支持,因此,您无法使用标准 gcc 编译包含此依赖项的代码。解决这个问题的一个好方法是安装 LLVM 并使用它进行编译。这是对我有用的顺序:

安装 LLVM:

brew install llvm

在 setup.py 中包含 OpenMP 标志(-fopenmp -lomp):

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize, build_ext


exts = [Extension(name='name_of_your_module',
                  sources=['your_module.pyx'],
                  extra_compile_args=['-fopenmp'],
                  extra_link_args=['-lomp']
                  )]

import numpy as np

setup(name = 'name_of_your_module',
      ext_modules=cythonize(exts,
      include_dirs=[np.get_include()],
      cmdclass={'build_ext': build_ext})

然后用LLVM编译代码:

CC=/usr/local/opt/llvm/bin/clang++ python setup.py build_ext --inplace

这应该会产生一个并行化的 .so

【讨论】:

  • 当 Apple 不提供 OpenMP 开发人员库时,您如何解释这一点?必须先构建它们或通过 Brew 安装,还是将它们与 llvm 一起引入?我知道 OpenMP 是 LLVM 的子项目,但我认为您必须从源代码编译开发文件并手动包含...
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多