- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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: Fast 1-d in-place circshift! #42676
Draft
Keno
wants to merge
1
commit into
master
Choose a base branch
from
kf/circshift
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
+156
−0
Conversation
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
I thought I needed this, but turns out I probably don't. I still think it could be useful in the future, so I'm putting it here, in case somebody (maybe me in the future) ever needs this. There are three algorithms implemented here, as well as a selection heuristic for switching between them. The three algorithms are: 1. Out-of-place with a stack buffer 2. Batched Cycle-chasing in place 3. The Knuth Triple-Reverse trick Of these, the cycle chasing algorithm has optimal memory complexity (by doing `N` loads and stores), but conventional wisdom is that it is slower than triple-reverse (which does 2N memory operations) because of cache inefficiencies. I found this to be true for non-batched cycle chasing, but not for batched cycle chasing once the batching factor was about a cache line or so. As such, the polyalgorithm does the following: a. For small arrays, use the on-stack copy b. Then, compute the GCD. If the problem admits large batching factors, do the batched cycle chasing algorithm. c. If not, use the triple reverse trick, which is less memory efficent, but has consistent performance (unlike cycle chasing whose performance depends on the batching factor). There are additional algorithms listed at https://github.com/scandum/rotate that could be investigated to replace the reversal fallback. There are variations that compute the GCD from the first cycle, but I found that to be unnecessary. For arrays small enough for the GCD computation cost to matter, we're using the on-stack copy anyway, and for anything larger, the GCD is negligible and we use it to make the polyalgorithm decision anyway. I think to finish this, all we'd need is - [] Some tests to make sure all the cases are covered (and fix cases that may be broken, I did some refactoring, without retesting those) - [] Ideally, a little more benchmarking to more carefully select the algorithmic cutovers.
Keno
added a commit
that referenced
this pull request
Oct 18, 2021
We have a swpacols! helper in Base that is used in the permuation code as well as in the bareiss factorization code. I was working on extending the latter, among others to sparse arrays and alternative pivot choices. To that end, this PR, adds swaprows! in analogy with swapcols! and adds optimized implementations for SparseMatrixCSC. Note that neither of these functions are currently exported (though since they are useful, we may want a generic swapslices! of some sort, but that's for a future PR). While we're at it, also replace the open-coded in-place circshift! by one on SubArray, such that they can automatically beneift if that method is optimized in the future (#42676).
Keno
added a commit
that referenced
this pull request
Oct 18, 2021
We have a swpacols! helper in Base that is used in the permuation code as well as in the bareiss factorization code. I was working on extending the latter, among others to sparse arrays and alternative pivot choices. To that end, this PR, adds swaprows! in analogy with swapcols! and adds optimized implementations for SparseMatrixCSC. Note that neither of these functions are currently exported (though since they are useful, we may want a generic swapslices! of some sort, but that's for a future PR). While we're at it, also replace the open-coded in-place circshift! by one on SubArray, such that they can automatically beneift if that method is optimized in the future (#42676).
Keno
added a commit
that referenced
this pull request
Oct 18, 2021
We have a swpacols! helper in Base that is used in the permuation code as well as in the bareiss factorization code. I was working on extending the latter, among others to sparse arrays and alternative pivot choices. To that end, this PR, adds swaprows! in analogy with swapcols! and adds optimized implementations for SparseMatrixCSC. Note that neither of these functions are currently exported (though since they are useful, we may want a generic swapslices! of some sort, but that's for a future PR). While we're at it, also replace the open-coded in-place circshift! by one on SubArray, such that they can automatically beneift if that method is optimized in the future (#42676).
Keno
added a commit
that referenced
this pull request
Oct 18, 2021
We have a swpacols! helper in Base that is used in the permuation code as well as in the bareiss factorization code. I was working on extending the latter, among others to sparse arrays and alternative pivot choices. To that end, this PR, adds swaprows! in analogy with swapcols! and adds optimized implementations for SparseMatrixCSC. Note that neither of these functions are currently exported (though since they are useful, we may want a generic swapslices! of some sort, but that's for a future PR). While we're at it, also replace the open-coded in-place circshift! by one on SubArray, such that they can automatically beneift if that method is optimized in the future (#42676).
LilithHafner
pushed a commit
to LilithHafner/julia
that referenced
this pull request
Feb 22, 2022
We have a swpacols! helper in Base that is used in the permuation code as well as in the bareiss factorization code. I was working on extending the latter, among others to sparse arrays and alternative pivot choices. To that end, this PR, adds swaprows! in analogy with swapcols! and adds optimized implementations for SparseMatrixCSC. Note that neither of these functions are currently exported (though since they are useful, we may want a generic swapslices! of some sort, but that's for a future PR). While we're at it, also replace the open-coded in-place circshift! by one on SubArray, such that they can automatically beneift if that method is optimized in the future (JuliaLang#42676).
LilithHafner
pushed a commit
to LilithHafner/julia
that referenced
this pull request
Mar 8, 2022
We have a swpacols! helper in Base that is used in the permuation code as well as in the bareiss factorization code. I was working on extending the latter, among others to sparse arrays and alternative pivot choices. To that end, this PR, adds swaprows! in analogy with swapcols! and adds optimized implementations for SparseMatrixCSC. Note that neither of these functions are currently exported (though since they are useful, we may want a generic swapslices! of some sort, but that's for a future PR). While we're at it, also replace the open-coded in-place circshift! by one on SubArray, such that they can automatically beneift if that method is optimized in the future (JuliaLang#42676).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
arrays
[a, r, r, a, y, s]
help wanted
Indicates that a maintainer wants help on an issue or pull request
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.
I thought I needed this, but turns out I probably don't.
I still think it could be useful in the future, so I'm putting
it here, in case somebody (maybe me in the future) ever needs this.
There are three algorithms implemented here, as well as a selection
heuristic for switching between them. The three algorithms are:
Of these, the cycle chasing algorithm has optimal memory complexity
(by doing
N
loads and stores), but conventional wisdom is that itis slower than triple-reverse (which does 2N memory operations)
because of cache inefficiencies. I found this to be true for non-batched
cycle chasing, but not for batched cycle chasing once the batching
factor was about a cache line or so.
As such, the polyalgorithm does the following:
a. For small arrays, use the on-stack copy
b. Then, compute the GCD. If the problem admits large batching factors,
do the batched cycle chasing algorithm.
c. If not, use the triple reverse trick, which is less memory efficent,
but has consistent performance (unlike cycle chasing whose performance
depends on the batching factor).
There are additional algorithms listed at https://github.com/scandum/rotate
that could be investigated to replace the reversal fallback.
There are variations that compute the GCD from the first cycle, but
I found that to be unnecessary. For arrays small enough for the GCD
computation cost to matter, we're using the on-stack copy anyway,
and for anything larger, the GCD is negligible and we use it to
make the polyalgorithm decision anyway.
I think to finish this, all we'd need is
may be broken, I did some refactoring, without retesting those)
algorithmic cutovers.
Fixes (the 1d case of) #16032 if merged.