Skip to content

Add support for rand(rng, n) #578

Closed

Description

@CarloLucibello

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]:1

Is 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.47421575

Activity

  1. maleadt commented on Jan 6, 2025

    @maleadt
    Member

    Is there anything blocking an implementation of rand(rng, n)?

    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.

  2. changed the title [-]missing support for rand(rng, n)[/-] [+]Add support for rand(rng, n)[/+] on Jan 6, 2025
  3. added
    arraysThe array abstraction: broadcasting, mapreduce, indexing, views, generic linear algebra.
    on Oct 19, 2026
  4. maleadt commented on Oct 19, 2026

    @maleadt
    Member

    Implemented by #707; rand(GPUArrays.RNG{CuArray}(), 5) now returns a CuArray{Float32}. (GPUArrays.default_rng(AT) has since been replaced by GPUArrays.RNG{AT}().)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arraysThe array abstraction: broadcasting, mapreduce, indexing, views, generic linear algebra.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions