-
-
Notifications
You must be signed in to change notification settings - Fork 26k
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
NicolasHug
merged 14 commits into
scikit-learn:master
from
jeremiedbb:openmp-effective-njobs
Oct 17, 2019
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d8439fd
openmp helper equivalent of effective_n_jobs
jeremiedbb b8900ab
protect openmp calls
jeremiedbb e47bdb8
comment openmp max threads
jeremiedbb 8050149
right place comment
jeremiedbb 280f551
don't import joblib if unecessary
jeremiedbb 1d8ade5
Merge branch 'master' into openmp-effective-njobs
jeremiedbb 2623403
make module private
jeremiedbb 322f499
revert ht eunintended revert | cln
jeremiedbb 969b637
Merge branch 'master' into openmp-effective-njobs
jeremiedbb 34d7735
allow more user control + more comments
jeremiedbb 41691ac
fix indent
jeremiedbb fe1f75f
import os :)
jeremiedbb 11be712
address comments
jeremiedbb eca64f7
better docstring
jeremiedbb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.