Skip to content

perf(sort): packed-key fast path for sort(A; dims=1) - #121

Draft
shreyas-omkar wants to merge 2 commits into
JuliaGPU:mainfrom
shreyas-omkar:sh/sort-dims-packed
Draft

perf(sort): packed-key fast path for sort(A; dims=1)#121
shreyas-omkar wants to merge 2 commits into
JuliaGPU:mainfrom
shreyas-omkar:sh/sort-dims-packed

Conversation

@shreyas-omkar

@shreyas-omkar shreyas-omkar commented Sep 8, 2026

Copy link
Copy Markdown
Member

Follow-up to #117.

Packs (slice_id, value key) into a single UInt64 and sorts that, instead of the comparator merge over (slice, value) tuples, for sort(A; dims=1) with a 32-bit eltype under the default ordering. Everything else falls back to the #117 path unchanged.

sort(A; dims=1), Float32, 1M elements, min of 30 runs:

device size old #117 new vendor
RTX 5080 (CUDA 6.3.1) 1024x1024 0.666 ms 0.556 ms 0.352 ms
RTX 5080 256x4096 0.654 ms 0.553 ms 0.430 ms
RTX 5080 512x2048 0.658 ms 0.557 ms 0.295 ms
RX 9060 XT (AMDGPU 2.8.0) 1024x1024 3.047 ms 2.498 ms n/a
RX 9060 XT 256x4096 3.246 ms 2.623 ms n/a
RX 9060 XT 512x2048 3.056 ms 2.646 ms n/a

Vendor is CUDA.jl / AMDGPU.jl native sort(A; dims). AMDGPU.jl has no GPU dims sort (it hits a scalar-indexing fallback), so AK is the only GPU path there. CUDA.jl uses a dedicated segmented sorter and stays ahead; closing that gap is tracked in #120.

Sort or permute each 1D slice along an integer `dims` independently,
matching Base. The default `dims=:` keeps the existing flat behaviour.

There is no batched sort kernel, so each element is tagged with its slice
and the whole array is sorted once by (slice, value), then scattered back
into place. sortperm carries the original index as the payload and uses it
to break ties, keeping the permutation stable. This reuses the backend's
tuned sort and runs unchanged on CPU and every GPU backend.
@shreyas-omkar
shreyas-omkar marked this pull request as draft September 8, 2026 15:26
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.

1 participant