Skip to content

Commit 7c81941

Browse files
staticfloatKristofferC
authored andcommitted
Limit initial OpenBLAS thread count (#46844)
* Limit initial OpenBLAS thread count We set OpenBLAS's initial thread count to `1` to prevent runaway allocation within OpenBLAS's initial thread startup. LinearAlgebra will later call `BLAS.set_num_threads()` to the actual value we require. * Support older names (cherry picked from commit 58b559f)
1 parent 5bbd01d commit 7c81941

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

stdlib/OpenBLAS_jll/src/OpenBLAS_jll.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ function __init__()
3737
ENV["OPENBLAS_MAIN_FREE"] = "1"
3838
end
3939

40+
# Ensure that OpenBLAS does not grab a huge amount of memory at first,
41+
# since it instantly allocates scratch buffer space for the number of
42+
# threads it thinks it needs to use.
43+
# X-ref: https://github.com/xianyi/OpenBLAS/blob/c43ec53bdd00d9423fc609d7b7ecb35e7bf41b85/README.md#setting-the-number-of-threads-using-environment-variables
44+
# X-ref: https://github.com/JuliaLang/julia/issues/45434
45+
if !haskey(ENV, "OPENBLAS_NUM_THREADS") &&
46+
!haskey(ENV, "GOTO_NUM_THREADS") &&
47+
!haskey(ENV, "OMP_NUM_THREADS")
48+
# We set this to `1` here, and then LinearAlgebra will update
49+
# to the true value in its `__init__()` function.
50+
ENV["OPENBLAS_NUM_THREADS"] = "1"
51+
end
52+
4053
global libopenblas_handle = dlopen(libopenblas)
4154
global libopenblas_path = dlpath(libopenblas_handle)
4255
global artifact_dir = dirname(Sys.BINDIR)

0 commit comments

Comments
 (0)