-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Detangle Slice and fix mixed-dimension in-place reductions #28941
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1e0d847
Detangle Slice between whole dimensions and axes
mbauman 6d0e354
Reduce allocations for in-place reductions and fix mixed-dimensionali…
mbauman 7702adc
Introduce a whole new IdentityUnitRange
mbauman d154e83
Test that maximum! allocates less
mbauman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
# for reductions that expand 0 dims to 1 | ||
reduced_index(i::OneTo) = OneTo(1) | ||
reduced_index(i::Slice) = first(i):first(i) | ||
reduced_index(i::Union{Slice, IdentityUnitRange}) = first(i):first(i) | ||
reduced_index(i::AbstractUnitRange) = | ||
throw(ArgumentError( | ||
""" | ||
|
@@ -218,17 +218,20 @@ Extract first entry of slices of array A into existing array R. | |
""" | ||
copyfirst!(R::AbstractArray, A::AbstractArray) = mapfirst!(identity, R, A) | ||
|
||
function mapfirst!(f, R::AbstractArray, A::AbstractArray) | ||
function mapfirst!(f, R::AbstractArray, A::AbstractArray{<:Any,N}) where {N} | ||
lsiz = check_reducedims(R, A) | ||
iA = axes(A) | ||
iR = axes(R) | ||
t = [] | ||
for i in 1:length(iR) | ||
iAi = iA[i] | ||
push!(t, iAi == iR[i] ? iAi : first(iAi)) | ||
end | ||
t = _firstreducedslice(axes(R), axes(A)) | ||
map!(f, R, view(A, t...)) | ||
end | ||
# We know that the axes of R and A are compatible, but R might have a different number of | ||
# dimensions than A, which is trickier than it seems due to offset arrays and type stability | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to have this inferrable/nonallocating. |
||
_firstreducedslice(::Tuple{}, a::Tuple{}) = () | ||
_firstreducedslice(::Tuple, ::Tuple{}) = () | ||
@inline _firstreducedslice(::Tuple{}, a::Tuple) = (_firstslice(a[1]), _firstreducedslice((), tail(a))...) | ||
@inline _firstreducedslice(r::Tuple, a::Tuple) = (length(r[1])==1 ? _firstslice(a[1]) : r[1], _firstreducedslice(tail(r), tail(a))...) | ||
_firstslice(i::OneTo) = OneTo(1) | ||
_firstslice(i::Slice) = Slice(_firstslice(i.indices)) | ||
_firstslice(i) = i[firstindex(i):firstindex(i)] | ||
|
||
function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArray) | ||
lsiz = check_reducedims(R,A) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ import .Base: | |
sort!, | ||
issorted, | ||
sortperm, | ||
Slice, | ||
to_indices | ||
|
||
export # also exported by Base | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's up with the type signature here? Forced specialization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had been having trouble getting
ntuple
to specialize with just constant propagation, so I switched toVal(N)
instead… but you're probably right thatVal(ndims(A))
would do just as well.