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

[LinearAlgebra] Initialise number of BLAS threads with jl_effective_threads #55574

Merged
Merged
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is a good place where to add this test, but I need uv_thread_getaffinity and AFFINITY_SUPPORTED, which are only defined here

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