-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarp_batch.jl
57 lines (49 loc) · 1.33 KB
/
warp_batch.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
# Code to accelerate gpu_batch but temporarily omitted for simplicity
macro merge_swap_shfl()
q = quote
b = shfl_sync(0xFFFFFFFF, sum, batch_floor(idx, 2 * n))
a = n - b
d = shfl_sync(0xFFFFFFFF, sum, batch_ceil(idx, 2 * n))
c = n - d
swap = value
sum = d + b
value = shfl_sync(0xFFFFFFFF, swap, batch_step_swap(idx, 2 * n, a, b, c))
n *= 2
end
return esc(q)
end
macro repeat_inline(N, expr)
function tail(expr, N)
N <= 0 ? :() : :($expr; $(tail(expr, N - 1)))
end
out = tail(expr, N)
return esc(out)
end
function shfl_reduce_kernel(src, dest, flags)
i = threadIdx().x
j = blockIdx().x
w = blockDim().x
floor = w * (j - 1)
idx = floor + i
@inbounds value = src[idx]
@inbounds sum = 1 & flags[idx]
n = 1
# five merges => 2 ^ 5 = 32 = size of correct batches
@repeat_inline 5 @merge_swap_shfl
@inbounds dest[idx] = value
return nothing
end
function shfl_reduce_kernel(A, flags)
i = threadIdx().x
j = blockIdx().x
w = blockDim().x
floor = w * (j - 1)
idx = floor + i
value = A[idx]
sum = 1 & flags[idx]
n = 1
# five merges => 2 ^ 5 = 32 = size of correct batches
@repeat_inline 5 @merge_swap_shfl
A[idx] = value
return nothing
end