-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_sort.hpp
362 lines (327 loc) · 13.6 KB
/
quick_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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#pragma once
#include <cassert>
#include <cmath>
#include <concepts>
#include <cstddef>
#include <string>
#include <tuple>
#include "cmp_sorters.hpp"
#include "common.hpp"
#include "data.hpp"
#include "simd.hpp"
#if __has_include("../bramas/sort512.hpp")
#include "../bramas/sort512.hpp"
#endif
#if __has_include("../bramas/sort512kv.hpp")
#include "../bramas/sort512kv.hpp"
#endif
namespace simd_sort::quick_sort {
template <typename K>
struct PartitionResult {
SortIndex splitIndex;
K smallestKey;
K largestKey;
};
struct PartitionerSequential {
static std::string name() { return "PartitionerSequential"; }
template <bool Up, typename K, typename... Ps>
static inline PartitionResult<KeyType<K>> partition(const KeyType<K> pivot,
const SortIndex left,
const SortIndex right,
K *const keys,
Ps *const... payloads) {
KeyType<K> smallestKey = std::numeric_limits<KeyType<K>>::max();
KeyType<K> largestKey = std::numeric_limits<KeyType<K>>::lowest();
SortIndex l = left;
SortIndex r = right;
while (l <= r) {
while (l <= r &&
(Up ? (getKey(keys[l]) <= pivot) : (getKey(keys[l]) > pivot))) {
smallestKey = std::min(smallestKey, getKey(keys[l]));
largestKey = std::max(largestKey, getKey(keys[l]));
l++;
}
while (l <= r &&
(Up ? (getKey(keys[r]) > pivot) : (getKey(keys[r]) <= pivot))) {
smallestKey = std::min(smallestKey, getKey(keys[r]));
largestKey = std::max(largestKey, getKey(keys[r]));
r--;
}
if (l < r) {
std::swap(keys[l], keys[r]);
(std::swap(payloads[l], payloads[r]), ...);
}
}
return {l, smallestKey, largestKey};
}
};
struct PartitionerSIMD {
static std::string name() { return "PartitionerSIMD"; }
template <bool Up, typename K, typename... Ps>
static inline PartitionResult<KeyType<K>> partition(const KeyType<K> pivot,
const SortIndex left,
const SortIndex right,
K *const keys,
Ps *const... payloads) {
static constexpr SortIndex numElemsPerVec = simd::Vec<K>::numElems;
const SortIndex numElems = right - left + 1;
SortIndex readPosLeft = left;
SortIndex readPosRight = right - numElemsPerVec + 1;
SortIndex writePosLeft = left;
SortIndex writePosRight = right;
simd::Vec<K, numElemsPerVec * sizeof(K)> keyVecStore;
simd::Vec<KeyType<K>, numElemsPerVec * sizeof(K)> minVec, maxVec;
std::tuple<simd::Vec<Ps, numElemsPerVec * sizeof(Ps)>...> payloadVecStore;
if (numElems >= numElemsPerVec) {
keyVecStore = simd::loadu<numElemsPerVec * sizeof(K)>(&keys[readPosLeft]);
minVec = simd::reinterpret<KeyType<K>>(keyVecStore);
maxVec = simd::reinterpret<KeyType<K>>(keyVecStore);
payloadVecStore = std::make_tuple(
simd::loadu<numElemsPerVec * sizeof(Ps)>(&payloads[readPosLeft])...);
readPosLeft += numElemsPerVec;
}
while (readPosLeft <= readPosRight) {
const auto keyVec = keyVecStore;
const auto payloadVec = payloadVecStore;
const auto [sortMaskLeft, sortMaskRight] =
getSortMasks<Up>(keyVec, pivot);
const SortIndex numElemsToLeft = simd::kpopcnt(sortMaskLeft);
const SortIndex numElemsToRight = numElemsPerVec - numElemsToLeft;
const bool areEnoughElemsFreeLeft =
(readPosLeft - writePosLeft) >= numElemsToLeft;
if (areEnoughElemsFreeLeft) {
keyVecStore =
simd::loadu<numElemsPerVec * sizeof(K)>(&keys[readPosRight]);
minVec = simd::min(minVec, simd::reinterpret<KeyType<K>>(keyVecStore));
maxVec = simd::max(maxVec, simd::reinterpret<KeyType<K>>(keyVecStore));
payloadVecStore =
std::make_tuple(simd::loadu<numElemsPerVec * sizeof(Ps)>(
&payloads[readPosRight])...);
readPosRight -= numElemsPerVec;
} else {
keyVecStore =
simd::loadu<numElemsPerVec * sizeof(K)>(&keys[readPosLeft]);
minVec = simd::min(minVec, simd::reinterpret<KeyType<K>>(keyVecStore));
maxVec = simd::max(maxVec, simd::reinterpret<KeyType<K>>(keyVecStore));
payloadVecStore =
std::make_tuple(simd::loadu<numElemsPerVec * sizeof(Ps)>(
&payloads[readPosLeft])...);
readPosLeft += numElemsPerVec;
}
compress_store_left_right(
writePosLeft, writePosRight - numElemsToRight + 1, sortMaskLeft,
sortMaskRight, keyVec, payloadVec, keys, payloads...);
writePosLeft += numElemsToLeft;
writePosRight -= numElemsToRight;
}
const SortIndex numElemsRest = readPosRight + numElemsPerVec - readPosLeft;
simd::Mask<numElemsPerVec> restMask = 0;
simd::Vec<K, numElemsPerVec * sizeof(K)> keyVecRest;
std::tuple<simd::Vec<Ps, numElemsPerVec * sizeof(Ps)>...> payloadVecRest;
if (numElemsRest != 0) {
restMask = simd::kshiftr(simd::knot(simd::Mask<numElemsPerVec>(0)),
numElemsPerVec - numElemsRest);
keyVecRest = simd::maskz_loadu<numElemsPerVec * sizeof(K)>(
restMask, &keys[readPosLeft]);
payloadVecRest =
std::make_tuple(simd::maskz_loadu<numElemsPerVec * sizeof(Ps)>(
restMask, &payloads[readPosLeft])...);
readPosLeft += numElemsRest;
}
if (numElems >= numElemsPerVec) {
const auto [sortMaskLeft, sortMaskRight] =
getSortMasks<Up>(keyVecStore, pivot);
const SortIndex numElemsToLeft = simd::kpopcnt(sortMaskLeft);
const SortIndex numElemsToRight = numElemsPerVec - numElemsToLeft;
compress_store_left_right(
writePosLeft, writePosRight - numElemsToRight + 1, sortMaskLeft,
sortMaskRight, keyVecStore, payloadVecStore, keys, payloads...);
writePosLeft += numElemsToLeft;
writePosRight -= numElemsToRight;
}
if (numElemsRest != 0) {
auto [sortMaskLeftRest, sortMaskRightRest] =
getSortMasks<Up>(keyVecRest, pivot);
sortMaskLeftRest = simd::kand(sortMaskLeftRest, restMask);
sortMaskRightRest = simd::kand(sortMaskRightRest, restMask);
const SortIndex numElemsToLeftRest = simd::kpopcnt(sortMaskLeftRest);
const SortIndex numElemsToRightRest = numElemsRest - numElemsToLeftRest;
compress_store_left_right(writePosLeft, writePosLeft + numElemsToLeftRest,
sortMaskLeftRest, sortMaskRightRest, keyVecRest,
payloadVecRest, keys, payloads...);
writePosLeft += numElemsToLeftRest;
writePosRight -= numElemsToRightRest;
}
KeyType<K> smallestKey = std::numeric_limits<KeyType<K>>::max();
KeyType<K> largestKey = std::numeric_limits<KeyType<K>>::lowest();
if (numElems >= numElemsPerVec) {
smallestKey =
simd::reduce_keys_min<sizeof(K) / sizeof(KeyType<K>)>(minVec);
largestKey =
simd::reduce_keys_max<sizeof(K) / sizeof(KeyType<K>)>(maxVec);
}
if (numElemsRest != 0) {
smallestKey = std::min(
smallestKey,
simd::reduce_keys_min<sizeof(K) / sizeof(KeyType<K>)>(
simd::reinterpret<KeyType<K>>(keyVecRest), numElemsRest));
largestKey = std::max(
largestKey,
simd::reduce_keys_max<sizeof(K) / sizeof(KeyType<K>)>(
simd::reinterpret<KeyType<K>>(keyVecRest), numElemsRest));
}
return {writePosLeft, smallestKey, largestKey};
}
private:
template <bool Up, std::size_t Bytes, typename K>
static inline std::tuple<simd::Mask<simd::Vec<K, Bytes>::numElems>,
simd::Mask<simd::Vec<K, Bytes>::numElems>>
getSortMasks(const simd::Vec<K, Bytes> keyVec, const KeyType<K> pivot) {
const auto mask = simd::cmple_keys(keyVec, pivot);
const auto invMask = simd::knot(mask);
if constexpr (Up) {
return std::make_tuple(mask, invMask);
} else {
return std::make_tuple(invMask, mask);
}
}
template <std::size_t NumElemsPerVec, typename K, typename... Ps>
static inline void compress_store_left_right(
const SortIndex leftPos, const SortIndex rightPos,
const simd::Mask<NumElemsPerVec> leftMask,
const simd::Mask<NumElemsPerVec> rightMask,
const simd::Vec<K, NumElemsPerVec * sizeof(K)> keyVec,
const std::tuple<simd::Vec<Ps, NumElemsPerVec * sizeof(Ps)>...>
payloadVec,
K *const keys, Ps *const... payloads) {
simd::mask_compressstoreu(&keys[leftPos], leftMask, keyVec);
std::apply(
[&](const auto... payloadVecs) {
(simd::mask_compressstoreu(&payloads[leftPos], leftMask, payloadVecs),
...);
},
payloadVec);
simd::mask_compressstoreu(&keys[rightPos], rightMask, keyVec);
std::apply(
[&](const auto... payloadVecs) {
(simd::mask_compressstoreu(&payloads[rightPos], rightMask,
payloadVecs),
...);
},
payloadVec);
}
};
static inline auto nextVal(const std::integral auto val) -> decltype(val) {
if (val == std::numeric_limits<decltype(val)>::max()) {
return val;
}
return val + 1;
}
static inline auto nextVal(const std::floating_point auto val) {
return std::nextafter(val, std::numeric_limits<decltype(val)>::infinity());
}
static inline auto getAverage(const std::integral auto a,
const std::integral auto b) {
return (a & b) + ((a ^ b) >> 1);
}
static inline auto getAverage(const std::floating_point auto a,
const std::floating_point auto b) {
return (a + b) / 2;
}
template <typename K>
static inline KeyType<K> median(const K a, const K b, const K c) {
if (a < b) {
if (b < c) {
return b;
} else if (a < c) {
return c;
} else {
return a;
}
} else {
if (a < c) {
return a;
} else if (b < c) {
return c;
} else {
return b;
}
}
}
template <typename K>
static inline KeyType<K> getMedianOf3(const SortIndex left,
const SortIndex right,
const K *const keys) {
const auto mid = left + (right - left) / 2;
return median(getKey(keys[left]), getKey(keys[mid]), getKey(keys[right]));
}
template <typename K>
static inline KeyType<K> getMedianOf9(const SortIndex left,
const SortIndex right,
const K *const keys) {
const auto leftMid = left + (right - left) / 3;
const auto rightMid = left + 2 * (right - left) / 3;
return median(getMedianOf3(left, leftMid, keys),
getMedianOf3(leftMid + 1, rightMid, keys),
getMedianOf3(rightMid + 1, right, keys));
}
template <bool Up, typename Partitioner, typename CmpSorter, typename K,
typename... Ps>
void quickRecursion(const SortIndex cmpSortThreshold, const SortIndex left,
const SortIndex right, const bool chooseAvg,
const KeyType<K> avg, K *const keys,
Ps *const... payloads) {
if (right - left <= 0) {
return;
}
if (right - left < cmpSortThreshold) {
CmpSorter::template sort<Up, K, Ps...>(left, right, keys, payloads...);
return;
}
const auto pivot = chooseAvg ? avg : getMedianOf9(left, right, keys);
const auto [split, smallestKey, largestKey] =
Partitioner::template partition<Up>(pivot, left, right, keys,
payloads...);
const double ratio =
(std::min(split - left, right - split + 1) / double(right - left + 1));
const bool nextChooseAvg = ratio < 0.2 ? !chooseAvg : chooseAvg;
if (Up ? pivot > smallestKey : nextVal(pivot) < largestKey) {
const auto avg = getAverage(pivot, Up ? smallestKey : largestKey);
quickRecursion<Up, Partitioner, CmpSorter>(cmpSortThreshold, left,
split - 1, nextChooseAvg, avg,
keys, payloads...);
}
if (Up ? nextVal(pivot) < largestKey : pivot > smallestKey) {
const auto avg = getAverage(pivot, Up ? largestKey : smallestKey);
quickRecursion<Up, Partitioner, CmpSorter>(
cmpSortThreshold, split, right, nextChooseAvg, avg, keys, payloads...);
}
}
template <bool Up = true, typename Partitioner, typename CmpSorter, typename K,
typename... Ps>
void sort(SortIndex cmpSortThreshold, const SortIndex num, K *const keys,
Ps *const... payloads) {
#if defined(SORT512_HPP) && defined(SORT512KV_HPP)
if constexpr (std::is_same_v<CmpSorter, CmpSorterBramasSmallSort>) {
// bramas small sort only supports as many elements as fit in 16 avx512
// registers
cmpSortThreshold =
std::min(cmpSortThreshold, (SortIndex)(16 * 64 / sizeof(K)));
}
#endif // SORT512_HPP && SORT512KV_HPP
quickRecursion<Up, Partitioner, CmpSorter>(
cmpSortThreshold, 0, num - 1, false,
getAverage(std::numeric_limits<KeyType<K>>::lowest(),
std::numeric_limits<KeyType<K>>::max()),
keys, payloads...);
}
template <bool Up = true, typename Partitioner, typename CmpSorter, typename K,
typename... Ps>
void sort(const SortIndex num, K *const keys, Ps *const... payloads) {
quickRecursion<Up, Partitioner, CmpSorter>(
16, 0, num - 1, false,
getAverage(std::numeric_limits<KeyType<K>>::lowest(),
std::numeric_limits<KeyType<K>>::max()),
keys, payloads...);
}
} // namespace simd_sort::quick_sort