-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Faster, indices-aware circshift (and non-allocating circshift!) #17861
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
Merged
+86
−14
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -490,6 +490,7 @@ export | |
checkbounds, | ||
checkindex, | ||
circshift, | ||
circshift!, | ||
clamp!, | ||
colon, | ||
conj!, | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -609,6 +609,57 @@ function copy!{T,N}(dest::AbstractArray{T,N}, src::AbstractArray{T,N}) | |
dest | ||
end | ||
|
||
function copy!(dest::AbstractArray, Rdest::CartesianRange, src::AbstractArray, Rsrc::CartesianRange) | ||
isempty(Rdest) && return dest | ||
size(Rdest) == size(Rsrc) || throw(ArgumentError("source and destination must have same size (got $(size(Rsrc)) and $(size(Rdest)))")) | ||
@boundscheck checkbounds(dest, Rdest.start) | ||
@boundscheck checkbounds(dest, Rdest.stop) | ||
@boundscheck checkbounds(src, Rsrc.start) | ||
@boundscheck checkbounds(src, Rsrc.stop) | ||
deltaI = Rdest.start - Rsrc.start | ||
for I in Rsrc | ||
@inbounds dest[I+deltaI] = src[I] | ||
end | ||
dest | ||
end | ||
|
||
# circshift! | ||
circshift!(dest::AbstractArray, src, ::Tuple{}) = copy!(dest, src) | ||
""" | ||
circshift!(dest, src, shifts) | ||
|
||
Circularly shift the data in `src`, storing the result in | ||
`dest`. `shifts` specifies the amount to shift in each dimension. | ||
|
||
The `dest` array must be distinct from the `src` array (they cannot | ||
alias each other). | ||
|
||
See also `circshift`. | ||
""" | ||
@noinline function circshift!{T,N}(dest::AbstractArray{T,N}, src, shiftamt::DimsInteger) | ||
dest === src && throw(ArgumentError("dest and src must be separate arrays")) | ||
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. The docs should probably note that 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. Fixed. |
||
inds = indices(src) | ||
indices(dest) == inds || throw(ArgumentError("indices of src and dest must match (got $inds and $(indices(dest)))")) | ||
_circshift!(dest, (), src, (), inds, fill_to_length(shiftamt, 0, Val{N})) | ||
end | ||
circshift!(dest::AbstractArray, src, shiftamt) = circshift!(dest, src, (shiftamt...,)) | ||
|
||
@inline function _circshift!(dest, rdest, src, rsrc, | ||
inds::Tuple{AbstractUnitRange,Vararg{Any}}, | ||
shiftamt::Tuple{Integer,Vararg{Any}}) | ||
ind1, d = inds[1], shiftamt[1] | ||
s = mod(d, length(ind1)) | ||
sf, sl = first(ind1)+s, last(ind1)-s | ||
r1, r2 = first(ind1):sf-1, sf:last(ind1) | ||
r3, r4 = first(ind1):sl, sl+1:last(ind1) | ||
tinds, tshiftamt = tail(inds), tail(shiftamt) | ||
_circshift!(dest, (rdest..., r1), src, (rsrc..., r4), tinds, tshiftamt) | ||
_circshift!(dest, (rdest..., r2), src, (rsrc..., r3), tinds, tshiftamt) | ||
end | ||
# At least one of inds, shiftamt is empty | ||
function _circshift!(dest, rdest, src, rsrc, inds, shiftamt) | ||
copy!(dest, CartesianRange(rdest), src, CartesianRange(rsrc)) | ||
end | ||
|
||
### BitArrays | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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.
The last time I tried this (in 1d), it was faster to do the out-of-place
circshift
directly. The in-place variant requires an extra pass over the array.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.
Oh, nevermind, I see that
circshift!
is not actually in-place, because you pass both the source and destination arrays.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.
map(Int, (shiftamt...,))
? There doesn't seem much point in usingInteger
here.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.
2 extra passes, actually, since the truly inplace version needs two passes and then there's the copy for the original array.
I was just keeping consistency with the previous version, but I'm happy to change it---perhaps as part of a final resolution to the inconsistencies noted in #17567?
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.
The function can still accept Integer, but there seems no point in passing Integer when casting to Int should be fine. So this seems independent of #17567