Sorry for recovering this thread, but I am not sure how this approach works when a remake(...) call is to be avoided. As far as I understand, remake always makes a deepcopy of the prob, yet this is something that I would like to avoid as this will create a lot of memory overhead to the point of crashes due to being out of memory.
In essence, this is related to this thread, where I am using TaskLocalValues in order not to deepcopy an ODEProblem (or SDEProblem). But, I cannot find any documentation on how to change the callback in-place, as to avoid using remake.
In essence, the code looks like
tlv_prob = TaskLocalValue{SDEProblem}(() -> deepcopy(prob))
T = Tuple{Vector{Float64}, Vector{Float64}}
saved_values = [SavedValues(Float64, T) for _ in 1:nseeds]
#/ Define prob_func that mutates the (local) SDEProblem
function prob_func(prob, i, nrepeats)
#~ Get local problem that can be mutated safely
localprob = tlv_prob[]
localcallbackset = CallbackSet(savingcallback(saved_values[i]))
#@TODO Should `remake(...)` be avoided?
localprob = remake(localprob, callback=localcallbackset)
return localprob
end
where savingcallback(...) is a function that defines the SavingCallback, which stores some (rolling) mean and variance of a stochastic trajectory. (Note that I use CallbackSet here, as in the full code I have multiple callbacks).
This works fine, but I could’ve just as well avoided the localprob and just use prob instead, as remake initiates a deepcopy of the problem at hand, and I run out of memory sometimes.
I know that the callback of the problem is stored in prob.kwargs.data.callback, but I believe it is stored as an immutable struct and thus it cannot be easily changed. Is there a way, perhaps using replace or something similar, to change the callback in-place, such that it works in conjunction with some SavedValues-arrays? Or can I adjust the saved_values somehow to deal with this?
In other words, is there a setp/setu equivalent for callbacks?