From a86a632239f8a6c9284d54e80a6c9c09de2e0930 Mon Sep 17 00:00:00 2001 From: david-cortes Date: Tue, 24 Oct 2023 20:22:06 +0200 Subject: [PATCH] keep lgb threads separate from omp threads --- include/LightGBM/utils/openmp_wrapper.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/include/LightGBM/utils/openmp_wrapper.h b/include/LightGBM/utils/openmp_wrapper.h index a337fc353b75..18e5ab2c2c60 100644 --- a/include/LightGBM/utils/openmp_wrapper.h +++ b/include/LightGBM/utils/openmp_wrapper.h @@ -17,20 +17,17 @@ #include #include +static int lgb_global_omp_threads = omp_get_max_threads(); + 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(); } }