Heterogeneous random seeding

Hello all,

Is there a unified way to set the random seed across heterogeneous devices, such as CPU and GPU?
I’d be happy if GPUArrays.jl or KernelAbstractions.jl provides such a method.

julia> using CUDA

julia> using Random

julia> A = CUDA.zeros(10);

julia> Random.seed!(0);

julia> rand!(A);

julia> B = CUDA.zeros(10);

julia> Random.seed!(0);

julia> rand!(B);

julia> A == B
false

julia> C = CUDA.zeros(10);

julia> CURAND.seed!(0);

julia> rand!(C);

julia> D = CUDA.zeros(10);

julia> CURAND.seed!(0);

julia> rand!(D);

julia> C == D
true

Use an explicit RNG:

julia> rng = CUDA.RNG();

julia> A = CUDA.zeros(10);

julia> Random.seed!(rng, 0);

julia> rand!(rng, A);

julia> B = CUDA.zeros(10);

julia> Random.seed!(rng, 0);

julia> rand!(rng, B);

julia> A == B
true

Only disadvantage is that you have to pick between the CUDA.jl native RNG (CUDA.RNG()) and the CURAND one(CURAND.RNG()). The latter is faster, but only supports types that are known to the CURAND C library.

1 Like