Skip to content

isolated_copy PR - #711

Closed
kaandocal wants to merge 2 commits into
SciML:masterfrom
kaandocal:isolated_copy
Closed

kaandocal wants to merge 2 commits into
SciML:masterfrom
kaandocal:isolated_copy

Conversation

@kaandocal

Copy link
Copy Markdown
Contributor

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Proposed fix for #287 by introducing isolated_copy for thread-safe duplication of problems.

@isaacsas isaacsas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together so quickly! A lot of it is what we want: the name and signature, the docs and tutorial updates, and especially the tests (interleaved live integrators for every aggregator, the spatial and variable-rate cases, threaded solves). The FunctionWrapper point is a good catch too: a deep-copied wrapper keeps calling the original object through objptr and doesn't keep that object alive. That also affects plain deepcopy(jprob), which our docs currently recommend: with DirectFW, deep-copying a problem and letting the original be collected gave garbage rates in 20 of 20 copies when I tried it. I've opened a fix upstream (JuliaLang/FunctionWrappers.jl#39). Until it's released, our docs will say that a deep copy needs the original problem kept alive, and that it doesn't isolate state captured by the wrapped rate and affect functions.

The main issue is that this PR deep-copies everything except prob and the FunctionWrappers, while the contract I posted in #287 copies only the working state that JumpProcesses' solvers mutate and shares everything else: the jumps, the user's functions and the stored callbacks. Sorry for the mixed signals there: my earlier summary in #287 said JP11's copy would still deep-copy the rate and affect closures, which came from an older version of the plan. The later contract is the one we want, for these reasons.

1. It copies much more than the jump state

Besides the aggregator's working state, deepcopy here also copies constant_jumps, variable_jumps, massaction_jump, the rate and affect functions held as tuples (Direct, FRM) or as not-yet-wrapped affects, Coevolve's bound functions, the dependency graphs, RSSA's brackets, the spatial system and hopping constants, the VR_FRM callback functors, and the callbacks and tstops stored on the problem. A solve never mutates any of these. Sharing prob removes most of deepcopy's cost, but for ModelingToolkit and Catalyst problems this still copies the generated affect object of every jump (through constant_jumps and the aggregator's affects), so the copy grows with the number of jumps and can cost more than a solve. I timed this on a Catalyst ring network: N species, each produced at a Hill-function rate of the previous one (N ConstantRateJumps) and degraded by mass action. "Per-field rebuild" is a quick prototype of the contract's approach (SSA aggregators only). All copies give seeded-identical solves. Median time and memory from BenchmarkTools, Julia 1.13:

N Aggregator deepcopy(jprob) this PR per-field rebuild one solve, tspan (0, 1)
10 Direct 570 µs, 369 KiB 50 µs, 69 KiB 1.2 µs, 3.0 KiB 5.1 µs
10 DirectFW 581 µs, 373 KiB 61 µs, 52 KiB 1.3 µs, 2.0 KiB 10.3 µs
10 NRM 586 µs, 377 KiB 69 µs, 73 KiB 1.8 µs, 2.6 KiB 3.9 µs
100 DirectFW 3933 µs, 2.5 MiB 580 µs, 1.1 MiB 1.6 µs, 6.5 KiB 1088 µs
100 NRM 3967 µs, 2.5 MiB 624 µs, 1.2 MiB 2.1 µs, 7.0 KiB 63 µs

For agent-based models, with one copy per cell and many short solves, the copy would then be the dominant cost.

2. Whether a user's function is isolated depends on the aggregator and on solve history

Because wrappers are shared and plain closures are copied, a stateful closure ends up copied or shared depending on things the user doesn't control. For a ConstantRateJump whose affect increments a captured counter:

Aggregator source never solved source solved once
Direct copied copied
DirectFW copied shared
NRM copied shared

(Direct keeps its affects in a tuple, so they're always copied. For the other aggregators the affects are a plain vector of closures until the first init wraps them; after that they're wrappers, which this PR shares.) The copy's constant_jumps can then hold copied closures while its aggregator calls the originals. We'd rather have one rule: user functions are always shared, as remake shares them and as an ODEProblem's f is shared across ensemble tasks. For jumps that do need per-integrator state, we plan an explicit opt-in for JumpProcesses 11, the jump counterpart of your SciMLBase#1661: an optional hook on a jump that builds its rate and affect for each integrator, so that isolation is something the user asks for rather than a side effect of copying. We still need to check that it can be done without reaching into the aggregators' internals.

3. Stored callbacks

The contract shares jprob.kwargs (the stored callback and tstops), as remake does, while this PR copies them and tests that it does. Per-integrator callback state is what your SciMLBase#1661 addresses, and it does it better: instantiate_callback at init gives every integrator fresh callback state whether the callback is stored on the problem or passed to solve, and whether or not the problem was copied (JumpProcesses would call it in SSAStepper's init too). Copying in isolated_copy would only cover callbacks stored on a JumpProblem, and only when the user copies. Until then, passing a fresh callback to init (init(isolated_copy(jprob), alg; callback = make_callback())) gives the same isolation.

4. Finding wrappers by walking types is fragile

share_function_wrappers! only looks inside JumpProcesses and SciMLBase types, tuples, named tuples and Arrays. A wrapper anywhere else (a user's callable struct, another package's callback functor, a non-Array vector) is deep-copied and silently calls back into the original. Rebuilding by field never copies a wrapper, so this can't arise.

How we'll proceed

Since this changes the core of the PR, I'll open a PR implementing the contract from #287: each aggregator is rebuilt through its positional constructor with only its working fields copied (numbers, arrays, heaps and priority tables), and the jump callbacks are rebuilt around it (VR_FRM's reused, VR_Direct's cache rebuilt); everything else is shared, so no wrapper is ever copied. It will also make resetted_jump_problem use isolated_copy, so StochasticDiffEq's per-solve copy on threads other than 1 becomes cheap. I'd like to bring over your tests and docs changes, adapted to the shared-callback contract, with you credited as a co-author, and I'll ping you for review since you know the agent-based use case best. Thanks again!

🤖 Generated with Claude Code 2.1.319 (model: claude-opus-5-5)

isaacsas added a commit that referenced this pull request Nov 11, 2026
`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 #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

Copy link
Copy Markdown
Contributor Author

Closing as this is superseeded by #714 (thanks Sam!)

@kaandocal kaandocal closed this Nov 11, 2026
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.

2 participants