简体   繁体   English

在OSX上使用openMP支持编译cython

[英]Compiling cython with openMP support on OSX

I'm using OSX v10.11.6 with a recent version of xcode installed. 我正在使用OSX v10.11.6并安装了最新版本的xcode。 So my default compiler is gcc , which is really clang . 所以我的默认编译器是gcc ,这真的很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. 我使用自制软件来安装gcc5以便我可以使用openMP,并在我的Makefiles中设置CC := g++-5作为我在C中的源代码,我可以成功编译C源代码,并且非常简单地使用-fopenmp。

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 . 我想要做的就是用Cython与gcc5编译,这样我可以用用Cython的本地PRANGE功能,如小例子证明了这里 I have written a minimal example in this gist , borrowed from the Neal Hughes page. 我在这个要点中写了一个最小的例子,借用了Neal Hughes页面。 When I attempt to compile omp_testing.pyx using setup.py , I get a (possibly unrelated) warning, and fatal error: 当我尝试使用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

After reading How to tell distutils to use gcc? 看完如何告诉distutils使用gcc? , what I attempted was setting the CC environment variable inside setup.py , but this did not work. ,我试图在setup.py设置CC环境变量,但这不起作用。 How should I modify my Cython setup.py file to compile using g++-5? 我应该如何修改我的Cython setup.py文件以使用g ++ - 5进行编译?

Apparently, Apple dropped the support of OpenMP sometime ago, therefore, you cannot compile the code that includes this dependency with a standard gcc. 显然,Apple不久前放弃了OpenMP的支持,因此,您无法使用标准gcc编译包含此依赖关系的代码。 A good way to get around that is to install LLVM and compile with it. 解决这个问题的一个好方法是安装LLVM并使用它进行编译。 Here is the sequence that worked for me: 这是对我有用的序列:

Install LLVM: 安装LLVM:

brew install llvm

Include OpenMP flags(-fopenmp -lomp) to setup.py: 在Open.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})

And then compile the code with LLVM: 然后使用LLVM编译代码:

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

This should result in a parallelized .so 这应该导致并行化的.so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM