Skip to content

sum of zero-array fails #362

Open
@mcabbott

Description

@mcabbott
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions