-
Notifications
You must be signed in to change notification settings - Fork 25
/
LSHReservoirSampler_segsort.cl
566 lines (473 loc) · 19.5 KB
/
LSHReservoirSampler_segsort.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* Code adapted from MANNING - OpenCL in action tutorial - bitonic sort.
These kernels perform segmented sort.
*/
/* Sort elements within a vector */
#define VECTOR_SORT(input, dir) \
comp = input < shuffle(input, mask2) ^ dir; \
input = shuffle(input, as_uint4(comp * 2 + add2)); \
comp = input < shuffle(input, mask1) ^ dir; \
input = shuffle(input, as_uint4(comp + add1)); \
#define VECTOR_SORT_KV(input, input_v, dir) \
comp = input < shuffle(input, mask2) ^ dir; \
input = shuffle(input, as_uint4(comp * 2 + add2)); \
input_v = shuffle(input_v, as_uint4(comp * 2 + add2)); \
comp = input < shuffle(input, mask1) ^ dir; \
input = shuffle(input, as_uint4(comp + add1)); \
input_v = shuffle(input_v, as_uint4(comp + add1)); \
#define VECTOR_SWAP(input1, input2, dir) \
temp = input1; \
comp = (input1 < input2 ^ dir) * 4 + add3; \
input1 = shuffle2(input1, input2, as_uint4(comp)); \
input2 = shuffle2(input2, temp, as_uint4(comp)); \
#define VECTOR_SWAP_KV(input1, input2, input1_v, input2_v, dir) \
comp = (input1 < input2 ^ dir) * 4 + add3; \
temp = input1; \
input1 = shuffle2(input1, input2, as_uint4(comp)); \
input2 = shuffle2(input2, temp, as_uint4(comp)); \
temp_v = input1_v; \
input1_v = shuffle2(input1_v, input2_v, as_uint4(comp)); \
input2_v = shuffle2(input2_v, temp_v, as_uint4(comp)); \
/* Perform initial sort */
__kernel void bsort_preprocess_kv(__global uint4 *g_data, __global uint4 *g_data_v, uint valMax) {
uint id, global_start;
uint4 input1, input2, input1_v, input2_v;
id = get_local_id(0) * 2; // Each work-item sorts two 4-vectors, total of 8 elements.
global_start = get_group_id(0) * get_local_size(0) * 2 + id;
// Copy two vector-4 from the global memory.
input1 = g_data[global_start];
input2 = g_data[global_start + 1];
input1_v = g_data_v[global_start];
input2_v = g_data_v[global_start + 1];
input1.x = input1.x * valMax + input1_v.x;
input1.y = input1.y * valMax + input1_v.y;
input1.z = input1.z * valMax + input1_v.z;
input1.w = input1.w * valMax + input1_v.w;
input2.x = input2.x * valMax + input2_v.x;
input2.y = input2.y * valMax + input2_v.y;
input2.z = input2.z * valMax + input2_v.z;
input2.w = input2.w * valMax + input2_v.w;
g_data[global_start] = input1;
g_data[global_start + 1] = input2;
g_data_v[global_start] = input1_v;
g_data_v[global_start + 1] = input2_v;
}
__kernel void bsort_postprocess_kv(__global uint4 *g_data, __global uint4 *g_data_v, uint valMax) {
uint id, global_start;
uint4 input1, input2, input1_v, input2_v;
id = get_local_id(0) * 2; // Each work-item sorts two 4-vectors, total of 8 elements.
global_start = get_group_id(0) * get_local_size(0) * 2 + id;
// Copy two vector-4 from the global memory.
input1 = g_data[global_start];
input2 = g_data[global_start + 1];
input1_v = g_data_v[global_start];
input2_v = g_data_v[global_start + 1];
input1.x = (input1.x - input1_v.x) / valMax;
input1.y = (input1.y - input1_v.y) / valMax;
input1.z = (input1.z - input1_v.z) / valMax;
input1.w = (input1.w - input1_v.w) / valMax;
input2.x = (input2.x - input2_v.x) / valMax;
input2.y = (input2.y - input2_v.y) / valMax;
input2.z = (input2.z - input2_v.z) / valMax;
input2.w = (input2.w - input2_v.w) / valMax;
g_data[global_start] = input1;
g_data[global_start + 1] = input2;
g_data_v[global_start] = input1_v;
g_data_v[global_start + 1] = input2_v;
}
/* Perform initial sort */
__kernel void bsort_init_manning_kv(__global uint4 *g_data, __local uint4 *l_data,
__global uint4 *g_data_v, __local uint4 *l_data_v) {
int dir;
uint id, global_start, size, stride;
uint4 input1, input2, temp;
uint4 input1_v, input2_v, temp_v;
int4 comp;
uint4 mask1 = (uint4)(1, 0, 3, 2);
uint4 mask2 = (uint4)(2, 3, 0, 1);
uint4 mask3 = (uint4)(3, 2, 1, 0);
int4 add1 = (int4)(1, 1, 3, 3);
int4 add2 = (int4)(2, 3, 2, 3);
int4 add3 = (int4)(1, 2, 2, 3);
id = get_local_id(0) * 2; // Each work-item sorts two 4-vectors, total of 8 elements.
global_start = get_group_id(0) * get_local_size(0) * 2 + id;
// Copy two vector-4 from the global memory.
input1 = g_data[global_start];
input2 = g_data[global_start + 1];
input1_v = g_data_v[global_start];
input2_v = g_data_v[global_start + 1];
// printf("%u %u %u %u ", input1_v.x, input1_v.y, input1_v.z, input1_v.w);
/* Sort input 1 - ascending */ /* For, values, only shuffle as is, do not compare. */
comp = input1 < shuffle(input1, mask1);
input1 = shuffle(input1, as_uint4(comp + add1));
input1_v = shuffle(input1_v, as_uint4(comp + add1));
comp = input1 < shuffle(input1, mask2);
input1 = shuffle(input1, as_uint4(comp * 2 + add2));
input1_v = shuffle(input1_v, as_uint4(comp * 2 + add2));
comp = input1 < shuffle(input1, mask3);
input1 = shuffle(input1, as_uint4(comp + add3));
input1_v = shuffle(input1_v, as_uint4(comp + add3));
/* Sort input 2 - descending */
comp = input2 > shuffle(input2, mask1);
input2 = shuffle(input2, as_uint4(comp + add1));
input2_v = shuffle(input2_v, as_uint4(comp + add1));
comp = input2 > shuffle(input2, mask2);
input2 = shuffle(input2, as_uint4(comp * 2 + add2));
input2_v = shuffle(input2_v, as_uint4(comp * 2 + add2));
comp = input2 > shuffle(input2, mask3);
input2 = shuffle(input2, as_uint4(comp + add3));
input2_v = shuffle(input2_v, as_uint4(comp + add3));
/* Swap corresponding elements of input 1 and 2 */
add3 = (int4)(4, 5, 6, 7);
dir = get_local_id(0) % 2 * -1;
temp = input1;
comp = (input1 < input2 ^ dir) * 4 + add3;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
temp_v = input1_v;
input1_v = shuffle2(input1_v, input2_v, as_uint4(comp));
input2_v = shuffle2(input2_v, temp_v, as_uint4(comp));
/* Sort data and store in local memory */
VECTOR_SORT_KV(input1, input1_v, dir);
VECTOR_SORT_KV(input2, input2_v, dir);
l_data[id] = input1;
l_data[id + 1] = input2;
l_data_v[id] = input1_v;
l_data_v[id + 1] = input2_v;
/* Create bitonic set */
// Outer stages.
for (size = 2; size < get_local_size(0); size <<= 1) {
dir = (get_local_id(0) / size & 1) * -1;
// Inner stages.
for (stride = size; stride > 1; stride >>= 1) {
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) + (get_local_id(0) / stride)*stride;
VECTOR_SWAP_KV(l_data[id], l_data[id + stride], l_data_v[id], l_data_v[id + stride], dir)
}
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) * 2;
input1 = l_data[id]; input2 = l_data[id + 1];
input1_v = l_data_v[id]; input2_v = l_data_v[id + 1];
comp = (input1 < input2 ^ dir) * 4 + add3;
temp = input1;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
temp_v = input1_v;
input1_v = shuffle2(input1_v, input2_v, as_uint4(comp));
input2_v = shuffle2(input2_v, temp_v, as_uint4(comp));
VECTOR_SORT_KV(input1, input1_v, dir);
VECTOR_SORT_KV(input2, input2_v, dir);
l_data[id] = input1;
l_data[id + 1] = input2;
l_data_v[id] = input1_v;
l_data_v[id + 1] = input2_v;
}
/* Perform bitonic merge */
dir = (get_group_id(0) % 2) * -1;
for (stride = get_local_size(0); stride > 1; stride >>= 1) {
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) + (get_local_id(0) / stride)*stride;
VECTOR_SWAP_KV(l_data[id], l_data[id + stride], l_data_v[id], l_data_v[id + stride], dir)
}
barrier(CLK_LOCAL_MEM_FENCE);
/* Perform final sort */
id = get_local_id(0) * 2;
input1 = l_data[id]; input2 = l_data[id + 1];
input1_v = l_data_v[id]; input2_v = l_data_v[id + 1];
comp = (input1 < input2 ^ dir) * 4 + add3;
temp = input1;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
temp_v = input1_v;
input1_v = shuffle2(input1_v, input2_v, as_uint4(comp));
input2_v = shuffle2(input2_v, temp_v, as_uint4(comp));
VECTOR_SORT_KV(input1, input1_v, dir);
VECTOR_SORT_KV(input2, input2_v, dir);
g_data[global_start] = input1;
g_data[global_start + 1] = input2;
g_data_v[global_start] = input1_v;
g_data_v[global_start + 1] = input2_v;
}
/* Perform initial sort */
__kernel void bsort_init_manning(__global uint4 *g_data, __local uint4 *l_data) {
int dir;
uint id, global_start, size, stride;
uint4 input1, input2, temp;
int4 comp;
uint4 mask1 = (uint4)(1, 0, 3, 2);
uint4 mask2 = (uint4)(2, 3, 0, 1);
uint4 mask3 = (uint4)(3, 2, 1, 0);
int4 add1 = (int4)(1, 1, 3, 3);
int4 add2 = (int4)(2, 3, 2, 3);
int4 add3 = (int4)(1, 2, 2, 3);
id = get_local_id(0) * 2; // Each work-item sorts two 4-vectors, total of 8 elements.
global_start = get_group_id(0) * get_local_size(0) * 2 + id;
// Copy two vector-4 from the global memory.
input1 = g_data[global_start];
input2 = g_data[global_start + 1];
/* Sort input 1 - ascending */
comp = input1 < shuffle(input1, mask1);
input1 = shuffle(input1, as_uint4(comp + add1));
comp = input1 < shuffle(input1, mask2);
input1 = shuffle(input1, as_uint4(comp * 2 + add2));
comp = input1 < shuffle(input1, mask3);
input1 = shuffle(input1, as_uint4(comp + add3));
/* Sort input 2 - descending */
comp = input2 > shuffle(input2, mask1);
input2 = shuffle(input2, as_uint4(comp + add1));
comp = input2 > shuffle(input2, mask2);
input2 = shuffle(input2, as_uint4(comp * 2 + add2));
comp = input2 > shuffle(input2, mask3);
input2 = shuffle(input2, as_uint4(comp + add3));
/* Swap corresponding elements of input 1 and 2 */
add3 = (int4)(4, 5, 6, 7);
dir = get_local_id(0) % 2 * -1;
temp = input1;
comp = (input1 < input2 ^ dir) * 4 + add3;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
/* Sort data and store in local memory */
VECTOR_SORT(input1, dir);
VECTOR_SORT(input2, dir);
l_data[id] = input1;
l_data[id + 1] = input2;
/* Create bitonic set */
// Outer stages.
for (size = 2; size < get_local_size(0); size <<= 1) {
dir = (get_local_id(0) / size & 1) * -1;
// Inner stages.
for (stride = size; stride > 1; stride >>= 1) {
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) + (get_local_id(0) / stride)*stride;
VECTOR_SWAP(l_data[id], l_data[id + stride], dir)
}
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) * 2;
input1 = l_data[id]; input2 = l_data[id + 1];
temp = input1;
comp = (input1 < input2 ^ dir) * 4 + add3;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
VECTOR_SORT(input1, dir);
VECTOR_SORT(input2, dir);
l_data[id] = input1;
l_data[id + 1] = input2;
}
/* Perform bitonic merge */
dir = (get_group_id(0) % 2) * -1;
for (stride = get_local_size(0); stride > 1; stride >>= 1) {
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) + (get_local_id(0) / stride)*stride;
VECTOR_SWAP(l_data[id], l_data[id + stride], dir)
}
barrier(CLK_LOCAL_MEM_FENCE);
/* Perform final sort */
id = get_local_id(0) * 2;
input1 = l_data[id]; input2 = l_data[id + 1];
temp = input1;
comp = (input1 < input2 ^ dir) * 4 + add3;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
VECTOR_SORT(input1, dir);
VECTOR_SORT(input2, dir);
g_data[global_start] = input1;
g_data[global_start + 1] = input2;
}
__kernel void bsort_stage_0_manning_kv(__global uint4 *g_data, __local uint4 *l_data,
__global uint4 *g_data_v, __local uint4 *l_data_v,
uint high_stage) {
int dir;
uint id, global_start, stride;
uint4 input1, input2, temp, input1_v, input2_v, temp_v;
int4 comp;
uint4 mask1 = (uint4)(1, 0, 3, 2);
uint4 mask2 = (uint4)(2, 3, 0, 1);
uint4 mask3 = (uint4)(3, 2, 1, 0);
int4 add1 = (int4)(1, 1, 3, 3);
int4 add2 = (int4)(2, 3, 2, 3);
int4 add3 = (int4)(4, 5, 6, 7);
/* Determine data location in global memory */
id = get_local_id(0);
dir = (get_group_id(0) / high_stage & 1) * -1;
global_start = get_group_id(0) * get_local_size(0) * 2 + id;
/* Perform initial swap */
input1 = g_data[global_start];
input2 = g_data[global_start + get_local_size(0)];
input1_v = g_data_v[global_start];
input2_v = g_data_v[global_start + get_local_size(0)];
comp = (input1 < input2 ^ dir) * 4 + add3;
l_data[id] = shuffle2(input1, input2, as_uint4(comp));
l_data[id + get_local_size(0)] = shuffle2(input2, input1, as_uint4(comp));
l_data_v[id] = shuffle2(input1_v, input2_v, as_uint4(comp));
l_data_v[id + get_local_size(0)] = shuffle2(input2_v, input1_v, as_uint4(comp));
/* Perform bitonic merge */
for (stride = get_local_size(0) / 2; stride > 1; stride >>= 1) {
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) + (get_local_id(0) / stride)*stride;
VECTOR_SWAP_KV(l_data[id], l_data[id + stride],
l_data_v[id], l_data_v[id + stride], dir)
}
barrier(CLK_LOCAL_MEM_FENCE);
/* Perform final sort */
id = get_local_id(0) * 2;
input1 = l_data[id]; input2 = l_data[id + 1];
input1_v = l_data_v[id]; input2_v = l_data_v[id + 1];
temp = input1;
comp = (input1 < input2 ^ dir) * 4 + add3;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
temp_v = input1_v;
input1_v = shuffle2(input1_v, input2_v, as_uint4(comp));
input2_v = shuffle2(input2_v, temp_v, as_uint4(comp));
VECTOR_SORT_KV(input1, input1_v, dir);
VECTOR_SORT_KV(input2, input2_v, dir);
/* Store output in global memory */
g_data[global_start + get_local_id(0)] = input1;
g_data[global_start + get_local_id(0) + 1] = input2;
g_data_v[global_start + get_local_id(0)] = input1_v;
g_data_v[global_start + get_local_id(0) + 1] = input2_v;
}
/* Perform lowest stage of the bitonic sort */
__kernel void bsort_stage_0_manning(__global uint4 *g_data, __local uint4 *l_data,
uint high_stage) {
int dir;
uint id, global_start, stride;
uint4 input1, input2, temp;
int4 comp;
uint4 mask1 = (uint4)(1, 0, 3, 2);
uint4 mask2 = (uint4)(2, 3, 0, 1);
uint4 mask3 = (uint4)(3, 2, 1, 0);
int4 add1 = (int4)(1, 1, 3, 3);
int4 add2 = (int4)(2, 3, 2, 3);
int4 add3 = (int4)(4, 5, 6, 7);
/* Determine data location in global memory */
id = get_local_id(0);
dir = (get_group_id(0) / high_stage & 1) * -1;
global_start = get_group_id(0) * get_local_size(0) * 2 + id;
/* Perform initial swap */
input1 = g_data[global_start];
input2 = g_data[global_start + get_local_size(0)];
comp = (input1 < input2 ^ dir) * 4 + add3;
l_data[id] = shuffle2(input1, input2, as_uint4(comp));
l_data[id + get_local_size(0)] = shuffle2(input2, input1, as_uint4(comp));
/* Perform bitonic merge */
for (stride = get_local_size(0) / 2; stride > 1; stride >>= 1) {
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) + (get_local_id(0) / stride)*stride;
VECTOR_SWAP(l_data[id], l_data[id + stride], dir)
}
barrier(CLK_LOCAL_MEM_FENCE);
/* Perform final sort */
id = get_local_id(0) * 2;
input1 = l_data[id]; input2 = l_data[id + 1];
temp = input1;
comp = (input1 < input2 ^ dir) * 4 + add3;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
VECTOR_SORT(input1, dir);
VECTOR_SORT(input2, dir);
/* Store output in global memory */
g_data[global_start + get_local_id(0)] = input1;
g_data[global_start + get_local_id(0) + 1] = input2;
}
/* Perform successive stages of the bitonic sort */
__kernel void bsort_stage_n_manning_kv(__global uint4 *g_data, __local uint4 *l_data,
__global uint4 *g_data_v, __local uint4 *l_data_v,
uint stage, uint high_stage) {
int dir;
uint4 input1, input2, input1_v, input2_v;
int4 comp, add;
uint global_start, global_offset;
add = (int4)(4, 5, 6, 7);
/* Determine location of data in global memory */
dir = (get_group_id(0) / high_stage & 1) * -1;
global_start = (get_group_id(0) + (get_group_id(0) / stage)*stage) *
get_local_size(0) + get_local_id(0);
global_offset = stage * get_local_size(0);
/* Perform swap */
input1 = g_data[global_start];
input2 = g_data[global_start + global_offset];
input1_v = g_data_v[global_start];
input2_v = g_data_v[global_start + global_offset];
comp = (input1 < input2 ^ dir) * 4 + add;
g_data[global_start] = shuffle2(input1, input2, as_uint4(comp));
g_data_v[global_start] = shuffle2(input1_v, input2_v, as_uint4(comp));
g_data[global_start + global_offset] = shuffle2(input2, input1, as_uint4(comp));
g_data_v[global_start + global_offset] = shuffle2(input2_v, input1_v, as_uint4(comp));
}
/* Perform successive stages of the bitonic sort */
__kernel void bsort_stage_n_manning(__global uint4 *g_data, __local uint4 *l_data,
uint stage, uint high_stage) {
int dir;
uint4 input1, input2;
int4 comp, add;
uint global_start, global_offset;
add = (int4)(4, 5, 6, 7);
/* Determine location of data in global memory */
dir = (get_group_id(0) / high_stage & 1) * -1;
global_start = (get_group_id(0) + (get_group_id(0) / stage)*stage) *
get_local_size(0) + get_local_id(0);
global_offset = stage * get_local_size(0);
/* Perform swap */
input1 = g_data[global_start];
input2 = g_data[global_start + global_offset];
comp = (input1 < input2 ^ dir) * 4 + add;
g_data[global_start] = shuffle2(input1, input2, as_uint4(comp));
g_data[global_start + global_offset] = shuffle2(input2, input1, as_uint4(comp));
}
/* Sort the bitonic set */
__kernel void bsort_merge_manning(__global uint4 *g_data, __local uint4 *l_data, uint stage, int dir) {
uint4 input1, input2;
int4 comp, add;
uint global_start, global_offset;
add = (int4)(4, 5, 6, 7);
/* Determine location of data in global memory */
global_start = (get_group_id(0) + (get_group_id(0) / stage)*stage) *
get_local_size(0) + get_local_id(0);
global_offset = stage * get_local_size(0);
/* Perform swap */
input1 = g_data[global_start];
input2 = g_data[global_start + global_offset];
comp = (input1 < input2 ^ dir) * 4 + add;
g_data[global_start] = shuffle2(input1, input2, as_uint4(comp));
g_data[global_start + global_offset] = shuffle2(input2, input1, as_uint4(comp));
}
/* Perform final step of the bitonic merge */
__kernel void bsort_merge_last_manning(__global uint4 *g_data, __local uint4 *l_data, int dir) {
uint id, global_start, stride;
uint4 input1, input2, temp;
int4 comp;
uint4 mask1 = (uint4)(1, 0, 3, 2);
uint4 mask2 = (uint4)(2, 3, 0, 1);
uint4 mask3 = (uint4)(3, 2, 1, 0);
int4 add1 = (int4)(1, 1, 3, 3);
int4 add2 = (int4)(2, 3, 2, 3);
int4 add3 = (int4)(4, 5, 6, 7);
/* Determine location of data in global memory */
id = get_local_id(0);
global_start = get_group_id(0) * get_local_size(0) * 2 + id;
/* Perform initial swap */
input1 = g_data[global_start];
input2 = g_data[global_start + get_local_size(0)];
comp = (input1 < input2 ^ dir) * 4 + add3;
l_data[id] = shuffle2(input1, input2, as_uint4(comp));
l_data[id + get_local_size(0)] = shuffle2(input2, input1, as_uint4(comp));
/* Perform bitonic merge */
for (stride = get_local_size(0) / 2; stride > 1; stride >>= 1) {
barrier(CLK_LOCAL_MEM_FENCE);
id = get_local_id(0) + (get_local_id(0) / stride)*stride;
VECTOR_SWAP(l_data[id], l_data[id + stride], dir)
}
barrier(CLK_LOCAL_MEM_FENCE);
/* Perform final sort */
id = get_local_id(0) * 2;
input1 = l_data[id]; input2 = l_data[id + 1];
temp = input1;
comp = (input1 < input2 ^ dir) * 4 + add3;
input1 = shuffle2(input1, input2, as_uint4(comp));
input2 = shuffle2(input2, temp, as_uint4(comp));
VECTOR_SORT(input1, dir);
VECTOR_SORT(input2, dir);
/* Store the result to global memory */
g_data[global_start + get_local_id(0)] = input1;
g_data[global_start + get_local_id(0) + 1] = input2;
}