我使用的是安装了xcode最新版本的OSXv10.11.6。所以我的默认编译器是gcc,它实际上是clang。我已经使用自制软件安装了gcc5,这样我就可以使用openMP了,并且通过在我的Makefiles中为我的C源代码设置CC := g++-5,我可以成功地使用-fopenmp编译C源代码。
我想要做的是让Cython使用gcc5进行编译,这样我就可以使用Cython的原生prange特性,如一个最小的示例here所示。我从Neal Hughes的页面上借用,用this gist写了一个最小的例子。当我尝试使用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进行编译?
发布于 2019-01-12 04:09:48
显然,苹果在一段时间前就放弃了对OpenMP的支持,因此,你不能用标准的gcc来编译包含这种依赖的代码。解决这个问题的一个好方法是安装LLVM并使用它进行编译。以下是对我起作用的序列:
安装LLVM:
brew install llvm将OpenMP标志(-fopenmp -lomp)包含到setup.py:
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
https://stackoverflow.com/questions/41292059
复制相似问题