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: Fast 1-d in-place circshift! #42676

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

WIP: Fast 1-d in-place circshift! #42676

wants to merge 1 commit into from

Conversation

Keno
Copy link
Member

@Keno Keno commented Oct 18, 2021

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.

Fixes (the 1d case of) #16032 if merged.

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 Keno added help wanted Indicates that a maintainer wants help on an issue or pull request arrays [a, r, r, a, y, s] labels Oct 18, 2021
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

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant