aph
aph

Reputation: 1855

Compiling cython with openMP support on OSX

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.

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

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?

Upvotes: 2

Views: 1466

Answers (1)

Alexander Ponamarev
Alexander Ponamarev

Reputation: 66

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:

Install LLVM:

brew install llvm

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})

And then compile the code with LLVM:

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

This should result in a parallelized .so

Upvotes: 1

Related Questions