Skip to content

Commit

Permalink
Implement a hook in base for disabling threading (#30004)
Browse files Browse the repository at this point in the history
of libraries, use it to decouple Distributed from LinearAlgebra.
  • Loading branch information
fredrikekre authored and JeffBezanson committed Dec 7, 2018
1 parent fcd031b commit 5509306
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ New library functions
* `uuid5` has been added to the `UUIDs` standard library ([#28761]).
* Predicate functions `Sys.isfreebsd`, `Sys.isopenbsd`, `Sys.isnetbsd`, and `Sys.isdragonfly` for
detecting BSD systems have been added ([#30249]).
* Internal `Base.disable_library_threading` that sets libraries to use one thread.
It executes function hooks that have been registered with
`Base.at_disable_library_threading` ([#30004]).

Standard library changes
------------------------
Expand Down
27 changes: 27 additions & 0 deletions base/initdefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,30 @@ function _atexit()
end
end
end

## hook for disabling threaded libraries ##

library_threading_enabled = true
const disable_library_threading_hooks = []

function at_disable_library_threading(f)
push!(disable_library_threading_hooks, f)
if !library_threading_enabled
disable_library_threading()
end
return
end

function disable_library_threading()
global library_threading_enabled = false
while !isempty(disable_library_threading_hooks)
f = pop!(disable_library_threading_hooks)
try
f()
catch err
@warn("a hook from a library to disable threading failed:",
exception = (err, catch_backtrace()))
end
end
return
end
1 change: 0 additions & 1 deletion stdlib/Distributed/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name = "Distributed"
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
Expand Down
6 changes: 0 additions & 6 deletions stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,6 @@ mutable struct LocalProcess
LocalProcess() = new(1)
end


import LinearAlgebra
function disable_threaded_libs()
LinearAlgebra.BLAS.set_num_threads(1)
end

worker_timeout() = parse(Float64, get(ENV, "JULIA_WORKER_TIMEOUT", "60.0"))


Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/process_messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ function handle_msg(msg::JoinPGRPMsg, header, r_stream, w_stream, version)
topology(msg.topology)

if !msg.enable_threaded_blas
disable_threaded_libs()
Base.disable_library_threading()
end

lazy = msg.lazy
Expand Down
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ function __init__()
Base.showerror_nostdio(ex,
"WARNING: Error during initialization of module LinearAlgebra")
end
# register a hook to disable BLAS threading
Base.at_disable_library_threading(() -> BLAS.set_num_threads(1))
end

end # module LinearAlgebra

0 comments on commit 5509306

Please sign in to comment.