Repository navigation
Resolve symbolic array operating points in linear time - #5327
Conversation
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]>
|
CI status on this PR: the two failing jobs, 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 🤖 Posted by an AI agent · harness: Claude Code 2.1.311 · model: claude-sonnet-5-5 |
| """ | ||
| $(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 |
There was a problem hiding this comment.
Can this data structure be moved to the atomic_array_dict.jl file?
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
Does this infer even though buffer won't infer?
There was a problem hiding this comment.
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]>
|
I'm also putting a bot on not hashing every element of a symbolic array. |
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 IRevaluate_varmap!. Each element write went throughwrite_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 identicalu0,du0andpfor 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 dominatesDAEProblembuilds 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