Skip to content

fixes seed_threadrngs tests #20

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

Merged
merged 3 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Tools/threading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end
@assert isassigned(rngs, tid)
end
end
@assert all(1:nt) do tid
@assert all(eachindex(rngs)) do tid
isassigned(rngs, tid)
end
rngs
Expand Down
30 changes: 8 additions & 22 deletions test/Tools/threading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,20 @@
end

@testset "seed_threadrngs!" begin
rngs = [MersenneTwister() for _ = 1:Threads.nthreads()]
x = zeros(1000)
y = zeros(1000)
seed_threadrngs!(1)
Threads.@threads for i=1:length(x)
y[i] = rand()
seed_threadrngs!(rngs, 1)
Threads.@threads for i = 1:length(x)
x[i] = rand(rngs[Threads.threadid()])
end
seed_threadrngs!(1)
Threads.@threads for i=1:length(x)
x[i] = rand()
seed_threadrngs!(rngs, 1)
Threads.@threads for i = 1:length(x)
y[i] = rand(rngs[Threads.threadid()])
end
@test x == y

states = map(rng -> rng.state, Random.THREAD_RNGs)
states = map(rng -> rng.state, rngs)
@test length(unique(states)) == Threads.nthreads()

# Test that naievely calling Random.seed! in a single thread doesn't
# seed all threads
x = zeros(1000)
y = zeros(1000)
Random.seed!(1)
Threads.@threads for i=1:length(x)
y[i] = rand()
end
Random.seed!(1)
Threads.@threads for i=1:length(x)
x[i] = rand()
end
@test x != y
end

@testset "nblasthreads/with_blasthreads" begin
Expand Down