Skip to content

FullSpecialize: discontinuity problems hold the zero function, not a FunctionWrapper - #5013

Open
pankgeorg wants to merge 1 commit into
SciML:masterfrom
pankgeorg:fullspec-disco
Open

pankgeorg wants to merge 1 commit into
SciML:masterfrom
pankgeorg:fullspec-disco

Conversation

@pankgeorg

@pankgeorg pankgeorg commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #4917.

Under FullSpecialize, each continuous callback's discontinuity problem (IntervalNonlinearProblem) now holds its zero_func_struct in an IntervalNonlinearFunction{false, FullSpecialize}, instead of a FunctionWrapper{Float64, Tuple{Float64, NullParameters}}.

  • The stored problem type is concrete. When the continuous callbacks differ in type, it is a Union of at most four problem types.
  • Other specializations keep the wrapper, and so does any case where that type isn't concrete or has more than four members.
  • find_discontinuity reads the zero function back through _disco_zero in both cases.

Why: the wrapper calls through a function pointer that is set at run time (reinit_wrapper/gen_fptr), which static compilation (--trim, WasmCodegen) can't resolve. The wrapper exists so the bracketing solve compiles once whatever the callback. FullSpecialize already opts in to compiling per function type, so there it can be dropped.

Compared with #4917:

Tests

  • New lib/OrdinaryDiffEqCore/test/disco_fullspec_tests.jl. It covers:
    • Full vs AutoSpecialize, scalar and vector callbacks: the problem holds the zero function vs the wrapper;
    • no dynamic calls in code_typed(find_discontinuity) under FullSpecialize;
    • a Vector{Any} callback set;
    • two callback types, which give a 2-member Union;
    • maybe_discontinuity = false;
    • SVector and MVector states;
    • detection on and off against an analytic solution;
    • reinit!.
  • Run locally on Julia 1.13:
    • disco_fullspec_tests.jl and disco_scalar_state_tests.jl;
    • test/Integrators_I/disco_tests.jl, without the RadauIIA5 blocks (FIRK wasn't in my env). I ran it once as is and once with every ODEProblem made FullSpecialize.
    • All pass.
  • Compared with master + Type the zero-function reads in find_discontinuity #4917 (the code it replaces) on 5 callback problems (Full/AutoSpecialize × Tsit5/Rodas5P × detection on/off): event times, states and step/reject counts are bit-identical.
  • Compile cost: first solve under FullSpecialize is within noise (≈ +0.07 s on about 2.8 s), and AutoSpecialize is unchanged.
  • WebAssembly (WasmCodegen), together with ModAB, ITP: read the bracket without splatting prob.tspan NonlinearSolve.jl#1397: 9 MTK models with continuous and discrete events build, and all 9 match native results in Node. Each module has 2 fewer functions than with Type the zero-function reads in find_discontinuity #4917, because the wrapper call path is gone. Without Let --trim=safe accept the re-initialization path (workaround for discussion) JuliaLang/FunctionWrappers.jl#38 they build as well, since no wrapper is reachable from the discontinuity path any more.

🤖 Generated with Claude Code

…FunctionWrapper

Under FullSpecialize, each continuous callback's IntervalNonlinearProblem holds its
zero_func_struct in an IntervalNonlinearFunction{false, FullSpecialize}, so the stored
problem type is concrete (a Union of at most four problem types when the continuous
callbacks differ in type). Other specializations, and any case where that type is not
concrete, keep the FunctionWrapper{Float64, Tuple{Float64, NullParameters}}.

The wrapper calls through a function pointer set at run time, which static compilation
(--trim, WasmCodegen) cannot resolve.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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