Julia 1.7 saving and restoring RNG state

Previously I checkpointed the RNG in a Julia session using multiple threads (40) with

using JLD2
using Random
jldopen(Filename,'w') do F
   F["RNG"] = Random.default_rng()`
end

and restored with

using JLD2
using Random
myRNG = jldopen(File,"r") do F
   F["RNG"]
end
copy!(Random.default_rng(), myRNG)

The new PRNG paradigm breaks that pattern and I can’t find a quick fix.

julia> copy!(Random.default_rng(), myRNG)
ERROR: MethodError: no method matching copy!(::TaskLocalRNG, ::TaskLocalRNG)
Closest candidates are:
  copy!(::TaskLocalRNG, ::Xoshiro) at [...]/usr/share/julia/stdlib/v1.7/Random/src/Xoshiro.jl:154
  copy!(::Xoshiro, ::TaskLocalRNG) at [...]/usr/share/julia/stdlib/v1.7/Random/src/Xoshiro.jl:160
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

Any ideas?

1 Like

I’m not sure if it’s easy to do (or possible?), because each thread now has its own seed (you likely would need to suspend them all to do this, for some worst-case scenario, is it possible?). If you know you do not threading then something is likely possible. Or even with threading you can always opt into using the old MersenneTwister RNG (and lose out on threaded RNG and its speedup) and then your old method should be workable (and the RNG is only global). You must be sure all your code is changed to use the old RNG, and not some place left out using the new one (you can use both, that’s not the problem, only for what you’re trying to do).

julia> using Random
help?> MersenneTwister

I could run have every thread store its PRNG and then load them back up, but that doesn’t resolve the issue with copy! (I’ve updated the OP to clarify).

After reading up on things, it is my understanding that each Task inherits a PRNG from its parent and subsequently modifies the parent. I’m saving and re-entering on the top task of my problem, so I think it is safe to do

using JLD2
using Random
myRNG = jldopen(File,"r") do F
   F["RNG"]
end
S = Xoshiro()
copy!(S, myRNG)
copy!(default_rng(), S)

This solution doesn’t throw any errors as of yet, but I’m also not sure whether it has the intended feature of “reproducibility”.