Repository navigation
NonlinearSolveAlg: report the work actually performed - #4045
Merged
ChrisRackauckas merged 1 commit intoJul 28, 2026
Merged
Conversation
`initialize!` forwarded the inner NonlinearSolve cache's counters verbatim, which got both `nf` and `njacs` wrong, in opposite directions. `nf`: `reinit!` evaluates the residual at the new `z` and *then* zeroes the inner counters, so that evaluation was never visible -- one lost `f` call per stage. On ROBER at rtol 1e-8 that is a 22-37% undercount (FBDF reported 1317 against 2085 real calls). `njacs`: under `W` reuse the inner solver's "Jacobian" is `WReuseJac`, which only copies the `W` this package assembled, so its `njacs` tracks `nw` rather than Jacobian evaluations, while the real evaluations in `_update_nlsolvealg_W!` were counted nowhere -- `calc_J!`, which does the counting for the legacy path, is bypassed there. TRBDF2 reported 1598 Jacobian evaluations against 34 real ones. Count `reinit!`'s evaluation, record the real Jacobian evaluation where it happens, and only take the inner cache's `njacs` when the inner solver actually owns the Jacobian. `njacs` becomes exact and `nf` lands within 0.1%; NLNewton is untouched and stays exact. Out-of-place improves 50% -> 25% but is still short: there the inner solver owns the Jacobian and does not count the evaluations its AD does, which is upstream of this package. Co-Authored-By: Chris Rackauckas <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ignore until reviewed by @ChrisRackauckas.
initialize!forwarded the innerNonlinearSolvecache's counters verbatim. That gets bothnfandnjacswrong, in opposite directions, so any work-precision comparison betweenNonlinearSolveAlgandNLNewtonis reading a different quantity for each.Measured against a counting
f/jacon ROBER,rtol=1e-8,atol=1e-10:nfreported / realnjacsreported / realnfreinit!evaluates the residual at the newzand then zeroes the inner counters, so that evaluation never appears incache.cache.stats.nf. It is exactly one lostfcall per stage, which the arithmetic confirms: FBDF misses 768 calls over 768 stages, TRBDF2 misses 6507 over 2 stages x ~3254 steps.njacsUnder
Wreuse the inner solver's "Jacobian" isWReuseJac, which copies theWassembled here rather than evaluating anything — itsnjacsequalsnwin every configuration. Meanwhile the real evaluations in_update_nlsolvealg_W!were counted nowhere, becausecalc_J!(which does the counting for the legacy path) is bypassed there.So: count
reinit!'s evaluation, record the real Jacobian evaluation where it happens, and only take the inner cache'snjacswhen the inner solver actually owns the Jacobian.After
nferrnjacsreported / realnjacsis exact;nfis within 0.1%. The few remaining calls are the inner cache's construction, which happens outside the per-stage accounting.Known remaining gap
Out-of-place improves from −50% to −25% but is still short. There
W === nothing, the inner solver owns the Jacobian, and it does not count thefevaluations its AD performs — that is upstream inNonlinearSolverather than here, so I did not paper over it from this side. Itsnjacs(1145) is genuine: withoutWreuse the inner solver really does build a Jacobian per iteration.Test
New
nsa_stats_tests.jl, registered inruntests.jl, asserting forKenCarp4/TRBDF2/FBDFxNLNewton/NonlinearSolveAlgthatnfnever overstates, is within 1% of the real count, andnjacsmatches exactly. Verified it fails without the source change:The six failures are exactly the
NonlinearSolveAlgrows; theNLNewtonrows pass either way, as they should. With the change 24/24, and the other four NSA files are unchanged (smooth_est10,sparse10,matrixfree14,jacreuse11). Runic clean.🤖 Generated with Claude Code