Skip to content

Add select! and select, which pick elements by sorted rank - #215

Merged
maleadt merged 5 commits into
mainfrom
tb/select
Nov 27, 2026
Merged

maleadt merged 5 commits into
mainfrom
tb/select

Conversation

@maleadt

@maleadt maleadt commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

AK.select!(dst, src, ranks; dims=:, rev=false, alg=Auto(), workspace) writes the elements of src at the given positions of its sorted order, or of each sorted slice along dims, without sorting or modifying src; AK.select(src, ranks; ...) allocates dst. ranks is a host vector of 1-based positions, common to all slices, in any order; with dims=d, dst has src's size with dimension d replaced by length(ranks). As with an unstable sort, any one of equal elements may be selected.

This is the primitive GPUArrays needs for Statistics' median and quantile (the follow-up to #64, which puts Statistics in GPUArrays rather than AK). Today those either sort a full copy or fall back to scalar indexing; Base's partialsort! contract (permute the input, return a value or a view) does not fit a GPU, and CUB has a top-k but no selection by rank.

Algorithms

  • RadixSelect: an MSD radix select over radix sort's order-preserving keys, 8-bit digits, for 32/64-bit integers and floats and one or two ranks (a median's two middle elements resolve together until their buckets diverge). Three kernel families:
    • slices of up to 160 elements (64 on Metal, 128 on oneAPI): each thread counts the elements that sort before its own, several slices per block;
    • up to 8192 elements: one block per slice, keys in registers, passes in local memory;
    • longer slices: global passes, several blocks per slice, with the state on the device (no host round trips). The first pass finds the extrema, whose common leading digits are skipped, and once the candidates fit 1/8 of the slice they are compacted, so later passes read only them.
  • SortSelect(; sort_alg=Auto(stable=false)): copy, sort, gather. Any element type and number of ranks.

Auto() picks RadixSelect on GPUs where it applies and SortSelect otherwise and on the host. Per-device values live in SelectTuning/select_tuning.

The global passes are AK's first kernels in which several blocks update the same global-memory atomics. Atomix before 1.7 implements atomics on OpenCL and oneAPI arrays with work-group scope (its extensions call the SPIR-V intrinsics; fixed for OpenCL in SPIRVIntrinsics 1.3.1, OpenCL.jl#531), so this PR requires Atomix 1.7. Through UnsafeAtomics 0.4, that raises the minimum backend versions, which are now declared: AMDGPU 2.9, CUDA/CUDACore 6.5, Metal 1.12, oneAPI 2.10 and OpenCL 1. The test environment already required these versions or newer.

Performance

Median ranks (two middle elements) against SortSelect with the fastest of Auto(stable=false), RadixSort, BitonicSort and MergeSort (copy + sort + gather), workspaces preallocated, minimum of 15 warm runs, ms:

CUDA, RTX 5080

case T data select (ms) sort baseline (ms) speedup
vector 2^26 Float32 random 1.126 11.066 (RadixSort) 9.8x
vector 2^26 Float32 equal 0.360 0.943 (RadixSort) 2.6x
vector 2^26 Float32 prefix 1.193 5.964 (RadixSort) 5.0x
vector 2^20 Float32 random 0.040 0.231 (RadixSort) 5.7x
64x1048576 dims=1 Float32 random 1.395 2.065 (Auto) 1.5x
256x262144 dims=1 Float32 random 1.996 4.132 (Auto) 2.1x
1024x65536 dims=1 Float32 random 0.852 4.464 (BitonicSort) 5.2x
4096x4096 dims=1 Float32 random 0.173 1.401 (BitonicSort) 8.1x
65536x1024 dims=1 Float32 random 1.253 11.642 (RadixSort) 9.3x
1048576x64 dims=1 Float32 random 1.132 11.761 (RadixSort) 10.4x
4096x4096 dims=2 Float32 random 0.269 2.136 (BitonicSort) 8.0x
vector 2^25 Float64 random 1.057 11.749 (RadixSort) 11.1x
vector 2^25 Float64 equal 0.361 0.959 (Auto) 2.7x
vector 2^25 Float64 prefix 1.021 8.477 (RadixSort) 8.3x
vector 2^20 Float64 random 0.068 0.431 (RadixSort) 6.4x
64x1048576 dims=1 Float64 random 1.787 5.011 (BitonicSort) 2.8x
256x262144 dims=1 Float64 random 2.361 7.461 (BitonicSort) 3.2x
1024x65536 dims=1 Float64 random 0.889 10.690 (BitonicSort) 12.0x
4096x4096 dims=1 Float64 random 0.232 3.859 (BitonicSort) 16.6x
65536x1024 dims=1 Float64 random 2.137 25.173 (RadixSort) 11.8x
1048576x64 dims=1 Float64 random 2.125 25.444 (RadixSort) 12.0x
4096x4096 dims=2 Float64 random 0.334 4.416 (BitonicSort) 13.2x

Metal, M1 (Float32 only)

case T data select (ms) sort baseline (ms) speedup
vector 2^26 Float32 random 50.145 266.546 (Auto) 5.3x
vector 2^26 Float32 equal 12.661 14.827 (RadixSort) 1.2x
vector 2^26 Float32 prefix 51.013 137.905 (Auto) 2.7x
vector 2^20 Float32 random 2.557 9.539 (BitonicSort) 3.7x
64x1048576 dims=1 Float32 random 72.325 77.415 (Auto) 1.1x
256x262144 dims=1 Float32 random 45.063 162.417 (BitonicSort) 3.6x
1024x65536 dims=1 Float32 random 21.568 170.192 (BitonicSort) 7.9x
4096x4096 dims=1 Float32 random 5.355 57.801 (BitonicSort) 10.8x
65536x1024 dims=1 Float32 random 53.814 378.872 (Auto) 7.0x
1048576x64 dims=1 Float32 random 51.243 332.650 (Auto) 6.5x
4096x4096 dims=2 Float32 random 11.834 73.661 (BitonicSort) 6.2x

oneAPI, Arc A770 (Float32 only)

case T data select (ms) sort baseline (ms) speedup
vector 2^26 Float32 random 4.091 59.383 (RadixSort) 14.5x
vector 2^26 Float32 equal 1.351 3.101 (RadixSort) 2.3x
vector 2^26 Float32 prefix 5.780 31.640 (Auto) 5.5x
vector 2^20 Float32 random 0.371 1.044 (RadixSort) 2.8x
64x1048576 dims=1 Float32 random 4.962 5.876 (Auto) 1.2x
256x262144 dims=1 Float32 random 6.082 23.778 (BitonicSort) 3.9x
1024x65536 dims=1 Float32 random 3.746 13.759 (BitonicSort) 3.7x
4096x4096 dims=1 Float32 random 1.185 5.406 (BitonicSort) 4.6x
65536x1024 dims=1 Float32 random 4.201 35.267 (RadixSort) 8.4x
1048576x64 dims=1 Float32 random 4.163 35.665 (Auto) 8.6x
4096x4096 dims=2 Float32 random 1.680 8.455 (BitonicSort) 5.0x

Selection wins everywhere measured, by 1.1-1.5x at the least (2^20 slices of 64 elements, where on the M1 it ranged from a tie to 1.1x across runs), so Auto does not fall back to sorting by shape. Scratch: none for slices of up to 8192 elements; a 1/8-of-src candidate buffer plus a few KiB per slice beyond that, against a full copy plus the sort's buffers (up to 1 GiB here) for the baseline.

Tests

test/generic/select.jl compares against sort on every backend: each kernel's length boundaries, all-equal, few-distinct and long-common-prefix data, medians straddling a bucket boundary of every digit, signed zeros, infinities and NaNs, rev, dims on 1-3D arrays, offset and strided ranks and destinations, the fallback with non-radix element types and many ranks, workspace reuse across data that skips and compacts differently, inference, and argument errors. test/algorithms.jl covers Auto's choices and the checks of explicit algorithms.

Run locally: CUDA (RTX 5080), OpenCL/PoCL, CPU threads and --cpu-ka on Julia 1.12 (full suite on CUDA/OpenCL/CPU), select/tunings/workspace on Julia 1.10, Metal (M1, Julia 1.13) and oneAPI (Arc A770, Julia 1.12).

Not included (follow-ups)

  • A sampling pre-filter for uniform data (e.g. rand in [0,1), whose keys share few leading digits but spread evenly), which would save one or two global passes.
  • A kernel with several threads per slice for slices of 64-512 elements, between counting and one block per slice (the weakest case above: 2^20 slices of 64).
  • by/lt/order keywords (SortSelect could take them; radix cannot).
  • Tuning the global passes on Metal, whose times are 10-50x those on the A770.
  • GPUArrays switching its sort-based median/quantile to select! after a release.

Atomix before 1.7 implements atomics on OpenCL and oneAPI arrays with
work-group scope, which kernels whose blocks share counters in global memory
cannot use. Atomix 1.7 needs UnsafeAtomics 0.4, which only these backend
versions support, so declare them as AK's minimum versions: AMDGPU 2.9, CUDA
and CUDACore 6.5, Metal 1.12, oneAPI 2.10 and OpenCL 1.
`AK.select!(dst, src, ranks; dims, rev)` writes the elements at the given
positions of the sorted array, or of each sorted slice along `dims`,
without sorting or modifying `src`. It is the primitive GPUArrays needs for
Statistics' `median` and `quantile`, which today sort a copy or fall back
to scalar indexing; `select` allocates the destination.

RadixSelect is an MSD radix select over radix sort's order-preserving keys,
for 32/64-bit integers and floats and one or two ranks. By default, slices
of up to 160 elements are selected by counting in local memory, slices of
up to 8192 in registers by one block each, and longer ones by global passes
whose state stays on the device, which skip the digits the extrema share
and compact the candidates once they are few. SortSelect sorts a copy and
gathers the ranks, for every other element type and number of ranks;
`Auto` picks it on the host.

The global passes are AK's first kernels whose atomics on global memory
several blocks update, which is why AK now requires Atomix 1.7.

On an RTX 5080, the median of 2^26 Float32s takes 1.1 ms against 10.8 ms
for copying, sorting and gathering, and in the shapes measured along
`dims`, medians are 1.5-17x faster than with the fastest sort.
Counting every pair of a slice's elements is quadratic in the slice length,
and the crossover with the one-block radix select comes earlier on an M1
(between 64 and 96 elements) and an Arc A770 (between 128 and 160) than on
the RTX 5080 the default was measured on.
A manual page with the algorithms, the tunings, and entries in the
overview, the README's function table and how `Auto` chooses.
@maleadt
maleadt merged commit edff3c7 into main Nov 27, 2026
16 of 17 checks passed
@maleadt
maleadt deleted the tb/select branch November 27, 2026 13:10
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