-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting_networks.cpp
356 lines (303 loc) · 7.54 KB
/
sorting_networks.cpp
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
/*
Experiments with batcher's odd-even sort, bitonic sort & radix sort.
Creating various permutations of sorting algorithms with different properties.
Intended for usage in GPU parallel sorting, prototyped on CPU.
Compiled using Visual Studio 2012.
(Dependencies include _BitScanReverse/concurrency::parallel_for/<ppl.h>/__rdtsc/__debugbreak, but can be easily removed)
*/
#include <iostream>
#include <stdio.h>
#include <vector>
#include <ppl.h>
void exchange(int* a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
void compare(int* a, int i, int j) {
//printf("compare %d %d\n", i, j);
if (a[i] > a[j])
exchange(a, i, j);
}
/**
* lo is the starting position and
* n is the length of the piece to be merged,
* r is the distance of the elements to be compared
*/
void oddEvenMerge_r1(int* a, int lo, int n, int r) {
int m = r * 2;
if (m >= n) {
compare(a, lo, lo + r);
}
else
{
oddEvenMerge_r1(a, lo, n, m); // even subsequence
oddEvenMerge_r1(a, lo + r, n, m); // odd subsequence
for (int i = lo + r; i + r < lo + n; i += m)
compare(a, i, i + r);
}
}
// transformation 1 (non recursive)
void oddEvenMerge_r2(int* a, int s_lo, int n, int s_r) {
int r = n/2;
while(r >= 1)
{
for(int lo=s_lo; lo < s_lo+r; lo++)
{
int m = r * 2;
if (m >= n) {
compare(a, lo, lo + r);
}
else
{
for (int i = lo + r; i + r < lo + n; i += m)
compare(a, i, i + r);
}
}
r >>= 1;
}
}
// transformation 2
void oddEvenMerge_r3(int* a, int s_lo, int n, int s_r) {
// do first depth iteration
int r = n >> 1;
for(int lo=s_lo; lo<s_lo + r; lo++)
compare(a, lo, lo + r);
// go to next iteration
for(r >>= 1; r >= 1; r >>= 1)
{
int m = r << 1;
for(int lo=s_lo; lo < s_lo+r; lo++)
{
for (int i = lo + r; i + r < lo + n; i += m)
compare(a, i, i + r);
}
}
}
// permutation 0: original two step algorithm
void oddEvenMergeSort_Merge(int* a, int n) {
for(int j=2; j<=n; j <<= 1)
{
for(int k=0; k<n; k += j)
{
//oddEvenMerge_r1(a, k, j, 1);
//oddEvenMerge_r2(a, k, j, 1);
oddEvenMerge_r3(a, k, j, 1);
}
}
}
// permutation 1: odd even merge in non colliding passes
// i.e. all comparisons in a pass can be run in parallel.
void oddEvenMergeSort_NCPasses_Branchless(int* a, int n)
{
for(int j=2; j<=n; j <<= 1)
{
// pass 0
int r = j >> 1;
for(int k=0; k<n; k += j)
{
for(int lo=k; lo< k+r; lo++)
compare(a, lo, lo + r);
}
// do pass 1-log2(n)
for(r >>= 1; r >= 1; r >>= 1)
{
for(int k=0; k<n; k += j)
{
int m = r << 1;
for(int lo=k; lo < k+r; lo++)
for (int i = lo + r; i + r < lo + j; i += m)
compare(a, i, i + r);
}
}
}
}
void oddEvenMergeSort_NCPasses_Branched(int* a, int n)
{
for(int j=2; j<=n; j <<= 1)
{
for(int r = j >> 1; r >= 1; r >>= 1)
{
for(int k=0; k<n; k += j)
{
for(int lo=k; lo < k+r; lo++)
{
if(r==(j>>1))
compare(a, lo, lo + r);
else
{
int m = r << 1;
for (int i = lo + r; i + r < lo + j; i += m)
compare(a, i, i + r);
}
}
}
}
}
}
// alternative algorithm that calculates swap partner and is easily parallelized (but slower due to a lot of wasted iterations)
unsigned int oddEvenMergeSort_GetPartner(int n, int l, int p)
{
if (p == 1)
return n ^ (1 << (l - 1));
int scale = (1 << (l - p));
int box = (1 << p);
int nDivScale = (n/scale);
int sn = nDivScale - ( nDivScale / box) * box;
if (sn == 0 || sn == (box - 1) || (sn & 1) == 0)
return n;
else
return n + scale;
}
void oddEvenMergeSort_Partner(int* a, unsigned int _n)
{
unsigned long d;
_BitScanReverse(&d, _n);
for (int l = 1; l <= d; l++) {
for (int p = 1; p <= l; p++) {
for (int n = 0; n < _n; n++) {
int partner = oddEvenMergeSort_GetPartner(n, l, p);
if(partner > n)
compare(a,n, partner);
}
}
}
}
void oddEvenMergeSort_Partner_MT(int* a, unsigned int _n)
{
unsigned long d;
_BitScanReverse(&d, _n);
for (int l = 1; l <= d; l++) {
for (int p = 1; p <= l; p++) {
concurrency::parallel_for(0U, _n, 1U, [l,p,d,a](int n) {
int partner = oddEvenMergeSort_GetPartner(n, l, p);
if(partner > n)
compare(a,n, partner);
});
}
}
}
template <class T>
void bitonicSort(T* a, unsigned int n)
{
int N = n;
int vi, i, j, k;
for (k = 2; k <= N; k = 2 * k)
{
for (j = k >> 1; j > 0; j = j >> 1)
{
for (vi = 0; vi < N / 2; vi++)
{
i = vi + (vi / j)*j;
int ixj = i + j;
if ((i&k) == 0)
compare(a, i, ixj);
else
compare(a, ixj, i);
}
}
}
}
template <class T>
void bitonicSort_v2(T* a, unsigned int n)
{
int N = n;
int HalfN = n/2;
int vi, i, j, k;
for (k = 2; k <= N; k <<= 1)
{
for (j = k >> 1; j > 0; j >>= 1)
{
int invJmask = ~(j-1);
for (vi = 0; vi < HalfN; vi++)
{
i = vi + (vi & invJmask);
int ixj = i + j;
if ((i&k) == 0)
compare(a, i, ixj);
else
compare(a, ixj, i);
}
}
}
}
void radixSort(int* arr, int N, unsigned int bitStart, unsigned int numBits)
{
# define RADIX_BIN(val) (((val) >> bitStart) & mask)
int* tmpArr = (int*)_alloca(sizeof(int)*N);
unsigned int bitMask = (1 << bitStart);
unsigned int counts[256];
unsigned int index[256];
unsigned int bits = (1 << numBits);
unsigned int mask = bits-1;
unsigned int idx = 0;
unsigned int idx2 = 0;
memset(counts, 0, bits*sizeof(unsigned int));
memset(index, 0, bits * sizeof(unsigned int));
// count
for (int i = 0; i < N; i++)
++counts[RADIX_BIN(arr[i])];
// calculate indices (prefix sum - exclusive scan)
index[0] = 0;
for (int i = 1; i < (1 << numBits); i++)
index[i] = index[i-1]+counts[i-1];
// sort
for (int i = 0; i < N; i++)
tmpArr[index[RADIX_BIN(arr[i])]++] = arr[i];
// scatter
for (int i = 0; i < N; i++)
arr[i] = tmpArr[i];
# undef RADIX_BIN
}
const int NUM_TESTS = 1000;
template<class T> bool runTest(int* a, int n, const T& testfunc)
{
unsigned long long timing=0;
for(int i=0; i<NUM_TESTS; i++)
{
int b[2048];
memcpy(b, a, n*sizeof(int));
unsigned long long clk = __rdtsc();
testfunc(b,n);
unsigned long long clk1 = __rdtsc();
timing += (clk1-clk);
int v=b[0];
for (int i = 0; i < n; i++)
{
if(b[i] < v)
{
std::cout << "error detected\n";
return false;
}
else
v = b[i];
}
}
std::cout << "execution time: " << timing/NUM_TESTS << " cycles\n";
return true;
}
int main() {
int a[2048];
int n=sizeof(a)/sizeof(a[0]);
for(int i=0; i<n; i++)
//a[i] = i/(n/16); // sequential
//a[i] = 15-(i/(n/16)); // reverse sequential
a[i] = rand()%15; // randomized
// comparison sorting networks
runTest(a, n, [](int* a, int n) { oddEvenMergeSort_Merge(a, n); });
runTest(a, n, [](int* a, int n) { oddEvenMergeSort_NCPasses_Branchless(a, n); });
runTest(a, n, [](int* a, int n) { oddEvenMergeSort_NCPasses_Branched(a, n); });
runTest(a, n, [](int* a, int n) { oddEvenMergeSort_Partner(a, n); });
runTest(a, n, [](int* a, int n) { oddEvenMergeSort_Partner_MT(a, n); });
runTest(a, n, [](int* a, int n) { bitonicSort(a, n); });
runTest(a, n, [](int* a, int n) { bitonicSort_v2(a, n); });
// standard library sort
runTest(a, n, [](int* a, int n) { std::sort(a, a+n); });
// radix sorts
runTest(a, n, [](int* a, int n) { radixSort(a, n, 0, 4);});
runTest(a, n, [](int* a, int n) { for (int i = 0; i < 8; i += 4) radixSort(a, n, i, 4);});
runTest(a, n, [](int* a, int n) { for (int i = 0; i < 16; i += 4) radixSort(a, n, i, 4);});
runTest(a, n, [](int* a, int n) { for (int i = 0; i < 16; i += 8) radixSort(a, n, i, 8);});
runTest(a, n, [](int* a, int n) { for (int i = 0; i < 32; i += 8) radixSort(a, n, i, 8);});
return(0);
}