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

`sum` of zero-array fails

Open mcabbott opened this issue 4 years ago • 0 comments

julia> using CUDA, GPUArrays

julia> v = cu(rand(3));

julia> view(v,2)
0-dimensional CuArray{Float32, 0}:
0.8010128

julia> sum(view(v,2))
ERROR: MethodError: no method matching ndims(::Base.Broadcast.Broadcasted{CUDA.CuArrayStyle{0}, Nothing, typeof(identity), Tuple{CuArray{Float32, 0}}})

julia> reshape(v[2:2])
0-dimensional CuArray{Float32, 0}:
0.8010128

julia> sum(reshape(v[2:2]))
ERROR: MethodError: no method matching ndims(::Base.Broadcast.Broadcasted{CUDA.CuArrayStyle{0}, Nothing, typeof(identity), Tuple{CuArray{Float32, 0}}})

julia> reshape(v[2:2]) .+ v  # other broadcasting works
3-element CuArray{Float32, 1}:
 1.3723278
 1.6020256
 0.95104384

julia> Base.sum(x::GPUArrays.AbstractGPUArray{<:Any,0}) = GPUArrays.@allowscalar x[]

julia> sum(reshape(v[2:2]))
0.8010128f0

This is a trivial operation, but I wondered whether it might be useful as a package-independent way to write @allowscalar v[2]. I wrote the simplest possible fix above, but perhaps it ought to hook into mapreduce somewhere to be more general?

mcabbott avatar Jun 22 '21 15:06 mcabbott