Skip to content
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
7 changes: 6 additions & 1 deletion CUDACore/src/compiler/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,12 @@ end
# cache of kernel instances
const _kernel_instances = Dict{Any, Any}()

make_seed(::HostKernel) = Random.rand(UInt32)
# task-local RNG for kernel launch seeds, so that launching a kernel does not
# perturb the user-visible `rand()` stream
launch_rng() = get!(Random.Xoshiro, task_local_storage(),
:CUDACore_launch_rng)::Random.Xoshiro

make_seed(::HostKernel) = rand(launch_rng(), UInt32)


## device-side kernels
Expand Down
25 changes: 25 additions & 0 deletions test/core/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1222,3 +1222,28 @@ end
end

############################################################################################

@testset "launch seed does not perturb host RNG" begin

dummy_kernel() = return

Random.seed!(0xdeadbeef)
a_before = rand(UInt64)
@cuda threads=1 dummy_kernel()
a_after = rand(UInt64)

Random.seed!(0xdeadbeef)
b_before = rand(UInt64)
b_after = rand(UInt64)

@test a_before == b_before
@test a_after == b_after

k = @cuda launch=false dummy_kernel()
seed1 = CUDACore.make_seed(k)
seed2 = CUDACore.make_seed(k)
@test seed1 != seed2

end

############################################################################################