-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.ispc
279 lines (221 loc) · 6.96 KB
/
sort.ispc
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
/*
Copyright (c) 2013, Durham University
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Durham University nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Contributors: Tomasz Koziara */
task void histogram (uniform int span, uniform int n, uniform int64 code[], uniform int pass, uniform int hist[])
{
uniform int start = taskIndex*span;
uniform int end = taskIndex == taskCount-1 ? n : start+span;
uniform int strip = (end-start)/programCount;
uniform int tail = (end-start)%programCount;
int i = programCount*taskIndex + programIndex;
int g [256];
cfor (int j = 0; j < 256; j ++)
{
g[j] = 0;
}
cfor (int k = start+programIndex*strip; k < start+(programIndex+1)*strip; k ++)
{
unsigned int8 *c = (unsigned int8*) &code[k];
g[c[pass]] ++;
}
if (programIndex == programCount-1) /* remainder is processed by the last lane */
{
for (int k = start+programCount*strip; k < start+programCount*strip+tail; k ++)
{
unsigned int8 *c = (unsigned int8*) &code[k];
g[c[pass]] ++;
}
}
cfor (int j = 0; j < 256; j ++)
{
hist[j*programCount*taskCount+i] = g[j];
}
}
task void permutation (uniform int span, uniform int n, uniform int64 code[], uniform int pass, uniform int hist[], uniform int64 perm[])
{
uniform int start = taskIndex*span;
uniform int end = taskIndex == taskCount-1 ? n : start+span;
uniform int strip = (end-start)/programCount;
uniform int tail = (end-start)%programCount;
int i = programCount*taskIndex + programIndex;
int g [256];
cfor (int j = 0; j < 256; j ++)
{
g[j] = hist[j*programCount*taskCount+i];
}
cfor (int k = start+programIndex*strip; k < start+(programIndex+1)*strip; k ++)
{
unsigned int8 *c = (unsigned int8*) &code[k];
int l = g[c[pass]];
perm[l] = code[k];
g[c[pass]] = l+1;
}
if (programIndex == programCount-1) /* remainder is processed by the last lane */
{
for (int k = start+programCount*strip; k < start+programCount*strip+tail; k ++)
{
unsigned int8 *c = (unsigned int8*) &code[k];
int l = g[c[pass]];
perm[l] = code[k];
g[c[pass]] = l+1;
}
}
}
task void copy (uniform int span, uniform int n, uniform int64 from[], uniform int64 to[])
{
uniform int start = taskIndex*span;
uniform int end = taskIndex == taskCount-1 ? n : start+span;
foreach (i = start ... end)
{
to[i] = from[i];
}
}
task void pack (uniform int span, uniform int n, uniform unsigned int code[], uniform int64 pair[])
{
uniform int start = taskIndex*span;
uniform int end = taskIndex == taskCount-1 ? n : start+span;
foreach (i = start ... end)
{
pair[i] = ((int64)i<<32)+code[i];
}
}
task void unpack (uniform int span, uniform int n, uniform int64 pair[], uniform int unsigned code[], uniform int order[])
{
uniform int start = taskIndex*span;
uniform int end = taskIndex == taskCount-1 ? n : start+span;
foreach (i = start ... end)
{
code[i] = pair[i];
order[i] = pair[i]>>32;
}
}
task void addup (uniform int h[], uniform int g[])
{
uniform int * uniform u = &h[256*programCount*taskIndex];
uniform int i, x, y = 0;
for (i = 0; i < 256*programCount; i ++)
{
x = u[i];
u[i] = y;
y += x;
}
g[taskIndex] = y;
}
task void bumpup (uniform int h[], uniform int g[])
{
uniform int * uniform u = &h[256*programCount*taskIndex];
uniform int z = g[taskIndex];
foreach (i = 0 ... 256*programCount)
{
u[i] += z;
}
}
static void prefix_sum (uniform int num, uniform int h[])
{
uniform int * uniform g = uniform new uniform int [num+1];
uniform int i;
launch[num] addup (h, g+1);
sync;
for (g[0] = 0, i = 1; i < num; i ++) g[i] += g[i-1];
launch[num] bumpup (h, g);
sync;
delete g;
}
/* parallel radix sort on unsigned integers */
void radix_sort (uniform int ntasks, uniform int n, uniform unsigned int code[], uniform int order[])
{
uniform int num = ntasks < 1 ? num_cores () : ntasks;
uniform int span = n / num;
uniform int hsize = 256*programCount*num;
uniform int * uniform hist = uniform new uniform int [hsize];
uniform int64 * uniform pair = uniform new uniform int64 [n];
uniform int64 * uniform temp = uniform new uniform int64 [n];
uniform int pass, i;
#if DEBUG
if (n < 100)
{
print ("input: ");
for (i = 0; i < n; i ++) print ("%, ", code[i]);
print ("\n");
}
#endif
launch[num] pack (span, n, code, pair);
sync;
for (pass = 0; pass < 4; pass ++)
{
launch[num] histogram (span, n, pair, pass, hist);
sync;
prefix_sum (num, hist);
launch[num] permutation (span, n, pair, pass, hist, temp);
sync;
launch[num] copy (span, n, temp, pair);
sync;
}
launch[num] unpack (span, n, pair, code, order);
sync;
#if DEBUG
for (i = 0; i < n; i ++)
{
if (i > 0 && code[i-1] > code[i])
print ("ERR at % => % > %; ", i, code[i-1], code[i]);
}
if (n < 100)
{
print ("output: ");
for (i = 0; i < n; i ++) print ("%, ", code[i]);
print ("\n");
print ("order: ");
for (i = 0; i < n; i ++) print ("%, ", order[i]);
print ("\n");
}
#endif
delete hist;
delete pair;
delete temp;
}
/* serial quick sort on unsigned integers */
void quick_sort (uniform int n, uniform unsigned int a[], uniform int order[])
{
uniform unsigned int p, t;
uniform int i, j;
if (n < 2) return;
p = a[n/2];
for (i = 0, j = n - 1;; i++, j--)
{
while (a[i] < p) i++;
while (p < a[j]) j--;
if (i >= j) break;
t = a[i];
a[i] = a[j];
a[j] = t;
t = order[i];
order[i] = order[j];
order[j] = t;
}
quick_sort (i, a, order);
quick_sort (n-i, a+i, order+i);
}