Skip to content
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
32 changes: 20 additions & 12 deletions lib/ModelingToolkitTearing/src/reassemble.jl
Original file line number Diff line number Diff line change
Expand Up @@ -755,17 +755,22 @@ function solve_into!(cache, A::AbstractMatrix, b::AbstractVector)
end

function get_inline_linsolve_cache(A::AbstractMatrix, b::AbstractVector)
tls = task_local_storage()
key = (INLINE_LINSOLVE_CACHE, typeof(A), size(A))
cache = get(tls, key, nothing)
if cache === nothing
# `A` and `b` are views into the diffcache buffers of whichever problem happens
# to call first, and the cache keeps what it is handed, so it needs copies it owns.
# `copy` rather than `Matrix`/`Vector` so the cache keeps the array type it was
# given, which matters for anything living off the CPU.
cache = CommonSolve.init(LinearProblem(copy(A), copy(b)))
tls[key] = cache
end
# one typed `Dict` under a `Symbol` key, since a tuple key into task local storage
# (an `IdDict{Any, Any}`) would be boxed on every call
caches = get!(InlineLinsolveCachesT, task_local_storage(),
INLINE_LINSOLVE_CACHE)::InlineLinsolveCachesT
cache = get(caches, (typeof(A), size(A)), nothing)
return cache === nothing ? init_inline_linsolve_cache!(caches, A, b) : cache
end

# out of line, since inlined it slows every inline solve by ~25 ns on Julia 1.13
@noinline function init_inline_linsolve_cache!(caches, A::AbstractMatrix, b::AbstractVector)
# `A` and `b` are views into the diffcache buffers of whichever problem happens
# to call first, and the cache keeps what it is handed, so it needs copies it owns.
# `copy` rather than `Matrix`/`Vector` so the cache keeps the array type it was
# given, which matters for anything living off the CPU.
cache = CommonSolve.init(LinearProblem(copy(A), copy(b)))
caches[(typeof(A), size(A))] = cache
return cache
end

Expand All @@ -787,10 +792,13 @@ end
const INLINE_LINEAR_SCC_OP = safe_ldiv

"""
Tag distinguishing this package's entries in task local storage. See [`numeric_ldiv!`](@ref).
Key of this package's entry in task local storage, which holds the `LinearCache`s of
[`numeric_ldiv!`](@ref) by array type and size.
"""
const INLINE_LINSOLVE_CACHE = :ModelingToolkitTearing_inline_linsolve_cache

const InlineLinsolveCachesT = Dict{Tuple{DataType, Dims{2}}, Any}

"""
$TYPEDSIGNATURES

Expand Down
6 changes: 5 additions & 1 deletion lib/ModelingToolkitTearing/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ end
f!(du, prob.u0, prob.p, 0.0)
# the solve used to build a fresh `LinearProblem` and `LinearCache` per call, which
# cost ~1.4 kB and 23 allocations regardless of the size of the system
@test @allocated(f!(du, prob.u0, prob.p, 0.0)) < 512
@test @allocated(f!(du, prob.u0, prob.p, 0.0)) < 256
# and the cache lookup used to box its key on every call
A, b = rand(2, 2), rand(2)
MTKTearing.get_inline_linsolve_cache(A, b)
@test @allocated(MTKTearing.get_inline_linsolve_cache(A, b)) == 0

# the same system without inlining is the reference for the values
@mtkcompile refsys = System(eqs, t)
Expand Down
Loading