Skip to content

Support generic callback event storage - #4398

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:fix/diffeqbase-generic-event-detection
Aug 28, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:fix/diffeqbase-generic-event-detection

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

What changed and why

findall_events! now uses a single map! method for all callback sign storage, including GPU-backed arrays. It calls the two-argument is_event_occurrence rather than a per-call closure. The CPU Array/SubArray loop specialization was dropped after a microbenchmark showed map! of that named function matches the @inbounds loop at callback-relevant lengths. The helper is now public and documented, and DiffEqBase is bumped from 7.18.2 to 7.19.0 because this adds public API.

This is the prerequisite for fixing vector continuous callbacks in DiffEqGPU's EnsembleGPUArray path.

Ignore this PR until it has been reviewed by @ChrisRackauckas.

Verification

Failing before the implementation, using the added generic-array regression test on clean upstream/master:

MethodError: no method matching findall_events!(::GenericEventArray{Float64}, ::GenericEventArray{Float64})
generic vector callback event storage | Fail 1  Error 1  Total 2

Passing after the implementation:

generic vector callback event storage | Pass 2  Total 2
Callbacks                             | Pass 25 Total 25
Testing DiffEqBase tests passed

After the follow-up that always uses map!(is_event_occurrence, …) (head 085a21e87), the same Core/QA commands completed locally:

GROUP=Core julia +release --project=lib/DiffEqBase -e 'using Pkg; Pkg.test()'
  Callbacks | Pass 35 Total 35
  Testing DiffEqBase tests passed

GROUP=QA julia +release --project=lib/DiffEqBase -e 'using Pkg; Pkg.test()'
  Aqua | Pass 9 Total 9
  Testing DiffEqBase tests passed

After rebasing over upstream master at a27ee037e, these exact commands completed locally on the previous head:

GROUP=Core julia +release --project=lib/DiffEqBase -e 'using Pkg; Pkg.test()'
  Callbacks | Pass 25 Total 25
  Testing DiffEqBase tests passed

GROUP=QA julia +release --project=lib/DiffEqBase -e 'using Pkg; Pkg.test()'
  Aqua | Pass 9 Total 9
  Testing DiffEqBase tests passed

julia +release --project=docs docs/make.jl
  completed through RenderDocument; deployment skipped outside CI; exit 0

julia +release -m Runic --check lib/DiffEqBase/src/DiffEqBase.jl lib/DiffEqBase/src/callbacks.jl lib/DiffEqBase/test/callbacks.jl
typos --force-exclude --format brief lib/DiffEqBase/Project.toml lib/DiffEqBase/src/DiffEqBase.jl lib/DiffEqBase/src/callbacks.jl lib/DiffEqBase/test/callbacks.jl docs/src/devtools/internals/public_api.md
git diff --check upstream/master...HEAD
  all exited 0 with no output

CI failure classification

All 14 failed checks on head e919d5b34 reproduce on clean upstream or are unchanged pre-existing failures outside this PR's DiffEqBase callback-event diff. No CI failure is branch-introduced, so this PR adds no unrelated fix.

Failure family Red checks Clean-base evidence / prerequisite
Stochastic nonlinear-cache contract Stochastic Interface1, Interface3, AlgConvergence, AlgConvergence3, Stochastic downgrade, DelayDiffEq SDDE Each rejects a StochasticDiffEqImplicit cache at nlsolve!. Clean master has the identical downgrade failure in job 98649264888. The focused prerequisite accepts the shared SciML cache contract: ChrisRackauckas-Claude@d7dad13
TauLeaping cache interface Stochastic Interface2 TauLeaping still defines the legacy stochastic alg_cache signature while current OrdinaryDiffEqCore calls the generic signature. Both definitions are identical on the PR base and current master; the legacy method entered with cea8ebb. This needs a separate Stochastic fix.
Hairer4 Newton-Krylov reproducibility Regression_I Julia 1, Regression_I Julia pre The unchanged test reports drift 1.4306299457644833e-4 and 5.135110298093437e-5 against 1e-5. The existing focused test setup fix is ChrisRackauckas-Claude@04240ce.
PDIRK downgrade floor OrdinaryDiffEqPDIRK downgrade Clean master has the same guard-test failure in job 98649264702. The focused compat-floor prerequisite is ChrisRackauckas-Claude@cffd6ea.
ModelingToolkit / SymbolicUtils metadata corruption MTK InterfaceI, MTK InterfaceII, MTK Initialization Clean master has the same Base.ImmutableDict{DataType,Any} typeassert failures in InterfaceI, InterfaceII, and Initialization. The owning-package fix is JuliaSymbolics/SymbolicUtils.jl#1033. InterfaceII also encounters the Stochastic cache failure above.
SciMLSensitivity mixed-precision Enzyme activity SciMLSensitivity Core1 Clean master has the identical EnzymeRuntimeActivityError at scimlstructures_interface.jl:154 in job 98638298114. The focused SciMLSensitivity prerequisite is ChrisRackauckas-Claude/SciMLSensitivity.jl@aaf9812.

Current upstream master is one unrelated DiffEqDevTools commit ahead (468bea34e); rebasing solely for that commit would invalidate this audited CI run and trigger the full matrix again.

Not verified locally

No CUDA device is available on this host, so the downstream CUDA callback path remains for DiffEqGPU CI to verify after this prerequisite is available. The unrelated failure families above were classified from their complete logs and matching clean-master runs; their separate focused fixes were not combined with this PR.

Links

🤖 Generated with Codex CLI 0.150.1 (model: unknown; session: local session ID 01a0441e-3d2f-7170-ab6e-58ef72975a9f).

Follow-up 085a21e87 generated with Grok 1.0.5 (model: grok-4.6; session: local session ID 01a045f9-5360-7622-b38d-e4d084141432).

Add a map!-based findall_events! fallback for non-Array storage such as GPU arrays, make the shared helper public, document it, and bump DiffEqBase for the new public API.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Codex <[email protected]>
Agent-Harness: Codex CLI 0.150.1
Agent-Model: unknown
Agent-Session: local session ID 01a0441e-3d2f-7170-ab6e-58ef72975a9f
@ChrisRackauckas-Claude
ChrisRackauckas-Claude force-pushed the fix/diffeqbase-generic-event-detection branch from 9463a94 to e919d5b Compare August 27, 2026 21:18
Drop the Array/SubArray loop specialization. With a non-capturing
two-argument is_event_occurrence, map! matches the inbounds loop at
callback-relevant lengths, so one generic method is enough.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Grok <[email protected]>
Agent-Harness: Grok 1.0.5 (5115b46bc9)
Agent-Model: grok-4.6
Agent-Session: local session ID 01a045f9-5360-7622-b38d-e4d084141432
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

map! vs the Array/SubArray loop

There is no remaining reason to keep a CPU-only loop. The specialization existed because the 2021 generic method captured runtime Bools (hasaffect / hasaffectneg) in a closure; that was allocating and type-unstable, so the @inbounds loop was the fast path (SciML/DiffEqBase.jl#705). After affect_neg! was dropped from VectorContinuousCallback, that generic method was deleted as unused (4e503e5d52). Restoring map! of a non-capturing named function removes the original reason for the split.

Microbenchmark on Julia 1.12.6, Vector{Float64}, 0 allocations for every variant that actually fuses:

n loop map! closure map! named @. named @. with &&
1 39 ns 40 ns 40 ns 40 ns 40 ns
8 50 ns 50 ns 50 ns 50 ns 50 ns
64 60 ns 60 ns 70 ns 60 ns 119 ns
1024 440 ns 490 ns 500 ns 430 ns 1550 ns
65536 26.9 µs 29.0 µs 29.2 µs 27.1 µs 413 µs

At callback-relevant lengths (n is callback.len, typically tiny) the loop, map! of a closure, and map! of a named function are the same ~40–70 ns. At 64k elements map! is about 8% slower; that size is not the VCC path. Inline @. next = prev != 0 && prev * next <= 0 is the one to avoid: short-circuit && does not fuse in broadcast (15× at 64k). @. next = is_event_occurrence(prev, next) matches the loop.

Closure

Follow-up 085a21e87 drops the anonymous (next, prev) -> … and the Array/SubArray method. findall_events! is now one method:

function findall_events!(next_sign, prev_sign)
    map!(is_event_occurrence, next_sign, prev_sign, next_sign)
    return any(isone, next_sign)
end

using a two-argument is_event_occurrence(prev, next) = prev != 0 && prev * next <= 0. Argument order matches the existing four-argument scalar method (prev, then next), so map! reads prev_sign then next_sign into dest next_sign. The four-argument method is unchanged: it still gates on affect! / affect_neg! for scalar ContinuousCallback.

The Union{Array, SubArray} method was also a GPU footgun for non-contiguous views (SubArray of a CuArray would have hit the scalar loop). Contiguous 1-D CUDA views are still CuArray via derive, so they already missed that method; always using map! makes that dispatch accidental rather than load-bearing.

Verification after the follow-up:

GROUP=Core  Callbacks | Pass 35 Total 35
GROUP=QA    Aqua      | Pass 9 Total 9

CUDA execution was not run here (no device). Downstream remains SciML/DiffEqGPU.jl#503.

🤖 Generated with Grok 1.0.5 (model: grok-4.6; session: local session ID 01a045f9-5360-7622-b38d-e4d084141432).

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review August 28, 2026 01:51
@ChrisRackauckas
ChrisRackauckas merged commit 52bf8f3 into SciML:master Aug 28, 2026
266 of 271 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 11, 2026
- Fix definite assignment in ROCK stages (#4321)
- Correct NonlinearSolve downgrade dependency floors (#4394)
- Fix ensemble work-precision error storage (#4390)
- Add explicit benchmark tag semantics (#4402)
- fix: require cache-aware nonlinear solver in PDIRK (#4406)
- fix: accept SciML caches in nlsolve! (#4407)
- Support generic callback event storage (#4398)
- test: account for Hairer4 BLAS variation (#4410)
- Fix Rosenbrock interpolation after callback truncation (#4411)
- Fix weak Platen methods for scalar Wiener processes (#4413)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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