select! and select

The elements at given positions of the sorted array, or of each sorted slice along dims, without sorting it: order statistics such as medians, quantiles or the k-th smallest element.

  • select!(dst, src, ranks) (into dst), select(src, ranks) (allocating it)

Base's closest function is partialsort!, which permutes its input and returns a value or a view; select! leaves its input untouched and writes the selected values, for any number of ranks at once and along dims. C++ calls a single selection std::nth_element; CUB has a top-k (cub::DeviceTopK) but no selection by rank.

Function signatures:

AcceleratedKernels.select! — Function
select!(
    dst::AbstractArray,
    src::AbstractArray,
    ranks::AbstractVector{<:Integer};
    dims::Union{Colon, Integer}=:,
    rev::Bool=false,
    alg::Algorithm=Auto(),
    backend=nothing,
    workspace=nothing,
) -> dst

Write to dst the elements of src at the sorted positions ranks, without sorting src: dst[j] is equal under isless (reversed with rev=true) to sort(vec(src); rev)[ranks[j]]. ranks is a host vector of 1-based positions, in any order and possibly repeated.

With dims=:, src is selected from as one vector, and dst is a vector of length(ranks). With an integer dims, every slice of src along dims is selected from, with the same ranks: dst has the size of src with dimension dims replaced by length(ranks).

src is not modified. dst must not overlap it, nor hold any element twice (as a view with repeated indices would). As for isless, -0.0 sorts before 0.0 and NaNs after every other value; a selected NaN is a NaN, though not necessarily with the bits of one in src.

alg is Auto() by default, which picks RadixSelect on GPUs for the element types it supports and one or two ranks, and SortSelect otherwise (and always on the host).

backend is derived from dst and src; pass it only for arrays that do not determine their backend. workspace takes the scratch memory of a workspace made for the same call.

Examples

import AcceleratedKernels as AKusing CUDAv = CuArray(rand(Float32, 1_000_000))m = CuArray{Float32}(undef, 2)AK.select!(m, v, [500_000, 500_001])          # the two middle elements, for a medianA = CuArray(rand(Float32, 1000, 64))q = CuArray{Float32}(undef, 1, 64)AK.select!(q, A, [100]; dims=1)                # the 100th smallest element of each column
source
AcceleratedKernels.select — Function
select(src::AbstractArray, ranks::AbstractVector{<:Integer}; kwargs...)

Out-of-place select!: allocate the destination for src, ranks and dims, select into it with the same keywords, and return it.

source

Example:

import AcceleratedKernels as AKusing CUDAv = CuArray(rand(Float32, 1_000_000))lo, hi = Array(AK.select(v, [500_000, 500_001]))median = (lo + hi) / 2A = CuArray(rand(Float32, 1000, 64))AK.select(A, [10, 990]; dims=1)              # a 2×64 matrix: the 10th and 990th order statistics of each column

Auto(), the default alg, picks RadixSelect on GPUs for the element types it supports and one or two ranks, and SortSelect otherwise and on the host:

AlgorithmRanksElement types, orderingsScratchBackends
RadixSelect1 or 232/64-bit integers and floats, default ordering or its reversenone for slices of up to 8192 elements; for longer ones, a buffer of 1/8 of src for the remaining candidates, and a few KiB of histograms and extrema per slicekernels
SortSelectanyalla copy of src, and the sort'sall

RadixSelect reads src once per 8-bit digit of the keys that the leading digits do not settle, and slices of up to 8192 elements in a single kernel; for a median of 2^26 Float32 values it takes about a tenth of the time of SortSelect. For more than two ranks, call select! once per pair, or use SortSelect when there are many.

AcceleratedKernels.RadixSelect — Type
RadixSelect(; block_size=nothing, items_per_thread=nothing, blocks=nothing)

GPU radix select, for UInt32, Int32, Float32, UInt64, Int64 and Float64 (floats ordered like isless) and one or two ranks. Slices of up to 8192 elements are selected by one block each, in local memory; longer ones by passes over the slices, with blocks blocks in all of block_size threads (a power of two from 256 to 1024) and items_per_thread elements each.

source