Skip to content

Make DAEProblem construction linear in array unknown size (evaluate_varmap! dedupe) - #5280

Closed
xtalax wants to merge 2 commits into
SciML:masterfrom
xtalax:evaluate-varmap-dedupe
Closed

xtalax wants to merge 2 commits into
SciML:masterfrom
xtalax:evaluate-varmap-dedupe

Conversation

@xtalax

@xtalax xtalax commented Oct 16, 2026

Copy link
Copy Markdown
Member

Constructing a DAEProblem for a system with a large array unknown takes time quadratic in the array length. This PR makes it linear: on a simple 46k-state array DAE, construction drops from 394 s and 316 GiB allocated to 2.6 s and 1.2 GiB.

Cause

__process_SciMLProblem with implicit_dae builds du0 with varmap_to_vars(op, ddvs), where ddvs holds one D(u[i]) per scalar unknown and no ir is passed. That selects the AbstractDict method of evaluate_varmap!. For each scalar key it calls split_indexed_var and then works on the whole array: get(varmap, arr) on the AtomicArrayDictSubstitutionWrapper scans every element for holes, and fixpoint_sub then substitutes the whole array value. Doing this once per element costs O(N²) for an array of length N.

Change

evaluate_varmap!(::AbstractDict, vars) now records each array it has processed in a Set and skips it when another element of the same array comes up. Scalar variables map to themselves under split_indexed_var, so they are visited exactly once as before.

The result is unchanged. Once an array value has been substituted to a fixpoint, substituting it again returns the same value, so the repeated passes did no useful work. The one case where they differed is a substitution that does not converge within limit: before, an array was resubstituted up to limit times per element; now it is resubstituted up to limit times in total, which matches what substitution_limit documents. Arrays that are only partly present in the varmap behave as before, because get on an array with holes returns nothing in both versions and the missing entries are reported in the same way. The IRStructure method, used for u0, is not touched.

Benchmark

DAEProblem(sys, op, (0.0, 1.0); build_initializeprob = false) for D(u) ~ -b .* u with u(t)[1:n] plus one scalar unknown, op giving D(u) => -b .* u. Times are for a warm call (compilation excluded), Julia 1.12.6.

states before: time before: allocated after: time after: allocated
12k 23.9 s 21.7 GiB 0.62 s 0.27 GiB
46k 394 s 316 GiB 2.59 s 1.16 GiB
183k not run not run 13.4 s 5.07 GiB

A log-log fit of time against state count gives an exponent of 2.05 before the change (3k to 46k, including a 3k point at 1.46 s and 1.41 GiB) and 1.13 after it (12k to 183k). For allocations the exponents are 1.98 before and 1.08 after. The 183k case was not run before the change; extrapolating the fit puts it at about 6600 s, which is an estimate, not a measurement.

This was found while building large MethodOfLines.jl systems (a 46M-state 2D FDTD discretization), where du0 construction dominated problem setup.

Tests

Two testsets are added to test/array_equation_dae.jl:

  • A DAEProblem with an array unknown, a scalar unknown and two parameters, where the varmap gives symbolic values that chain through other entries (D(u[i]) => b * u[i] + D(w), D(w) => b * w, b => 2c, u => w .* (1:n)). It checks u0 and du0 against hand-computed values, so it exercises the fixpoint substitution and not just constant values.
  • A complexity guard that compares the allocations of DAEProblem construction at n = 500 and n = 2000. The ratio is 3.9 with this change and 14.4 without it; the test requires it to be below 8. It counts bytes rather than wall-clock time, so it is not sensitive to machine load.

array_equation_dae.jl, initial_values.jl, sciml_problem_inputs.jl, initializationsystem.jl and odesystem.jl pass locally (the only non-passing results are the existing @test_brokens). The complexity guard fails on current master, as expected.

🤖 Posted by an AI agent · harness: Claude Code 2.1.299 · model: claude-opus-5-5
Review: unreviewed
Session: 644ed0ff-c8c9-4cc3-b4c6-f326231a4030

The AbstractDict method of evaluate_varmap! maps every element of an array
variable to the array and substitutes the whole array, but it did so once per
element. DAEProblem builds du0 from one D(u[i]) per scalar unknown, so its
construction was quadratic in the array length. Skip arrays already visited.

Agent-Harness: Claude Code 2.1.299
Agent-Model: claude-opus-5-5
Agent-Session: 644ed0ff-c8c9-4cc3-b4c6-f326231a4030
Agent-Review: unreviewed
Co-authored-by: Claude Code <[email protected]>
@xtalax
xtalax marked this pull request as ready for review October 19, 2026 19:29
@xtalax

xtalax commented Oct 19, 2026

Copy link
Copy Markdown
Member Author

Closing in favour of #5190, which fixes the same quadratic evaluate_varmap! cost (#5187), was opened earlier, and keeps master's substitution semantics exactly. This PR deduplicates per array, which differs from master when a fixpoint substitution hits its iteration limit.

One data point for #5190: a 46k-state MethodOfLines 2D TM DAE, where DAEProblem construction took 394 s and 316 GiB on master, and 2.6 s and 1.2 GiB with the per-array dedupe here.

🤖 Posted by an AI agent · harness: Claude Code 2.1.304 · model: claude-opus-5-5
Review: unreviewed
Session: aa65e380-52df-443a-9f57-8c20c56babb9

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.

1 participant