Skip to content

Add opt-in GPU radix sort via sort alg keyword - #90

Merged
maleadt merged 4 commits into
JuliaGPU:mainfrom
shreyas-omkar:sh/sort-optim
Jun 30, 2026
Merged

Add opt-in GPU radix sort via sort alg keyword#90
maleadt merged 4 commits into
JuliaGPU:mainfrom
shreyas-omkar:sh/sort-optim

Conversation

@shreyas-omkar

@shreyas-omkar shreyas-omkar commented Jun 24, 2026

Copy link
Copy Markdown
Member

This PR adds an opt-in GPU radix sort implementation and makes it reachable through the public sort API:

AK.sort!(v; alg=AK.RadixSort())
AK.sort(v; alg=AK.RadixSort())

The radix path supports UInt32, Int32, Float32, UInt64, Int64, and Float64. Unsupported element types or custom lt / by settings fall back to merge sort.

The default behavior is unchanged: GPU sort! still uses merge sort unless alg=AK.RadixSort() is passed explicitly.

Details

  • Adds a stable 8-bit LSD radix sort implementation for GPU arrays.
  • Adds AK.RadixSort() as a sort algorithm selector.
  • Adds AK.MergeSort() / AK.SampleSort() selectors so algorithm choice is consistent across sort backends.
  • Adds correctness coverage for supported radix element types, rev=true, edge sizes, etc

Closes #84

@christiangnrd

Copy link
Copy Markdown
Member

KA has supports_float64(::Backend)

@shreyas-omkar

Copy link
Copy Markdown
Member Author

KA has supports_float64(::Backend)

Yea.. I checked it now. Thanks for giving up heads up. I will change the commit.

@shreyas-omkar
shreyas-omkar force-pushed the sh/sort-optim branch 2 times, most recently from b5f4620 to 270e578 Compare June 24, 2026 10:50
@shreyas-omkar
shreyas-omkar marked this pull request as ready for review June 27, 2026 18:41
maleadt and others added 3 commits June 30, 2026 14:29
Exclusive ScanPrefixes scans should keep each block's local exclusive output and store the full block aggregate separately for the inter-block prefix pass. Add a non-uniform multi-block regression test that catches the all-ones masking case.

Co-authored-by: shreyas-omkar <shreyashegdeplus06@gmail.com>
Bring over the PR JuliaGPU#90 sort changes without the ScanPrefixes fix: GPU sortperm now defaults to merge_sortperm!, merge_sort! hoists non-identity by= transforms, and radix_sort!/radix_sort are available as direct APIs with correctness tests and a benchmark driver.

The radix path remains opt-in at this point; sort! still defaults to merge sort.

Co-authored-by: shreyas-omkar <shreyashegdeplus06@gmail.com>
Add SortAlgorithm marker types and route sort!/sort/sortperm!/sortperm through alg=. RadixSort is reachable only through the public sort API, while MergeSort(lowmem=true) selects the existing low-memory permutation path.

Keep the default behavior unchanged: GPU sort uses merge sort, GPU sortperm uses merge_sortperm!, and CPU sort uses sample sort.
@maleadt maleadt changed the title Optimising sortperm! Add opt-in GPU radix sort via sort alg keyword Jun 30, 2026
@maleadt

maleadt commented Jun 30, 2026

Copy link
Copy Markdown
Member

I squashed into three logical commits. LGTM for an initial version; let's merge this a bit more rapidly rather than dragging the PR on like with mapreduce.

@shreyas-omkar

Copy link
Copy Markdown
Member Author

Sure @maleadt, I'll try and get done with this PR ASAP.

@maleadt

maleadt commented Jun 30, 2026

Copy link
Copy Markdown
Member

Seems like I made a mistake here, let me investigate.

EDIT: turns out my added tests exposes an issue with the DecoupledLoopback implementation.

@christiangnrd

Copy link
Copy Markdown
Member

That latest commit closes #84. If we're squashing i think we should split that one up into a separate PR

@maleadt

maleadt commented Jun 30, 2026

Copy link
Copy Markdown
Member

I was going to merge; hence cleaning up the history here first.

… test

The non-uniform exclusive accumulate test exposes block-carry bugs that
all-ones data masks. ScanPrefixes (the default algorithm) is correct here, but
DecoupledLookback's exclusive multi-block carries are still wrong on
non-uniform data, so it is excluded from this assertion and tracked as a
separate follow-up.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@maleadt
maleadt merged commit cce53a4 into JuliaGPU:main Jun 30, 2026
37 of 39 checks passed
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.

Bug: AK.accumulate!(...; inclusive=false) produces incorrect results for large arrays on both CUDA and Metal

3 participants