Skip to content

Commit 4f8ee3a

Browse files
authored
fix: reduce number of threads to 1 on rush worker (#281)
1 parent 0cbfda7 commit 4f8ee3a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Suggests:
4343
progressr,
4444
processx,
4545
redux,
46+
RhpcBLASctl,
4647
testthat (>= 3.0.0),
4748
rush (>= 0.1.2)
4849
Remotes:

R/worker_loops.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ bbotk_worker_loop = function(rush, optimizer, instance) {
1717
instance$rush = rush
1818
instance$archive$rush = rush
1919

20+
# reduce number of threads to 1
21+
old_dt = data.table::getDTthreads()
22+
on.exit(data.table::setDTthreads(old_dt), add = TRUE)
23+
data.table::setDTthreads(1, restore_after_fork = TRUE)
24+
25+
# RhpcBLASctl is licensed under AGPL and therefore should be in suggest
26+
if (require_namespaces("RhpcBLASctl", quietly = TRUE)) {
27+
old_blas_threads = RhpcBLASctl::blas_get_num_procs()
28+
on.exit(RhpcBLASctl::blas_set_num_threads(old_blas_threads), add = TRUE)
29+
RhpcBLASctl::blas_set_num_threads(1)
30+
} else { # try the bare minimum to disable threading of the most popular blas implementations
31+
old_blas = Sys.getenv("OPENBLAS_NUM_THREADS")
32+
old_mkl = Sys.getenv("MKL_NUM_THREADS")
33+
Sys.setenv(OPENBLAS_NUM_THREADS = 1)
34+
Sys.setenv(MKL_NUM_THREADS = 1)
35+
36+
on.exit({
37+
Sys.setenv(OPENBLAS_NUM_THREADS = old_blas)
38+
Sys.setenv(MKL_NUM_THREADS = old_mkl)
39+
}, add = TRUE)
40+
}
41+
2042
call_back("on_worker_begin", instance$objective$callbacks, instance$objective$context)
2143

2244
# run optimizer loop

0 commit comments

Comments
 (0)