Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cac1672

Browse files
authored
CI Fix pre-build checks to handle compilers from build_ext options (#16193)
1 parent 342d3e4 commit cac1672

File tree

7 files changed

+122
-8
lines changed

7 files changed

+122
-8
lines changed

.travis.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ matrix:
2222
# Linux environment to test scikit-learn against numpy and scipy master
2323
# installed from their CI wheels in a virtualenv with the Python
2424
# interpreter provided by travis.
25-
- python: 3.7
26-
env: CHECK_WARNINGS="true"
27-
if: type = cron OR commit_message =~ /\[scipy-dev\]/
25+
- python: 3.7
26+
env: CHECK_WARNINGS="true"
27+
if: type = cron OR commit_message =~ /\[scipy-dev\]/
28+
29+
# As above but build scikit-learn with Intel C compiler (ICC).
30+
- python: 3.7
31+
env:
32+
- CHECK_WARNING="true"
33+
- BUILD_WITH_ICC="true"
34+
if: type = cron OR commit_message =~ /\[icc-build\]/
2835

2936
install: source build_tools/travis/install.sh
3037
script:

build_tools/travis/install.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ python --version
6666
python -c "import numpy; print('numpy %s' % numpy.__version__)"
6767
python -c "import scipy; print('scipy %s' % scipy.__version__)"
6868

69+
if [[ "$BUILD_WITH_ICC" == "true" ]]; then
70+
wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB
71+
sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB
72+
rm GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB
73+
sudo add-apt-repository "deb https://apt.repos.intel.com/oneapi all main"
74+
sudo apt-get update
75+
sudo apt-get install intel-oneapi-icc
76+
source /opt/intel/inteloneapi/setvars.sh
77+
78+
# The build_clib command is implicitly used to build libsvm-skl. To compile
79+
# with a different compiler we also need to specify the compiler for this
80+
# command.
81+
python setup.py build_ext --compiler=intelem -i -j 3 build_clib --compiler=intelem
82+
else
83+
# Use setup.py instead of `pip install -e .` to be able to pass the -j flag
84+
# to speed-up the building multicore CI machines.
85+
python setup.py build_ext --inplace -j 3
86+
fi
87+
6988
python setup.py develop
7089

7190
ccache --show-stats

build_tools/travis/test_docs.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
set -e
44
set -x
55

6+
if [[ "$BUILD_WITH_ICC" == "true" ]]; then
7+
# the tools in the oneAPI toolkits are configured via environment variables
8+
# which are also required at runtime.
9+
source /opt/intel/inteloneapi/setvars.sh
10+
fi
11+
612
make test-doc

build_tools/travis/test_script.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ except ImportError:
2020
"
2121
python -c "import multiprocessing as mp; print('%d CPUs' % mp.cpu_count())"
2222

23+
if [[ "$BUILD_WITH_ICC" == "true" ]]; then
24+
# the tools in the oneAPI toolkits are configured via environment variables
25+
# which are also required at runtime.
26+
source /opt/intel/inteloneapi/setvars.sh
27+
fi
28+
2329
run_tests() {
2430
TEST_CMD="pytest --showlocals --durations=20 --pyargs"
2531

doc/developers/advanced_installation.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,53 @@ the base system and these steps will not be necessary.
388388
.. _Homebrew: https://brew.sh
389389
.. _virtualenv: https://docs.python.org/3/tutorial/venv.html
390390
.. _conda environment: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
391+
392+
Alternative compilers
393+
=====================
394+
395+
The command::
396+
397+
pip install --verbose --editable .
398+
399+
will build scikit-learn using your default C/C++ compiler. If you want to build
400+
scikit-learn with another compiler handled by ``distutils`` or by
401+
``numpy.distutils``, use the following command::
402+
403+
python setup.py build_ext --compiler=<compiler> -i build_clib --compiler=<compiler>
404+
405+
To see the list of available compilers run::
406+
407+
python setup.py build_ext --help-compiler
408+
409+
If your compiler is not listed here, you can specify it via the ``CC`` and
410+
``LDSHARED`` environment variables (does not work on windows)::
411+
412+
CC=<compiler> LDSHARED="<compiler> -shared" python setup.py build_ext -i
413+
414+
Building with Intel C Compiler (ICC) using oneAPI on Linux
415+
----------------------------------------------------------
416+
417+
Intel provides access to all of its oneAPI toolkits and packages through a
418+
public APT repository. First you need to get and install the public key of this
419+
repository::
420+
421+
wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB
422+
sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB
423+
rm GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB
424+
425+
Then, add the oneAPI repository to your APT repositories::
426+
427+
sudo add-apt-repository "deb https://apt.repos.intel.com/oneapi all main"
428+
sudo apt-get update
429+
430+
Install ICC, packaged under the name ``intel-oneapi-icc``::
431+
432+
sudo apt-get install intel-oneapi-icc
433+
434+
Before using ICC, you need to set up environment variables::
435+
436+
source /opt/intel/inteloneapi/setvars.sh
437+
438+
Finally, you can build scikit-learn. For example on Linux x86_64::
439+
440+
python setup.py build_ext --compiler=intelem -i build_clib --compiler=intelem

sklearn/_build_utils/openmp_helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Helpers for OpenMP support during the build."""
22

33
# This code is adapted for a large part from the astropy openmp helpers, which
4-
# can be found at: https://github.com/astropy/astropy-helpers/blob/master/astropy_helpers/openmp_helpers.py # noqa
4+
# can be found at: https://github.com/astropy/extension-helpers/blob/master/extension_helpers/_openmp_helpers.py # noqa
55

66

77
import os
@@ -25,8 +25,8 @@ def get_openmp_flag(compiler):
2525
return ['/Qopenmp']
2626
elif sys.platform == "win32":
2727
return ['/openmp']
28-
elif sys.platform == "darwin" and ('icc' in compiler or 'icl' in compiler):
29-
return ['-openmp']
28+
elif sys.platform in ("darwin", "linux") and "icc" in compiler:
29+
return ['-qopenmp']
3030
elif sys.platform == "darwin" and 'openmp' in os.getenv('CPPFLAGS', ''):
3131
# -fopenmp can't be passed as compile flag when using Apple-clang.
3232
# OpenMP support has to be enabled during preprocessing.

sklearn/_build_utils/pre_build_helpers.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,40 @@
77
import textwrap
88
import subprocess
99

10+
from distutils.dist import Distribution
1011
from distutils.sysconfig import customize_compiler
1112
from numpy.distutils.ccompiler import new_compiler
13+
from numpy.distutils.command.config_compiler import config_cc
14+
15+
16+
def _get_compiler():
17+
"""Get a compiler equivalent to the one that will be used to build sklearn
18+
19+
Handles compiler specified as follows:
20+
- python setup.py build_ext --compiler=<compiler>
21+
- CC=<compiler> python setup.py build_ext
22+
"""
23+
dist = Distribution({'script_name': os.path.basename(sys.argv[0]),
24+
'script_args': sys.argv[1:],
25+
'cmdclass': {'config_cc': config_cc}})
26+
dist.parse_config_files()
27+
dist.parse_command_line()
28+
29+
cmd_opts = dist.command_options.get('build_ext')
30+
if cmd_opts is not None and 'compiler' in cmd_opts:
31+
compiler = cmd_opts['compiler'][1]
32+
else:
33+
compiler = None
34+
35+
ccompiler = new_compiler(compiler=compiler)
36+
customize_compiler(ccompiler)
37+
38+
return ccompiler
1239

1340

1441
def compile_test_program(code, extra_preargs=[], extra_postargs=[]):
1542
"""Check that some C code can be compiled and run"""
16-
ccompiler = new_compiler()
17-
customize_compiler(ccompiler)
43+
ccompiler = _get_compiler()
1844

1945
# extra_(pre/post)args can be a callable to make it possible to get its
2046
# value from the compiler

0 commit comments

Comments
 (0)