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

factor out uses of omp_get_num_threads() and omp_get_max_threads() outside of OpenMP wrapper #6133

Merged
merged 2 commits into from
Oct 9, 2023

Conversation

jameslamb
Copy link
Collaborator

Contributes to #4705.
Contributes to #5987.

LightGBM uses OpenMP for to parallelize many operations via multi-threading.

It's C/C++ codebase uses some wrapper functions defined in a header file openmp_wrapper.h to get and set the number of threads to use in such operations:

inline int OMP_NUM_THREADS() {
int ret = 1;
#pragma omp parallel
#pragma omp master
{ ret = omp_get_num_threads(); }
return ret;
}
inline void OMP_SET_NUM_THREADS(int num_threads) {
static const int default_omp_num_threads = OMP_NUM_THREADS();
if (num_threads > 0) {
omp_set_num_threads(num_threads);
} else {
omp_set_num_threads(default_omp_num_threads);
}
}

There are a few places in the code which bypass those helpers and instead directly call omp_get_num_threads() and omp_get_max_threads().

This PR proposes switching such calls to instead use LightGBM's wrappers. That reduces the number of different code paths that multi-threading configuration goes through, therefore reducing the risk of configuration inconsistencies and issues like #4705 and #5124.

@jameslamb jameslamb changed the title WIP: factor out uses of omp_get_num_threads() and omp_get_max_threads() out of OpenMP wrapper factor out uses of omp_get_num_threads() and omp_get_max_threads() outside of OpenMP wrapper Oct 8, 2023
@jameslamb jameslamb marked this pull request as ready for review October 9, 2023 01:35
@jameslamb
Copy link
Collaborator Author

thanks so much for the quick reviews @guolinke !

@jameslamb jameslamb merged commit 992f505 into master Oct 9, 2023
41 checks passed
@jameslamb jameslamb deleted the tighter-thread-control branch October 9, 2023 03:43
Ten0 pushed a commit to Ten0/LightGBM that referenced this pull request Jan 12, 2024
Copy link

github-actions bot commented Oct 9, 2024

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants