Repository navigation
Compile the TS callbacks for the context they run with - #316
Merged
Merged
Conversation
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.
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
@cfunctioncompiled into the package image, which calls its target throughjl_apply_genericand boxes the arguments; the target then dispatched again on the abstractTSContext. The RHS callback also built twoPETSc.VecPtrs, each an allocation plus aPetscObjectGetCommcall, and wrapped each array in a new Array. The attempt check that counts maxiters runs at every stage throughTSSetFunctionDomainErrorand 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 witheval, since a package image's@cfunctionwhose signature names a static parameter loses that parameter. The no-context hooks take the TS pointer as aPtr{C}, so the context type is known statically, and the RHS and monitor copy vectors through a non-allocating wrapper and rawVecGetArraycalls. When another package precompiles code that solves, it cannotevalinto PETScDiffEq, so the callbacks fall back to untyped entry points there: a throwaway package that solves with TSRK and TSRosW in its@compile_workloadfailed 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 untypedrelayoutfield off the plain path, the dense W assembly reads the mass matrix outside the n² loop,TSHandles.tshas a concrete type, the event finder builds its crossing list only when a condition crosses, and the attempt check skips itstrywhile 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 differentrhspointers 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.