-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement bitonic sorting network for SVectors
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import Base.@_inline_meta | ||
import Base.Order: Ordering, Forward, ReverseOrdering, ord | ||
import Base.Sort: Algorithm, defalg, lt, sort | ||
|
||
|
||
struct BitonicSortAlg <: Algorithm end | ||
struct MinSizeSortAlg <: Algorithm end | ||
struct MinDepthSortAlg <: Algorithm end | ||
const MinSortAlg = Union{MinSizeSortAlg,MinDepthSortAlg} | ||
|
||
const BitonicSort = BitonicSortAlg() | ||
const MinSizeSort = MinSizeSortAlg() | ||
const MinDepthSort = MinDepthSortAlg() | ||
|
||
defalg(::SVector) = BitonicSort | ||
|
||
function sort(a::SVector; | ||
alg::Algorithm = defalg(a), | ||
lt = isless, | ||
by = identity, | ||
rev::Union{Bool,Nothing} = nothing, | ||
order::Ordering = Forward) | ||
ordr = ord(lt, by, rev, order) | ||
println(Size(a)) | ||
(Size(a) == Size(0) || Size(a) == Size(1)) && return a | ||
return _sort(Size(a), alg, ordr, a) | ||
end | ||
|
||
_sort(::Size{T}, alg, _, _) where T = | ||
error("sorting algorithm $alg unimplemented for static array of size $T") | ||
|
||
@generated function _sort(::Size{S}, ::BitonicSortAlg, order, a) where {S} | ||
function swap_expr(i, j, rev) | ||
ai = Symbol('a', i) | ||
aj = Symbol('a', j) | ||
order = rev ? :revorder : :order | ||
return :( ($ai, $aj) = lt($order, $ai, $aj) ? ($ai, $aj) : ($aj, $ai) ) | ||
end | ||
|
||
function merge_exprs(idx, rev) | ||
exprs = Expr[] | ||
length(idx) == 1 && return exprs | ||
|
||
ci = 2^(ceil(Int, log2(length(idx))) - 1) | ||
# TODO: generate simd code for these swaps | ||
for i in first(idx):last(idx)-ci | ||
push!(exprs, swap_expr(i, i+ci, rev)) | ||
end | ||
append!(exprs, merge_exprs(idx[1:ci], rev)) | ||
append!(exprs, merge_exprs(idx[ci+1:end], rev)) | ||
return exprs | ||
end | ||
|
||
function sort_exprs(idx, rev=false) | ||
exprs = Expr[] | ||
length(idx) == 1 && return exprs | ||
|
||
append!(exprs, sort_exprs(idx[1:end÷2], !rev)) | ||
append!(exprs, sort_exprs(idx[end÷2+1:end], rev)) | ||
append!(exprs, merge_exprs(idx, rev)) | ||
return exprs | ||
end | ||
|
||
idx = 1:prod(S) | ||
symlist = (Symbol('a', i) for i in idx) | ||
sym_exprs = (:( $ai = a[$i] ) for (i, ai) in enumerate(symlist)) | ||
return quote | ||
@_inline_meta | ||
revorder = Base.Order.ReverseOrdering(order) | ||
@inbounds ($(sym_exprs...);) | ||
($(sort_exprs(idx)...);) | ||
return SVector(($(symlist...))) | ||
end | ||
end | ||
|
||
|
||
## TODO: manually implementing minimal sorting networks for small lengths might | ||
## be worthwhile | ||
# | ||
#@inline _cmpswap(order, a, b) = lt(order, a, b) ? (a, b) : (b, a) | ||
# | ||
#macro _cmpswap(order, a, b) | ||
# return esc(:( ($a, $b) = _cmpswap(order, $a, $b) )) | ||
#end | ||
# | ||
#@inline _sort(::Size{(2,)}, _, order, (a1, a2)) = SVector(_cmpswap(order, a1, a2)) | ||
# | ||
#@inline function _sort(::Size{(3,)}, ::MinSortAlg, order, (a1, a2, a3)) | ||
# @_cmpswap order a1 a3 | ||
# @_cmpswap order a1 a2 | ||
# @_cmpswap order a2 a3 | ||
# return SVector(a1, a2, a3) | ||
#end | ||
# | ||
#@inline function _sort(::Size{(4,)}, ::MinSortAlg, order, (a1, a2, a3, a4)) | ||
# @_cmpswap order a1 a3 | ||
# @_cmpswap order a2 a4 | ||
# @_cmpswap order a1 a2 | ||
# @_cmpswap order a3 a4 | ||
# @_cmpswap order a2 a3 | ||
# return SVector(a1, a2, a3, a4) | ||
#end |