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

WIP: a more principled take on dimensional reduction inits #55318

Open
wants to merge 18 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
8 changes: 7 additions & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ function _collect(::Type{T}, itr, isz::SizeUnknown) where T
end

# make a collection similar to `c` and appropriate for collecting `itr`
_similar_for(c, ::Type{T}, itr, isz, shp) where {T} = similar(c, T)
_similar_for(c, ::Type{T}, itr, isz, shp) where {T} = similar(c, T, shp)

_similar_shape(itr, ::SizeUnknown) = nothing
_similar_shape(itr, ::HasLength) = length(itr)::Integer
Expand Down Expand Up @@ -699,6 +699,12 @@ collect(A::AbstractArray) = _collect_indices(axes(A), A)

collect_similar(cont, itr) = _collect(cont, itr, IteratorEltype(itr), IteratorSize(itr))

struct _Allocator{T}
f::T
end
similar(f::_Allocator, ::Type{T}, dims) where {T} = f.f(T, dims)
collect_allocator(f::F, itr) where {F} = _collect(_Allocator(f), itr, IteratorEltype(itr), IteratorSize(itr))

_collect(cont, itr, ::HasEltype, isz::Union{HasLength,HasShape}) =
copyto!(_similar_for(cont, eltype(itr), itr, isz, _similar_shape(itr, isz)), itr)

Expand Down
3 changes: 3 additions & 0 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ Base.similar(::Broadcasted{ArrayConflict}, ::Type{ElType}, dims) where ElType =
similar(Array{ElType, length(dims)}, dims)
Base.similar(::Broadcasted{ArrayConflict}, ::Type{Bool}, dims) =
similar(BitArray, dims)
# As well as the default behavior
Base.similar(::Broadcasted, ::Type{ElType}, dims) where ElType =
similar(Array{ElType, length(dims)}, dims)

@inline Base.axes(bc::Broadcasted) = _axes(bc, bc.axes)
_axes(::Broadcasted, axes::Tuple) = axes
Expand Down
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,8 @@ end

@deprecate stat(fd::Integer) stat(RawFD(fd))

function reducedim_initarray end
const _dep_message_reducedim_initarray = ", these internals have been removed. To customize the array returned by dimensional reductions, implement mapreduce_similar instead"
deprecate(Base, :reducedim_initarray)

# END 1.12 deprecations
5 changes: 0 additions & 5 deletions base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,6 @@ minimum_fast(a; kw...) = Base.reduce(min_fast, a; kw...)
maximum_fast(f, a; kw...) = Base.mapreduce(f, max_fast, a; kw...)
minimum_fast(f, a; kw...) = Base.mapreduce(f, min_fast, a; kw...)

Base.reducedim_init(f, ::typeof(max_fast), A::AbstractArray, region) =
Base.reducedim_init(f, max, A::AbstractArray, region)
Base.reducedim_init(f, ::typeof(min_fast), A::AbstractArray, region) =
Base.reducedim_init(f, min, A::AbstractArray, region)

maximum!_fast(r::AbstractArray, A::AbstractArray; kw...) =
maximum!_fast(identity, r, A; kw...)
minimum!_fast(r::AbstractArray, A::AbstractArray; kw...) =
Expand Down
8 changes: 5 additions & 3 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ foldr(op, itr; kw...) = mapfoldr(identity, op, itr; kw...)
@noinline function mapreduce_impl(f, op, A::AbstractArrayOrBroadcasted,
ifirst::Integer, ilast::Integer, blksize::Int)
if ifirst == ilast
@inbounds a1 = A[ifirst]
return mapreduce_first(f, op, a1)
throw(AssertionError("mapreduce_impl must not be called with only one element"))
elseif ilast - ifirst < blksize
# sequential portion
@inbounds a1 = A[ifirst]
Expand Down Expand Up @@ -348,6 +347,7 @@ reduce_empty(::typeof(mul_prod), ::Type{T}) where {T<:BitUnsignedSmall} = one(UI

reduce_empty(op::BottomRF, ::Type{T}) where {T} = reduce_empty(op.rf, T)
reduce_empty(op::MappingRF, ::Type{T}) where {T} = mapreduce_empty(op.f, op.rf, T)
reduce_empty(op::MappingRF{<:Any,<:BottomRF}, ::Type{T}) where {T} = mapreduce_empty(op.f, op.rf.rf, T)
reduce_empty(op::FilteringRF, ::Type{T}) where {T} = reduce_empty(op.rf, T)
reduce_empty(op::FlipArgs, ::Type{T}) where {T} = reduce_empty(op.f, T)

Expand Down Expand Up @@ -1335,7 +1335,9 @@ end

## count

_bool(f) = x->f(x)::Bool
_assert_bool(x) = x::Bool
_bool(f) = _assert_bool ∘ f
mapreduce_empty(::ComposedFunction{typeof(_assert_bool), <:Any}, ::typeof(add_sum), itr) = false+false

"""
count([f=identity,] itr; init=0) -> Integer
Expand Down
Loading