在OSX上使用openMP支持编译cython

时间:2016-12-22 21:40:31

标签: python c++ macos gcc cython

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

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

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?之后,我尝试了在CC内设置setup.py环境变量,但这不起作用。我应该如何修改我的Cython setup.py文件以使用g ++ - 5进行编译?

1 个答案:

答案 0 :(得分:0)

显然,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

相关问题