Repository navigation
Skip redundant sparse-Jacobian structure rebuild in calc_J! - #3980
ChrisRackauckas merged 4 commits into
Conversation
For a user-supplied sparse analytic Jacobian, calc_J!/calc_J_dae! rebuilt J's sparsity structure on *every* evaluation via `J .= true .* jac_prototype` (added in a1278ad for issue SciML#2653). f.jac only writes into already-stored entries, so J's structure is invariant after the first build; the rebuild is a per-eval O(nnz) sparse broadcast plus allocation that is only needed when J does not already match the prototype. Guard the rebuild behind a `same_sparsity_structure` check via a new `prepare_sparse_jac!` helper. When J already matches the prototype (the common path, including the `similar(jac_prototype)` allocation build_J_W uses), only the nzval zeroing runs; otherwise the full rebuild runs exactly as before, so the SciML#2653 mismatch case is preserved. This also stops clobbering the user's `jac_prototype.nzval` to all-ones as a side effect of every Jacobian evaluation (the old unconditional `set_all_nzval!(jac_prototype, true)`); the fast path now leaves it untouched. Measured on a 1122x1122, 31k-nnz Jacobian: rebuild ~78us -> guarded ~14us (~64us saved per Jacobian evaluation). Most impactful for strict Rosenbrock methods (e.g. Rodas5P) that re-evaluate J every step, and for smaller stiff systems where the LU factorization does not dominate the step. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01LWooL8qipuUf7nTrDUUzqY
End-to-end A/B resultsBaseline vs this branch, same script in each env, Julia 1.11. Each problem's standard dense RHS is Solver statistics (
† BRUSS100 re-measured over 40 independent solves (medians shown) after an initial run produced a contention outlier; the clean numbers are consistent with the rest. FBDF is within noise of zero on all of these — it performs very few Jacobian evaluations (1–11 here), so there is almost nothing to save. Honest readThe isolated operation drops cleanly (rebuild ~78 µs → ~14 µs, a 5.6× reduction of that step), but end-to-end this is a small, strictly-positive, zero-downside improvement (~1–4% on Jacobian-heavy Rodas5P, neutral on FBDF), not a dramatic speedup — the rebuild is only a slice of the total step, and the relative benefit shrinks as the LU factorization comes to dominate (on very large systems like BCR it is lost entirely behind the ~40 ms factorization). The value is that it removes provably-redundant per-eval work and fixes the |
Covers the has_jac + sparse jac_prototype path (the branch guarded by prepare_sparse_jac!), which the existing nonfulldiagonal_sparse test does not reach (it supplies only a jac_prototype, hitting the AD path). Asserts, for both the matching-structure fast path and the mismatched-structure rebuild path, that the result is byte-identical to the old unconditional rebuild, and that the fast path no longer clobbers the user's jac_prototype.nzval. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01LWooL8qipuUf7nTrDUUzqY
Validation update
Note: the existing |
ArrayInterface 7.28 adds a public `same_sparsity_structure(A, B)` (JuliaArrays/ ArrayInterface.jl#496), so drop the local copy and its SparseArrays-extension method and call the public API from `prepare_sparse_jac!`. Removes the duplication and lets the sparse-structure check improve/extend (dense, GPU CSC/CSR, structured types) in one shared place. - raise ArrayInterface compat floor 7.19 -> 7.28 (the version that adds the function); OrdinaryDiffEqDifferentiation 3.3.1 -> 3.3.2 - test now imports same_sparsity_structure from ArrayInterface Behavior-preserving: the call site is gated by `is_sparse_csc`, so both arguments are always `SparseMatrixCSC` and hit ArrayInterface's CSC method (identical colptr/rowval comparison). End-to-end solves are unchanged (identical njacs/ naccept), and the sublib test passes against ArrayInterface 7.28. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01LWooL8qipuUf7nTrDUUzqY
Restacked onto the public
|
What
calc_J!/calc_J_dae!rebuild a user-supplied sparse analytic Jacobian's sparsity structure on every evaluation:This was added in a1278ad for #2653 (a user
jac_prototypewhose pattern doesn't match whatWneeds). Butf.jaconly writes into already-stored entries, soJ's structure is invariant after the first build. The rebuild only needs to run whenJdoes not already match the prototype — in practice just the first call.This PR guards the rebuild behind a
same_sparsity_structurecheck via a newprepare_sparse_jac!helper. WhenJalready matches the prototype (the common path — including thesimilar(jac_prototype)allocation thatbuild_J_Wuses), only thenzvalzeroing runs. When it doesn't, the full rebuild runs exactly as before, preserving the #2653 fix.It also stops a side effect: the old unconditional
set_all_nzval!(jac_prototype, true)clobbers the user'sjac_prototype.nzvalto all-ones on every Jacobian evaluation. The fast path now leaves it untouched.Why it matters
Most impactful for strict Rosenbrock methods (e.g.
Rodas5P) that re-evaluateJevery step, and for smaller stiff systems where the LU factorization does not dominate the step (on very large systems like BCR the ~40 ms sparse factorization dwarfs it).Component measurement on a 1122×1122, 31 369-nnz Jacobian (BCR's
W), Julia 1.11:The rebuild costs ~2× the Jacobian evaluation it precedes.
Correctness
Verified standalone that old and new produce byte-identical
colptr/rowval/nzvalin both cases:The only observable difference is that the new fast path preserves the user's
jac_prototype.nzvalinstead of clobbering it to ones.The
same_sparsity_structurefallback (no SparseArrays extension loaded) returnsfalse, so any non-CSC path conservatively takes the rebuild branch. The change is a pure no-op that skips provably-redundant work and self-corrects (iff.jacever did alter structure, the guard fails next call and rebuilds).Validation status
test/prepare_sparse_jac_tests.jl(9 tests, passing)Success, identical solver stats baseline vs patched; ~1–4% faster on jac-heavy Rodas5P, neutral on FBDF (full table in a comment below)dae_jacobian2w_sparse_testscovering thecalc_J_dae!change,nzval_helpers_tests,nojac_tests)Draft pending review by @ChrisRackauckas.
🤖 Generated with Claude Code
https://claude.ai/code/session_01LWooL8qipuUf7nTrDUUzqY