forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUCommonAlgorithm.h
More file actions
461 lines (394 loc) · 10.3 KB
/
GPUCommonAlgorithm.h
File metadata and controls
461 lines (394 loc) · 10.3 KB
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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file GPUCommonAlgorithm.h
/// \author David Rohr
#ifndef GPUCOMMONALGORITHM_H
#define GPUCOMMONALGORITHM_H
#include "GPUCommonDef.h"
#if !defined(GPUCA_GPUCODE) // Could also enable custom search on the CPU, but it is not always faster, so we stick to std::sort
#include <algorithm>
#define GPUCA_ALGORITHM_STD
#endif
// ----------------------------- SORTING -----------------------------
namespace o2
{
namespace gpu
{
class GPUCommonAlgorithm
{
public:
template <class T>
GPUd() static void sort(T* begin, T* end);
template <class T>
GPUd() static void sortInBlock(T* begin, T* end);
template <class T>
GPUd() static void sortDeviceDynamic(T* begin, T* end);
template <class T, class S>
GPUd() static void sort(T* begin, T* end, const S& comp);
template <class T, class S>
GPUd() static void sortInBlock(T* begin, T* end, const S& comp);
template <class T, class S>
GPUd() static void sortDeviceDynamic(T* begin, T* end, const S& comp);
template <class T>
GPUd() static void swap(T& a, T& b);
private:
// Quicksort implementation
template <typename I>
GPUd() static void QuickSort(I f, I l) noexcept;
// Quicksort implementation
template <typename I, typename Cmp>
GPUd() static void QuickSort(I f, I l, Cmp cmp) noexcept;
// Insertionsort implementation
template <typename I, typename Cmp>
GPUd() static void InsertionSort(I f, I l, Cmp cmp) noexcept;
// Helper for Quicksort implementation
template <typename I, typename Cmp>
GPUd() static I MedianOf3Select(I f, I l, Cmp cmp) noexcept;
// Helper for Quicksort implementation
template <typename I, typename T, typename Cmp>
GPUd() static I UnguardedPartition(I f, I l, T piv, Cmp cmp) noexcept;
// Helper
template <typename I>
GPUd() static void IterSwap(I a, I b) noexcept;
};
} // namespace gpu
} // namespace o2
namespace o2
{
namespace gpu
{
#ifndef GPUCA_ALGORITHM_STD
template <typename I>
GPUdi() void GPUCommonAlgorithm::IterSwap(I a, I b) noexcept
{
auto tmp = *a;
*a = *b;
*b = tmp;
}
template <typename I, typename Cmp>
GPUdi() void GPUCommonAlgorithm::InsertionSort(I f, I l, Cmp cmp) noexcept
{
auto it0{f};
while (it0 != l) {
auto tmp{*it0};
auto it1{it0};
while (it1 != f && cmp(tmp, it1[-1])) {
it1[0] = it1[-1];
--it1;
}
it1[0] = tmp;
++it0;
}
}
template <typename I, typename Cmp>
GPUdi() I GPUCommonAlgorithm::MedianOf3Select(I f, I l, Cmp cmp) noexcept
{
auto m = f + (l - f) / 2;
--l;
if (cmp(*f, *m)) {
if (cmp(*m, *l)) {
return m;
} else if (cmp(*f, *l)) {
return l;
} else {
return f;
}
} else if (cmp(*f, *l)) {
return f;
} else if (cmp(*m, *l)) {
return l;
} else {
return m;
}
}
template <typename I, typename T, typename Cmp>
GPUdi() I GPUCommonAlgorithm::UnguardedPartition(I f, I l, T piv, Cmp cmp) noexcept
{
do {
while (cmp(*f, piv)) {
++f;
}
--l;
while (cmp(piv, *l)) {
--l;
}
if (l <= f) {
return f;
}
IterSwap(f, l);
++f;
} while (true);
}
template <typename I, typename Cmp>
GPUdi() void GPUCommonAlgorithm::QuickSort(I f, I l, Cmp cmp) noexcept
{
if (f == l) {
return;
}
using IndexType = uint16_t;
struct pair {
IndexType first;
IndexType second;
};
struct Stack {
pair data[11];
uint8_t n{0};
GPUd() void emplace(IndexType x, IndexType y)
{
data[n++] = {x, y};
}
GPUd() bool empty() const { return n == 0; }
GPUd() pair& top() { return data[n - 1]; }
GPUd() void pop() { --n; }
};
Stack s;
s.emplace(0, l - f);
while (!s.empty()) {
const auto it0 = f + s.top().first;
const auto it1 = f + s.top().second;
s.pop();
const auto piv = *MedianOf3Select(it0, it1, cmp);
const auto pp = UnguardedPartition(it0, it1, piv, cmp);
constexpr auto cutoff = 50u;
const auto lsz = pp - it0;
const auto rsz = it1 - pp;
if (lsz < rsz) {
if (rsz > cutoff) {
s.emplace(pp - f, it1 - f);
}
if (lsz > cutoff) {
s.emplace(it0 - f, pp - f);
}
} else {
if (lsz > cutoff) {
s.emplace(it0 - f, pp - f);
}
if (rsz > cutoff) {
s.emplace(pp - f, it1 - f);
}
}
}
InsertionSort(f, l, cmp);
}
template <typename I>
GPUdi() void GPUCommonAlgorithm::QuickSort(I f, I l) noexcept
{
QuickSort(f, l, [](auto&& x, auto&& y) { return x < y; });
}
#endif
typedef GPUCommonAlgorithm CAAlgo;
} // namespace gpu
} // namespace o2
#if (((defined(__CUDACC__) && !defined(__clang__)) || defined(__HIPCC__))) && !defined(GPUCA_GPUCODE_GENRTC) && !defined(GPUCA_GPUCODE_HOSTONLY)
#include "GPUCommonAlgorithmThrust.h"
#else
namespace o2
{
namespace gpu
{
template <class T>
GPUdi() void GPUCommonAlgorithm::sortDeviceDynamic(T* begin, T* end)
{
#ifndef GPUCA_GPUCODE
GPUCommonAlgorithm::sort(begin, end);
#else
GPUCommonAlgorithm::sortDeviceDynamic(begin, end, [](auto&& x, auto&& y) { return x < y; });
#endif
}
template <class T, class S>
GPUdi() void GPUCommonAlgorithm::sortDeviceDynamic(T* begin, T* end, const S& comp)
{
GPUCommonAlgorithm::sort(begin, end, comp);
}
} // namespace gpu
} // namespace o2
#endif // THRUST
// sort and sortInBlock below are not taken from Thrust, since our implementations are faster
namespace o2
{
namespace gpu
{
template <class T>
GPUdi() void GPUCommonAlgorithm::sort(T* begin, T* end)
{
#ifdef GPUCA_ALGORITHM_STD
std::sort(begin, end);
#else
QuickSort(begin, end, [](auto&& x, auto&& y) { return x < y; });
#endif
}
template <class T, class S>
GPUdi() void GPUCommonAlgorithm::sort(T* begin, T* end, const S& comp)
{
#ifdef GPUCA_ALGORITHM_STD
std::sort(begin, end, comp);
#else
QuickSort(begin, end, comp);
#endif
}
template <class T>
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end)
{
#ifndef GPUCA_GPUCODE
GPUCommonAlgorithm::sort(begin, end);
#else
GPUCommonAlgorithm::sortInBlock(begin, end, [](auto&& x, auto&& y) { return x < y; });
#endif
}
template <class T, class S>
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end, const S& comp)
{
#ifndef GPUCA_GPUCODE
GPUCommonAlgorithm::sort(begin, end, comp);
#else
int32_t n = end - begin;
for (int32_t i = 0; i < n; i++) {
for (int32_t tIdx = get_local_id(0); tIdx < n; tIdx += get_local_size(0)) {
int32_t offset = i % 2;
int32_t curPos = 2 * tIdx + offset;
int32_t nextPos = curPos + 1;
if (nextPos < n) {
if (!comp(begin[curPos], begin[nextPos])) {
IterSwap(&begin[curPos], &begin[nextPos]);
}
}
}
GPUbarrier();
}
#endif
}
#ifdef GPUCA_GPUCODE
template <class T>
GPUdi() void GPUCommonAlgorithm::swap(T& a, T& b)
{
auto tmp = a;
a = b;
b = tmp;
}
#else
template <class T>
GPUdi() void GPUCommonAlgorithm::swap(T& a, T& b)
{
std::swap(a, b);
}
#endif
} // namespace gpu
} // namespace o2
// ----------------------------- WORK GROUP FUNCTIONS -----------------------------
#ifdef __OPENCL__
// Nothing to do, work_group functions available
#pragma OPENCL EXTENSION cl_khr_subgroups : enable
template <class T>
GPUdi() T work_group_scan_inclusive_add_FUNC(T v)
{
return sub_group_scan_inclusive_add(v);
}
template <> // FIXME: It seems OpenCL does not support 8 and 16 bit subgroup operations
GPUdi() uint8_t work_group_scan_inclusive_add_FUNC<uint8_t>(uint8_t v)
{
return sub_group_scan_inclusive_add((uint32_t)v);
}
template <class T>
GPUdi() T work_group_broadcast_FUNC(T v, int32_t i)
{
return sub_group_broadcast(v, i);
}
template <>
GPUdi() uint8_t work_group_broadcast_FUNC<uint8_t>(uint8_t v, int32_t i)
{
return sub_group_broadcast((uint32_t)v, i);
}
#define warp_scan_inclusive_add(v) work_group_scan_inclusive_add_FUNC(v)
#define warp_broadcast(v, i) work_group_broadcast_FUNC(v, i)
#elif (defined(__CUDACC__) || defined(__HIPCC__))
// CUDA and HIP work the same way using cub, need just different header
#if !defined(GPUCA_GPUCODE_COMPILEKERNELS) && !defined(GPUCA_GPUCODE_HOSTONLY)
#if defined(__CUDACC__)
#include <cub/cub.cuh>
#elif defined(__HIPCC__)
#include <hipcub/hipcub.hpp>
#endif
#endif
#define work_group_scan_inclusive_add(v) work_group_scan_inclusive_add_FUNC(v, smem)
template <class T, class S>
GPUdi() T work_group_scan_inclusive_add_FUNC(T v, S& smem)
{
typename S::BlockScan(smem.cubTmpMem).InclusiveSum(v, v);
__syncthreads();
return v;
}
#define work_group_broadcast(v, i) work_group_broadcast_FUNC(v, i, smem)
template <class T, class S>
GPUdi() T work_group_broadcast_FUNC(T v, int32_t i, S& smem)
{
if ((int32_t)threadIdx.x == i) {
smem.tmpBroadcast = v;
}
__syncthreads();
T retVal = smem.tmpBroadcast;
__syncthreads();
return retVal;
}
#define work_group_reduce_add(v) work_group_reduce_add_FUNC(v, smem)
template <class T, class S>
GPUdi() T work_group_reduce_add_FUNC(T v, S& smem)
{
v = typename S::BlockReduce(smem.cubReduceTmpMem).Sum(v);
__syncthreads();
v = work_group_broadcast(v, 0);
return v;
}
#define warp_scan_inclusive_add(v) warp_scan_inclusive_add_FUNC(v, smem)
template <class T, class S>
GPUdi() T warp_scan_inclusive_add_FUNC(T v, S& smem)
{
typename S::WarpScan(smem.cubWarpTmpMem).InclusiveSum(v, v);
return v;
}
#define warp_broadcast(v, i) warp_broadcast_FUNC(v, i)
template <class T>
GPUdi() T warp_broadcast_FUNC(T v, int32_t i)
{
#ifdef __CUDACC__
return __shfl_sync(0xFFFFFFFF, v, i);
#else // HIP
return __shfl(v, i);
#endif
}
#else
// Trivial implementation for the CPU
template <class T>
GPUdi() T work_group_scan_inclusive_add(T v)
{
return v;
}
template <class T>
GPUdi() T work_group_reduce_add(T v)
{
return v;
}
template <class T>
GPUdi() T work_group_broadcast(T v, int32_t i)
{
return v;
}
template <class T>
GPUdi() T warp_scan_inclusive_add(T v)
{
return v;
}
template <class T>
GPUdi() T warp_broadcast(T v, int32_t i)
{
return v;
}
#endif
#endif