-
Notifications
You must be signed in to change notification settings - Fork 5
/
reduce_opencl.cl
executable file
·311 lines (274 loc) · 11.7 KB
/
reduce_opencl.cl
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Project: SandboxGPUs.
// Author: Ashot Vardanian.
// Created: 04/09/2019.
// Copyright: Check "License" file.
//
/**
* Most of the algorithms here have follwong properties:
* - takes log(n) steps for n input elements,
* - uses n threads,
* - only works for power-of-2 arrays.
*/
__kernel void reduce_simple(__global float const *inputs, __global float *outputs, __local float *buffer) {
uint global_item_id = get_global_id(0);
uint local_item_id = get_local_id(0);
uint items_count = get_global_size(0);
uint items_per_group = get_local_size(0);
// First iteration: Copy from global to local
buffer[local_item_id] = inputs[global_item_id];
barrier(CLK_LOCAL_MEM_FENCE);
for (uint stride = items_per_group / 2; stride > 0; stride /= 2) {
// First n work-items read from second n work-items (n=stride)
if (local_item_id < stride)
buffer[local_item_id] += buffer[local_item_id + stride];
barrier(CLK_LOCAL_MEM_FENCE);
}
// Last iteration: write result to n-th position in global x array (n=work-group id)
if (local_item_id == 0)
outputs[get_group_id(0)] = buffer[0];
barrier(CLK_LOCAL_MEM_FENCE);
if (global_item_id == 0) {
for (uint i = 1; i < items_count / items_per_group; ++i)
outputs[0] += outputs[i];
}
}
/**
* This reduction interleaves which threads are active by using the modulo
* operator. This operator is very expensive on GPUs, and the interleaved
* inactivity means that no whole warps are active, which is also very
* inefficient.
*/
__kernel void reduce_w_modulo(__global float const *inputs, __global float *outputs, ulong const n,
__local float *buffer) {
ulong const idx_in_block = get_local_id(0);
ulong const idx_global = get_global_id(0);
buffer[idx_in_block] = (idx_global < n) ? inputs[idx_global] : 0;
barrier(CLK_LOCAL_MEM_FENCE);
// Perform reduction in the shared memory.
for (ulong i = 1; i < get_local_size(0); i *= 2) {
// Modulo arithmetic is slow!
if ((idx_in_block % (2 * i)) == 0)
buffer[idx_in_block] += buffer[idx_in_block + i];
barrier(CLK_LOCAL_MEM_FENCE);
}
// Export this block to global memory.
if (idx_in_block == 0)
outputs[get_group_id(0)] = buffer[0];
}
/**
* This version uses contiguous threads, but its interleaved
* addressing results in many shared memory bank conflicts.
*/
__kernel void reduce_in_shared(__global float const *inputs, __global float *outputs, ulong const n,
__local float *buffer) {
ulong const idx_in_block = get_local_id(0);
ulong const idx_global = get_global_id(0);
buffer[idx_in_block] = (idx_global < n) ? inputs[idx_global] : 0;
barrier(CLK_LOCAL_MEM_FENCE);
// Perform reduction in the shared memory..
for (ulong i = 1; i < get_local_size(0); i *= 2) {
ulong const index = 2 * i * idx_in_block;
if (index < get_local_size(0))
buffer[index] += buffer[index + i];
barrier(CLK_LOCAL_MEM_FENCE);
}
// Export this block to global memory.
if (idx_in_block == 0)
outputs[get_group_id(0)] = buffer[0];
}
/**
* This version uses sequential addressing.
* No divergence or bank conflicts.
*/
__kernel void reduce_w_sequential_addressing(__global float const *inputs, __global float *outputs, ulong const n,
__local float *buffer) {
ulong const idx_in_block = get_local_id(0);
ulong const idx_global = get_global_id(0);
buffer[idx_in_block] = (idx_global < n) ? inputs[idx_global] : 0;
barrier(CLK_LOCAL_MEM_FENCE);
// Do reduction in shared mem.
for (ulong i = get_local_size(0) / 2; i > 0; i >>= 1) {
if (idx_in_block < i)
buffer[idx_in_block] += buffer[idx_in_block + i];
barrier(CLK_LOCAL_MEM_FENCE);
}
// Write result for this block to global mem.
if (idx_in_block == 0)
outputs[get_group_id(0)] = buffer[0];
}
/**
* This version uses n/2 threads - it performs the first level
* of reduction when reading from global memory.
*/
__kernel void reduce_bi_step(__global float const *inputs, __global float *outputs, ulong const n,
__local float *buffer) {
ulong const idx_in_block = get_local_id(0);
ulong const idx_global = get_group_id(0) * (get_local_size(0) * 2) + get_local_id(0);
buffer[idx_in_block] = (idx_global < n) ? inputs[idx_global] : 0;
// Perform first level of reduction,
// reading from global memory,
// writing to shared memory.
if (idx_global + get_local_size(0) < n)
buffer[idx_in_block] += inputs[idx_global + get_local_size(0)];
barrier(CLK_LOCAL_MEM_FENCE);
// Perform reduction in the shared memory.
for (ulong i = get_local_size(0) / 2; i > 0; i >>= 1) {
if (idx_in_block < i)
buffer[idx_in_block] += buffer[idx_in_block + i];
barrier(CLK_LOCAL_MEM_FENCE);
}
// Export this block to global memory.
if (idx_in_block == 0)
outputs[get_group_id(0)] = buffer[0];
}
/**
* Unrolls the last warp to avoid synchronization where
* where its not needed.
*/
__kernel void reduce_unrolled(__global float const *inputs, __global float *outputs, ulong const n,
__local float *buffer) {
ulong const idx_in_block = get_local_id(0);
ulong const idx_global = get_group_id(0) * (get_local_size(0) * 2) + get_local_id(0);
ulong const block_size = get_local_size(0);
buffer[idx_in_block] = (idx_global < n) ? inputs[idx_global] : 0;
// Perform first level of reduction,
// reading from global memory,
// writing to shared memory.
if (idx_global + get_local_size(0) < n)
buffer[idx_in_block] += inputs[idx_global + get_local_size(0)];
barrier(CLK_LOCAL_MEM_FENCE);
// Perform reduction in the shared memory.
#pragma unroll 1
for (ulong i = get_local_size(0) / 2; i > 32; i >>= 1) {
if (idx_in_block < i)
buffer[idx_in_block] += buffer[idx_in_block + i];
barrier(CLK_LOCAL_MEM_FENCE);
}
if (idx_in_block < 32) {
if (block_size >= 64)
buffer[idx_in_block] += buffer[idx_in_block + 32];
if (block_size >= 32)
buffer[idx_in_block] += buffer[idx_in_block + 16];
if (block_size >= 16)
buffer[idx_in_block] += buffer[idx_in_block + 8];
if (block_size >= 8)
buffer[idx_in_block] += buffer[idx_in_block + 4];
if (block_size >= 4)
buffer[idx_in_block] += buffer[idx_in_block + 2];
if (block_size >= 2)
buffer[idx_in_block] += buffer[idx_in_block + 1];
}
// Export this block to global memory.
if (idx_in_block == 0)
outputs[get_group_id(0)] = buffer[0];
}
/**
* This version is completely unrolled. It uses a template parameter to achieve
* optimal code for any (power of 2) number of threads. This requires a switch
* statement in the host code to handle all the different thread block sizes at
* compile time.
*/
__kernel void reduce_unrolled_fully(__global float const *inputs, __global float *outputs, ulong const n,
__local float *buffer) {
ulong const idx_in_block = get_local_id(0);
ulong const idx_global = get_group_id(0) * (get_local_size(0) * 2) + get_local_id(0);
ulong const block_size = get_local_size(0);
buffer[idx_in_block] = (idx_global < n) ? inputs[idx_global] : 0;
// Perform first level of reduction,
// reading from global memory,
// writing to shared memory.
if (idx_global + block_size < n)
buffer[idx_in_block] += inputs[idx_global + block_size];
barrier(CLK_LOCAL_MEM_FENCE);
// Perform reduction in the shared memory.
if (block_size >= 512) {
if (idx_in_block < 256)
buffer[idx_in_block] += buffer[idx_in_block + 256];
barrier(CLK_LOCAL_MEM_FENCE);
}
if (block_size >= 256) {
if (idx_in_block < 128)
buffer[idx_in_block] += buffer[idx_in_block + 128];
barrier(CLK_LOCAL_MEM_FENCE);
}
if (block_size >= 128) {
if (idx_in_block < 64)
buffer[idx_in_block] += buffer[idx_in_block + 64];
barrier(CLK_LOCAL_MEM_FENCE);
}
if (idx_in_block < 32) {
if (block_size >= 64)
buffer[idx_in_block] += buffer[idx_in_block + 32];
if (block_size >= 32)
buffer[idx_in_block] += buffer[idx_in_block + 16];
if (block_size >= 16)
buffer[idx_in_block] += buffer[idx_in_block + 8];
if (block_size >= 8)
buffer[idx_in_block] += buffer[idx_in_block + 4];
if (block_size >= 4)
buffer[idx_in_block] += buffer[idx_in_block + 2];
if (block_size >= 2)
buffer[idx_in_block] += buffer[idx_in_block + 1];
}
// Export this block to global memory.
if (idx_in_block == 0)
outputs[get_group_id(0)] = buffer[0];
}
/**
* Uses Brent's Theorem optimization.
* This version adds multiple elements per thread sequentially.
* This reduces the overall cost of the algorithm while keeping
* the work complexity O(n) and the step complexity O(log n).
*/
__kernel void reduce_w_brents_theorem(__global float const *inputs, __global float *outputs, ulong const n,
__local float *buffer) {
ulong const block_size = get_local_size(0);
ulong const idx_in_block = get_local_id(0);
ulong idx_global = get_group_id(0) * (get_local_size(0) * 2) + get_local_id(0);
ulong const grid_size = block_size * 2 * get_num_groups(0);
buffer[idx_in_block] = (idx_global < n) ? inputs[idx_global] : 0;
// We reduce multiple elements per thread.
// The number is determined by the number of active thread blocks (via gridDim).
// More blocks will result in a larger grid_size and therefore fewer elements per thread.
while (idx_global < n) {
buffer[idx_in_block] += inputs[idx_global];
// Ensure we don't read out of bounds -- this is optimized away for powerOf2 sized arrays.
if (idx_global + block_size < n)
buffer[idx_in_block] += inputs[idx_global + block_size];
idx_global += grid_size;
}
barrier(CLK_LOCAL_MEM_FENCE);
// Perform reduction in the shared memory.
if (block_size >= 512) {
if (idx_in_block < 256)
buffer[idx_in_block] += buffer[idx_in_block + 256];
barrier(CLK_LOCAL_MEM_FENCE);
}
if (block_size >= 256) {
if (idx_in_block < 128)
buffer[idx_in_block] += buffer[idx_in_block + 128];
barrier(CLK_LOCAL_MEM_FENCE);
}
if (block_size >= 128) {
if (idx_in_block < 64)
buffer[idx_in_block] += buffer[idx_in_block + 64];
barrier(CLK_LOCAL_MEM_FENCE);
}
if (idx_in_block < 32) {
if (block_size >= 64)
buffer[idx_in_block] += buffer[idx_in_block + 32];
if (block_size >= 32)
buffer[idx_in_block] += buffer[idx_in_block + 16];
if (block_size >= 16)
buffer[idx_in_block] += buffer[idx_in_block + 8];
if (block_size >= 8)
buffer[idx_in_block] += buffer[idx_in_block + 4];
if (block_size >= 4)
buffer[idx_in_block] += buffer[idx_in_block + 2];
if (block_size >= 2)
buffer[idx_in_block] += buffer[idx_in_block + 1];
}
// Export this block to global memory.
if (idx_in_block == 0)
outputs[get_group_id(0)] = buffer[0];
}