set explicit number of threads in every OpenMP parallel
region
#6135
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.
Contributes to #4705
Contributes to #5124
Contributes to #5987
Short Description
Proposes adding an explicit
num_threads
clause to every OpenMPparallel
region in this project that doesn't have one, and a CI check to enforce that.As of this PR, such regions will still fall back to the global value from
omp_get_num_threads()
... I'll resolve that and propose switching them to a LightGBM-controlled value in a follow-up PR.Longer Description
LightGBM uses OpenMP for to parallelize many operations via multi-threading.
It allows users to customize the degree of parallelism (i.e. number of threads) by passing through a parameter
num_threads
in several parts of the the public API, which is then used to set the global1 default number of threads for OpenMP viaomp_set_num_threads()
.LightGBM/src/c_api.cpp
Line 2454 in 992f505
LightGBM/include/LightGBM/utils/openmp_wrapper.h
Lines 28 to 35 in 992f505
Many parallel regions in the project then implicitly rely on that global configuration to determine the number of threads used, in blocks like this without a
num_threads()
clause:LightGBM/src/c_api.cpp
Lines 440 to 441 in 992f505
The MSVC docs for OpenMP2 have a good description of how OpenMP implementations determine the number of threads for a
parallel
region without an explicitnum_threads
clause:As described in #4705, that reliance on global state can lead to undesirable side effects. LightGBM's use of
omp_set_num_threads()
affects all OpenMP-using routines in the same process, and conversely LightGBM can be affected by any other programs in the same process which callomp_set_num_threads()
.Footnotes
By "global" here, I mean "affecting and affected by all OpenMP-using routine in the same process". ↩
https://learn.microsoft.com/en-us/cpp/parallel/openmp/2-directives?view=msvc-170#23-parallel-construct. See also the OpenMP spec: https://www.openmp.org/spec-html/5.0/openmpse14.html#x54-800002.6. ↩