-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitonic_sort.jl
73 lines (61 loc) · 1.81 KB
/
bitonic_sort.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function bitonic_sort_parallel_step(A, n, k, buff, groupsize, boxsize, stride, dir)
i = threadIdx().x + (blockIdx().x - 1) * blockDim().x
if i > k
return
end
groupid = div(i - 1, groupsize)
groupdir = !dir
if mod(groupid, 2) == 0
groupdir = dir
end
compInBox = ((mod(i-1, boxsize) + stride) < boxsize)
if compInBox
j = i + stride
this_elem = i <= n ? A[i] : buff[i-n]
swap_elem = j <= n ? A[j] : buff[j-n]
if groupdir == (this_elem > swap_elem)
if i > n
i = i - n
j = j - n
tmp = buff[i]
buff[i] = buff[j]
buff[j] = tmp
elseif j > n
j = j - n
tmp = A[i]
A[i] = buff[j]
buff[j] = tmp
else
tmp = A[i]
A[i] = A[j]
A[j] = tmp
end
end
end
return
end
# dir=true indicates ascending
function bitonic_sort(A, dir=true, max=nothing)
if max === nothing
max = typemax(typeof(A[1]))
end
n = length(A)
groupsize = 2
k = nextpow(2, n)
buff = CuArrays.fill(max, k-n)
threads = 256
blocks = cld(k, threads)
# Intuitive representation of the sorter network graph in https://en.wikipedia.org/wiki/Bitonic_sorter
# group = blue/green, box = red/light red, stride = arrow length
while groupsize <= k
stride = div(groupsize, 2)
boxsize = groupsize
while stride >= 1
@cuda threads=threads blocks=blocks bitonic_sort_parallel_step(A, n, k, buff, groupsize, boxsize, stride, dir)
stride = div(stride, 2)
boxsize = div(boxsize, 2)
end
groupsize = groupsize * 2
end
return A
end