Skip to content

[do not merge] Test GPUArrays sort #2830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions perf/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,21 @@ let group = addgroup!(group, "random")
end

let group = addgroup!(group, "sorting")
group["1d"] = @async_benchmarkable sort($gpu_vec)
group["2d"] = @async_benchmarkable sort($gpu_mat; dims=1)
group["by"] = @async_benchmarkable sort($gpu_vec; by=sin)
let group = addgroup!(group, "Float32")
group["1d"] = @async_benchmarkable sort($gpu_vec)
group["by=sin"] = @async_benchmarkable sort($gpu_vec; by=sin)
# group["dims=1"] = @async_benchmarkable sort($gpu_mat; dims=1)
# group["dims=2"] = @async_benchmarkable sort($gpu_mat; dims=2)
# group["dims=1L"] = @async_benchmarkable sort($gpu_mat_long; dims=1)
# group["dims=2L"] = @async_benchmarkable sort($gpu_mat_long; dims=2)
end
let group = addgroup!(group, "Int64")
group["1d"] = @async_benchmarkable sort($gpu_vec_ints)
# group["dims=1"] = @async_benchmarkable sort($gpu_mat_ints; dims=1)
# group["dims=2"] = @async_benchmarkable sort($gpu_mat_ints; dims=2)
# group["dims=1L"] = @async_benchmarkable sort($gpu_mat_long_ints; dims=1)
# group["dims=2L"] = @async_benchmarkable sort($gpu_mat_long_ints; dims=2)
end
end

let group = addgroup!(group, "permutedims")
Expand Down
2 changes: 2 additions & 0 deletions perf/runbenchmarks.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# benchmark suite execution and codespeed submission
using Pkg
Pkg.add(url="https://github.com/christiangnrd/GPUArrays.jl", rev="sort")

using CUDA

Expand Down
180 changes: 90 additions & 90 deletions src/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -970,93 +970,93 @@ end

# Base interface implementation

using .BitonicSortImpl
using .QuickSortImpl


abstract type SortingAlgorithm end
struct QuickSortAlg <: SortingAlgorithm end
struct BitonicSortAlg <: SortingAlgorithm end

const QuickSort = QuickSortAlg()
const BitonicSort = BitonicSortAlg()


function Base.sort!(c::AnyCuVector, alg::QuickSortAlg; lt=isless, by=identity, rev=false)
# for reverse sorting, invert the less-than function
if rev
lt = !lt
end

quicksort!(c; lt, by, dims=1)
return c
end

function Base.sort!(c::AnyCuArray, alg::BitonicSortAlg; kwargs...)
return bitonic_sort!(c; kwargs...)
end

function Base.sort!(c::AnyCuArray; alg::SortingAlgorithm = BitonicSort, kwargs...)
return sort!(c, alg; kwargs...)
end

function Base.sort(c::AnyCuArray; kwargs...)
return sort!(copy(c); kwargs...)
end

function Base.partialsort!(c::AnyCuVector, k::Union{Integer, OrdinalRange},
alg::BitonicSortAlg; lt=isless, by=identity, rev=false)

sort!(c, alg; lt, by, rev)
return @allowscalar copy(c[k])
end

function Base.partialsort!(c::AnyCuVector, k::Union{Integer, OrdinalRange},
alg::QuickSortAlg; lt=isless, by=identity, rev=false)
# for reverse sorting, invert the less-than function
if rev
lt = !lt
end

function out(k::OrdinalRange)
return copy(c[k])
end

# work around disallowed scalar index
function out(k::Integer)
return Array(c[k:k])[1]
end

quicksort!(c; lt, by, dims=1, partial_k=k)
return out(k)
end

function Base.partialsort!(c::AnyCuArray, k::Union{Integer, OrdinalRange};
alg::SortingAlgorithm=BitonicSort, kwargs...)
return partialsort!(c, k, alg; kwargs...)
end

function Base.partialsort(c::AnyCuArray, k::Union{Integer, OrdinalRange}; kwargs...)
return partialsort!(copy(c), k; kwargs...)
end

function Base.sortperm!(ix::AnyCuArray, A::AnyCuArray; initialized=false, kwargs...)
if axes(ix) != axes(A)
throw(ArgumentError("index array must have the same size/axes as the source array, $(axes(ix)) != $(axes(A))"))
end

if !initialized
ix .= LinearIndices(A)
end
bitonic_sort!((A, ix); kwargs...)
return ix
end

function Base.sortperm(c::AnyCuVector; kwargs...)
sortperm!(CuArray(1:length(c)), c; initialized=true, kwargs...)
end

function Base.sortperm(c::AnyCuArray; dims, kwargs...)
# Base errors for Matrices without dims arg, we should too
sortperm!(reshape(CuArray(1:length(c)), size(c)), c; initialized=true, dims, kwargs...)
end
# using .BitonicSortImpl
# using .QuickSortImpl


# abstract type SortingAlgorithm end
# struct QuickSortAlg <: SortingAlgorithm end
# struct BitonicSortAlg <: SortingAlgorithm end

# const QuickSort = QuickSortAlg()
# const BitonicSort = BitonicSortAlg()


# function Base.sort!(c::AnyCuVector, alg::QuickSortAlg; lt=isless, by=identity, rev=false)
# # for reverse sorting, invert the less-than function
# if rev
# lt = !lt
# end

# quicksort!(c; lt, by, dims=1)
# return c
# end

# function Base.sort!(c::AnyCuArray, alg::BitonicSortAlg; kwargs...)
# return bitonic_sort!(c; kwargs...)
# end

# function Base.sort!(c::AnyCuArray; alg::SortingAlgorithm = BitonicSort, kwargs...)
# return sort!(c, alg; kwargs...)
# end

# function Base.sort(c::AnyCuArray; kwargs...)
# return sort!(copy(c); kwargs...)
# end

# function Base.partialsort!(c::AnyCuVector, k::Union{Integer, OrdinalRange},
# alg::BitonicSortAlg; lt=isless, by=identity, rev=false)

# sort!(c, alg; lt, by, rev)
# return @allowscalar copy(c[k])
# end

# function Base.partialsort!(c::AnyCuVector, k::Union{Integer, OrdinalRange},
# alg::QuickSortAlg; lt=isless, by=identity, rev=false)
# # for reverse sorting, invert the less-than function
# if rev
# lt = !lt
# end

# function out(k::OrdinalRange)
# return copy(c[k])
# end

# # work around disallowed scalar index
# function out(k::Integer)
# return Array(c[k:k])[1]
# end

# quicksort!(c; lt, by, dims=1, partial_k=k)
# return out(k)
# end

# function Base.partialsort!(c::AnyCuArray, k::Union{Integer, OrdinalRange};
# alg::SortingAlgorithm=BitonicSort, kwargs...)
# return partialsort!(c, k, alg; kwargs...)
# end

# function Base.partialsort(c::AnyCuArray, k::Union{Integer, OrdinalRange}; kwargs...)
# return partialsort!(copy(c), k; kwargs...)
# end

# function Base.sortperm!(ix::AnyCuArray, A::AnyCuArray; initialized=false, kwargs...)
# if axes(ix) != axes(A)
# throw(ArgumentError("index array must have the same size/axes as the source array, $(axes(ix)) != $(axes(A))"))
# end

# if !initialized
# ix .= LinearIndices(A)
# end
# bitonic_sort!((A, ix); kwargs...)
# return ix
# end

# function Base.sortperm(c::AnyCuVector; kwargs...)
# sortperm!(CuArray(1:length(c)), c; initialized=true, kwargs...)
# end

# function Base.sortperm(c::AnyCuArray; dims, kwargs...)
# # Base errors for Matrices without dims arg, we should too
# sortperm!(reshape(CuArray(1:length(c)), size(c)), c; initialized=true, dims, kwargs...)
# end
Loading