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

[tests][R-package] run all tests using 2 threads (fixes #5102) #5367

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions R-package/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Sys.setenv(LGBM_DEFAULT_NUM_THREADS = "2")
library(testthat)
library(lightgbm)

Expand Down
18 changes: 17 additions & 1 deletion include/LightGBM/utils/openmp_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <LightGBM/utils/log.h>

#include <omp.h>
#include <stdlib.h>

#include <exception>
#include <memory>
Expand All @@ -25,9 +26,24 @@ inline int OMP_NUM_THREADS() {
return ret;
}

inline int LGBM_DEFAULT_NUM_THREADS() {
const char* val = std::getenv("LGBM_DEFAULT_NUM_THREADS");
if (val) {
char* end;
int32_t ans = strtol(val, &end, 10);
if (ans > 0) { // strtol returns 0 if no valid conversion could be performed
return static_cast<int>(ans);
}
}
return -1;
}

inline void OMP_SET_NUM_THREADS(int num_threads) {
static const int default_omp_num_threads = OMP_NUM_THREADS();
if (num_threads > 0) {
static const int lgbm_default_num_threads = LGBM_DEFAULT_NUM_THREADS();
if (lgbm_default_num_threads > 0) {
omp_set_num_threads(lgbm_default_num_threads);
} else if (num_threads > 0) {
omp_set_num_threads(num_threads);
} else {
omp_set_num_threads(default_omp_num_threads);
Expand Down