This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
sort.hpp
338 lines (288 loc) · 10.8 KB
/
sort.hpp
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
/* Copyright (c) 2015-2018 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and/or associated documentation files (the
"Materials"), to deal in the Materials without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Materials, and to
permit persons to whom the Materials are furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Materials.
MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
https://www.khronos.org/registry/
THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#ifndef __SYCL_IMPL_ALGORITHM_SORT__
#define __SYCL_IMPL_ALGORITHM_SORT__
#include <type_traits>
#include <typeinfo>
#include <algorithm>
/** sort_kernel_bitonic.
* Class used to name the bitonic kernel sort per type.
*/
template <typename T>
class sort_kernel_bitonic;
/** sequential_sort_name.
* Class used to name the bitonic kernel sort per type.
*/
template <typename T>
class sequential_sort_name {
T userGivenKernelName;
};
/** bitonic_sort_name.
* Class used to name the bitonic kernel sort per type.
*/
template <typename T>
class bitonic_sort_name {
T userGivenKernelName;
};
/* sort_swap.
* Basic simple swap used inside the sort functions.
*/
template <typename T>
void sort_swap(T &lhs, T &rhs) {
auto temp = rhs;
rhs = lhs;
lhs = temp;
}
/* sort_kernel_sequential.
* Simple kernel to sequentially sort a vector
*/
template <typename T>
class sort_kernel_sequential {
/* Aliases for SYCL accessors */
using sycl_rw_acc =
cl::sycl::accessor<T, 1, cl::sycl::access::mode::read_write,
cl::sycl::access::target::global_buffer>;
sycl_rw_acc a_;
size_t vS_;
public:
sort_kernel_sequential(sycl_rw_acc a, size_t vectorSize)
: a_(a), vS_(vectorSize){};
// Simple sequential sort
void operator()() {
for (size_t i = 0; i < vS_; i++) {
for (size_t j = 1; j < vS_; j++) {
if (a_[j - 1] > a_[j]) {
sort_swap<T>(a_[j - 1], a_[j]);
}
}
}
}
}; // class sort_kernel
/* sort_kernel_sequential.
* Simple kernel to sequentially sort a vector
*/
template <typename T, class ComparableOperator>
class sort_kernel_sequential_comp {
/* Aliases for SYCL accessors */
using sycl_rw_acc =
cl::sycl::accessor<T, 1, cl::sycl::access::mode::read_write,
cl::sycl::access::target::global_buffer>;
sycl_rw_acc a_;
size_t vS_;
ComparableOperator comp_;
public:
sort_kernel_sequential_comp(sycl_rw_acc a, size_t vectorSize,
ComparableOperator comp)
: a_(a), vS_(vectorSize), comp_(comp){};
// Simple sequential sort
void operator()() {
for (size_t i = 0; i < vS_; i++) {
for (size_t j = 1; j < vS_; j++) {
if (comp_(a_[j - 1], a_[j])) {
sort_swap<T>(a_[j - 1], a_[j]);
}
}
}
}
}; // class sort_kernel
namespace sycl {
namespace impl {
/* Aliases for SYCL accessors */
template <typename T>
using sycl_rw_acc = cl::sycl::accessor<T, 1, cl::sycl::access::mode::read_write,
cl::sycl::access::target::global_buffer>;
/** isPowerOfTwo.
* Quick check to ensure num is a power of two.
* Will only work with integers.
* @return true if num is power of two
*/
template <typename T>
inline bool isPowerOfTwo(T num) {
return (num != 0) && !(num & (num - 1));
}
template <>
inline bool isPowerOfTwo<float>(float num) = delete;
template <>
inline bool isPowerOfTwo<double>(double num) = delete;
/** sequential_sort.
* Command group to call the sequential sort kernel */
template <typename T, typename Alloc>
void sequential_sort(cl::sycl::queue q, cl::sycl::buffer<T, 1, Alloc> buf,
size_t vectorSize) {
auto f = [buf, vectorSize](cl::sycl::handler &h) mutable {
auto a = buf.template get_access<cl::sycl::access::mode::read_write>(h);
h.single_task(sort_kernel_sequential<T>(a, vectorSize));
};
q.submit(f);
}
/** sequential_sort.
* Command group to call the sequential sort kernel */
template <typename T, typename Alloc, class ComparableOperator, typename Name>
void sequential_sort(cl::sycl::queue q, cl::sycl::buffer<T, 1, Alloc> buf,
size_t vectorSize, ComparableOperator comp) {
auto f = [buf, vectorSize, comp](cl::sycl::handler &h) mutable {
auto a = buf.template get_access<cl::sycl::access::mode::read_write>(h);
h.single_task<Name>(sort_kernel_sequential_comp<T, ComparableOperator>(
a, vectorSize, comp));
};
q.submit(f);
}
/* bitonic_sort.
* Performs a bitonic sort on the given buffer
*/
template <typename T, typename Alloc>
void bitonic_sort(cl::sycl::queue q, cl::sycl::buffer<T, 1, Alloc> buf,
size_t vectorSize) {
int numStages = 0;
// 2^numStages should be equal to length
// i.e number of times you halve the lenght to get 1 should be numStages
for (int tmp = vectorSize; tmp > 1; tmp >>= 1) {
++numStages;
}
cl::sycl::range<1> r{vectorSize / 2};
for (int stage = 0; stage < numStages; ++stage) {
// Every stage has stage + 1 passes
for (int passOfStage = 0; passOfStage < stage + 1; ++passOfStage) {
auto f = [=](cl::sycl::handler &h) mutable {
auto a = buf.template get_access<cl::sycl::access::mode::read_write>(h);
h.parallel_for<sort_kernel_bitonic<T>>(
cl::sycl::range<1>{r},
[a, stage, passOfStage](cl::sycl::item<1> it) {
int sortIncreasing = 1;
cl::sycl::id<1> id = it.get_id();
int threadId = id.get(0);
int pairDistance = 1 << (stage - passOfStage);
int blockWidth = 2 * pairDistance;
int leftId = (threadId % pairDistance) +
(threadId / pairDistance) * blockWidth;
int rightId = leftId + pairDistance;
T leftElement = a[leftId];
T rightElement = a[rightId];
int sameDirectionBlockWidth = 1 << stage;
if ((threadId / sameDirectionBlockWidth) % 2 == 1) {
sortIncreasing = 1 - sortIncreasing;
}
T greater;
T lesser;
if (leftElement > rightElement) {
greater = leftElement;
lesser = rightElement;
} else {
greater = rightElement;
lesser = leftElement;
}
a[leftId] = sortIncreasing ? lesser : greater;
a[rightId] = sortIncreasing ? greater : lesser;
});
}; // command group functor
q.submit(f);
} // passStage
} // stage
} // bitonic_sort
/* bitonic_sort.
* Performs a bitonic sort on the given buffer
*/
template <typename T, typename Alloc, class ComparableOperator, typename Name>
void bitonic_sort(cl::sycl::queue q, cl::sycl::buffer<T, 1, Alloc> buf,
size_t vectorSize, ComparableOperator comp) {
int numStages = 0;
// 2^numStages should be equal to length
// i.e number of times you halve the lenght to get 1 should be numStages
for (int tmp = vectorSize; tmp > 1; tmp >>= 1) {
++numStages;
}
cl::sycl::range<1> r{vectorSize / 2};
for (int stage = 0; stage < numStages; ++stage) {
// Every stage has stage + 1 passes
for (int passOfStage = 0; passOfStage < stage + 1; ++passOfStage) {
auto f = [=](cl::sycl::handler &h) mutable {
auto a = buf.template get_access<cl::sycl::access::mode::read_write>(h);
h.parallel_for<Name>(
cl::sycl::range<1>{r},
[a, stage, passOfStage, comp](cl::sycl::item<1> it) {
int sortIncreasing = 1;
cl::sycl::id<1> id = it.get_id();
int threadId = id.get(0);
int pairDistance = 1 << (stage - passOfStage);
int blockWidth = 2 * pairDistance;
int leftId = (threadId % pairDistance) +
(threadId / pairDistance) * blockWidth;
int rightId = leftId + pairDistance;
T leftElement = a[leftId];
T rightElement = a[rightId];
int sameDirectionBlockWidth = 1 << stage;
if ((threadId / sameDirectionBlockWidth) % 2 == 1) {
sortIncreasing = 1 - sortIncreasing;
}
T greater;
T lesser;
if (comp(leftElement, rightElement)) {
greater = leftElement;
lesser = rightElement;
} else {
greater = rightElement;
lesser = leftElement;
}
a[leftId] = sortIncreasing ? lesser : greater;
a[rightId] = sortIncreasing ? greater : lesser;
});
}; // command group functor
q.submit(f);
} // passStage
} // stage
} // bitonic_sort
template<typename T>
struct buffer_traits;
template<typename T, typename Alloc>
struct buffer_traits<cl::sycl::buffer<T, 1, Alloc>> {
typedef Alloc allocator_type;
};
/** sort
* @brief Function that takes a Comp Operator and applies it to the given range
* @param sep : Execution Policy
* @param first : Start of the range
* @param last : End of the range
* @param comp : Comp Operator
*/
template <class ExecutionPolicy, class RandomIt, class CompareOp>
void sort(ExecutionPolicy &sep, RandomIt first, RandomIt last, CompareOp comp) {
cl::sycl::queue q(sep.get_queue());
typedef typename std::iterator_traits<RandomIt>::value_type type_;
auto buf = std::move(sycl::helpers::make_buffer(first, last));
auto vectorSize = buf.get_count();
typedef typename buffer_traits<decltype(buf)>::allocator_type allocator_;
if (impl::isPowerOfTwo(vectorSize)) {
sycl::impl::bitonic_sort<
type_, allocator_, CompareOp,
bitonic_sort_name<typename ExecutionPolicy::kernelName>>(
q, buf, vectorSize, comp);
} else {
sycl::impl::sequential_sort<
type_, allocator_, CompareOp,
sequential_sort_name<typename ExecutionPolicy::kernelName>>(
q, buf, vectorSize, comp);
}
}
} // namespace impl
} // namespace sycl
#endif // __SYCL_IMPL_ALGORITHM_SORT__