Skip to content

Commit

Permalink
[LinearAlgebra] Initialise number of BLAS threads with `jl_effective_…
Browse files Browse the repository at this point in the history
…threads` (#55574)

This is a safer estimate than `Sys.CPU_THREADS` to avoid oversubscribing
the machine when running distributed applications, or when the Julia
process is constrained by external controls (`taskset`, `cgroups`,
etc.).

Fix #55572
  • Loading branch information
giordano authored and KristofferC committed Sep 12, 2024
1 parent d70ae6e commit b5a905d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Standard library changes
between different eigendecomposition algorithms ([#49355]).
* Added a generic version of the (unblocked) pivoted Cholesky decomposition
(callable via `cholesky[!](A, RowMaximum())`) ([#54619]).
* The number of default BLAS threads now respects process affinity, instead of
using total number of logical threads available on the system ([#55574]).

#### Logging

Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,9 @@ function __init__()
# https://github.com/xianyi/OpenBLAS/blob/c43ec53bdd00d9423fc609d7b7ecb35e7bf41b85/README.md#setting-the-number-of-threads-using-environment-variables
if !haskey(ENV, "OPENBLAS_NUM_THREADS") && !haskey(ENV, "GOTO_NUM_THREADS") && !haskey(ENV, "OMP_NUM_THREADS")
@static if Sys.isapple() && Base.BinaryPlatforms.arch(Base.BinaryPlatforms.HostPlatform()) == "aarch64"
BLAS.set_num_threads(max(1, Sys.CPU_THREADS))
BLAS.set_num_threads(max(1, @ccall(jl_effective_threads()::Cint)))
else
BLAS.set_num_threads(max(1, Sys.CPU_THREADS ÷ 2))
BLAS.set_num_threads(max(1, @ccall(jl_effective_threads()::Cint) ÷ 2))
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions test/threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,18 @@ end
@test jl_setaffinity(0, mask, cpumasksize) == 0
end
end

# Make sure default number of BLAS threads respects CPU affinity: issue #55572.
@testset "LinearAlgebra number of default threads" begin
if AFFINITY_SUPPORTED
allowed_cpus = findall(uv_thread_getaffinity())
cmd = addenv(`$(Base.julia_cmd()) --startup-file=no -E 'using LinearAlgebra; BLAS.get_num_threads()'`,
# Remove all variables which could affect the default number of threads
"OPENBLAS_NUM_THREADS"=>nothing,
"GOTO_NUM_THREADS"=>nothing,
"OMP_NUM_THREADS"=>nothing)
for n in 1:min(length(allowed_cpus), 8) # Cap to 8 to avoid too many tests on large systems
@test readchomp(setcpuaffinity(cmd, allowed_cpus[1:n])) == string(max(1, n ÷ 2))
end
end
end

0 comments on commit b5a905d

Please sign in to comment.