Skip to content

Commit b8c603d

Browse files
Only make isbits parameter buffers static
`FullSpecialize` was turning every parameter buffer into a `StaticArray`, including ones whose elements are heap objects. That buys nothing — an `MVector{1, Vector{Float64}}` still holds a pointer, so it is no more GPU-resident than a `Vector{Vector{Float64}}` — and it breaks two ways: `MArray` refuses `setindex!` on a non-isbits eltype (the FMI array-valued discrete write), and the `SizedVector` workaround for that makes `remake` recurse forever, because `similar_type` is a no-op on a fully parameterized `SizedVector{N, T, Vector{T}}`. Gate on `isbitstype` in both places that pick a buffer type: the generated map's `_static_initialization_buffer`, and the `get_p_constructor` closure the reconstructor routes through. Both sides have to use the same rule or the map and `MTKParametersReconstructor` disagree on the buffer type. `get_p_constructor` only started seeing non-isbits buffers because this PR is what makes `pType` static in the first place. Tunable and initials buffers are isbits and stay `MVector`, which is the allocation and GPU win this PR is for. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Agent-Harness: Claude Code 2.0.14 Agent-Model: claude-opus-5[1m] Agent-Session: https://claude.ai/code/session_01GdSpCLd7NBZuuePJmcDzU7 Claude-Session: https://claude.ai/code/session_01GdSpCLd7NBZuuePJmcDzU7
1 parent fed8cdb commit b8c603d

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎lib/ModelingToolkitBase/src/ModelingToolkitBase.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ using PrecompileTools: PrecompileTools, @recompile_invalidations
1111
using Reexport: Reexport, @reexport
1212
@recompile_invalidations begin
1313
import StaticArrays
14-
using StaticArraysCore: StaticArraysCore, MVector, SVector, SizedVector, StaticArray,
15-
StaticVector, similar_type
14+
using StaticArraysCore: StaticArraysCore, MVector, SVector, StaticArray, StaticVector,
15+
similar_type
1616
import Symbolics
1717
import ImplicitDiscreteSolve
1818
using ImplicitDiscreteSolve: IDSolve

‎lib/ModelingToolkitBase/src/systems/problem_utils.jl‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ function _static_initialization_buffer(prototype, values)
17121712
elseif isbitstype(T)
17131713
return MVector{length(values), T}(values)
17141714
else
1715-
return SizedVector{length(values), T}(collect(T, values))
1715+
return collect(T, values)
17161716
end
17171717
end
17181718

@@ -1892,6 +1892,11 @@ function get_p_constructor(p_constructor, pType::Type, floatT::Type)
18921892
p_constructor === identity || return p_constructor
18931893
pType <: StaticArray || return p_constructor
18941894
return function (vals)
1895+
# Only isbits buffers become static. A buffer whose elements are heap objects
1896+
# (nonnumeric parameters, array-valued discretes) gains nothing from a
1897+
# `StaticArray` — the elements are still pointers — and `MArray` cannot
1898+
# `setindex!` a non-isbits eltype at all.
1899+
isbitstype(eltype(vals)) || return vals
18951900
return SymbolicUtils.Code.create_array(
18961901
pType, eltype(vals) <: AbstractFloat ? floatT : nothing, Val(ndims(vals)), Val(size(vals)), vals...
18971902
)

‎lib/ModelingToolkitBase/test/initializationsystem.jl‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,12 @@ end
18961896
SciMLStructures.replace!(portion, full_p, replacement)
18971897
@test SciMLStructures.canonicalize(portion, full_p)[1] == replacement
18981898
end
1899+
# Only isbits buffers become static. A `StaticArray` over heap elements is not
1900+
# GPU-resident anyway (the elements are pointers), `MArray` cannot `setindex!` a
1901+
# non-isbits eltype, and a `SizedVector` sends `remake`'s `similar_type`
1902+
# reconstruction into infinite recursion.
18991903
nonbits = ModelingToolkitBase._static_initialization_buffer(Any[Ref(1)], (Ref(2),))
1900-
@test nonbits isa SizedVector
1904+
@test !(nonbits isa StaticArray)
19011905
nonbits[1] = Ref(3)
19021906
@test only(nonbits)[] == 3
19031907

@@ -1961,6 +1965,29 @@ end
19611965
@test typeof(second_data.initializeprobmap) === typeof(full_data.initializeprobmap)
19621966
@test typeof(second_data.initializeprobpmap) === typeof(full_data.initializeprobpmap)
19631967
@test second_data.initializeprobmap(second_data.initializeprob) == full_u
1968+
1969+
# A callable parameter closing over an array is nonnumeric and not isbits. Its buffer
1970+
# must stay on the heap while the isbits buffers still go static.
1971+
nonbits_fn = let d = [2.0]
1972+
x -> d[1] * x
1973+
end
1974+
@test !isbitstype(typeof(nonbits_fn))
1975+
@variables nonbits_x(t)
1976+
@parameters nonbits_rate = 1.0
1977+
@parameters (nonbits_scale::typeof(nonbits_fn))(..) = nonbits_fn [tunable = false]
1978+
@mtkcompile nonbits_sys = System(
1979+
[D(nonbits_x) ~ -nonbits_rate * nonbits_scale(nonbits_x)], t;
1980+
initialization_eqs = [nonbits_x^3 + nonbits_x ~ 2]
1981+
)
1982+
nonbits_prob = ODEProblem{true, SciMLBase.FullSpecialize}(
1983+
nonbits_sys, [], (0.0, 1.0); guesses = [nonbits_x => 1.0]
1984+
)
1985+
nonbits_init = nonbits_prob.f.initialization_data
1986+
nonbits_p = nonbits_init.initializeprobpmap(nonbits_prob, nonbits_init.initializeprob)
1987+
@test nonbits_p.tunable isa StaticArray
1988+
@test nonbits_p.initials isa StaticArray
1989+
@test !any(buf -> buf isa StaticArray, nonbits_p.nonnumeric)
1990+
@test remake(nonbits_prob, p = [nonbits_rate => 3.0]).p.tunable == [3.0]
19641991
end
19651992

19661993
@testset "Issue#3570, #3552: `Initial`s/guesses are copied to `u0` during `solve`/`init`" begin

0 commit comments

Comments
 (0)