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

Keep number of threads in a global variable separate from global OMP config #6152

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 5 additions & 8 deletions include/LightGBM/utils/openmp_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@
#include <stdexcept>
#include <vector>

static int lgb_global_omp_threads = omp_get_max_threads();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been testing an approach like this over on my fork (jameslamb#55) and I don't think just having static int in a header here will work.

I found that when I did that, everything that included openmp_wrapper.h got its own copy of that integer, and that therefore updates like this:

OMP_SET_NUM_THREADS(config_.num_threads);

did not propagate through everywhere.


inline int OMP_NUM_THREADS() {
int ret = 1;
#pragma omp parallel
#pragma omp master
{ ret = omp_get_num_threads(); }
return ret;
return lgb_global_omp_threads;
}

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);
lgb_global_omp_threads = num_threads;
} else {
omp_set_num_threads(default_omp_num_threads);
lgb_global_omp_threads = omp_get_max_threads();
}
}

Expand Down