Skip to content

Commit

Permalink
specialize on chunk size & benchmark vs. old radix (passing)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner committed Nov 19, 2021
1 parent 85b2ac5 commit 6c960fd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
30 changes: 30 additions & 0 deletions raw_radix_v_radix_benchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using SortMark, SortingAlgorithms
using Base.Sort
using Base.Order

struct RawRadixSort2Alg <: Algorithm end
const RawRadixSort2 = RawRadixSort2Alg()
function Base.sort!(v::AbstractVector{<:Unsigned}, lo::Integer, hi::Integer, ::RawRadixSort3Alg, o::ForwardOrdering)
#compression, bits, chunk_size = heuristic(typemin(eltype(v)), typemax(eltype(v)), hi-lo+1)
out = SortingAlgorithms.radix_sort!(v, similar(v), lo, hi, unsigned(8*sizeof(eltype(v))), Val(0xb))
out === v || copyto!(v, out)
v
end

df = SortMark.make_df([RawRadixSort2, RadixSort],
Types=[UInt128], orders=[Base.Order.Forward],
lens=SortMark.lengths(2, 10_000_000, 3),
sources=Dict(:simple=>SortMark.sources[:simple]),
seconds=nothing, samples=30)
compute!(df)
stat!(df)
#df.samples[df.pvalue .>= .01] .*= 3
#compute!(df[df.pvalue .>= .01, :])
#stat!(df)
display(df[:, [:len, :Type, :pvalue, :point_estimate, :confint]])
x = df[df.worst_first, :]
x = df[df.Type .== UInt128, :]
display(x[:, [:len, :Type, :pvalue, :point_estimate, :confint]])
#display(df[sortperm(df.len), [:len, :Type, :pvalue, :point_estimate, :confint]])
plot(log10.(df.len), first.(df.confint))
plot!(log10.(df.len), last.(df.confint), legend=false)
16 changes: 8 additions & 8 deletions src/radix.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
function radix_sort!(ps::AbstractVector{U}, ts::AbstractVector{U}, lo::Integer, hi::Integer, bits::Unsigned,
chunk_size::Unsigned) where {U <: Unsigned}
::Val{CHUNK_SIZE}) where {U <: Unsigned, CHUNK_SIZE}

bits == 0 && return ps

@inbounds begin

counts = Vector{unsigned(typeof(hi-lo))}(undef, 2^chunk_size+1)
mask = 2^chunk_size-1
MASK = UInt(1) << CHUNK_SIZE - 0x1
counts = Vector{unsigned(typeof(hi-lo))}(undef, MASK+2)

#ps0 = ps
for shift in 0:chunk_size:bits-1
for shift in 0:CHUNK_SIZE:bits-1

counts .= zero(eltype(counts))

for k in lo:hi
x = ps[k]
idx = (x >> shift)&mask + 2
idx = (x >> shift)&MASK + 2
counts[idx] += one(eltype(counts))
end

sum = lo-1
for i in 1:2^chunk_size
for i in 1:(MASK+1)
sum += counts[i]
counts[i] = sum
end
#accumulate!(+, counts, counts)

for k in lo:hi
for k in lo:hi # Is this iteration slower than it could be?
x = ps[k]
i = (x >> shift)&mask + 1
i = (x >> shift)&MASK + 1
j = counts[i] += 1
ts[j] = x
#sortperm here
Expand Down
2 changes: 1 addition & 1 deletion src/radix_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function sort!(v::AbstractVector, lo::Integer, hi::Integer, ::RadixSort2Alg, o::

us = compress!(us, compression)

us = radix_sort!(us, similar(us, lo:hi), lo, hi, unsigned(bits), unsigned(chunk_size))
us = radix_sort!(us, similar(us, lo:hi), lo, hi, unsigned(bits), Val(UInt8(chunk_size)))

deserialize!(v, us, lo, hi, o, compression)
end
Expand Down

0 comments on commit 6c960fd

Please sign in to comment.