-
Notifications
You must be signed in to change notification settings - Fork 61
/
performance.jl
197 lines (152 loc) · 5.97 KB
/
performance.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# EXCLUDE FROM TESTING
using KernelAbstractions, Test, Random
include(joinpath(dirname(pathof(KernelAbstractions)), "../examples/utils.jl")) # Load backend
using KernelAbstractions.Extras: @unroll
using NVTX # TODO: Common front-end
const nreps = 3
const N = 2048
const T = Float32
const TILE_DIM = 32
const BLOCK_ROWS = 8
# Simple variants
@kernel function simple_copy_kernel!(output, @Const(input))
I, J = @index(Global, NTuple)
@inbounds output[I, J] = input[I, J]
end
@kernel function simple_transpose_kernel!(output, @Const(input))
I, J = @index(Global, NTuple)
@inbounds output[J, I] = input[I, J]
end
# Local memory variants
@kernel function lmem_copy_kernel!(output, @Const(input),
::Val{BANK}=Val(1)) where BANK
I, J = @index(Global, NTuple)
i, j = @index(Local, NTuple)
N = @uniform @groupsize()[1]
M = @uniform @groupsize()[2]
# +1 to avoid bank conflicts on shared memory
tile = @localmem eltype(output) (N+BANK, M)
@inbounds tile[i, j] = input[I, J]
@synchronize
@inbounds output[I, J] = tile[i, j]
end
@kernel function lmem_transpose_kernel!(output, @Const(input),
::Val{BANK}=Val(1)) where BANK
gi, gj = @index(Group, NTuple)
i, j = @index(Local, NTuple)
N = @uniform @groupsize()[1]
M = @uniform @groupsize()[2]
# +1 to avoid bank conflicts on shared memory
tile = @localmem eltype(output) (N+BANK, M)
# Manually calculate global indexes
# Later on we need to pivot the group index
I = (gi-1) * N + i
J = (gj-1) * M + j
@inbounds tile[i, j] = input[I, J]
@synchronize
# Pivot the group index
I = (gj-1) * M + i
J = (gi-1) * N + j
@inbounds output[I, J] = tile[j, i]
end
# Local Memory + process multiple elements per lane
@kernel function coalesced_copy_kernel!(output, @Const(input),
::Val{BANK}=Val(1)) where BANK
gi, gj = @index(Group, NTuple)
i, j = @index(Local, NTuple)
TILE_DIM = @uniform @groupsize()[1]
BLOCK_ROWS = @uniform @groupsize()[2]
# +1 to avoid bank conflicts on shared memory
tile = @localmem eltype(output) (TILE_DIM+BANK, TILE_DIM)
# Can't use @index(Global), because we use a smaller ndrange
I = (gi-1) * TILE_DIM + i
J = (gj-1) * TILE_DIM + j
@unroll for k in 0:BLOCK_ROWS:(TILE_DIM-1)
@inbounds tile[i, j+k] = input[I, J+k]
end
@synchronize
@unroll for k in 0:BLOCK_ROWS:(TILE_DIM-1)
@inbounds output[I, J+k] = tile[i, j+k]
end
end
@kernel function coalesced_transpose_kernel!(output, @Const(input),
::Val{BANK}=Val(1)) where BANK
gi, gj = @index(Group, NTuple)
i, j = @index(Local, NTuple)
TILE_DIM = @uniform @groupsize()[1]
BLOCK_ROWS = @uniform @groupsize()[2]
# +1 to avoid bank conflicts on shared memory
tile = @localmem eltype(output) (TILE_DIM+BANK, TILE_DIM)
# Can't use @index(Global), because we use a smaller ndrange
I = (gi-1) * TILE_DIM + i
J = (gj-1) * TILE_DIM + j
@unroll for k in 0:BLOCK_ROWS:(TILE_DIM-1)
@inbounds tile[i, j+k] = input[I, J+k]
end
@synchronize
# Transpose block offsets
I = (gj-1) * TILE_DIM + i
J = (gi-1) * TILE_DIM + j
@unroll for k in 0:BLOCK_ROWS:(TILE_DIM-1)
@inbounds output[I, J+k] = tile[j+k, i]
end
end
# Benchmark simple
for block_dims in ((TILE_DIM, TILE_DIM), (TILE_DIM*TILE_DIM, 1), (1, TILE_DIM*TILE_DIM))
for (name, kernel) in (
("copy", simple_copy_kernel!(backend, block_dims)),
("transpose", simple_transpose_kernel!(backend, block_dims)),
)
NVTX.@range "Simple $name $block_dims" let
input = rand!(allocate(backend, T, N, N))
output = similar(input)
# compile kernel
kernel(output, input, ndrange=size(output))
for rep in 1:nreps
kernel(output, input, ndrange=size(output))
end
KernelAbstractions.synchronize(backend)
end
end
end
# Benchmark localmem
for (name, kernel) in (
("copy", lmem_copy_kernel!(backend, (TILE_DIM, TILE_DIM))),
("transpose", lmem_transpose_kernel!(backend, (TILE_DIM, TILE_DIM))),
)
for bank in (true, false)
NVTX.@range "Localmem $name ($TILE_DIM, $TILE_DIM) bank=$bank" let
input = rand!(allocate(backend, T, N, N))
output = similar(input)
# compile kernel
kernel(output, input, Val(Int(bank)), ndrange=size(output))
for rep in 1:nreps
kernel(output, input, Val(Int(bank)), ndrange=size(output))
end
KernelAbstractions.synchronize(backend)
end
end
end
# Benchmark localmem + multiple elements per lane
for (name, kernel) in (
("copy", coalesced_copy_kernel!(backend, (TILE_DIM, BLOCK_ROWS))),
("transpose", coalesced_transpose_kernel!(backend, (TILE_DIM, BLOCK_ROWS))),
)
for bank in (true, false)
NVTX.@range "Localmem + multiple elements $name ($TILE_DIM, $BLOCK_ROWS) bank=$bank" let
input = rand!(allocate(backend, T, N, N))
output = similar(input)
# We want a number of blocks equivalent to (TILE_DIM, TILE_DIM)
# but our blocks are (TILE_DIM, BLOCK_ROWS) so we need to remove
# a factor from the size of the array otherwise we get to many blocks
block_factor = div(TILE_DIM, BLOCK_ROWS)
ndrange = (N, div(N, block_factor))
# compile kernel
kernel(output, input, Val(Int(bank)), ndrange=ndrange)
for rep in 1:nreps
kernel(output, input, Val(Int(bank)), ndrange=ndrange)
end
KernelAbstractions.synchronize(backend)
end
end
end