在OSX上编译带有OpenMP支持的cython [英] Compiling cython with openMP support on OSX

查看:43
本文介绍了在OSX上编译带有OpenMP支持的cython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using OSX v10.11.6 with a recent version of xcode installed. So my default compiler is gcc, which is really clang. I have used homebrew to install gcc5 so that I can use openMP, and by setting CC := g++-5 in my Makefiles for my source code in C, I can successfully compile C source code with non-trivial usage of -fopenmp.

我想做的是让Cython与gcc5一起编译,以便我可以使用Cython的本机prange功能,如最小示例中所示此要点中写了一个最小的示例,该示例取自Neal Hughes页面.当我尝试使用 setup.py 编译 omp_testing.pyx 时,我收到(可能不相关的)警告和致命错误:

What I want to do is get Cython to compile with gcc5 so that I can use Cython's native prange feature, as demonstrated in a minimal example here. I have written a minimal example in this gist, borrowed from the Neal Hughes page. When I attempt to compile omp_testing.pyx using setup.py, I get a (possibly unrelated) warning, and fatal error:

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

阅读如何告诉distutils使用gcc吗?,我试图在 setup.py 中设置 CC 环境变量,但这没有用.我应该如何修改Cython setup.py 文件以使用g ++-5进行编译?

After reading How to tell distutils to use gcc?, what I attempted was setting the CC environment variable inside setup.py, but this did not work. How should I modify my Cython setup.py file to compile using g++-5?

推荐答案

很显然,Apple在某个时候放弃了对OpenMP的支持,因此,您不能使用标准gcc编译包含此依赖项的代码.解决该问题的一个好方法是安装LLVM并对其进行编译.这是对我有用的顺序:

Apparently, Apple dropped the support of OpenMP sometime ago, therefore, you cannot compile the code that includes this dependency with a standard gcc. A good way to get around that is to install LLVM and compile with it. Here is the sequence that worked for me:

安装LLVM:

brew install llvm

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

Include OpenMP flags(-fopenmp -lomp) to 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编译代码:

And then compile the code with LLVM:

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

这应该导致并行化.so

This should result in a parallelized .so

这篇关于在OSX上编译带有OpenMP支持的cython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆