Skip to content

Compile the TS callbacks for the context they run with - #316

Merged
singhharsh1708 merged 12 commits into
mainfrom
perf-step
Oct 23, 2026
Merged

singhharsh1708 merged 12 commits into
mainfrom
perf-step

Conversation

@singhharsh1708

Copy link
Copy Markdown
Member

The SciMLBenchmarks PETScDiffEq pages (SciMLBenchmarks #1954) put TSRK("5dp") at 21x DP5 and TSRK("3bs") at 11x BS3 on Lotka-Volterra, and TSRosW("ra34pw2") at 13x ROS34PW2 on HIRES. Each pair shares its coefficients and takes the same steps, so the gap is the cost of a step. On main that cost was mostly the way PETSc's callbacks reach Julia.

Measured with long solves at abstol = reltol = 1e-10 (1e-8 for 3bs) and fixed per-solve cost subtracted, on Julia 1.12 with PETSc 3.25.4, as the minimum of interleaved runs on main and this branch: Lotka-Volterra 5dp goes from 1419 to 808 ns per step (DP5: 137) and 3bs from 885 to 509 ns (BS3: 101); Lorenz-96 with 200 states under 5dp from 4.71 to 4.25 µs (DP5: 2.76); HIRES TSRosW("ra34pw2") at 1e-12 from 8.32 to 7.09 µs (ROS34PW2: 0.75); a dense 50-state linear problem under TSRosW with an analytic jac from 32.5 to 26.2 µs; Lotka-Volterra 5dp through the integrator with a DiscreteCallback from 2.25 to 1.55 µs, and with a ContinuousCallback from 3.87 to 2.59 µs. Allocations per step fall from 1787 B to 0 on Lotka-Volterra and from 2944 B to 320 B on HIRES. The f-call counts do not change.

On main every callback went through a @cfunction compiled into the package image, which calls its target through jl_apply_generic and boxes the arguments; the target then dispatched again on the abstract TSContext. The RHS callback also built two PETSc.VecPtrs, each an allocation plus a PetscObjectGetComm call, and wrapped each array in a new Array. The attempt check that counts maxiters runs at every stage through TSSetFunctionDomainError and looked its context up in a Dict, asserted an abstract Tuple and dispatched dynamically each time, about 340 ns of a 5dp step on 1.10.

The callbacks now take the context as Ref{C} and are made per context type at run time with eval, since a package image's @cfunction whose signature names a static parameter loses that parameter. The no-context hooks take the TS pointer as a Ptr{C}, so the context type is known statically, and the RHS and monitor copy vectors through a non-allocating wrapper and raw VecGetArray calls. When another package precompiles code that solves, it cannot eval into PETScDiffEq, so the callbacks fall back to untyped entry points there: a throwaway package that solves with TSRK and TSRosW in its @compile_workload failed with "Evaluation into the closed module" before that fallback and precompiles on 1.10 and 1.12 with it. The callback table is guarded by a lock; a 4-thread EnsembleThreads run with new context types and a concurrent first-use loop over 360 new context types both pass. Smaller changes: the state read keeps the untyped relayout field off the plain path, the dense W assembly reads the mass matrix outside the n² loop, TSHandles.ts has a concrete type, the event finder builds its crossing list only when a condition crosses, and the attempt check skips its try while more than one attempt remains.

The cost is at first solve, minimum of five fresh processes against main: on 1.10 first TSRK +13 ms, first TSRosW +49 ms, first solve with a new context type +46 ms; on 1.12 +8, +122 and +87 ms. The trace-compile testset still sees 3 compiled PETScDiffEq methods. For scale, PETSc's own TSRK with a C right-hand side takes about 530 ns per step for 5dp and 345 ns for 3bs on Lotka-Volterra, and TSRosW on HIRES with a C IFunction and IJacobian 5.5 to 5.8 µs, so most of what remains is in PETSc.

sol.t, sol.u, the stats and the retcode are bitwise equal to main on 1.10 and 1.12 in 102 cases: TSRK 3bs, 5dp, 8vr and the default, three TSRosW subtypes, BDF, ARKIMEX 3 and 4, beuler, TSDAE, mass matrices and analytic jacs, with and without saving, dense output, saveat, save_idxs, fixed dt, discrete and continuous callbacks, maxiters, dtmin, unstable_check, a reversed span, Float32, a matrix state, tstops, an out-of-place f and the integrator. The testset "several PETSc builds in one process" checked per-build pointers from the removed _callbacks(petsclib); it now checks that the Float64 and Float32 contexts get different rhs pointers and that a second lookup returns the cached one. On 1.10, 56 testsets pass (9399 tests), among them Float32, complex states, the maxiters counts, Integrator interface, ContinuousCallback, VectorContinuousCallback, tstops, Mass matrices, DAEProblem, a DMStag as the dm, second-order and partitioned problems, PETScAdjoint and the precompile and first-solve testsets; on 1.12, 10 of them pass (797 tests).

AI Disclosure: Claude assisted with the code. Testing and review by @singhharsh1708.

@singhharsh1708
singhharsh1708 merged commit 8f2a7a8 into main Oct 23, 2026
28 of 32 checks passed
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