Skip to content

Commit 1a265e4

Browse files
committed
Decouple sum/prod promotion from reduce
1 parent 8cba5cd commit 1a265e4

File tree

4 files changed

+54
-44
lines changed

4 files changed

+54
-44
lines changed

base/reduce.jl

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,22 @@ end
1515
const CommonReduceResult = Union{UInt64,UInt128,Int64,Int128,Float16,Float32,Float64}
1616
const WidenReduceResult = Union{SmallSigned, SmallUnsigned}
1717

18-
promote_sys_size{T}(::Type{T}) = T
19-
promote_sys_size{T<:SmallSigned}(::Type{T}) = Int
20-
promote_sys_size{T<:SmallUnsigned}(::Type{T}) = UInt
21-
# r_promote_type: promote T to the type of reduce(op, ::Array{T})
22-
# (some "extra" methods are required here to avoid ambiguity warnings)
23-
r_promote_type(op, ::Type{T}) where {T} = T
24-
r_promote_type(op, ::Type{T}) where {T<:WidenReduceResult} = promote_sys_size(T)
25-
r_promote_type(::typeof(+), ::Type{T}) where {T<:WidenReduceResult} = promote_sys_size(T)
26-
r_promote_type(::typeof(*), ::Type{T}) where {T<:WidenReduceResult} = promote_sys_size(T)
27-
r_promote_type(::typeof(+), ::Type{T}) where {T<:Number} = typeof(zero(T)+zero(T))
28-
r_promote_type(::typeof(*), ::Type{T}) where {T<:Number} = typeof(one(T)*one(T))
29-
r_promote_type(::typeof(scalarmax), ::Type{T}) where {T<:WidenReduceResult} = T
30-
r_promote_type(::typeof(scalarmin), ::Type{T}) where {T<:WidenReduceResult} = T
31-
r_promote_type(::typeof(max), ::Type{T}) where {T<:WidenReduceResult} = T
32-
r_promote_type(::typeof(min), ::Type{T}) where {T<:WidenReduceResult} = T
33-
34-
# r_promote: promote x to the type of reduce(op, [x])
35-
r_promote(op, x::T) where {T} = convert(r_promote_type(op, T), x)
18+
# Certain reductions like sum and prod may wish to promote the items being
19+
# reduced over to an appropriate size.
20+
promote_sys_size(x) = x
21+
promote_sys_size(x::SmallSigned) = Int(x)
22+
promote_sys_size(x::SmallUnsigned) = UInt(x)
3623

3724
## foldl && mapfoldl
3825

3926
@noinline function mapfoldl_impl(f, op, v0, itr, i)
4027
# Unroll the while loop once; if v0 is known, the call to op may
4128
# be evaluated at compile time
4229
if done(itr, i)
43-
return r_promote(op, v0)
30+
return v0
4431
else
4532
(x, i) = next(itr, i)
46-
v = op(r_promote(op, v0), f(x))
33+
v = op(v0, f(x))
4734
while !done(itr, i)
4835
@inbounds (x, i) = next(itr, i)
4936
v = op(v, f(x))
@@ -108,10 +95,10 @@ function mapfoldr_impl(f, op, v0, itr, i::Integer)
10895
# Unroll the while loop once; if v0 is known, the call to op may
10996
# be evaluated at compile time
11097
if isempty(itr) || i == 0
111-
return r_promote(op, v0)
98+
return v0
11299
else
113100
x = itr[i]
114-
v = op(f(x), r_promote(op, v0))
101+
v = op(f(x), v0)
115102
while i > 1
116103
x = itr[i -= 1]
117104
v = op(f(x), v)
@@ -180,12 +167,12 @@ foldr(op, itr) = mapfoldr(identity, op, itr)
180167
@noinline function mapreduce_impl(f, op, A::AbstractArray, ifirst::Integer, ilast::Integer, blksize::Int)
181168
if ifirst == ilast
182169
@inbounds a1 = A[ifirst]
183-
return r_promote(op, f(a1))
170+
return f(a1)
184171
elseif ifirst + blksize > ilast
185172
# sequential portion
186173
@inbounds a1 = A[ifirst]
187174
@inbounds a2 = A[ifirst+1]
188-
v = op(r_promote(op, f(a1)), r_promote(op, f(a2)))
175+
v = op(f(a1), f(a2))
189176
@simd for i = ifirst + 2 : ilast
190177
@inbounds ai = A[i]
191178
v = op(v, f(ai))
@@ -246,10 +233,12 @@ pairwise_blocksize(::typeof(abs2), ::typeof(+)) = 4096
246233
_empty_reduce_error() = throw(ArgumentError("reducing over an empty collection is not allowed"))
247234
mr_empty(f, op, T) = _empty_reduce_error()
248235
# use zero(T)::T to improve type information when zero(T) is not defined
249-
mr_empty(::typeof(identity), op::typeof(+), T) = r_promote(op, zero(T)::T)
250-
mr_empty(::typeof(abs), op::typeof(+), T) = r_promote(op, abs(zero(T)::T))
251-
mr_empty(::typeof(abs2), op::typeof(+), T) = r_promote(op, abs2(zero(T)::T))
252-
mr_empty(::typeof(identity), op::typeof(*), T) = r_promote(op, one(T)::T)
236+
mr_empty(::typeof(identity), op::typeof(+), T) = zero(T)::T
237+
mr_empty(::typeof(abs), op::typeof(+), T) = abs(zero(T)::T)
238+
mr_empty(::typeof(abs2), op::typeof(+), T) = abs2(zero(T)::T)
239+
mr_empty(::typeof(identity), op::typeof(*), T) = one(T)::T
240+
mr_empty(::typeof(promote_sys_size), op, T) =
241+
promote_sys_size(mr_empty(identity, op, T))
253242
mr_empty(::typeof(abs), op::typeof(scalarmax), T) = abs(zero(T)::T)
254243
mr_empty(::typeof(abs2), op::typeof(scalarmax), T) = abs2(zero(T)::T)
255244
mr_empty(::typeof(abs), op::typeof(max), T) = mr_empty(abs, scalarmax, T)
@@ -271,12 +260,12 @@ function _mapreduce(f, op, ::IndexLinear, A::AbstractArray{T}) where T
271260
return mr_empty(f, op, T)
272261
elseif n == 1
273262
@inbounds a1 = A[inds[1]]
274-
return r_promote(op, f(a1))
263+
return f(a1)
275264
elseif n < 16 # process short array here, avoid mapreduce_impl() compilation
276265
@inbounds i = inds[1]
277266
@inbounds a1 = A[i]
278267
@inbounds a2 = A[i+=1]
279-
s = op(r_promote(op, f(a1)), r_promote(op, f(a2)))
268+
s = op(f(a1), f(a2))
280269
while i < last(inds)
281270
@inbounds Ai = A[i+=1]
282271
s = op(s, f(Ai))
@@ -353,7 +342,7 @@ julia> sum(abs2, [2; 3; 4])
353342
29
354343
```
355344
"""
356-
sum(f::Callable, a) = mapreduce(f, +, a)
345+
sum(f::Callable, a) = mapreduce(promote_sys_size f, +, a)
357346

358347
"""
359348
sum(itr)
@@ -365,7 +354,7 @@ julia> sum(1:20)
365354
210
366355
```
367356
"""
368-
sum(a) = mapreduce(identity, +, a)
357+
sum(a) = mapreduce(promote_sys_size, +, a)
369358
sum(a::AbstractArray{Bool}) = countnz(a)
370359

371360

@@ -380,7 +369,7 @@ summation algorithm for additional accuracy.
380369
"""
381370
function sum_kbn(A)
382371
T = _default_eltype(typeof(A))
383-
c = r_promote(+, zero(T)::T)
372+
c = promote_sys_size(zero(T)::T)
384373
i = start(A)
385374
if done(A, i)
386375
return c
@@ -411,7 +400,7 @@ julia> prod(abs2, [2; 3; 4])
411400
576
412401
```
413402
"""
414-
prod(f::Callable, a) = mapreduce(f, *, a)
403+
prod(f::Callable, a) = mapreduce(promote_sys_size f, *, a)
415404

416405
"""
417406
prod(itr)
@@ -423,7 +412,7 @@ julia> prod(1:20)
423412
2432902008176640000
424413
```
425414
"""
426-
prod(a) = mapreduce(identity, *, a)
415+
prod(a) = mapreduce(promote_sys_size, *, a)
427416

428417
## maximum & minimum
429418

base/reducedim.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,22 @@ any!(r, A)
574574
for (fname, op) in [(:sum, :+), (:prod, :*),
575575
(:maximum, :scalarmax), (:minimum, :scalarmin),
576576
(:all, :&), (:any, :|)]
577+
function compose_pss(x)
578+
if fname in [:sum, :prod]
579+
:(promote_sys_size $x)
580+
else
581+
x
582+
end
583+
end
577584
fname! = Symbol(fname, '!')
578585
@eval begin
586+
# TODO: check this
579587
$(fname!)(f::Function, r::AbstractArray, A::AbstractArray; init::Bool=true) =
580588
mapreducedim!(f, $(op), initarray!(r, $(op), init), A)
581589
$(fname!)(r::AbstractArray, A::AbstractArray; init::Bool=true) = $(fname!)(identity, r, A; init=init)
582590

583591
$(fname)(f::Function, A::AbstractArray, region) =
584-
mapreducedim(f, $(op), A, region)
592+
mapreducedim($(compose_pss(:f)), $(op), A, region)
585593
$(fname)(A::AbstractArray, region) = $(fname)(identity, A, region)
586594
end
587595
end

base/tuple.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,13 @@ reverse(t::Tuple) = revargs(t...)
294294

295295
# TODO: these definitions cannot yet be combined, since +(x...)
296296
# where x might be any tuple matches too many methods.
297+
# TODO: this is inconsistent with the regular sum in cases where the arguments
298+
# require size promotion to system size.
297299
sum(x::Tuple{Any, Vararg{Any}}) = +(x...)
298300

299301
# NOTE: should remove, but often used on array sizes
302+
# TODO: this is inconsistent with the regular prod in cases where the arguments
303+
# require size promotion to system size.
300304
prod(x::Tuple{}) = 1
301305
prod(x::Tuple{Any, Vararg{Any}}) = *(x...)
302306

test/reduce.jl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# fold(l|r) & mapfold(l|r)
44
@test foldl(+, Int64[]) === Int64(0) # In reference to issues #7465/#20144 (PR #20160)
5-
@test foldl(+, Int16[]) === Int(0)
5+
@test foldl(+, Int16[]) === Int16(0) # In reference to issues #21536
66
@test foldl(-, 1:5) == -13
77
@test foldl(-, 10, 1:5) == -5
88

@@ -19,7 +19,7 @@
1919
@test Base.mapfoldl((x)-> x true, |, false, [true false true false false]) == true
2020

2121
@test foldr(+, Int64[]) === Int64(0) # In reference to issue #20144 (PR #20160)
22-
@test foldr(+, Int16[]) === Int(0)
22+
@test foldr(+, Int16[]) === Int16(0) # In reference to issues #21536
2323
@test foldr(-, 1:5) == 3
2424
@test foldr(-, 10, 1:5) == -7
2525
@test foldr(+, [1]) == 1 # Issue #21493
@@ -29,7 +29,7 @@
2929

3030
# reduce
3131
@test reduce(+, Int64[]) === Int64(0) # In reference to issue #20144 (PR #20160)
32-
@test reduce(+, Int16[]) === Int(0)
32+
@test reduce(+, Int16[]) === Int16(0) # In reference to issues #21536
3333
@test reduce((x,y)->"($x+$y)", 9:11) == "((9+10)+11)"
3434
@test reduce(max, [8 6 7 5 3 0 9]) == 9
3535
@test reduce(+, 1000, 1:5) == (1000 + 1 + 2 + 3 + 4 + 5)
@@ -69,12 +69,21 @@
6969
typeof(mapreduce(abs, +, Float32[10, 11, 12, 13]))
7070

7171
# sum
72+
@testset "sums promote to at least machine size" begin
73+
@testset for T in [Int8, Int16, Int32]
74+
@test sum(T[]) === Int(0)
75+
end
76+
@testset for T in [UInt8, UInt16, UInt32]
77+
@test sum(T[]) === UInt(0)
78+
end
79+
@testset for T in [Int, Int64, Int128, UInt, UInt64, UInt128,
80+
Float16, Float32, Float64]
81+
@test sum(T[]) === T(0)
82+
end
83+
@test sum(BigInt[]) == big(0) && sum(BigInt[]) isa BigInt
84+
end
7285

73-
@test sum(Int8[]) === Int(0)
74-
@test sum(Int[]) === Int(0)
75-
@test sum(Float64[]) === 0.0
76-
77-
@test sum(Int8(3)) === Int8(3)
86+
@test sum(Int8(3)) === Int(3)
7887
@test sum(3) === 3
7988
@test sum(3.0) === 3.0
8089

0 commit comments

Comments
 (0)