-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
faster circshift! for SparseMatrixCSC #30317
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1815a13
implement circshift! for SparseMatrixCSC
abraunst cf8cd90
factor helper function shifter!, implement efficient circshift! for S…
abraunst 9cc9583
remove useless reallocation and fix bug with different in/out types, …
abraunst 31d54c2
add some @inbounds for improved performance
abraunst 04485c5
remove allocations completely, giving a large improvement for small m…
abraunst 17cd9f9
some renaming to avoid polluting the module namespace
abraunst 18968ce
avoid action if iszero(r) and/or iszero(c), move sparse vector shifti…
abraunst 0ae61fc
Make shift amounts deterministic in tests, move sparse vector tests i…
abraunst 288d2ef
comment fix
abraunst 0c8d70e
for some reason, copy!(a::SparseVector, b::SparseVector) does not work
abraunst 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
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 |
---|---|---|
|
@@ -1975,3 +1975,42 @@ function fill!(A::Union{SparseVector, SparseMatrixCSC}, x) | |
end | ||
return A | ||
end | ||
|
||
|
||
|
||
# in-place swaps (dense) blocks start:split and split+1:fin in col | ||
function _swap!(col::AbstractVector, start::Integer, fin::Integer, split::Integer) | ||
split == fin && return | ||
reverse!(col, start, split) | ||
reverse!(col, split + 1, fin) | ||
reverse!(col, start, fin) | ||
return | ||
end | ||
|
||
|
||
# in-place shifts a sparse subvector by r. Used also by sparsematrix.jl | ||
function subvector_shifter!(R::AbstractVector, V::AbstractVector, start::Integer, fin::Integer, m::Integer, r::Integer) | ||
split = fin | ||
@inbounds for j = start:fin | ||
# shift positions ... | ||
R[j] += r | ||
if R[j] <= m | ||
split = j | ||
else | ||
R[j] -= m | ||
end | ||
end | ||
# ...but rowval should be sorted within columns | ||
_swap!(R, start, fin, split) | ||
_swap!(V, start, fin, split) | ||
end | ||
|
||
|
||
function circshift!(O::SparseVector, X::SparseVector, (r,)::Base.DimsInteger{1}) | ||
O .= X | ||
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. Is there a bug in 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. Yes, I opened a separate issue #30443 |
||
subvector_shifter!(O.nzind, O.nzval, 1, length(O.nzind), O.n, mod(r, X.n)) | ||
return O | ||
end | ||
|
||
|
||
circshift!(O::SparseVector, X::SparseVector, r::Real,) = circshift!(O, X, (Integer(r),)) |
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.
Skip this loop if
iszero(r)
. Similarly the code above can be replace with a copy ifiszero(c)
.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.
Thank you @stevengj. I implemented the suggestions, let me know if I interpreted correctly. I also moved subvector_shifter! to sparsevector.jl, it seemed more appropriate (should I prepend the name with _ given that it's a helper, or better not, as it is used also by sparsematrix.jl?).