Skip to content
Closed
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
110 changes: 104 additions & 6 deletions lib/ModelingToolkitTearing/src/reassemble.jl
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ function generate_system_equations!(state::TearingState, neweqs::Vector{Equation
var_sccs::Vector{Vector{Int}}, extra_eqs_vars::NTuple{2, Vector{Int}},
iv::Union{SymbolicT, Nothing}, D::Union{Differential, Shift, Nothing};
simplify::Bool = false, inline_linear_sccs = false, analytical_linear_scc_limit = 2,
inline_linear_scc_generic_lu::Bool = false,
allow_symbolic::Bool = false, allow_parameter::Bool = true)
(; fullvars, sys, structure) = state
(; solvable_graph, var_to_diff, eq_to_diff, graph) = structure
Expand Down Expand Up @@ -490,7 +491,7 @@ function generate_system_equations!(state::TearingState, neweqs::Vector{Equation
# algebraic variables.
linsol_result = nothing
if !is_disc && inline_linear_sccs
linsol_result = get_linear_scc_linsol(state, escc, vscc, neweqs, var_eq_matching, full_var_eq_matching, total_sub, analytical_linear_scc_limit, simplify)
linsol_result = get_linear_scc_linsol(state, escc, vscc, neweqs, var_eq_matching, full_var_eq_matching, total_sub, analytical_linear_scc_limit, simplify; generic_lu = inline_linear_scc_generic_lu)
end
if linsol_result isa Tuple{SymbolicT, BitVector, BitVector}
linsol, eqs_mask, vars_mask = linsol_result
Expand Down Expand Up @@ -701,6 +702,14 @@ function dump_linear_scc_to_file(fname, A, x)
return nothing
end

"""
GenericInlineLU()

Selects [`generic_lu_ldiv!`](@ref) for the runtime solves of inlined linear SCCs. See the
`inline_linear_scc_generic_lu` field of [`DefaultReassembleAlgorithm`](@ref).
"""
struct GenericInlineLU end

function safe_ldiv(A, b)
A = unwrap(A)
b = unwrap(b)
Expand All @@ -715,6 +724,74 @@ function safe_ldiv(A, b)
return numeric_ldiv!(A, b)
end

function safe_ldiv(A, b, alg::GenericInlineLU)
A = unwrap(A)
b = unwrap(b)
if A isa SymbolicT || A isa AbstractMatrix{Num} || A isa AbstractMatrix{SymbolicT} ||
b isa SymbolicT || b isa AbstractVector{Num} || b isa AbstractVector{SymbolicT}
return Symbolics.STerm(
safe_ldiv, Symbolics.SArgsT((A, b, SU.Const{VartypeT}(alg)));
type = Vector{Real},
shape = SU.promote_shape(safe_ldiv, SU.shape(A), SU.shape(b))
)
end
# `A` and `b` can be buffers shared with another inline solve, so neither is overwritten
return generic_lu_ldiv!(copy(A), copy(b))
end

"""
$TYPEDSIGNATURES

Solve `A x = b` by Gaussian elimination with partial pivoting, overwriting `b` with `x`, and
return `b`. `A` is used as workspace: afterwards its upper triangle holds the upper triangular
factor of the row-permuted `A`, and its strictly lower triangle is unspecified.

It is written in plain Julia over the element type, so it neither allocates nor keeps any
state between calls, and dual numbers propagate through it. Pivots are chosen by `abs`, which
for `ForwardDiff.Dual` compares the primal values.

There is no rank-deficient fallback: a singular `A` produces non-finite entries in the result.
"""
function generic_lu_ldiv!(A::AbstractMatrix, b::AbstractVector)
Base.require_one_based_indexing(A, b)
n = LinearAlgebra.checksquare(A)
length(b) == n ||
throw(DimensionMismatch("matrix has $n rows but the right hand side has length $(length(b))"))
@inbounds for k in 1:n
p = k
amax = abs(A[k, k])
for i in (k + 1):n
ai = abs(A[i, k])
if ai > amax
p = i
amax = ai
end
end
if p != k
for j in k:n
A[k, j], A[p, j] = A[p, j], A[k, j]
end
b[k], b[p] = b[p], b[k]
end
akk = A[k, k]
for i in (k + 1):n
l = A[i, k] / akk
for j in (k + 1):n
A[i, j] -= l * A[k, j]
end
b[i] -= l * b[k]
end
end
@inbounds for k in n:-1:1
s = b[k]
for j in (k + 1):n
s -= A[k, j] * b[j]
end
b[k] = s / A[k, k]
end
return b
end

"""
$TYPEDSIGNATURES

Expand Down Expand Up @@ -780,6 +857,10 @@ function SU.promote_symtype(::typeof(safe_ldiv), TA::SU.TypeT, TB::SU.TypeT)
return Vector{Real}
end

function SU.promote_symtype(::typeof(safe_ldiv), TA::SU.TypeT, TB::SU.TypeT, ::SU.TypeT)
return Vector{Real}
end

function SU.promote_shape(::typeof(safe_ldiv), sha::SU.ShapeT, shb::SU.ShapeT)
@nospecialize sha shb
if sha isa SU.Unknown
Expand All @@ -789,6 +870,11 @@ function SU.promote_shape(::typeof(safe_ldiv), sha::SU.ShapeT, shb::SU.ShapeT)
end
end

function SU.promote_shape(::typeof(safe_ldiv), sha::SU.ShapeT, shb::SU.ShapeT, ::SU.ShapeT)
@nospecialize sha shb
return SU.promote_shape(safe_ldiv, sha, shb)
end

const INLINE_LINEAR_SCC_OP = safe_ldiv

"""
Expand Down Expand Up @@ -818,7 +904,7 @@ function get_linear_scc_linsol(state::TearingState, alg_eqs::Vector{Int},
total_sub::MTKBase.ExpandDerivativeDict{SymbolicT, Dict{SymbolicT, SymbolicT}},
analytical_linear_scc_limit::Int,
simplify::Bool; allow_symbolic::Bool = false,
allow_parameter::Bool = true)
allow_parameter::Bool = true, generic_lu::Bool = false)
(; fullvars, sys, structure) = state
(; graph, solvable_graph, var_to_diff) = structure
diff_to_var = invview(var_to_diff)
Expand Down Expand Up @@ -997,7 +1083,8 @@ function get_linear_scc_linsol(state::TearingState, alg_eqs::Vector{Int},
b = SU.Code.with_allocator(b_allocator, SU.Const{VartypeT}(b))
state.sys = sys

return (INLINE_LINEAR_SCC_OP(A, b), eqs_mask, vars_mask)
linsol = generic_lu ? INLINE_LINEAR_SCC_OP(A, b, GenericInlineLU()) : INLINE_LINEAR_SCC_OP(A, b)
return (linsol, eqs_mask, vars_mask)
end

function __reduce_linear_system!(A::StateSelection.CLIL.SparseMatrixCLIL{Num, Int}, b::Vector{SymbolicT}, var_eq_matching::StateSelection.VarEqMatchingT, alg_eqs::Vector{Int}, alg_vars::Vector{Int}, eqs_mask::BitVector, vars_mask::BitVector)
Expand Down Expand Up @@ -1592,6 +1679,15 @@ $TYPEDFIELDS
equations which is solved symbolically rather than using `LinearSolve.jl`.
"""
analytical_linear_scc_limit::Int = 2
"""
If `inline_linear_sccs == true`, whether the runtime solves of inlined linear SCCs use
[`generic_lu_ldiv!`](@ref) on copies of `A` and `b` instead of a `LinearSolve.jl`
cache kept in task local storage. The generated code then makes no dynamic calls for
these solves, which ahead-of-time compilation with a closed world (such as
`juliac --trim`) requires. Each solve allocates its copies, and there is no fallback
for singular systems.
"""
inline_linear_scc_generic_lu::Bool = false
end

function (alg::DefaultReassembleAlgorithm)(state::TearingState,
Expand All @@ -1600,7 +1696,8 @@ function (alg::DefaultReassembleAlgorithm)(state::TearingState,
fully_determined::Bool = true,
allow_symbolic::Bool = false,
allow_parameter::Bool = true, kw...)
(; simplify, array_hack, inline_linear_sccs, analytical_linear_scc_limit) = alg
(; simplify, array_hack, inline_linear_sccs, analytical_linear_scc_limit,
inline_linear_scc_generic_lu) = alg
(; var_eq_matching, full_var_eq_matching, var_sccs) = tearing_result

extra_eqs_vars = get_extra_eqs_vars(
Expand Down Expand Up @@ -1649,7 +1746,8 @@ function (alg::DefaultReassembleAlgorithm)(state::TearingState,
inline_blocks = generate_system_equations!(
state, neweqs, var_eq_matching, full_var_eq_matching,
var_sccs, extra_eqs_vars, iv, D; simplify, inline_linear_sccs,
analytical_linear_scc_limit, allow_symbolic, allow_parameter)
analytical_linear_scc_limit, inline_linear_scc_generic_lu,
allow_symbolic, allow_parameter)
state = reorder_vars!(
state, var_eq_matching, var_sccs, eq_ordering, var_ordering, nelim_eq, nelim_var)
# var_eq_matching and full_var_eq_matching are now invalidated
Expand All @@ -1666,7 +1764,7 @@ function (alg::DefaultReassembleAlgorithm)(state::TearingState,
inline_blocks = generate_system_equations!(
state, neweqs, var_eq_matching, full_var_eq_matching,
var_sccs, extra_eqs_vars, iv, D; simplify, inline_linear_sccs,
analytical_linear_scc_limit)
analytical_linear_scc_limit, inline_linear_scc_generic_lu)
state = reorder_vars!(
state, var_eq_matching, var_sccs, eq_ordering, var_ordering, nelim_eq, nelim_var)
# var_eq_matching and full_var_eq_matching are now invalidated
Expand Down
112 changes: 112 additions & 0 deletions lib/ModelingToolkitTearing/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using Setfield
using ForwardDiff
using LinearAlgebra
import ModelingToolkitBase as MTKBase
import SymbolicIndexingInterface

@testset "`InferredDiscrete` validation" begin
k = ShiftIndex()
Expand Down Expand Up @@ -204,6 +205,117 @@ end
@test MTKTearing.numeric_ldiv!(copy(A), [1.0, 2.0]) ≈ expected
end

@testset "`generic_lu_ldiv!` solves in place with partial pivoting" begin
# a zero leading entry needs a row swap; the solution is [1, 2, 3]
A = [0.0 2.0 1.0; 1.0 1.0 1.0; 2.0 0.0 3.0]
b = [7.0, 6.0, 11.0]
u = MTKTearing.generic_lu_ldiv!(copy(A), b)
@test u === b
@test u ≈ [1.0, 2.0, 3.0]
A1, b1 = copy(A), [7.0, 6.0, 11.0]
MTKTearing.generic_lu_ldiv!(copy(A), copy(b1))
@test @allocated(MTKTearing.generic_lu_ldiv!(A1, b1)) == 0

# derivative of x(s) = A(s) \ b with A(s) = A + s * E is -A⁻¹ E x
E = [1.0 0.0 0.0; 0.0 0.0 1.0; 0.0 1.0 0.0]
dx = ForwardDiff.derivative(
s -> MTKTearing.generic_lu_ldiv!(A + s * E, [7.0, 6.0, 11.0] .+ 0 * s), 0.0)
@test dx ≈ -(A \ (E * [1.0, 2.0, 3.0]))

# no rank-deficient fallback
S = [1.0 2.0; 2.0 4.0]
@test !all(isfinite, MTKTearing.generic_lu_ldiv!(copy(S), [1.0, 2.0]))
end

@testset "Inline linear SCCs with `inline_linear_scc_generic_lu`" begin
@variables x(t) = 1.0 y(t) = 1.0 z(t) = 1.0 w(t) = 1.0 q(t) = 1.0
reassemble_alg = MTKTearing.DefaultReassembleAlgorithm(;
inline_linear_sccs = true, inline_linear_scc_generic_lu = true)
@mtkcompile sys = System(
[
D(x) ~ 2t + 1,
(2 + x) * y + x * z + w ~ 4,
(4 + x) * y + 3z + 2w ~ 7 + t,
2x * y + (3 + x) * z + w ~ 10,
D(q) ~ 2w + 3z + y,
],
t
) reassemble_alg = reassemble_alg

blk = only(MTKTearing.inline_linear_systems(sys))
@test SU.operation(unwrap(blk.expression)) === MTKTearing.INLINE_LINEAR_SCC_OP

# the block written out by hand, as the reference for values and derivatives
Aref(xv) = [2+xv xv 1; 4+xv 3 2; 2xv 3+xv 1]
bref(tv) = [4.0, 7 + tv, 10.0]
dAdx = [1.0 1.0 0.0; 1.0 0.0 0.0; 2.0 1.0 0.0]
c = [1.0, 3.0, 2.0]

prob = ODEProblem(sys, [x => 1.5], (0.0, 1.0))
f! = prob.f.f.f_iip
ix = SymbolicIndexingInterface.variable_index(sys, x)
iq = SymbolicIndexingInterface.variable_index(sys, q)
u0, p, tv = copy(prob.u0), prob.p, 0.3
X = Aref(1.5) \ bref(tv)

# each check runs in a fresh task, so the task local cache of the default solver
# cannot be there unless this solve created it
res = fetch(Threads.@spawn begin
du = similar(u0)
f!(du, u0, p, tv)
first_dq = du[iq]
f!(du, u0, p, tv)
J = ForwardDiff.jacobian((d, u) -> (f!(d, u, p, tv); nothing), similar(u0), u0)
dt = ForwardDiff.derivative(
s -> (d = similar(u0, typeof(s)); f!(d, u0, p, s); d), tv)
(; first_dq, dq = du[iq], J, dt,
tls = haskey(task_local_storage(), MTKTearing.INLINE_LINSOLVE_CACHE))
end)
@test res.first_dq ≈ c' * X
@test res.dq ≈ c' * X
@test !res.tls
@test res.J[iq, ix] ≈ -c' * (Aref(1.5) \ (dAdx * X))
@test res.dt[iq] ≈ c' * (Aref(1.5) \ [0.0, 1.0, 0.0])
end

@testset "`inline_linear_scc_generic_lu` with blocks sharing `A` or `b`" begin
@variables x(t) = 1.5 q(t) = 1.0 y(t) z(t) w(t) a(t) c(t) d(t)
reassemble_alg = MTKTearing.DefaultReassembleAlgorithm(;
inline_linear_sccs = true, inline_linear_scc_generic_lu = true)
A(xv) = [2+xv xv 1; 4+xv 3 2; 2xv 3+xv 1]
B(xv) = [3+xv xv 1; 1 3+xv 2; 2xv 1 4+xv]
first_block = [
(2 + x) * y + x * z + w ~ 4,
(4 + x) * y + 3z + 2w ~ 7,
2x * y + (3 + x) * z + w ~ 10,
]
same_A = [
(2 + x) * a + x * c + d ~ 5,
(4 + x) * a + 3c + 2d ~ 8,
2x * a + (3 + x) * c + d ~ 11,
]
same_b = [
(3 + x) * a + x * c + d ~ 4,
a + (3 + x) * c + 2d ~ 7,
2x * a + c + (4 + x) * d ~ 10,
]
for (second_block, reference) in [
(same_A, xv -> (A(xv) \ [4.0, 7, 10], A(xv) \ [5.0, 8, 11])),
(same_b, xv -> (A(xv) \ [4.0, 7, 10], B(xv) \ [4.0, 7, 10])),
]
eqs = [D(x) ~ 2t + 1; first_block; second_block; D(q) ~ 2w + 3z + y + 5d + 7c + 11a]
@named sys = System(eqs, t)
sys = mtkcompile(sys; reassemble_alg)
@test length(MTKTearing.inline_linear_systems(sys)) == 2
prob = ODEProblem(sys, [], (0.0, 1.0))
du = similar(prob.u0)
prob.f.f.f_iip(du, prob.u0, prob.p, 0.0)
X1, X2 = reference(prob.u0[SymbolicIndexingInterface.variable_index(sys, x)])
@test du[SymbolicIndexingInterface.variable_index(sys, q)] ≈
[1, 3, 2]' * X1 + [11, 7, 5]' * X2
end
end

@testset "Inline linear SCC derivatives stay correct across repeated solves" begin
@variables x(t) = 1.0 y(t) = 1.0 z(t) = 1.0 w(t) = 1.0 q(t) = 1.0
reassemble_alg = MTKTearing.DefaultReassembleAlgorithm(; inline_linear_sccs = true)
Expand Down
Loading