Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit ddcbd70

Browse files
vtjnashJeffBezanson
authored andcommitted
Random,threads: allocate state at runtime for each thread (JuliaLang/julia#32407)
The `Random.GLOBAL_RNG` is now a singleton placeholder object which implements the prior `Random` public API for MersenneTwister as a shim to support existing clients until Julia v2.0.
1 parent b8635ad commit ddcbd70

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/Test.jl

+13-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export TestSetException
2727
import Distributed: myid
2828

2929
using Random
30-
using Random: AbstractRNG, GLOBAL_RNG
30+
using Random: AbstractRNG, get_local_rng
3131
using InteractiveUtils: gen_call_with_extracted_types
3232
using Core.Compiler: typesubtract
3333

@@ -1106,18 +1106,19 @@ function testset_beginend(args, tests, source)
11061106
# we reproduce the logic of guardseed, but this function
11071107
# cannot be used as it changes slightly the semantic of @testset,
11081108
# by wrapping the body in a function
1109-
oldrng = copy(GLOBAL_RNG)
1109+
local RNG = get_local_rng()
1110+
oldrng = copy(RNG)
11101111
try
1111-
# GLOBAL_RNG is re-seeded with its own seed to ease reproduce a failed test
1112-
Random.seed!(GLOBAL_RNG.seed)
1112+
# RNG is re-seeded with its own seed to ease reproduce a failed test
1113+
Random.seed!(RNG.seed)
11131114
$(esc(tests))
11141115
catch err
11151116
err isa InterruptException && rethrow()
11161117
# something in the test block threw an error. Count that as an
11171118
# error in this test set
11181119
record(ts, Error(:nontest_error, :(), err, Base.catch_stack(), $(QuoteNode(source))))
11191120
finally
1120-
copy!(GLOBAL_RNG, oldrng)
1121+
copy!(RNG, oldrng)
11211122
end
11221123
pop_testset()
11231124
finish(ts)
@@ -1176,7 +1177,7 @@ function testset_forloop(args, testloop, source)
11761177
pop_testset()
11771178
push!(arr, finish(ts))
11781179
# it's 1000 times faster to copy from tmprng rather than calling Random.seed!
1179-
copy!(GLOBAL_RNG, tmprng)
1180+
copy!(RNG, tmprng)
11801181

11811182
end
11821183
ts = $(testsettype)($desc; $options...)
@@ -1195,9 +1196,10 @@ function testset_forloop(args, testloop, source)
11951196
arr = Vector{Any}()
11961197
local first_iteration = true
11971198
local ts
1198-
local oldrng = copy(GLOBAL_RNG)
1199-
Random.seed!(GLOBAL_RNG.seed)
1200-
local tmprng = copy(GLOBAL_RNG)
1199+
local RNG = get_local_rng()
1200+
local oldrng = copy(RNG)
1201+
Random.seed!(RNG.seed)
1202+
local tmprng = copy(RNG)
12011203
try
12021204
$(Expr(:for, Expr(:block, [esc(v) for v in loopvars]...), blk))
12031205
finally
@@ -1206,7 +1208,7 @@ function testset_forloop(args, testloop, source)
12061208
pop_testset()
12071209
push!(arr, finish(ts))
12081210
end
1209-
copy!(GLOBAL_RNG, oldrng)
1211+
copy!(RNG, oldrng)
12101212
end
12111213
arr
12121214
end
@@ -1648,7 +1650,7 @@ Base.similar(A::GenericArray, s::Integer...) = GenericArray(similar(A.a, s...))
16481650

16491651
"`guardseed(f)` runs the function `f()` and then restores the
16501652
state of the global RNG as it was before."
1651-
function guardseed(f::Function, r::AbstractRNG=GLOBAL_RNG)
1653+
function guardseed(f::Function, r::AbstractRNG=get_local_rng())
16521654
old = copy(r)
16531655
try
16541656
f()

test/runtests.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ end
672672

673673
@testset "test guarded Random.seed!" begin
674674
seed = rand(UInt)
675-
orig = copy(Random.GLOBAL_RNG)
675+
orig = copy(Random.get_local_rng())
676676
@test guardseed(()->rand(), seed) == guardseed(()->rand(), seed)
677677
@test guardseed(()->rand(Int), seed) == guardseed(()->rand(Int), seed)
678678
r1, r2 = MersenneTwister(0), MersenneTwister(0)
@@ -686,7 +686,7 @@ end
686686
end::Tuple{Float64,Int}
687687
@test a == c == rand(r1) == rand(r2)
688688
@test b == d == rand(r1, Int) == rand(r2, Int)
689-
@test orig == Random.GLOBAL_RNG
689+
@test orig == Random.get_local_rng()
690690
@test rand(orig) == rand()
691691
end
692692

0 commit comments

Comments
 (0)