From 5a0fa7e308a5a8c9ccf1390eb8c0c9095c32cf0f Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Mon, 10 Jan 2022 17:39:33 +0800 Subject: [PATCH 1/2] Make `has_fast_linear_indexing` based on `IndexStyle` and `ndims` --- base/reducedim.jl | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/base/reducedim.jl b/base/reducedim.jl index 1d74fb0d498a5..4ab786804ff4c 100644 --- a/base/reducedim.jl +++ b/base/reducedim.jl @@ -196,11 +196,8 @@ end ## generic (map)reduction -has_fast_linear_indexing(a::AbstractArrayOrBroadcasted) = false -has_fast_linear_indexing(a::Array) = true -has_fast_linear_indexing(::Union{Number,Ref,AbstractChar}) = true # 0d objects, for Broadcasted -has_fast_linear_indexing(bc::Broadcast.Broadcasted) = - all(has_fast_linear_indexing, bc.args) +has_fast_linear_indexing(a::AbstractArrayOrBroadcasted) = IndexStyle(a) === IndexLinear() +has_fast_linear_indexing(a::AbstractVector) = true function check_reducedims(R, A) # Check whether R has compatible dimensions w.r.t. A for reduction From a23a31855f9d61fed59d4578489ea256f3944278 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Mon, 10 Jan 2022 17:46:33 +0800 Subject: [PATCH 2/2] add test --- test/broadcast.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/broadcast.jl b/test/broadcast.jl index e4309bf81419f..b2232258744ac 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -979,6 +979,10 @@ end @test sum(bc, dims=1, init=0) == [5] bc = Broadcast.instantiate(Broadcast.broadcasted(*, ['a','b'], 'c')) @test prod(bc, dims=1, init="") == ["acbc"] + + a = rand(-10:10,32,4); b = rand(-10:10,32,4) + bc = Broadcast.instantiate(Broadcast.broadcasted(+,a,b)) + @test sum(bc; dims = 1, init = 0.0) == sum(collect(bc); dims = 1, init = 0.0) end # treat Pair as scalar: