Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/sort/merge_sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ end
# GPU merge sort of `v` (or of each slice along `dims`), in place, with the scratch buffers of
# `sort_plan(::MergeSort, ...)`.
function merge_sort!(
v::AbstractArray, backend::Backend, bufs::NamedTuple, ord::Base.Order.Ordering, by;
v::AbstractArray, backend::Backend, bufs::NamedTuple, ord::Base.Order.Ordering, by::B;
block_size::Int, dims::Union{Colon, Integer},
)
) where {B}
layout = slice_layout(v, dims)
(isempty(v) || layout.len <= 1) && return v

Expand Down Expand Up @@ -262,8 +262,8 @@ end
# GPU merge sort permutation: sorts a copy of the keys, `by.(v)`, carrying the indices along.
function merge_sortperm!(
ix::AbstractArray, v::AbstractArray, backend::Backend, bufs::NamedTuple,
ord::Base.Order.Ordering, by; block_size::Int, dims::Union{Colon, Integer},
)
ord::Base.Order.Ordering, by::B; block_size::Int, dims::Union{Colon, Integer},
) where {B}
layout = slice_layout(v, dims)

# Initialise the linear indices that will be sorted by the keys in v
Expand Down
45 changes: 23 additions & 22 deletions src/sort/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ function sort!(
backend::Union{Nothing, Backend}=nothing,
alg::Algorithm=Auto(),
dims::Union{Colon, Integer}=Colon(),
lt=isless,
by=identity,
lt::L=isless,
by::B=identity,
rev::Union{Nothing, Bool}=nothing,
order::Base.Order.Ordering=Base.Order.Forward,
workspace=nothing,
)
) where {L, B}
s = sort_setup(v; backend, alg, dims, lt, by, rev, order)
p = s.plan
sort_run!(p.alg, one_based(v), p.backend, dims, s.ord, by, scratch_buffers(p, workspace, v))
Expand All @@ -75,9 +75,9 @@ plan(::typeof(sort!), v::AbstractArray; kwargs...) = sort_setup(v; kwargs...).pl

function sort_setup(
v; backend=nothing, alg::Algorithm=Auto(), dims::Union{Colon, Integer}=Colon(),
lt=isless, by=identity, rev::Union{Nothing, Bool}=nothing,
lt::L=isless, by::B=identity, rev::Union{Nothing, Bool}=nothing,
order::Base.Order.Ordering=Base.Order.Forward,
)
) where {L, B}
backend = resolve_backend(backend, v)
v = one_based(v)
ord = sort_ordering(lt, by, rev, order)
Expand All @@ -93,7 +93,7 @@ end
# need them, so that its plans infer: the swap buffer of the global passes, or the keys `by`
# computes, sorted carrying the elements along. A zero-dimensional array, whose one element needs
# no sorting, has no empty buffers of its shape, and gets none.)
function sort_plan(a::MergeSort, v, backend, dims, by)
function sort_plan(a::MergeSort, v, backend, dims, by::B) where {B}
ndims(v) == 0 && return Plan(backend, a, (;))
len = slice_layout(v, dims).len
if by !== identity
Expand All @@ -111,12 +111,12 @@ sort_plan(a::CPUThreads.SampleSort, v, backend, dims, by) =

# The ordering of the sorting keywords, as `Base.Order.ord` makes it but with a `by` always
# outermost, so that MergeSort can sort the keys `by.(v)` under `ord.order` instead
function sort_ordering(lt, by, rev, order)
function sort_ordering(lt::L, by::B, rev, order) where {L, B}
ord = Base.Order.ord(lt, identity, rev, order)
return by === identity ? ord : Base.Order.By(by, ord)
end

function sort_run!(a::MergeSort, v, backend, dims, ord, by, bufs)
function sort_run!(a::MergeSort, v, backend, dims, ord, by::B, bufs) where {B}
merge_sort!(v, backend, bufs, ord, by; block_size=a.block_size, dims)
end

Expand Down Expand Up @@ -189,12 +189,12 @@ function sortperm!(
backend::Union{Nothing, Backend}=nothing,
alg::Algorithm=Auto(),
dims::Union{Colon, Integer}=Colon(),
lt=isless,
by=identity,
lt::L=isless,
by::B=identity,
rev::Union{Nothing, Bool}=nothing,
order::Base.Order.Ordering=Base.Order.Forward,
workspace=nothing,
)
) where {L, B}
check_sortperm_shape(ix, v, dims)
sortperm_unchecked!(ix, v; backend, alg, dims, lt, by, rev, order, workspace)
end
Expand All @@ -215,9 +215,10 @@ end

# `sortperm!` without the check of `ix`'s shape
function sortperm_unchecked!(ix, v; backend=nothing, alg::Algorithm=Auto(),
dims::Union{Colon, Integer}=Colon(), lt=isless, by=identity,
dims::Union{Colon, Integer}=Colon(), lt::L=isless, by::B=identity,
rev::Union{Nothing, Bool}=nothing,
order::Base.Order.Ordering=Base.Order.Forward, workspace=nothing)
order::Base.Order.Ordering=Base.Order.Forward,
workspace=nothing) where {L, B}
s = sortperm_setup(ix, v; backend, alg, dims, lt, by, rev, order)
p = s.plan
bufs = scratch_buffers(p, workspace, ix, v)
Expand All @@ -236,9 +237,9 @@ end

function sortperm_setup(
ix, v; backend=nothing, alg::Algorithm=Auto(), dims::Union{Colon, Integer}=Colon(),
lt=isless, by=identity, rev::Union{Nothing, Bool}=nothing,
lt::L=isless, by::B=identity, rev::Union{Nothing, Bool}=nothing,
order::Base.Order.Ordering=Base.Order.Forward,
)
) where {L, B}
backend = resolve_backend(backend, ix, v)
ix, v = one_based(ix), one_based(v)
ord = sort_ordering(lt, by, rev, order)
Expand All @@ -248,7 +249,7 @@ function sortperm_setup(
end

# The scratch of `sortperm!` with each algorithm
function sortperm_scratch(a::MergeSort, ix, v, dims, by)
function sortperm_scratch(a::MergeSort, ix, v, dims, by::B) where {B}
(ndims(ix) == 0 || ndims(v) == 0) && return (;)
len = slice_layout(v, dims).len
merges = len > 2 * a.block_size
Expand All @@ -265,7 +266,7 @@ end
sortperm_scratch(a::CPUThreads.SampleSort, ix, v, dims, by) =
dims isa Colon ? sample_sort_scratch(a, eltype(ix), length(ix)) : (;)

function sortperm_run!(a::MergeSort, ix, v, backend, dims, ord, by, bufs)
function sortperm_run!(a::MergeSort, ix, v, backend, dims, ord, by::B, bufs) where {B}
if a.lowmem
merge_sortperm_lowmem!(ix, v, backend, bufs, ord; block_size=a.block_size, dims)
else
Expand Down Expand Up @@ -347,12 +348,12 @@ function sort_by_key!(
backend::Union{Nothing, Backend}=nothing,
alg::Algorithm=Auto(),
dims::Union{Colon, Integer}=Colon(),
lt=isless,
by=identity,
lt::L=isless,
by::B=identity,
rev::Union{Nothing, Bool}=nothing,
order::Base.Order.Ordering=Base.Order.Forward,
workspace=nothing,
)
) where {L, B}
s = sort_by_key_setup(keys, values; backend, alg, dims, lt, by, rev, order)
p = s.plan
sort_by_key_run!(p.alg, one_based(keys), one_based(values), p.backend, dims, s.ord,
Expand All @@ -365,9 +366,9 @@ plan(::typeof(sort_by_key!), keys::AbstractArray, values::AbstractArray; kwargs.

function sort_by_key_setup(
keys, values; backend=nothing, alg::Algorithm=Auto(), dims::Union{Colon, Integer}=Colon(),
lt=isless, by=identity, rev::Union{Nothing, Bool}=nothing,
lt::L=isless, by::B=identity, rev::Union{Nothing, Bool}=nothing,
order::Base.Order.Ordering=Base.Order.Forward,
)
) where {L, B}
backend = resolve_backend(backend, keys, values)
ord = sort_ordering(lt, by, rev, order)
if dims isa Colon
Expand Down
11 changes: 11 additions & 0 deletions test/generic/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ end
sort(vh; by=x -> (x % 2, x))
end

# Julia does not specialize a method on a function argument that it only passes on, so the
# methods that take `by` or `lt` name its type. Otherwise they run code compiled for any
# `Function`, in which the setup infers the type of the keys `by` computes on every call.
@testset "sort: no inference at run time" begin
v = array_from_host(randn(Float32, 1000))
for alg in unique([SORT_ALG, sort_alg()])
@test warm_compile_time(AK.sort!, v; alg, by=abs) == 0
@test warm_compile_time(AK.sort!, reshape(v, 10, :); alg, dims=1, by=abs) == 0
end
end


@testset "sort: rejected algorithms" begin
# The entry points reject what resolution does (see test/algorithms.jl), before sorting
Expand Down
10 changes: 10 additions & 0 deletions test/generic/sortperm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ end
sortperm(1:1000; by=x -> x % 10)
end

# As for `sort!` (see "sort: no inference at run time")
@testset "sortperm: no inference at run time" begin
v = array_from_host(randn(Float32, 1000))
ix = array_from_host(zeros(Int, 1000))
for alg in unique([SORT_ALG, sort_alg()])
@test warm_compile_time(AK.sortperm!, ix, v; alg, by=abs) == 0
@test warm_compile_time(AK.sortperm!, ix, v; alg, lt=(>)) == 0
end
end


@testset "sortperm: dims" begin
Random.seed!(0)
Expand Down
16 changes: 16 additions & 0 deletions test/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ end
with_workspace(op, args...; kwargs...) =
op(args...; kwargs..., workspace=AK.workspace(op, args...; kwargs...))

# The compilation time of the fastest of a few warm calls by dynamic dispatch (other tasks may
# compile once; an inferred caller could inline past the entry point)
function warm_compile_time(f, args...; kwargs...)
invokelatest(f, args...; kwargs...)
minimum(1:3) do _
Base.cumulative_compile_timing(true)
try
t = Base.cumulative_compile_time_ns()[1]
invokelatest(f, args...; kwargs...)
Base.cumulative_compile_time_ns()[1] - t
finally
Base.cumulative_compile_timing(false)
end
end
end

# The algorithms of tests that do not choose one: `Auto()`, except where `HOST_KERNELS`, whose
# point is to run AK's kernels on the host backend, on which `Auto()` picks the threaded algorithms
default_alg(kernel_alg) = HOST_KERNELS ? kernel_alg : AK.Auto()
Expand Down
Loading