CUDA.jl icon indicating copy to clipboard operation
CUDA.jl copied to clipboard

Indexing a view uses scalar indexing

Open CarloLucibello opened this issue 2 years ago • 0 comments

Probably there is an issue already corresponding to this but I couldn't find it. We encountered this error in Fux https://github.com/FluxML/Flux.jl/issues/1935 but ultimately can be reduced to indexing a view of a CuArray:

julia> using CUDA

julia> CUDA.allowscalar(false)

julia> y = rand(5) |> cu
5-element CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}:
 0.15889172
 0.5090761
 0.45227328
 0.86806625
 0.11143296

julia> v = view(y,[5,4,3,2,1])
5-element view(::CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, [5, 4, 3, 2, 1]) with eltype Float32:
 0.11143296
 0.86806625
 0.45227328
 0.5090761
 0.15889172

julia> y[1:2]  # indexing a CuArray with a range (or abstract vector)  is ok
2-element CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}:
 0.15889172
 0.5090761

julia> v[1:2]  # indexing a view of a CuArray gives error
ERROR: Scalar indexing is disallowed.
Invocation of getindex 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 are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:33
  [2] assertscalar(op::String)
    @ GPUArrays ~/.julia/packages/GPUArrays/Zecv7/src/host/indexing.jl:53
  [3] getindex
    @ ~/.julia/packages/GPUArrays/Zecv7/src/host/indexing.jl:86 [inlined]
  [4] reindex
    @ ./subarray.jl:254 [inlined]
  [5] getindex
    @ ./subarray.jl:276 [inlined]
  [6] macro expansion
    @ ./multidimensional.jl:867 [inlined]
  [7] macro expansion
    @ ./cartesian.jl:64 [inlined]
  [8] _unsafe_getindex!
    @ ./multidimensional.jl:862 [inlined]
  [9] _unsafe_getindex(#unused#::IndexCartesian, A::SubArray{Float32, 1, CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Tuple{CuArray{Int64, 1, CUDA.Mem.DeviceBuffer}}, false}, I::UnitRange{Int64})
    @ Base ./multidimensional.jl:853
 [10] _getindex
    @ ./multidimensional.jl:839 [inlined]
 [11] getindex(A::SubArray{Float32, 1, CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Tuple{CuArray{Int64, 1, CUDA.Mem.DeviceBuffer}}, false}, I::UnitRange{Int64})
    @ Base ./abstractarray.jl:1218
 [12] top-level scope
    @ REPL[22]:1
 [13] top-level scope
    @ ~/.julia/packages/CUDA/5jdFl/src/initialization.jl:52

CarloLucibello avatar Apr 09 '22 06:04 CarloLucibello