8000 MNT New helper for effective number of OpenMP threads by jeremiedbb · Pull Request #14196 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

MNT New helper for effective number of OpenMP threads #14196

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

Merged
merged 14 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions sklearn/utils/_openmp_helpers.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
IF SKLEARN_OPENMP_SUPPORTED:
import os
cimport openmp
from joblib import cpu_count


cpdef _openmp_effective_n_threads(n_threads=None):
"""Determine the effective number of threads to be used for OpenMP calls

- For ``n_threads = None``,
- if the ``OMP_NUM_THREADS`` environment variable is set, return
``openmp.omp_get_max_threads()``
- otherwise, return the minimum between ``openmp.omp_get_max_threads()``
and the number of cpus, taking cgroups quotas into account. Cgroups
quotas can typically be set by tools such as Docker.
The result of ``omp_get_max_threads`` can be influenced by environment
variable ``OMP_NUM_THREADS`` or at runtime by ``omp_set_num_threads``.

- For ``n_threads > 0``, return this as the maximal number of threads for
parallel OpenMP calls.

- For ``n_threads < 0``, return the maximal number of threads minus
``|n_threads + 1|``. In particular ``n_threads = -1`` will use as many
threads as there are available cores on the machine.

- Raise a ValueError for ``n_threads = 0``.

If scikit-learn is built without OpenMP support, always return 1.
"""
if n_threads == 0:
raise ValueError("n_threads = 0 is invalid")

IF SKLEARN_OPENMP_SUPPORTED:
if os.getenv("OMP_NUM_THREADS"):
# Fall back to user provided number of threads making it possible
# to exceed the number of cpus.
max_n_threads = openmp.omp_get_max_threads()
else:
max_n_threads = min(openmp.omp_get_max_threads(), cpu_count())

if n_threads is None:
return max_n_threads
elif n_threads < 0:
return max(1, max_n_threads + n_threads + 1)

return n_threads
ELSE:
# OpenMP disabled at build-time => sequential mode
return 1


4 changes: 4 additions & 0 deletions sklearn/utils/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def configuration(parent_package='', top_path=None):
include_dirs=[numpy.get_include()],
libraries=libraries)

config.add_extension('_openmp_helpers',
sources=['_openmp_helpers.pyx'],
libraries=libraries)

# generate files from a template
pyx_templates = ['sklearn/utils/_seq_dataset.pyx.tp',
'sklearn/utils/_seq_dataset.pxd.tp']
Expand Down
0