Repository navigation
Add support for rand(rng, n) #578
Copy link
Copy link
Closed
Labels
arraysThe array abstraction: broadcasting, mapreduce, indexing, views, generic linear algebra.The array abstraction: broadcasting, mapreduce, indexing, views, generic linear algebra.
Description
Currently we have the following error (show for CUDA but the same happens with Metal).
julia> using GPUArrays, CUDA
julia> rng = GPUArrays.default_rng(CuArray);
julia> rand(rng, 5)
ERROR: MethodError: no method matching rng_native_52(::GPUArrays.RNG)
The function `rng_native_52` exists, but no method is defined for this combination of argument types.
Closest candidates are:
rng_native_52(::Random.MersenneTwister)
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/RNGs.jl:439
rng_native_52(::Random.RandomDevice)
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/RNGs.jl:36
rng_native_52(::Random.TaskLocalRNG)
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Xoshiro.jl:230
...
Stacktrace:
[1] rand(r::GPUArrays.RNG, ::Random.SamplerTrivial{Random.UInt52Raw{UInt64}, UInt64})
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/generation.jl:114
[2] rand(rng::GPUArrays.RNG, X::Random.UInt52Raw{UInt64})
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:255
[3] rand(r::GPUArrays.RNG, ::Random.SamplerTrivial{Random.UInt52{UInt64}, UInt64})
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/generation.jl:125
[4] rand(rng::GPUArrays.RNG, X::Random.UInt52{UInt64})
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:255
[5] rand(r::GPUArrays.RNG, ::Random.SamplerTrivial{Random.CloseOpen12{Float64}, Float64})
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/generation.jl:32
[6] rand(rng::GPUArrays.RNG, X::Random.CloseOpen12{Float64})
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:255
[7] rand(r::GPUArrays.RNG, ::Random.SamplerTrivial{Random.CloseOpen01{Float64}, Float64})
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/generation.jl:35
[8] rand!
@ ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:273 [inlined]
[9] rand!
@ ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:269 [inlined]
[10] rand
@ ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:290 [inlined]
[11] rand(r::GPUArrays.RNG, dims::Int64)
@ Random ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:278
[12] top-level scope
@ REPL[17]:1Is there anything blocking an implementation of rand(rng, n)? Notice that the following works fine instead:
julia> rng = CUDA.default_rng()
CUDA.RNG(0xe8e5e5ff, 0x00000029)
julia> rand(rng, 5)
5-element CuArray{Float32, 1, CUDA.DeviceMemory}:
0.45878232
0.55591
0.1085031
0.66130507
0.47421575Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
arraysThe array abstraction: broadcasting, mapreduce, indexing, views, generic linear algebra.The array abstraction: broadcasting, mapreduce, indexing, views, generic linear algebra.
Activity
maleadt commentedon Jan 6, 2025
Not really, just somebody improving the current implementation. A literal port of the CUDA.jl RNG is going to be tricky though, as it contains a lot of low-level hackery.
[-]missing support for rand(rng, n)[/-][+]Add support for rand(rng, n)[/+]maleadt commentedon Oct 19, 2026
Implemented by #707;
rand(GPUArrays.RNG{CuArray}(), 5)now returns aCuArray{Float32}. (GPUArrays.default_rng(AT)has since been replaced byGPUArrays.RNG{AT}().)