Skip to content

Write one-element slice equations without scalar indexing in DAE residuals - #5274

Merged
AayushSabharwal merged 7 commits into
SciML:masterfrom
xtalax:dae-scalarize-opt
Nov 17, 2026
Merged

AayushSabharwal merged 7 commits into
SciML:masterfrom
xtalax:dae-scalarize-opt

Conversation

@xtalax

@xtalax xtalax commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Status: no-op until a Symbolics release contains JuliaSymbolics/Symbolics.jl#2097. #2097, which adds the scalarize_single_element_regions field to CodegenFunctionOptions, is merged (4c96e0cc) but not yet released; the latest Symbolics release is v7.45.0. SymbolicUtils v4.51.0, released 2026-10-14, contains JuliaSymbolics/SymbolicUtils.jl#1154, which implements the option. The new code here is guarded on the field existing, so with registered Symbolics it does nothing and code generation is unchanged. It takes effect once a Symbolics release with the field is installed, and the Symbolics compat floor can then be raised to that release. The du side of the same GPU problem is #5242, which is independent of this change. This branch is rebased onto master aea9c2e.

Summary

generate_rhs now passes scalarize_single_element_regions = false to Symbolics' codegen for implicit-DAE residuals assembled from array equations. A one-element slice equation, such as u[1:1] ~ 0 or D(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 ArrayMaker with 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 with batched_setindex!. Arrays that disallow scalar indexing, such as CuArray under CUDA.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 existing setproperties on codegen_opts:

if implicit_dae && assemble_residuals &&
        hasfield(typeof(codegen_opts), :scalarize_single_element_regions)
    codegen_opts = setproperties(codegen_opts, (; scalarize_single_element_regions = false))
end

With a Symbolics version that lacks the field, the condition is false and nothing changes.

Scalar equations reach the ArrayMaker as array_literal terms, 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).

  • One-element slices: for D(u[1:1]) ~ -u[1:1] and u[1:1] ~ zeros(1), the in-place residual contains no batched_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.
  • Scalar equation: 0 ~ u[1] contains one in every configuration.
  • Values: the DAEProblem residual equals hand-computed values, unconditionally.

Results, locally, Julia 1.12.6:

setup array_equation_dae.jl
registered Symbolics 7.45.0 and SymbolicUtils 4.51.0 (as in CI), rebased branch 36/36; the field is absent, so the guard makes the change a no-op
Symbolics master 4c96e0cc (contains #2097) and SymbolicUtils v4.51.0 (a4f5c92a), rebased branch 36/36; the view write is active
first commit (no guard), Symbolics with the field, before the source change 34 passed, 2 failed (the two one-element-slice assertions)
  • GROUP=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:66 throws ArgumentError: 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.
  • Runic --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:

These five also fail on current master aea9c2e:

job failure master run
[InterfaceI] (lts, 1) variable_parsing.jl:66: ArgumentError: Variable x must have default of matching size Sublibrary CI 38488998911 (jobs 115657054737, 115657054545)
[RegressionI] (lts, 1) latexify.jl:68: output does not match latexify/50.tex Sublibrary CI 38488998911 (jobs 115657054604, 115657054748)
SciMLBase.jl/SymbolicIndexingInterface/1/ (downstream) comprehensive_indexing.jl:127 and :164: BoundsError: attempt to access 1-element Vector{Float64} at index [[1, 2]] IntegrationTest 38100651261, and the same job on master's earlier runs

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 DAEProblem residual was called on CuArrays under CUDA.allowscalar(false) with N = 32:

equations batched_setindex! GPU, allowscalar(false)
D(u) ~ -u 0 passes, matches CPU
D(u[2:N]) ~ u[1:N-1] .- u[2:N], u[1:1] ~ zeros(1) 0 passes, matches CPU
same slice, D(u[1:1]) ~ -u[1:1] 0 passes, matches CPU
same slice, 0 ~ u[1] (scalar equation) 1 fails: a scalar equation reads 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 2 batched_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

xtalax and others added 2 commits October 14, 2026 17:43
…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>
@xtalax
xtalax force-pushed the dae-scalarize-opt branch from eb7665c to 15ea23e Compare October 14, 2026 16:53
@xtalax
xtalax marked this pull request as ready for review October 14, 2026 16:54
xtalax added a commit to xtalax/ModelingToolkit.jl that referenced this pull request Oct 14, 2026
…/dae-gpu

# Conflicts:
#	lib/ModelingToolkitBase/test/array_equation_dae.jl
xtalax and others added 3 commits October 17, 2026 01:04
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>
@xtalax-droid

Copy link
Copy Markdown
Contributor

Pushed cfc44ade07, a merge of current master into this branch. The only conflict was in generate_function_wrapper in codegen.jl: both the scalarize_single_element_regions = false block and the new chunk_size = -1 call are kept, since they are independent. array_equation_dae.jl passes (all testsets) and Runic is clean on the touched files.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

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>
@xtalax-droid

Copy link
Copy Markdown
Contributor

Pushed 870388c486, a merge of current master into this branch (no conflicts). Against master the branch is still only the original change: generate_function_wrapper in lib/ModelingToolkitBase/src/systems/codegen.jl (+7) and array_equation_dae.jl (+35).

array_equation_dae.jl passes 59/59 and dae_jacobian.jl 1/1 on the merged head; Runic reports no diff on the two touched files. The only red checks before this push were the known master-red ModelOrderReduction Core job (deim.jl DimensionMismatch) and the sublibrary QA job; the new run will show whether that is still the case.

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
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

@AayushSabharwal
AayushSabharwal merged commit 16f1111 into SciML:master Nov 17, 2026
103 of 111 checks passed
@xtalax-droid

Copy link
Copy Markdown
Contributor

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
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants