首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OSX上编译具有openMP支持的cython

在OSX上编译具有openMP支持的cython
EN

Stack Overflow用户
提问于 2016-12-23 05:40:31
回答 1查看 1.2K关注 0票数 1

我使用的是安装了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时,我得到一个(可能不相关的)警告和致命错误:

代码语言:javascript
复制
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进行编译?

EN

回答 1

Stack Overflow用户

发布于 2019-01-12 04:09:48

显然,苹果在一段时间前就放弃了对OpenMP的支持,因此,你不能用标准的gcc来编译包含这种依赖的代码。解决这个问题的一个好方法是安装LLVM并使用它进行编译。以下是对我起作用的序列:

安装LLVM:

代码语言:javascript
复制
brew install llvm

将OpenMP标志(-fopenmp -lomp)包含到setup.py:

代码语言:javascript
复制
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编译代码:

代码语言:javascript
复制
CC=/usr/local/opt/llvm/bin/clang++ python setup.py build_ext --inplace

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41292059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档