Repository navigation
Conversation
`sort!`, `sortperm!` and their setup helpers only pass `by` and `lt` on, so Julia compiles them for `by::Function`. There, `Base.promote_op(by, eltype(v))`, which sizes MergeSort's key buffers, can't fold and runs inference on every call (`sortperm!` even with the default `by=identity`). That also affects Base's `sort!` and `sortperm` of GPU arrays, which go through AK since GPUArrays 12. Name their types with `where` parameters, as the reductions do for `f` and `op`, and likewise in `sort_by_key!`, which shares the ordering code. A warm `sort!(x; by=abs)` of a 10,000-element `CuArray` then compiles nothing and drops from 335 to 256 host allocations.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #191, for the sorts: a warm
sort!withby, and any warmsortperm!, still runs inference on every call. Since GPUArrays 12 that includes Base'ssort!andsortpermof GPU arrays.sort!,sortperm!and their setup helpers only passbyandlton, so Julia compiles them forby::Function. In that code, theBase.promote_op(by, eltype(v))that sizes MergeSort's key buffers can't fold.sortperm!evaluates it even with the defaultby=identity. This PR names the types ofbyandltwithwhereparameters in the sort methods (includingsort_by_key!, which shares the ordering code), the same change #191 makes for the predicates.Afterwards a warm call compiles nothing (the
@timeabove: 135 allocations, 4.2 KiB, no compilation). On an RTX 5080, with 10,000Float32s called by dynamic dispatch (median of five BenchmarkTools runs):sort!(x; by=abs)sortperm!(ix, x; by=abs)sortperm!(ix, x; lt=>)sort!(X; dims=1, by=abs)sort!(x)The kernels are unchanged: they were already compiled for the run-time types of
byand the ordering.The reductions'
dims=:default (Colon <: Function) causes no inference at run time: warmsumandreducecompile nothing on CPU and CUDA. Neither do thesearchsorted*!functions, so they're left as they are.The new tests check that a warm call of
sort!andsortperm!by dynamic dispatch compiles nothing. They fail on main and pass with this PR on CPU threads, the KA host backend, and CUDA (Julia 1.12), and on CPU with Julia 1.10. They use the compile-time helper from #191, added totest/setup.jlaswarm_compile_time(notcompile_time, which ParallelTestRunner assigns as a global in each test module); #191 can switch to it once both are merged.