-
Notifications
You must be signed in to change notification settings - Fork 0
/
radixsort.sc
391 lines (314 loc) · 9.49 KB
/
radixsort.sc
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
/* tab:8
*
* radixsort.sc - Integer sort by radix
*
*
* "Copyright (c) 1994,1995 The Regents of the University of Maryland.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF MARYLAND HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Authors: David A. Bader <dbader@umiacs.umd.edu>
* Joseph F. Ja'Ja' <joseph@umiacs.umd.edu>
* Institute for Advanced Computer Studies
* Department of Electrical Engineering
* AV Williams Building
* College Park, MD 20742
*
* Version: 1.0
* Creation Date: March 11, 1995
* Filename: radixsort.sc
* History:
*/
/* Radix Sort in Split-C */
#include "radixsort.h"
#include "droute.h"
/****************************************************/
static inline
void all_countsort_safe(int q,
int Key[PROCS]::[q],
int Sorted[PROCS]::[q],
int R,
int bitOff, int m,
int route)
/****************************************************/
/* R (range) must be a multiple of PROCS */
/* q (elems/proc) must be a multiple of PROCS */
{
register int
j,
k,
s,
get_proc,
offset;
int *lKey,
*lAddr,
*lIndex,
*lScanTran;
intpair_t
*lIntScanTran,
*lScans;
int Addr[PROCS]::[q],
Index[PROCS]::[R],
ScanTran[PROCS]::[R],
addrBits[q];
intpair_t
IntScanTran[PROCS]::[R],
Scans[PROCS]::[R];
lKey = tolocal(Key[MYPROC]);
lAddr = tolocal(Addr[MYPROC]);
lIndex = tolocal(Index[MYPROC]);
lScanTran = tolocal(ScanTran[MYPROC]);
lIntScanTran = tolocal(IntScanTran[MYPROC]);
lScans = tolocal(Scans[MYPROC]);
barrier();
if (R < PROCS)
R = PROCS;
s = R/PROCS;
for (k=0 ; k<R ; k++)
lIndex[k] = 0;
for (k=0 ; k<q ; k++) {
addrBits[k] = bits(lKey[k],bitOff,m);
lIndex[addrBits[k]]++;
}
/* Perform Matrix Transpose of Index -> ScanTran */
barrier(); /* Wait for everyone */
for (k=0 ; k<PROCS ; k++) {
get_proc = (MYPROC + k) % PROCS; /* Stagger prefetches using MYPROC */
bulk_read(&lScanTran[s*get_proc],
&(Index[get_proc][s*MYPROC]),
s*sizeof(int));
barrier();
}
/* Calculate local Prefix Sums */
for (j=0 ; j<s ; j++)
for (k=1 ; k<PROCS ; k++)
lScanTran[k*s + j] += lScanTran[(k-1)*s + j];
/* Create IntLeaveScan by interleaving ScanTran[.][*] with ScanTran[P-1][*]*/
for (j=0 ; j<s ; j++)
for (k=0 ; k<PROCS ; k++) {
lIntScanTran[k*s + j].a = lScanTran[k*s + j];
lIntScanTran[k*s + j].b = lScanTran[(PROCS-1)*s + j];
}
barrier();
/* Perform Inverse Matrix Transpose of IntScanTran -> Scans */
for (k=0 ; k<PROCS ; k++) {
get_proc = (MYPROC + k) % PROCS; /* Stagger prefetches using MYPROC */
bulk_read(&lScans[s*get_proc],
&(IntScanTran[get_proc][s*MYPROC]),
s*sizeof(intpair_t));
barrier();
}
offset = 0;
for (k=0 ; k<R ; k++) {
lIndex[k] = (lScans[k].a - lIndex[k]) + offset;
offset += lScans[k].b;
}
for (k=0 ; k<q ; k++) {
lAddr[k] = lIndex[addrBits[k]];
lIndex[addrBits[k]]++;
}
switch (route) {
case 1 : all_route_h_rel_det(q, 1, Key, Addr, Sorted); break;
case 2 : all_route_h_rel_det2(q, 1, Key, Addr, Sorted); break;
case 3 : all_route_h_rel_det3(q, 1, Key, Addr, Sorted); break;
case 4 : all_route_h_rel_det4(q, 1, Key, Addr, Sorted); break;
case 5 : all_route_linear_perm(q, 1, Key, Addr, Sorted); break;
case 6 : all_route_single_phase(q, 1, Key, Addr, Sorted); break;
}
}
/****************************************************/
void all_countsort(int q,
int Key[PROCS]::[q],
int Sorted[PROCS]::[q],
int R,
int bitOff, int m,
int route)
/****************************************************/
/* R (range) must be a multiple of PROCS */
/* q (elems/proc) must be a multiple of PROCS */
{
register int
j,
k,
s,
get_proc,
offset;
int *lKey,
*lAddr,
*lIndex,
*lScanTran,
*t1ptr,
*t2ptr,
*t3ptr;
intpair_t
*lIntScanTran,
*lScans,
*p1ptr,
*p2ptr;
int *spread Addr;
int *spread Index;
int *spread ScanTran;
int *addrBits;
intpair_t *spread IntScanTran;
intpair_t *spread Scans;
Addr = all_spread_malloc(PROCS,q*sizeof(int));
assert_spread_malloc(Addr);
Index = all_spread_malloc(PROCS,R*sizeof(int));
assert_spread_malloc(Index);
ScanTran = all_spread_malloc(PROCS,R*sizeof(int));
assert_spread_malloc(ScanTran);
addrBits = (int*)malloc(q*sizeof(int));
assert_malloc(addrBits);
IntScanTran = all_spread_malloc(PROCS,R*sizeof(intpair_t));
assert_spread_malloc(IntScanTran);
Scans = all_spread_malloc(PROCS,R*sizeof(intpair_t));
assert_spread_malloc(Scans);
lKey = (int *)(Key + MYPROC);
lAddr = (int *)(Addr + MYPROC);
lIndex = (int *)(Index + MYPROC);
lScanTran = (int *)(ScanTran + MYPROC);
lIntScanTran = (intpair_t *)(IntScanTran + MYPROC);
lScans = (intpair_t *)(Scans + MYPROC);
barrier();
if (R < PROCS)
R = PROCS;
s = DIVPROCS(R);
t1ptr = lIndex-1;
t2ptr = lIndex+R;
while (++t1ptr<t2ptr)
*t1ptr = 0;
for (k=0 ; k<q ; k++) {
*(t1ptr = addrBits+k) = bits(*(lKey+k),bitOff,m);
(*(lIndex + *t1ptr))++;
}
/* Perform Matrix Transpose of Index -> ScanTran */
barrier(); /* Wait for everyone */
#if (defined(CM5))
for (k=0 ; k<PROCS ; k++) {
get_proc = MODPROCS(MYPROC + k); /* Stagger prefetches using MYPROC */
bulk_read(lScanTran + (s*get_proc),
(int *global)(Index + get_proc) + (s*MYPROC),
s*sizeof(int));
barrier();
}
#else
for (k=0 ; k<PROCS ; k++) {
get_proc = MODPROCS(MYPROC + k); /* Stagger prefetches using MYPROC */
bulk_get(lScanTran + (s*get_proc),
(int *global)(Index + get_proc) + (s*MYPROC),
s*sizeof(int));
}
sync();
barrier();
#endif
/* Calculate local Prefix Sums */
for (j=0 ; j<s ; j++) {
t2ptr = t1ptr = lScanTran+j;
t2ptr += s;
t3ptr = t1ptr + PROCS*s;
while (t2ptr < t3ptr) {
*t2ptr += *t1ptr;
t1ptr = t2ptr;
t2ptr += s;
}
}
/* Create IntLeaveScan by interleaving ScanTran[.][*] with ScanTran[P-1][*]*/
t2ptr = lScanTran + (PROCS-1)*s;
for (j=0 ; j<s ; j++) {
t1ptr = lScanTran + j;
p1ptr = lIntScanTran + j;
p2ptr = p1ptr + s*PROCS;
while (p1ptr < p2ptr) {
p1ptr->a = *t1ptr;
p1ptr->b = *t2ptr;
t1ptr += s;
p1ptr += s;
}
t2ptr++;
}
barrier();
/* Perform Inverse Matrix Transpose of IntScanTran -> Scans */
#if (defined(CM5))
for (k=0 ; k<PROCS ; k++) {
get_proc = MODPROCS(MYPROC + k); /* Stagger prefetches using MYPROC */
bulk_read(lScans + (s*get_proc),
(intpair_t *global)(IntScanTran + get_proc) + (s*MYPROC),
s*sizeof(intpair_t));
barrier();
}
#else
for (k=0 ; k<PROCS ; k++) {
get_proc = MODPROCS(MYPROC + k); /* Stagger prefetches using MYPROC */
bulk_get(lScans + (s*get_proc),
(intpair_t *global)(IntScanTran + get_proc) + (s*MYPROC),
s*sizeof(intpair_t));
}
sync();
barrier();
#endif
offset = 0;
for (k=0 ; k<R ; k++) {
*(lIndex + k) = ((p1ptr=lScans+k)->a - *(lIndex+k)) + offset;
offset += p1ptr->b;
}
for (k=0 ; k<q ; k++) {
*(lAddr + k) = *(t1ptr = (lIndex + *(addrBits+k)));
(*t1ptr)++;
}
all_spread_free(Scans);
all_spread_free(IntScanTran);
free(addrBits);
all_spread_free(ScanTran);
all_spread_free(Index);
switch (route) {
case 1 : all_route_h_rel_det(q, 1, Key, Addr, Sorted); break;
case 2 : all_route_h_rel_det2(q, 1, Key, Addr, Sorted); break;
case 3 : all_route_h_rel_det3(q, 1, Key, Addr, Sorted); break;
case 4 : all_route_h_rel_det4(q, 1, Key, Addr, Sorted); break;
case 5 : all_route_linear_perm(q, 1, Key, Addr, Sorted); break;
case 6 : all_route_single_phase(q, 1, Key, Addr, Sorted); break;
}
all_spread_free(Addr);
}
/****************************************************/
inline void all_radixsort20(int q,
int Keys[PROCS]::[q],
int Sorted[PROCS]::[q],
int route)
/****************************************************/
{
all_countsort(q, Keys, Sorted, (1<<10), 0, 10, route);
all_countsort(q, Sorted, Sorted, (1<<10), 10, 10, route);
}
/****************************************************/
inline void all_radixsort_passInfo(int q,
int Keys[PROCS]::[q],
int Sorted[PROCS]::[q],
int passes,
int *b)
/****************************************************/
{
register int
i,
j;
j=0;
all_countsort(q, Keys, Sorted, (1<<b[0]), 0, b[0], 1);
for (i=1 ; i<passes ; i++)
all_countsort(q, Sorted, Sorted, max((1<<b[i]),PROCS),
(j+=b[i-1]), b[i], 1);
}