jonniedie / ComponentArrays.jl

Arrays with arbitrarily nested named components.
MIT License
286 stars 34 forks source link

implement `Base.sum!` #251

Open vpuri3 opened 4 months ago

vpuri3 commented 4 months ago

On master

julia> x = (; x = zeros(4)) |> ComponentArray |> cu; y = ones(4,4) |> cu; sum!(x, y)
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 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/GMsgk/src/GPUArraysCore.jl:155
  [3] _assertscalar(op::String, behavior::GPUArraysCore.ScalarIndexing)                                                                                                                                                                                      
    @ GPUArraysCore ~/.julia/packages/GPUArraysCore/GMsgk/src/GPUArraysCore.jl:128
  [4] assertscalar(op::String)
    @ GPUArraysCore ~/.julia/packages/GPUArraysCore/GMsgk/src/GPUArraysCore.jl:116
  [5] getindex       
    @ ~/.julia/packages/GPUArrays/Hd5Sk/src/host/indexing.jl:48 [inlined]
  [6] scalar_getindex(A::CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Is::Int64)                                                                                                                                                                              

on this branch

julia> using CUDA, ComponentArrays; x = (; x = zeros(4)) |> ComponentArray |> cu; y = ones(4,4) |> cu; sum!(x, y)
Precompiling ComponentArrays
  2 dependencies successfully precompiled in 3 seconds. 99 already precompiled.
4×1 CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}:
 4.0
 4.0
 4.0
 4.0
jonniedie commented 4 months ago

Are we going to need this for all of the other ones as well?