Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dims::Tuple in sum #909

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ end
end
end

@inline _mapreduce(f, op, D::Tuple{<:Any}, init, sz::Size{S}, a::StaticArray) where {S} =
_mapreduce(f, op, first(D), init, sz, a)
Comment on lines +161 to +162
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things like sum(abs2, SA[1 2; 3 4], dims = (1,2)) still won't work, that seems like a bigger project. It would be easy to add a method here to make them give a friendly "not yet supported" error, instead of an "internal" error. But opinions vary as to whether that's a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change needed is not large though, we can use Base.mapreducedim! to generated the code we want:

@generated function _mapfoldl(f, op, dims::Val{D}, init,
                               ::Size{S}, a::StaticArray) where {S,D}
    _init = init === _InitialValue ? :default : :init
    iter = CartesianIndices(S)
    exprs = fill!(similar(iter, Any, Base.reduced_indices(iter, D)), _init)
    rf(ex, nex) = ex === :default ? :(Base.reduce_first(op, $nex)) : :(op($ex,$nex))
    Base.mapreducedim!(I->:(f(a[$(I.I...)])), rf, exprs, iter)
    return quote
        @_inline_meta
        @inbounds elements = tuple($(exprs...))
        @inbounds return similar_type(a, eltype(elements), Size($(size(exprs))))(elements)
    end
end

The current test passed locally, and the influence on compile time seems negligible.


@generated function _mapfoldl(f, op, dims::Val{D}, init,
::Size{S}, a::StaticArray) where {S,D}
N = length(S)
Expand Down Expand Up @@ -209,6 +212,14 @@ reduce(::typeof(hcat), A::StaticArray{<:Tuple,<:StaticVecOrMatLike}) =
@inline _reduce(op, a::StaticArray, dims, init = _InitialValue()) =
_mapreduce(identity, op, dims, init, Size(a), a)

@inline function _reduce(op, a::StaticArray, dims::Tuple, init = _InitialValue())
b = _reduce(op, a, first(dims))
return _reduce(op, b, Base.tail(dims), init)
end
_reduce(op, a::StaticArray, dims::Tuple{}, ::_InitialValue) = a
_reduce(op, a::StaticArray, dims::Tuple{}, init) = op.(init, a)


################
## (map)foldl ##
################
Expand Down
12 changes: 12 additions & 0 deletions test/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,24 @@ using Statistics: mean
v2 = [4,3,2,1]; sv2 = SVector{4}(v2)
@test reduce(+, sv1) === reduce(+, v1)
@test reduce(+, sv1; init=0) === reduce(+, v1; init=0)
@test reduce(+, sv1; init=99) === reduce(+, v1; init=99)
@test reduce(max, sa; dims=Val(1), init=-1.) === SMatrix{1,J}(reduce(max, a, dims=1, init=-1.))
@test reduce(max, sa; dims=1, init=-1.) === SMatrix{1,J}(reduce(max, a, dims=1, init=-1.))
@test reduce(max, sa; dims=2, init=-1.) === SMatrix{I,1}(reduce(max, a, dims=2, init=-1.))
@test reduce(*, sa; dims=(1,2), init=2.0) ≈ SMatrix{1,1}(reduce(*, a, dims=(1,2), init=2.0))
@test reduce(*, sa; dims=(), init=(1.0+im)) === SMatrix{I,J}(reduce(*, a, dims=(), init=(1.0+im)))
@test mapreduce(-, +, sv1) === mapreduce(-, +, v1)
@test mapreduce(-, +, sv1; init=0) === mapreduce(-, +, v1, init=0)
@test mapreduce(*, +, sv1, sv2) === 40
@test mapreduce(*, +, sv1, sv2; init=0) === 40
@test mapreduce(x->x^2, max, sa; dims=Val(1), init=-1.) == SMatrix{1,J}(mapreduce(x->x^2, max, a, dims=1, init=-1.))
@test mapreduce(x->x^2, max, sa; dims=1, init=-1.) == SMatrix{1,J}(mapreduce(x->x^2, max, a, dims=1, init=-1.))
@test mapreduce(x->x^2, max, sa; dims=2, init=-1.) == SMatrix{I,1}(mapreduce(x->x^2, max, a, dims=2, init=-1.))

# Zero-dim array
a0 = fill(rand()); sa0 = SArray{Tuple{}}(a0)
@test reduce(+, sa0) === reduce(+, a0)
@test reduce(/, sa0, dims=(), init=1.2) === SArray{Tuple{}}(reduce(/, a0, dims=(), init=1.2))
end

@testset "[map]foldl" begin
Expand Down Expand Up @@ -102,6 +110,7 @@ using Statistics: mean
RSArray1 = SArray{Tuple{1,J,K}} # reduced in dimension 1
RSArray2 = SArray{Tuple{I,1,K}} # reduced in dimension 2
RSArray3 = SArray{Tuple{I,J,1}} # reduced in dimension 3
RSArray13 = SArray{Tuple{1,J,1}} # reduced in dimension 1 and 3
a = randn(I,J,K); sa = OSArray(a)
b = rand(Bool,I,J,K); sb = OSArray(b)
z = zeros(I,J,K); sz = OSArray(z)
Expand All @@ -111,8 +120,11 @@ using Statistics: mean
@test sum(sa) === sum(a)
@test sum(abs2, sa) === sum(abs2, a)
@test sum(sa, dims=2) === RSArray2(sum(a, dims=2))
@test sum(sa, dims=(2,)) === RSArray2(sum(a, dims=2))
@test sum(sa, dims=Val(2)) === RSArray2(sum(a, dims=2))
@test sum(sa, dims=(1,3)) === RSArray13(sum(a, dims=(1,3)))
@test sum(abs2, sa; dims=2) === RSArray2(sum(abs2, a, dims=2))
@test sum(abs2, sa; dims=(2,)) === RSArray2(sum(abs2, a, dims=2))
@test sum(abs2, sa; dims=Val(2)) === RSArray2(sum(abs2, a, dims=2))

@test prod(sa) === prod(a)
Expand Down