-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unpredictable behavior of Random.seed!
on workers using Distributed
#51225
Comments
Note that on the remote process all RPCs are executed on separate tasks. You can observe the same behaviour locally:
This is expected behavior see https://docs.julialang.org/en/v1/stdlib/Random/
Since version 1.6, Julia uses a per-task random number generator and thus the seeding it is only influencing the task itself and it's children. If you want deterministic behavior you will need to use a non-default RNG. Do note that remote-process calls execute concurrently and thus you may need to protect that global RNG with a lock. We should improve the documentation on this. |
Got it! Thanks for the help! |
I am doing the following as a workaround for this. julia> using Distributed
julia> addprocs(1);
julia> @everywhere using Random
julia> @everywhere const rng_copy = copy(Random.default_rng());
julia> fetch(@spawnat 2 let
Random.seed!(1234)
copy!(getglobal(Main,:rng_copy), Random.default_rng())
nothing
end)
julia> fetch(@spawnat 2 let
copy!(Random.default_rng(), getglobal(Main,:rng_copy))
x = rand()
copy!(getglobal(Main,:rng_copy), Random.default_rng())
x
end)
0.32597672886359486 While this works on 1.9, I'm pretty sure this code may break in future releases because |
A related issue in Pluto: fonsp/Pluto.jl#2290 |
We do actually have tests that copy works like that, so it is unlikely it would change or break that example |
In 1.10 a similar example is broken. julia> using Distributed
julia> addprocs(1);
julia> @everywhere begin
using Random
myrand() = fetch(Threads.@spawn(rand()))
const rng_copy = copy(Random.default_rng())
end
julia> fetch(@spawnat 2 let
Random.seed!(1234)
copy!(getglobal(Main,:rng_copy), Random.default_rng())
nothing
end)
julia> fetch(@spawnat 2 let
copy!(Random.default_rng(), getglobal(Main,:rng_copy))
x = myrand()
copy!(getglobal(Main,:rng_copy), Random.default_rng())
x
end)
julia> fetch(@spawnat 2 let
copy!(Random.default_rng(), getglobal(Main,:rng_copy))
x = myrand()
copy!(getglobal(Main,:rng_copy), Random.default_rng())
x
end)
julia> Random.seed!(1234);
julia> myrand()
julia> myrand() In 1.9 both remote and local calls return the same random sequence. In 1.10.0-beta2 the remote call always returns the constant 0.47487231547644215, so in this case, using copy! seems to break the rng of any spawned tasks. |
Yeah in 1.10 spawning a task doesn't update anymore (compared to 1.9) the current task's local RNG state ( |
So that might be an issue with |
I'm having trouble setting the seed on worker tasks using
Distributed
. If I set the seed and then get a random number in one remote call, the result is correct. If I set the seed and then get the random number in two remote calls, the random number is different each time.MWE
Output:
The text was updated successfully, but these errors were encountered: