Skip to content

Can't use rand! or randn! with a collection of elements on a CuArray #2936

@kshyatt

Description

@kshyatt

MWE:

julia> using CUDA

julia> A = CUDA.zeros(128);

julia> CUDA.rand!(A, 1:64)
ERROR: Scalar indexing is disallowed.
Invocation of setindex! resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore should be avoided.

If you want to allow scalar iteration, use `allowscalar` or `@allowscalar`
to enable scalar iteration globally or for the operations in question.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] errorscalar(op::String)
   @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:151
 [3] _assertscalar(op::String, behavior::GPUArraysCore.ScalarIndexing)
   @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:124
 [4] assertscalar(op::String)
   @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:112
 [5] setindex!
   @ ~/.julia/dev/GPUArrays/src/host/indexing.jl:58 [inlined]
 [6] rand!(rng::Random.TaskLocalRNG, A::CuArray{Float32, 1, CUDA.DeviceMemory}, sp::Random.SamplerRangeNDL{UInt64, Int64})
   @ Random ~/.julia/juliaup/julia-1.11.7+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:273
 [7] rand!
   @ ~/.julia/juliaup/julia-1.11.7+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:268 [inlined]
 [8] rand!(A::CuArray{Float32, 1, CUDA.DeviceMemory}, X::UnitRange{Int64})
   @ Random ~/.julia/juliaup/julia-1.11.7+0.x64.linux.gnu/share/julia/stdlib/v1.11/Random/src/Random.jl:265
 [9] top-level scope
   @ REPL[2]:1

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions