Skip to content

Commit

Permalink
[LinearAlgebra] Cache lbt_get_config() result (JuliaLang#44383)
Browse files Browse the repository at this point in the history
* [LinearAlgebra] Cache `lbt_get_config()` result

In the event that users want to call `lbt_get_config()` multiple times
(e.g. for runtime checks of which BLAS vendor is providing a symbol),
let's cache the value and clear it only when someone calls something
that would cause the config itself to change.

* Use double-checked locking to cache LBT config (JuliaLang#44387)

* Use double-checked locking to cache LBT config

* Use Any field for lock-free load and store

* Revert "Use Any field for lock-free load and store"

This reverts commit c6acae5.

The compiler already handled it:
JuliaLang#44387 (comment)

* (rerun)

* Add lock as a field of ConfigCache

Co-authored-by: Takafumi Arakaki <[email protected]>
  • Loading branch information
staticfloat and tkf committed Mar 3, 2022
1 parent 2f67b51 commit dbb0e50
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions stdlib/LinearAlgebra/src/lbt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,32 @@ function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, lbt::LBTConfig)
end
end

mutable struct ConfigCache
@atomic config::Union{Nothing,LBTConfig}
lock::ReentrantLock
end

# In the event that users want to call `lbt_get_config()` multiple times (e.g. for
# runtime checks of which BLAS vendor is providing a symbol), let's cache the value
# and clear it only when someone calls something that would cause it to change.
const _CACHED_CONFIG = ConfigCache(nothing, ReentrantLock())

function lbt_get_config()
config_ptr = ccall((:lbt_get_config, libblastrampoline), Ptr{lbt_config_t}, ())
return LBTConfig(unsafe_load(config_ptr))
config = @atomic :acquire _CACHED_CONFIG.config
config === nothing || return config
return lock(_CACHED_CONFIG.lock) do
local config = @atomic :monotonic _CACHED_CONFIG.config
config === nothing || return config
config_ptr = ccall((:lbt_get_config, libblastrampoline), Ptr{lbt_config_t}, ())
@atomic :release _CACHED_CONFIG.config = LBTConfig(unsafe_load(config_ptr))
end
end

function _clear_config_with(f)
lock(_CACHED_CONFIG.lock) do
@atomic :release _CACHED_CONFIG.config = nothing
f()
end
end

function lbt_get_num_threads()
Expand All @@ -185,11 +208,15 @@ function lbt_set_num_threads(nthreads)
end

function lbt_forward(path; clear::Bool = false, verbose::Bool = false, suffix_hint::Union{String,Nothing} = nothing)
ccall((:lbt_forward, libblastrampoline), Int32, (Cstring, Int32, Int32, Cstring), path, clear ? 1 : 0, verbose ? 1 : 0, something(suffix_hint, C_NULL))
_clear_config_with() do
return ccall((:lbt_forward, libblastrampoline), Int32, (Cstring, Int32, Int32, Cstring), path, clear ? 1 : 0, verbose ? 1 : 0, something(suffix_hint, C_NULL))
end
end

function lbt_set_default_func(addr)
return ccall((:lbt_set_default_func, libblastrampoline), Cvoid, (Ptr{Cvoid},), addr)
_clear_config_with() do
return ccall((:lbt_set_default_func, libblastrampoline), Cvoid, (Ptr{Cvoid},), addr)
end
end

function lbt_get_default_func()
Expand Down Expand Up @@ -241,17 +268,19 @@ end
function lbt_set_forward(symbol_name, addr, interface,
complex_retstyle = LBT_COMPLEX_RETSTYLE_NORMAL,
f2c = LBT_F2C_PLAIN; verbose::Bool = false)
return ccall(
(:lbt_set_forward, libblastrampoline),
Int32,
(Cstring, Ptr{Cvoid}, Int32, Int32, Int32, Int32),
string(symbol_name),
addr,
Int32(interface),
Int32(complex_retstyle),
Int32(f2c),
verbose ? Int32(1) : Int32(0),
)
_clear_config_with() do
return ccall(
(:lbt_set_forward, libblastrampoline),
Int32,
(Cstring, Ptr{Cvoid}, Int32, Int32, Int32, Int32),
string(symbol_name),
addr,
Int32(interface),
Int32(complex_retstyle),
Int32(f2c),
verbose ? Int32(1) : Int32(0),
)
end
end
function lbt_set_forward(symbol_name, addr, interface::Symbol,
complex_retstyle::Symbol = :normal,
Expand Down

0 comments on commit dbb0e50

Please sign in to comment.