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

Improve BitonicSort performance for sorting floats #952

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

vyu
Copy link
Contributor

@vyu vyu commented Aug 20, 2021

Also: add docstring, extend tests, and add benchmarks.

The speedup (for sorting isbits floats under isless) is based on @stev47's improvement to isless in Julia 1.7 (stev47 also authored the BitonicSort implementation!), but instead of NaN-checking we subtract an offset to wrap the NaNs with negative sign to the greatest integers (see code for details), giving branchless code. We apply this transformation only once at the start of each sort to sort them as integers, then apply the inverse transformation at the end to get back the floats. (This technique cannot be used for isless in Julia Base because isless(NaN, NaN) returns false regardless of payload and sign.)

Comparative benchmark results on Julia 1.7.0-beta3:

  • x86-64 (Intel Haswell, mobile)
  • x86-64 (Intel Silvermont, microserver)
  • AArch64 (Arm Neoverse N1, Ampere A1 instance on Oracle Cloud)

(The couple of timing fluctuations that show up for integer sorting are just noise.)

I have a vectorized implementation that I hope to contribute later after cleaning it up. (SIMD performance is of course heavily dependent on CPU capabilities and compiler support, which is why I'm benchmarking on three machines. Didn't matter for this PR, though.)

Additional changes to sort.jl:

- Put sort.jl into a module to avoid polluting StaticArrays namespace.

- Add docstring.

- Extend tests.

- Add benchmarks.
Copy link
Contributor

@stev47 stev47 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transformation by wrapping -NaN around is indeed a nice idea! I've added some quick comments on your implementation.
The separate benchmark seems quite extensive, but I haven't looked at it in detail.

src/sort.jl Show resolved Hide resolved
src/sort.jl Outdated Show resolved Hide resolved
src/sort.jl Outdated Show resolved Hide resolved
src/sort.jl Outdated Show resolved Hide resolved
test/sort.jl Outdated Show resolved Hide resolved
src/sort.jl Outdated Show resolved Hide resolved
test/sort.jl Outdated Show resolved Hide resolved
@vyu
Copy link
Contributor Author

vyu commented Aug 22, 2021

@stev47 Thank you for the review. I've made some changes based on your feedback.

Instead of using generated functions to unroll tuples, use `ntuple(f,
Val(N))` where `f` has an inline hint.
@stev47
Copy link
Contributor

stev47 commented Sep 6, 2021

It is a bit regrettable that we now have a different _sort api than Base, but I have no quick idea around it.
It looks good to me.

@vyu
Copy link
Contributor Author

vyu commented Oct 6, 2021

Could a maintainer approve the pending workflows? This is ready for merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants