Repository navigation
Add isolated_copy, a JumpProblem with its own jump state - #714
Conversation
`isolated_copy(jprob)` returns a problem that can be solved concurrently with `jprob`, or used for an integrator alive at the same time as one of its, and shares everything that solvers don't modify: the wrapped problem, the jumps and their functions, the dependency maps and the stored callbacks. Each aggregator lists its working fields; the copy rebuilds it through its default constructor from copies of those fields and its other fields as they are, and rebuilds the jump callbacks around it (a new VR_Direct cache; VR_FRM's callbacks are reused). No closure or FunctionWrapper is copied. `resetted_jump_problem`, which StochasticDiffEq calls for `alias_jumps = false` (the default off thread 1), now returns an `isolated_copy` instead of a `deepcopy`. The docs recommend `isolated_copy` over `deepcopy`, and give `deepcopy`'s limitations with FunctionWrapper-based aggregators. Replaces SciML#711, whose tests of concurrent and interleaved use are adapted here. Co-Authored-By: Kaan Öcal <26488673+kaandocal@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@kaandocal we can potentially add a kwarg to do a deeper level of copying like your PR if you need that, but I'd prefer this as the default. Let me know your thoughts. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
Benchmark Results (Julia lts)Time benchmarks
Memory benchmarks
|
typos flagged the affects `ab!` and `ba!` of an A <--> B test problem (in #714) as misspellings of `by` and `be`. Two-letter identifiers are almost always names, so ignore them; longer misspellings are still caught. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
This looks even better! Together with the callback sharing PR I submitted to SciMLBase I think this will work quite nicely, thanks Sam! |
|
Yeah, that PR looks helpful too. I think the only other thing we need outside of JumpProcesses is the trait to opt out of type erasure for specific callbacks and we should be in good shape. |
|
How exactly? I can add this to that PR, or feel free to update it! |
|
If you build a JumpProblem over an ODEProblem all callbacks, including the jumps, get type erased. I think this was done to enable reuse of integrators without recompilation, but it has significant performance impacts on models with jumps. SSAStepper avoids it, and JumpProblem over SDEProblem avoids it as the jump callback gets added after user callbacks are type erased. The trait would be something a callback can opt into to tell DiffEqBase not to type erase it, but honestly I don't know how easy this would be to implement at a individual callback level (i.e. it might require storing two sets of callbacks). The easier solution would be if any one callback declares it shouldn't be type erased you just don't type erase any. In the meantime one should always declare JumpProblems over ODEProblems with full specialization for the ODEProblem unless solver performance really doesn't matter. |
|
i.e. you should do: |
|
(ModelingToolkit now constructs JumpProblems over any other problem type to fully specialize by default, so this shouldn't be an issue with Catalyst-generated models.) |
|
I see, you raised that issue in DiffEqBase.jl the other day. I will leave that one to you experts... |
… buffers
From review: copying arrays of non-bits numbers element by element read the unassigned
entries of a fresh aggregator's buffers (`Vector{BigFloat}(undef, n)`), so `isolated_copy`
threw `UndefRefError` for unsolved problems with `BigFloat` times or `BigInt` states. Arrays
of numbers are now copied as containers.
Each type that holds jump state now declares, next to its definition, the fields that hold
definitions (`shared_fields`), and `isolated_copy` copies every other field. A field nobody
classified is copied, or makes `isolated_copy` throw with an explanation if its type can't
be copied, instead of being shared by mistake; this caught `TimeGrouper`, which isn't a bits
type with `BigFloat` times. The generated rebuild now calls `shared_fields` from the
generated code rather than in the generator, so declarations made in other packages are
seen. Copy times are unchanged.
Tests copy unsolved problems with BigFloat times for each aggregator that supports them, a
BigInt state with RSSA and BigFloat ODEs with VR_Direct and VR_DirectFW, and check that every
aggregator and hopping-rate type declares its shared fields.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test/isolated_copy.jl uses `subtypes` to check that every aggregator declares its shared fields; Pkg.test only loads declared test dependencies, so the ThreadSafety jobs stopped with "Package InteractiveUtils not found". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds
isolated_copy(jprob), which returns aJumpProblemthat can be solved concurrently withjprob, or used for an integrator alive at the same time as one of its, without copying the wrapped problem or the jumps (#287). It replaces #711; thanks to @kaandocal for the report and for the tests, which are adapted here.What is copied and what is shared
DiscreteCallback(new_agg), and forVR_Direct/VR_DirectFWa new cache throughbuild_variable_integcallback.VR_FRM's callbacks are reused, since their state lives in the integrator'su.jump_u.f, the symbolic system,u0,p,tspan), the jumps with their functions andFunctionWrappers, theJumpDependencies, and the stored callbacks andtstops. No closure or wrapper is copied.Each type that holds jump state (the aggregators, the spatial rate tables, the priority tables, the
VR_Directcache) declares next to its definition the fields that hold definitions,shared_fields, which joins the methods a new aggregator defines. A generated function rebuilds the object through its default constructor, sharing those fields and copying all the others, so a field nobody classified is copied, or makesisolated_copythrow with an explanation if its type can't be copied, but is never shared by mistake. A test checks that every aggregator and hopping-rate type declares its shared fields.The copy starts from the source's working data, which every solver re-initializes at
init; onlySortingDirect's learned search order carries over. A copy may be taken betweenstep!calls of a live integrator (the agent-based-model case), but not while another thread is solving the source.StochasticDiffEq
resetted_jump_problem, which StochasticDiffEq calls foralias_jumps = false(the default off thread 1), now returns anisolated_copyinstead of adeepcopy, so that copy becomes cheap with no downstream change.Docs
The
JumpProblemandremakedocstrings, the API page, FAQ, jump types page, migration guide and the ensembles tutorial now recommendisolated_copy(with a short agent-based-model pattern in the tutorial).deepcopystays documented as the fully isolating option, with its limits for jumps stored inFunctionWrappers: the copy may call the original's rate and affect functions and needs the original kept alive. The upstream fix for that is JuliaLang/FunctionWrappers.jl#39. Theisolated_copydocstring also lists what a copy doesn't isolate:pandu0, state captured by user functions, and a noise process passed to anSDEProblem, which StochasticDiffEq writes back intoprob.noiseafter each solve.Timing
Median times on Julia 1.13 for one
isolated_copy, onedeepcopyand one solve overtspan = (0, 1)(about 10 jumps per reaction channel,save_positions = (false, false)). These problems are built without an aggregator, so each row uses the aggregator that automatic selection picks:isolated_copydeepcopyConstantRateJumpring, dependency graph onlyConstantRateJumpring, dependency graph onlyConstantRateJumpring, dependency graph onlyConstantRateJumpring, dependency graph onlyFor these aggregators a copy costs at most 4% of such a solve, while
deepcopycosts 50 to 9,000 times as much as a copy (5,500 to 9,000 times for the Catalyst problems, where it also copies the symbolic system). Across all 12 SSA aggregators, a copy costs at most 11% of such a solve (CCNRM at 10 jumps) and at most 3% from 100 jumps up.All 12 SSA aggregators
Each aggregator given explicitly, on the mass-action ring for 10 to 10,000 jumps and, for some, on the Catalyst problem (2N jumps: N Hill-rate
ConstantRateJumps and N mass-action decays). The copy column also gives the memory allocated.isolated_copydeepcopyTests
New
test/isolated_copy.jl, also in the ThreadSafety group: shared and copied fields for every aggregator (including spatial and the variable-rate caches), seeded equivalence, integrators alive at the same time, threaded solves (SSA and jump-ODE problems), copies taken from liveSSAStepperand ODE integrators (with the original continuing unchanged, also when the copy's integrator type differs), stored versusinitcallbacks, independence from the original's memory, jump-diffusions throughresetted_jump_problem,alias_u0 = true, andPureLeaping.Follow-ups (not in this PR)
isolated_copyhook forsafetycopyand a trait to skip the per-task copy inEnsembleThreads, so ensembles stop deep-copying jump problems.🤖 Generated with Claude Code 2.1.319 (model: claude-opus-5-5)