Repository navigation
Write one-element slice equations without scalar indexing in DAE residuals - #5274
Conversation
…duals An implicit-DAE residual with array equations is assembled as a SymbolicUtils `ArrayMaker` with one region per equation. By default SymbolicUtils scalarizes a one-element region that holds an array expression, such as the residual of `u[1:1] ~ 0` or `D(u[1:1]) ~ -u[1:1]`, so the generated code reads its inputs element by element and stores into the output with `batched_setindex!`. Arrays that disallow scalar indexing, such as `CuArray` under `CUDA.allowscalar(false)`, reject that, which is what MethodOfLines' one-element boundary equations produce. Set Symbolics' `scalarize_single_element_regions = false` for implicit-DAE residuals, so those regions are written through a view of the output like every other array equation. Scalar equations are still written as scalars. Agent-Harness: Claude Code 2.1.296 Agent-Model: claude-opus-5-5 Agent-Session: aa65e380-52df-443a-9f57-8c20c56babb9 Agent-Review: unreviewed Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Registered Symbolics does not have the option yet, and `setproperties` would fail on the missing field, so guard on `hasfield`. With an older Symbolics the residual keeps the scalarized form, and the view write activates once a Symbolics release with the field is installed. The test expects the view write only when that field exists and SymbolicUtils is newer than 4.50.0 (needed for the rewrite to take effect), and today's scalar write otherwise. Agent-Harness: Claude Code 2.1.296 Agent-Model: claude-opus-5-5 Agent-Session: aa65e380-52df-443a-9f57-8c20c56babb9 Agent-Review: unreviewed Co-authored-by: Claude Code <agent@noreply.juliahub.com>
eb7665c to
15ea23e
Compare
…/dae-gpu # Conflicts: # lib/ModelingToolkitBase/test/array_equation_dae.jl
Agent-Harness: Claude Code 2.1.308 Agent-Model: claude-sonnet-5-5 Agent-Session: fbf12fae-28a4-496f-b740-3339fe3ac7a1 Agent-Review: unreviewed Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Conflict in lib/ModelingToolkitBase/test/array_equation_dae.jl: both sides appended a testset at the end of the file; kept both (the one-element slice testset from this PR and master's mixed array initial parameters testset). codegen.jl merged automatically. Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Keeps the one-element-slice scalarization option alongside the RHS chunking from SciML#5308. Agent-Harness: Claude Code 2.1.312 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <agent@noreply.juliahub.com>
|
Pushed 🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5 |
Agent-Harness: Claude Code 2.1.325 Agent-Model: claude-sonnet-5-5 Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b Agent-Review: unreviewed Co-authored-by: Claude Code <agent@noreply.juliahub.com>
|
Pushed
This PR is part of a stack needed for large sparse DAE models with array unknowns. Review is requested from the maintainers. 🤖 Posted by an AI agent · harness: Claude Code 2.1.325 · model: claude-sonnet-5-5 |
|
Fix for DAE codegen with array unknowns (#5274: one-element slice equations written without scalar indexing) is merged. Could a patch release of ModelingToolkit and ModelingToolkitBase be cut when convenient? A downstream package currently pins an integration branch and needs a registered release containing this change. 🤖 Posted by an AI agent · harness: Claude Code 2.1.325 · model: claude-sonnet-5-5 |
Summary
generate_rhsnow passesscalarize_single_element_regions = falseto Symbolics' codegen for implicit-DAE residuals assembled from array equations. A one-element slice equation, such asu[1:1] ~ 0orD(u[1:1]) ~ -u[1:1], is then written through a view of the output like every other array equation, instead of being scalarized.Motivation
An implicit-DAE residual with array equations is assembled as a SymbolicUtils
ArrayMakerwith one region per equation. By default SymbolicUtils scalarizes a one-element region that holds an array expression: the generated in-place residual reads the expression's inputs element by element and stores the result withbatched_setindex!. Arrays that disallow scalar indexing, such asCuArrayunderCUDA.allowscalar(false), reject both operations. MethodOfLines'DAEProblem(pdesys, disc)path keeps array equations intact so the residual can run on the GPU, and one-element slice equations are how boundary rows are expressed without falling back to scalar equations.Change
In
generate_rhs(lib/ModelingToolkitBase/src/systems/codegen.jl), right after the existingsetpropertiesoncodegen_opts:With a Symbolics version that lacks the field, the condition is false and nothing changes.
Scalar equations reach the
ArrayMakerasarray_literalterms, which SymbolicUtils writes as scalars under either setting, so their generated code is unchanged. The ODE and nonlinear paths are not changed.Tests
A new testset in
lib/ModelingToolkitBase/test/array_equation_dae.jl, "one-element slice equations are written without scalar indexing", uses an N = 32 system (longer than SymbolicUtils'FILL_ARR_LIMIT).D(u[1:1]) ~ -u[1:1]andu[1:1] ~ zeros(1), the in-place residual contains nobatched_setindex!when the option can take effect: Symbolics has the field and SymbolicUtils is newer than 4.50.0. Otherwise it still contains one, which is today's behaviour.0 ~ u[1]contains one in every configuration.DAEProblemresidual equals hand-computed values, unconditionally.Results, locally, Julia 1.12.6:
array_equation_dae.jlGROUP=InterfaceII, before the rebase, with the view write active (Symbolics at Sensitivity and Vector Parameters Symbolic probems #2097's head, SymbolicUtils master containing Retain all properties when writing to a file #1154): passes (Testing ModelingToolkitBase tests passed).GROUP=InterfaceI, same setup:InterfaceI | 1776 passed, 1 errored, 5 broken. The error is pre-existing and unrelated:variable_parsing.jl:66throwsArgumentError: Variable x must have default of matching size. Got [1, 2] with size (2,)from Symbolics'setdefaultval. The same error occurs when that file is run alone on unchanged master (37d63bc) with registered Symbolics 7.45.0, SymbolicUtils 4.50.0 and DynamicQuantities 1.13.0.--check: clean on both changed files.CI
The run before the rebase (base 37d63bc) failed 12 jobs, all of them pre-existing and none involving this change. The new testset passed in both InterfaceI jobs ("Array equations reach DAEProblem" 36/36). Seven of the 12 pass on current master, which is why this branch was rebased:
[Extended](lts, 1):ccompile.jl:11, "CTarget with expression=Val{false} is not supported". Fixed on master by Compile CTarget C source in ccompile test (Symbolics no longer accepts expression=Val{false}) #5261.[Optimization](lts, 1),tests / Optimization(lts, 1) anddowngrade-mtkbase (Optimization):optimizationsystem.jl:814and:874, from OptimizationMOI 1.4.2 being resolved instead of 1.5.0. Current master passes these.These five also fail on current master aea9c2e:
[InterfaceI](lts, 1)variable_parsing.jl:66:ArgumentError: Variable x must have default of matching size[RegressionI](lts, 1)latexify.jl:68: output does not matchlatexify/50.texSciMLBase.jl/SymbolicIndexingInterface/1/(downstream)comprehensive_indexing.jl:127and:164:BoundsError: attempt to access 1-element Vector{Float64} at index [[1, 2]]GPU check
This was run with the first commit (c4584e4, before the guard and the rebase, which does not change behaviour when the field exists) cherry-picked onto #5242's head (0d8c71e), plus the SymbolicUtils and Symbolics versions above, on an RTX 4080 SUPER with CUDA.jl 6.4.1. Each in-place
DAEProblemresidual was called onCuArrays underCUDA.allowscalar(false)with N = 32:batched_setindex!allowscalar(false)D(u) ~ -uD(u[2:N]) ~ u[1:N-1] .- u[2:N],u[1:1] ~ zeros(1)D(u[1:1]) ~ -u[1:1]0 ~ u[1](scalar equation)u[1]Without this change, both one-element-slice variants fail on the GPU with
Scalar indexing is disallowed, even with #5242. For a 273-state MethodOfLines FDTD residual (TinyFDTD,dx = 0.5), the generated code is unchanged, with 2batched_setindex!, because MethodOfLines still emits its 1D boundaries as scalar equations.Merge note
#5242 also appends testsets to the end of
array_equation_dae.jl, so whichever of the two merges second has a trivial conflict there (keep both testsets).🤖 Posted by an AI agent · harness: Claude Code 2.1.296 · model: claude-opus-5-5
Review: unreviewed
Session: aa65e380-52df-443a-9f57-8c20c56babb9