Skip to content

Commit c8d3ee7

Browse files
committed
Decouple sum/prod promotion from reduce
1 parent 6194487 commit c8d3ee7

File tree

6 files changed

+104
-72
lines changed

6 files changed

+104
-72
lines changed

base/process.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ setenv(cmd::Cmd; dir="") = Cmd(cmd; dir=dir)
230230
(&)(left::AbstractCmd, right::AbstractCmd) = AndCmds(left, right)
231231
redir_out(src::AbstractCmd, dest::AbstractCmd) = OrCmds(src, dest)
232232
redir_err(src::AbstractCmd, dest::AbstractCmd) = ErrOrCmds(src, dest)
233-
Base.mr_empty(f, op::typeof(&), T::Type{<:Base.AbstractCmd}) =
234-
throw(ArgumentError("reducing over an empty collection of type $T with operator & is not allowed"))
235233

236234
# Stream Redirects
237235
redir_out(dest::Redirectable, src::AbstractCmd) = CmdRedirect(src, dest, STDIN_NO)

base/reduce.jl

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,29 @@
55
###### Generic (map)reduce functions ######
66

77
if Int === Int32
8-
const SmallSigned = Union{Int8,Int16}
9-
const SmallUnsigned = Union{UInt8,UInt16}
8+
const SmallSigned = Union{Int8,Int16}
9+
const SmallUnsigned = Union{UInt8,UInt16}
1010
else
11-
const SmallSigned = Union{Int8,Int16,Int32}
12-
const SmallUnsigned = Union{UInt8,UInt16,UInt32}
11+
const SmallSigned = Union{Int8,Int16,Int32}
12+
const SmallUnsigned = Union{UInt8,UInt16,UInt32}
1313
end
1414

15-
const CommonReduceResult = Union{UInt64,UInt128,Int64,Int128,Float16,Float32,Float64}
16-
const WidenReduceResult = Union{SmallSigned, SmallUnsigned}
17-
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)
15+
# Certain reductions like sum and prod may wish to promote the items being
16+
# reduced over to an appropriate size.
17+
promote_sys_size(x) = x
18+
promote_sys_size(x::SmallSigned) = Int(x)
19+
promote_sys_size(x::SmallUnsigned) = UInt(x)
3620

3721
## foldl && mapfoldl
3822

3923
@noinline function mapfoldl_impl(f, op, v0, itr, i)
4024
# Unroll the while loop once; if v0 is known, the call to op may
4125
# be evaluated at compile time
4226
if done(itr, i)
43-
return r_promote(op, v0)
27+
return v0
4428
else
4529
(x, i) = next(itr, i)
46-
v = op(r_promote(op, v0), f(x))
30+
v = op(v0, f(x))
4731
while !done(itr, i)
4832
@inbounds (x, i) = next(itr, i)
4933
v = op(v, f(x))
@@ -108,10 +92,10 @@ function mapfoldr_impl(f, op, v0, itr, i::Integer)
10892
# Unroll the while loop once; if v0 is known, the call to op may
10993
# be evaluated at compile time
11094
if isempty(itr) || i == 0
111-
return r_promote(op, v0)
95+
return v0
11296
else
11397
x = itr[i]
114-
v = op(f(x), r_promote(op, v0))
98+
v = op(f(x), v0)
11599
while i > 1
116100
x = itr[i -= 1]
117101
v = op(f(x), v)
@@ -180,12 +164,12 @@ foldr(op, itr) = mapfoldr(identity, op, itr)
180164
@noinline function mapreduce_impl(f, op, A::AbstractArray, ifirst::Integer, ilast::Integer, blksize::Int)
181165
if ifirst == ilast
182166
@inbounds a1 = A[ifirst]
183-
return r_promote(op, f(a1))
167+
return f(a1)
184168
elseif ifirst + blksize > ilast
185169
# sequential portion
186170
@inbounds a1 = A[ifirst]
187171
@inbounds a2 = A[ifirst+1]
188-
v = op(r_promote(op, f(a1)), r_promote(op, f(a2)))
172+
v = op(f(a1), f(a2))
189173
@simd for i = ifirst + 2 : ilast
190174
@inbounds ai = A[i]
191175
v = op(v, f(ai))
@@ -245,17 +229,24 @@ pairwise_blocksize(::typeof(abs2), ::typeof(+)) = 4096
245229
# handling empty arrays
246230
_empty_reduce_error() = throw(ArgumentError("reducing over an empty collection is not allowed"))
247231
mr_empty(f, op, T) = _empty_reduce_error()
248-
# 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)
253-
mr_empty(::typeof(abs), op::typeof(scalarmax), T) = abs(zero(T)::T)
254-
mr_empty(::typeof(abs2), op::typeof(scalarmax), T) = abs2(zero(T)::T)
232+
mr_empty(::typeof(identity), op::typeof(+), T) = zero(T)
233+
mr_empty(::typeof(abs), op::typeof(+), T) = abs(zero(T))
234+
mr_empty(::typeof(abs2), op::typeof(+), T) = abs2(zero(T))
235+
mr_empty(::typeof(identity), op::typeof(*), T) = one(T)
236+
mr_empty(::typeof(promote_sys_size), op, T) =
237+
promote_sys_size(mr_empty(identity, op, T))
238+
mr_empty(::typeof(abs), op::typeof(scalarmax), T) = abs(zero(T))
239+
mr_empty(::typeof(abs2), op::typeof(scalarmax), T) = abs2(zero(T))
255240
mr_empty(::typeof(abs), op::typeof(max), T) = mr_empty(abs, scalarmax, T)
256241
mr_empty(::typeof(abs2), op::typeof(max), T) = mr_empty(abs2, scalarmax, T)
257-
mr_empty(f, op::typeof(&), T) = true
258-
mr_empty(f, op::typeof(|), T) = false
242+
mr_empty(::typeof(identity), op::typeof(&), ::Type{Bool}) = true
243+
mr_empty(::typeof(identity), op::typeof(|), ::Type{Bool}) = false
244+
245+
# Allow mr_empty to “see through” promote_sys_size
246+
let ComposedFunction = typename(typeof(identity identity)).wrapper
247+
global mr_empty(f::ComposedFunction{typeof(promote_sys_size)}, op, T) =
248+
promote_sys_size(mr_empty(f.g, op, T))
249+
end
259250

260251
mr_empty_iter(f, op, itr, ::HasEltype) = mr_empty(f, op, eltype(itr))
261252
mr_empty_iter(f, op::typeof(&), itr, ::EltypeUnknown) = true
@@ -271,12 +262,12 @@ function _mapreduce(f, op, ::IndexLinear, A::AbstractArray{T}) where T
271262
return mr_empty(f, op, T)
272263
elseif n == 1
273264
@inbounds a1 = A[inds[1]]
274-
return r_promote(op, f(a1))
265+
return f(a1)
275266
elseif n < 16 # process short array here, avoid mapreduce_impl() compilation
276267
@inbounds i = inds[1]
277268
@inbounds a1 = A[i]
278269
@inbounds a2 = A[i+=1]
279-
s = op(r_promote(op, f(a1)), r_promote(op, f(a2)))
270+
s = op(f(a1), f(a2))
280271
while i < last(inds)
281272
@inbounds Ai = A[i+=1]
282273
s = op(s, f(Ai))
@@ -352,7 +343,7 @@ julia> sum(abs2, [2; 3; 4])
352343
29
353344
```
354345
"""
355-
sum(f::Callable, a) = mapreduce(f, +, a)
346+
sum(f::Callable, a) = mapreduce(promote_sys_size f, +, a)
356347

357348
"""
358349
sum(itr)
@@ -364,7 +355,7 @@ julia> sum(1:20)
364355
210
365356
```
366357
"""
367-
sum(a) = mapreduce(identity, +, a)
358+
sum(a) = mapreduce(promote_sys_size, +, a)
368359
sum(a::AbstractArray{Bool}) = count(a)
369360

370361

@@ -379,7 +370,7 @@ summation algorithm for additional accuracy.
379370
"""
380371
function sum_kbn(A)
381372
T = _default_eltype(typeof(A))
382-
c = r_promote(+, zero(T)::T)
373+
c = promote_sys_size(zero(T)::T)
383374
i = start(A)
384375
if done(A, i)
385376
return c
@@ -410,7 +401,7 @@ julia> prod(abs2, [2; 3; 4])
410401
576
411402
```
412403
"""
413-
prod(f::Callable, a) = mapreduce(f, *, a)
404+
prod(f::Callable, a) = mapreduce(promote_sys_size f, *, a)
414405

415406
"""
416407
prod(itr)
@@ -422,7 +413,7 @@ julia> prod(1:20)
422413
2432902008176640000
423414
```
424415
"""
425-
prod(a) = mapreduce(identity, *, a)
416+
prod(a) = mapreduce(promote_sys_size, *, a)
426417

427418
## maximum & minimum
428419

base/reducedim.jl

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,22 @@ reducedim_init(f, op::typeof(|), A::AbstractArray, region) = reducedim_initarray
137137

138138
# specialize to make initialization more efficient for common cases
139139

140-
for (IT, RT) in ((CommonReduceResult, :(eltype(A))), (SmallSigned, :Int), (SmallUnsigned, :UInt))
141-
T = Union{[AbstractArray{t} for t in uniontypes(IT)]..., [AbstractArray{Complex{t}} for t in uniontypes(IT)]...}
142-
@eval begin
143-
reducedim_init(f::typeof(identity), op::typeof(+), A::$T, region) =
144-
reducedim_initarray(A, region, zero($RT))
145-
reducedim_init(f::typeof(identity), op::typeof(*), A::$T, region) =
146-
reducedim_initarray(A, region, one($RT))
147-
reducedim_init(f::Union{typeof(abs),typeof(abs2)}, op::typeof(+), A::$T, region) =
148-
reducedim_initarray(A, region, real(zero($RT)))
149-
reducedim_init(f::Union{typeof(abs),typeof(abs2)}, op::typeof(*), A::$T, region) =
150-
reducedim_initarray(A, region, real(one($RT)))
151-
end
140+
let
141+
BitIntFloat = Union{BitInteger, Math.IEEEFloat}
142+
T = Union{
143+
[AbstractArray{t} for t in uniontypes(BitIntFloat)]...,
144+
[AbstractArray{Complex{t}} for t in uniontypes(BitIntFloat)]...}
145+
146+
global reducedim_init(f::typeof(identity), op::typeof(+), A::T, region) =
147+
reducedim_initarray(A, region, zero(eltype(A)))
148+
global reducedim_init(f::typeof(identity), op::typeof(*), A::T, region) =
149+
reducedim_initarray(A, region, one(eltype(A)))
150+
global reducedim_init(f::Union{typeof(abs),typeof(abs2)}, op::typeof(+), A::T, region) =
151+
reducedim_initarray(A, region, real(zero(eltype(A))))
152+
global reducedim_init(f::Union{typeof(abs),typeof(abs2)}, op::typeof(*), A::T, region) =
153+
reducedim_initarray(A, region, real(one(eltype(A))))
152154
end
155+
153156
reducedim_init(f::Union{typeof(identity),typeof(abs),typeof(abs2)}, op::typeof(+), A::AbstractArray{Bool}, region) =
154157
reducedim_initarray(A, region, 0)
155158

@@ -610,14 +613,21 @@ any!(r, A)
610613
for (fname, op) in [(:sum, :+), (:prod, :*),
611614
(:maximum, :scalarmax), (:minimum, :scalarmin),
612615
(:all, :&), (:any, :|)]
616+
function compose_promote_sys_size(x)
617+
if fname in [:sum, :prod]
618+
:(promote_sys_size $x)
619+
else
620+
x
621+
end
622+
end
613623
fname! = Symbol(fname, '!')
614624
@eval begin
615625
$(fname!)(f::Function, r::AbstractArray, A::AbstractArray; init::Bool=true) =
616626
mapreducedim!(f, $(op), initarray!(r, $(op), init, A), A)
617627
$(fname!)(r::AbstractArray, A::AbstractArray; init::Bool=true) = $(fname!)(identity, r, A; init=init)
618628

619629
$(fname)(f::Function, A::AbstractArray, region) =
620-
mapreducedim(f, $(op), A, region)
630+
mapreducedim($(compose_promote_sys_size(:f)), $(op), A, region)
621631
$(fname)(A::AbstractArray, region) = $(fname)(identity, A, region)
622632
end
623633
end

base/tuple.jl

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

306306
# TODO: these definitions cannot yet be combined, since +(x...)
307307
# where x might be any tuple matches too many methods.
308+
# TODO: this is inconsistent with the regular sum in cases where the arguments
309+
# require size promotion to system size.
308310
sum(x::Tuple{Any, Vararg{Any}}) = +(x...)
309311

310312
# NOTE: should remove, but often used on array sizes
313+
# TODO: this is inconsistent with the regular prod in cases where the arguments
314+
# require size promotion to system size.
311315
prod(x::Tuple{}) = 1
312316
prod(x::Tuple{Any, Vararg{Any}}) = *(x...)
313317

test/reduce.jl

Lines changed: 31 additions & 10 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)
@@ -48,8 +48,8 @@
4848
@test mapreduce(abs2, +, Float64[]) === 0.0
4949
@test mapreduce(abs2, Base.scalarmax, Float64[]) === 0.0
5050
@test mapreduce(abs, max, Float64[]) === 0.0
51-
@test mapreduce(abs2, &, Float64[]) === true
52-
@test mapreduce(abs2, |, Float64[]) === false
51+
@test_throws ArgumentError mapreduce(abs2, &, Float64[])
52+
@test_throws ArgumentError mapreduce(abs2, |, Float64[])
5353

5454
# mapreduce() type stability
5555
@test typeof(mapreduce(*, +, Int8[10])) ===
@@ -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

@@ -135,6 +144,14 @@ end
135144
@test sum_kbn([-0.0]) === -0.0
136145
@test sum_kbn([-0.0,-0.0]) === -0.0
137146

147+
# check sum(abs, ...) for support of empty collections
148+
@testset "sum(abs, [])" begin
149+
@test @inferred(sum(abs, Float64[])) === 0.0
150+
@test @inferred(sum(abs, Int[])) === 0
151+
@test @inferred(sum(abs, Set{Int}())) === 0
152+
@test_throws MethodError sum(abs, Any[])
153+
end
154+
138155
# prod
139156

140157
@test prod(Int[]) === 1
@@ -380,3 +397,7 @@ test18695(r) = sum( t^2 for t in r )
380397

381398
# issue #21107
382399
@test foldr(-,2:2) == 2
400+
401+
# test neutral element not picked incorrectly for &, |
402+
@test @inferred(foldl(&, Int[1])) === 1
403+
@test_throws ArgumentError foldl(&, Int[])

test/reducedim.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,11 @@ for region in Any[-1, 0, (-1, 2), [0, 1], (1,-2,3), [0 1;
333333
@test_throws ArgumentError maximum(abs, Areduc, region)
334334
@test_throws ArgumentError minimum(abs, Areduc, region)
335335
end
336+
337+
# check type of result
338+
under_test = [UInt8, Int8, Int32, Int64, BigInt]
339+
@testset "type of sum(::Array{$T}" for T in under_test
340+
result = sum(T[1 2 3; 4 5 6; 7 8 9], 2)
341+
@test result == hcat([6, 15, 24])
342+
@test eltype(result) === typeof(promote_sys_size(zero(T)))
343+
end

0 commit comments

Comments
 (0)