Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Update setup and travis to support OpenMP #13053

Merged
merged 10 commits into from Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions build_tools/travis/install.sh
Expand Up @@ -25,6 +25,22 @@ then
# export CCACHE_LOGFILE=/tmp/ccache.log
# ~60M is used by .ccache when compiling from scratch at the time of writing
ccache --max-size 100M --show-stats
elif [ $TRAVIS_OS_NAME = "osx" ]
then
# install OpenMP not present by default on osx
brew install libomp

# enable OpenMP support for Apple-clang
export CC=clang
export CXX=clang++
export CPPFLAGS="$CPPFLAGS -Xpreprocessor -fopenmp"
export CFLAGS="$CFLAGS -I/usr/local/opt/libomp/include"
export CXXFLAGS="$CXXFLAGS -I/usr/local/opt/libomp/include"
export LDFLAGS="$LDFLAGS -L/usr/local/opt/libomp/lib -lomp"
export DYLD_LIBRARY_PATH=/usr/local/opt/libomp/lib

# avoid error due to multiple OpenMP libraries loaded simultaneously
export KMP_DUPLICATE_LIB_OK=TRUE
fi

make_conda() {
Expand Down
29 changes: 27 additions & 2 deletions doc/developers/advanced_installation.rst
Expand Up @@ -46,7 +46,8 @@ Scikit-learn requires:

Building Scikit-learn also requires

- Cython >=0.28.5
- Cython >=0.28.5
- OpenMP

Running tests requires

Expand Down Expand Up @@ -102,6 +103,30 @@ On Unix-like systems, you can simply type ``make`` in the top-level folder to
build in-place and launch all the tests. Have a look at the ``Makefile`` for
additional utilities.

Mac OSX
-------

The default C compiler, Apple-clang, on Mac OSX does not directly support
OpenMP. The first solution to build scikit-learn is to install another C
compiler such as gcc or llvm-clang. Another solution is to enable OpenMP
support on the default Apple-clang.
ogrisel marked this conversation as resolved.
Show resolved Hide resolved

You first need to install the OpenMP library::

brew install libomp

Then you need to set the following environment variables::

export CC=clang
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we specify clang explicitly?

export CC=/usr/bin/clang 
export CXX=/usr/bin/clang++

This is to make sure the system clang is used and not clang from something like anaconda.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeremiedbb this comment was not addressed prior to merging the PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I thought it was for the travis build, and not for the install doc, but it's been added in #13093

export CXX=clang++
export CPPFLAGS="$CPPFLAGS -Xpreprocessor -fopenmp"
export CFLAGS="$CFLAGS -I/usr/local/opt/libomp/include"
export CXXFLAGS="$CXXFLAGS -I/usr/local/opt/libomp/include"
export LDFLAGS="$LDFLAGS -L/usr/local/opt/libomp/lib -lomp"
export DYLD_LIBRARY_PATH=/usr/local/opt/libomp/lib

Finally you can build the package using the standard command.

Installing build dependencies
=============================

Expand All @@ -111,7 +136,7 @@ Linux
Installing from source requires you to have installed the scikit-learn runtime
dependencies, Python development headers and a working C/C++ compiler.
Under Debian-based operating systems, which include Ubuntu::

sudo apt-get install build-essential python3-dev python3-setuptools \
python3-numpy python3-scipy \
libatlas-dev libatlas3-base
Expand Down
53 changes: 52 additions & 1 deletion setup.py
Expand Up @@ -102,7 +102,58 @@ def run(self):
shutil.rmtree(os.path.join(dirpath, dirname))


cmdclass = {'clean': CleanCommand}
def get_openmp_flag(compiler):
if sys.platform == "win32" and ('icc' in compiler or 'icl' in compiler):
return ['/Qopenmp']
elif sys.platform == "win32":
return ['/openmp']
elif sys.platform == "darwin" and ('icc' in compiler or 'icl' in compiler):
return ['-openmp']
elif sys.platform == "darwin" and 'openmp' in os.getenv('CPPFLAGS', ''):
# -fopenmp can't be passed as compile flag when using Apple-clang.
# OpenMP support has to be enabled during preprocessing.
#
# For example, our macOS wheel build jobs use the following environment
# variables to build with Apple-clang and the brew installed "libomp":
#
# export CPPFLAGS="$CPPFLAGS -Xpreprocessor -fopenmp"
# export CFLAGS="$CFLAGS -I/usr/local/opt/libomp/include"
# export LDFLAGS="$LDFLAGS -L/usr/local/opt/libomp/lib -lomp"
# export DYLD_LIBRARY_PATH=/usr/local/opt/libomp/lib
return ['']
# Default flag for GCC and clang:
return ['-fopenmp']
ogrisel marked this conversation as resolved.
Show resolved Hide resolved


OPENMP_EXTENSIONS = ["sklearn.cluster._k_means_lloyd",
ogrisel marked this conversation as resolved.
Show resolved Hide resolved
"sklearn.cluster._k_means_elkan"]


# custom build_ext command to set OpenMP compile flags depending on os and
# compiler
# build_ext has to be imported after setuptools
from numpy.distutils.command.build_ext import build_ext # noqa


class build_ext_subclass(build_ext):
def build_extensions(self):
if hasattr(self.compiler, 'compiler'):
compiler = self.compiler.compiler[0]
else:
compiler = self.compiler.__class__.__name__

openmp_flag = get_openmp_flag(compiler)

for e in self.extensions:
if e.name in OPENMP_EXTENSIONS:
e.extra_compile_args += openmp_flag
e.extra_link_args += openmp_flag

build_ext.build_extensions(self)


cmdclass = {'clean': CleanCommand, 'build_ext': build_ext_subclass}


# Optional wheelhouse-uploader features
# To automate release of binary packages for scikit-learn we need a tool
Expand Down