Skip to content

Resolve symbolic array operating points in linear time - #5327

Merged
AayushSabharwal merged 4 commits into
SciML:masterfrom
xtalax-droid:forge/varmap-array-resolve2
Oct 30, 2026
Merged

AayushSabharwal merged 4 commits into
SciML:masterfrom
xtalax-droid:forge/varmap-array-resolve2

Conversation

@xtalax-droid

Copy link
Copy Markdown
Contributor

Building a problem from an operating point whose array value is symbolic (for example u => [p*i for i in 1:N]) took O(N^2) in the IR evaluate_varmap!. Each element write went through write_possibly_indexed_array!, which scalarizes, copies and rehashes the whole array (70.8 s at N = 8000).

The IR evaluate_varmap! now holds element writes to constant arrays and array literals in per-array buffers (PendingWritesWrapper) and stores an array back only when the substituter next reads it, and at the end. Other array expressions and all other keys take the old path, so rules for (expr)[i] still apply. The non-IR method also settles an array key once substitution leaves it unchanged. Substitution order and results are unchanged.

The new test file array_varmap_resolve.jl (registered in the InterfaceI group) checks identical u0, du0 and p for array, per-element and mixed element values, symbolic array values, element-referencing chains, a matrix unknown, and array-expression rules, on both paths.

Symbolic initial condition x => [q*i for i in 1:N], IR path: 1.03 / 4.08 / 16.3 / 70.8 s on master versus 0.008 / 0.017 / 0.041 / 0.093 s here, at N = 1000 / 2000 / 4000 / 8000.

This is independent of, and complementary to, #5190. #5190 removes the O(N) array-key lookup per element in the non-IR evaluate_varmap!. This PR does not touch that, and it still dominates DAEProblem builds with array unknowns (13.4 s at N = 10000 with this PR alone). This PR fixes the symbolic-array case that #5190 leaves quadratic. It is the "per-element varmap resolution" item of the FiniteElementMethod.jl upstream-blocker list.

Element-dependency chains (x[i] => x[i-1] + 3.0) on the IR path are unchanged from master and still quadratic (about 16 s at N = 4000 on both), because each read of a neighbouring element flushes the pending buffer.

Tests: Initialization group passes; InterfaceI has 1822 passed and one failure, components.jl:446 "Causal connections generate causal equations", which also fails on master. Codex review (two rounds) approved.


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

xtalax and others added 2 commits October 26, 2026 16:44
evaluate_varmap! rewrote the whole array value on every element write, and the non-IR method re-substituted an array whose value stayed symbolic once per element, so a symbolic array initial condition cost O(N^2) (62 s at N = 8000 on the IR path). The IR method now buffers element writes per array and stores the array back only when it is next read, and the non-IR method settles a key whose substitution reached a fixpoint. Substitution order and results are unchanged. Independent of, and complementary to, SciML#5190.

Agent-Harness: Claude Code 2.1.310
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <[email protected]>
Only constant arrays and array literals are buffered per element. Other array expressions keep (expr)[i] on first read, so a substitution rule for expr still applies, as before the buffering.

Agent-Harness: Claude Code 2.1.310
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <[email protected]>
@xtalax-droid

Copy link
Copy Markdown
Contributor Author

CI status on this PR: the two failing jobs, Benchmarks (1) and Benchmarks (lts), fail before any benchmark runs, in Pkg resolution inside the AirspeedVelocity action:

Unsatisfiable requirements detected for package ModelingToolkitBase [7771a370]:
 ├─possible versions are: 1.77.5 or uninstalled
 ├─restricted to versions 1.78.0 - 1 by ModelingToolkit [961ee093] — no versions left
 │ └─ModelingToolkit [961ee093] is fixed to version 11.46.0
 └─ModelingToolkitBase [7771a370] is fixed to version 1.77.5

The benchmark environment pins the registered ModelingToolkitBase 1.77.5, while the checked-out ModelingToolkit requires 1.78 or newer, which is not in the registry yet. This PR does not change either Project.toml. Run: https://github.com/SciML/ModelingToolkit.jl/actions/runs/40401452121


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

Comment on lines +613 to +625
"""
$(TYPEDEF)

Wrapper over an `AtomicArrayDictSubstitutionWrapper` that holds element writes to array
values in per-array buffers and stores each buffer back as a whole array only when that
array is next read through the wrapper, or on `flush_pending_writes!`. Writing `n`
elements of one array through the wrapped dictionary copies and rehashes the array each
time, which is quadratic in its length.
"""
struct PendingWritesWrapper{D} <: AbstractDict{SymbolicT, SymbolicT}
base::AtomicArrayDictSubstitutionWrapper{D}
buffers::Dict{SymbolicT, Array{SymbolicT}}
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this data structure be moved to the atomic_array_dict.jl file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved PendingWritesWrapper, its constructor, flush_pending_writes! and its Base dictionary methods (get, haskey, length, iterate) into atomic_array_dict.jl, after AADSubWrapper (4ebe173). pending_element_buffer! and the evaluate_varmap! loop stay in problem_utils.jl. atomic_array_dict.jl is included before problem_utils.jl, so nothing else changes.


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

buffer = pending_element_buffer!(pending, k)
if buffer !== nothing
idx = get_stable_index(k)
v = buffer[idx]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this infer even though buffer won't infer?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not fully infer. @code_warntype of the IR path shows buffer::Any in the loop, because pending_element_buffer! returns Any (the @match result is not inferred, and Array{SymbolicT} is abstract in its dimension count), so the buffer getindex/setindex! calls are dynamic dispatches. evaluate_varmap! itself still returns Nothing.

The cost is small: the x => [q*i for i in 1:N] case on the IR path takes 0.012 s at N = 4000 and 0.037 s at N = 8000 on the machine I measured on (single warm runs, no JET), and the time is dominated by the per-element substitution, not the buffer access. Annotating pending_element_buffer! with ::Union{Nothing, Array{SymbolicT}} makes it infer, but it measured no faster (0.014 s and 0.038 s, about 4% more allocation), so it was left unannotated. The annotation is a one-line change if you would still prefer it.


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

Review feedback on SciML#5327: the data structure and its dictionary interface live in atomic_array_dict.jl; the buffering logic stays in problem_utils.jl.

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 <[email protected]>
@AayushSabharwal

Copy link
Copy Markdown
Member

I'm also putting a bot on not hashing every element of a symbolic array.

@AayushSabharwal
AayushSabharwal merged commit b2c264a into SciML:master Oct 30, 2026
93 of 111 checks passed
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