COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
radixSort.h
Go to the documentation of this file.
1// This code is part of the Problem Based Benchmark Suite (PBBS)
2// Copyright (c) 2011 Guy Blelloch and the PBBS team
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the
6// "Software"), to deal in the Software without restriction, including
7// without limitation the rights (to use, copy, modify, merge, publish,
8// distribute, sublicense, and/or sell copies of the Software, and to
9// permit persons to whom the Software is furnished to do so, subject to
10// the following conditions:
11//
12// The above copyright notice and this permission notice shall be included
13// in all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23#ifndef _S_RADIX_INCLUDED
24#define _S_RADIX_INCLUDED
25
26#include <iostream>
27#include <algorithm>
28#include <math.h>
29#include "utils.h"
30
31
32
33namespace intSort {
34
35 // Cannot be greater than 8 without changing definition of bIndexT
36 // from unsigned char to unsigned int (or unsigned short)
37#define MAX_RADIX 8
38#define BUCKETS (1 << MAX_RADIX)
39
40 // a type that must hold MAX_RADIX bits
41 typedef unsigned char bIndexT;
42
43 // input in A, output in B
44 template <class E, class F>
45 void radixStep(E* A, E* B, bIndexT *Tmp, int* counts,
46 int n, int m, F extract) {
47 for (int i = 0; i < m; i++) counts[i] = 0;
48 for (int j = 0; j < n; j++) {
49 int k = Tmp[j] = extract(A[j]);
50 counts[k]++;
51 }
52 int s = 0;
53 for (int i = 0; i < m; i++) {
54 s += counts[i];
55 counts[i] = s;
56 }
57 for (int j = n-1; j >= 0; j--) {
58 int x = --counts[Tmp[j]];
59 B[x] = A[j];
60 }
61 }
62
63 // a function to extract "bits" bits starting at bit location "offset"
64 template <class E, class F>
65 struct eBits {
66 F _f; int _mask; int _offset;
67// ADAM: moved initialization of _f to the front to remove "x will be initialized after y" warning
68 eBits(int bits, int offset, F f): _f(f), _mask((1<<bits)-1),
69 _offset(offset) {}
70 int operator() (E p) {return _mask&(_f(p)>>_offset);}
71 };
72
73 // Radix sort with low order bits first
74 template <class E, class F>
75 void iSort(E *A, int n, int m, F f) {
76 int bits = utils::log2Up(m);
77
78 // temporary space
79 E* B = (E*) malloc(sizeof(E)*n);
80 bIndexT* Tmp = (bIndexT*) malloc(sizeof(bIndexT)*n);
81 int* counts = (int*) malloc(sizeof(int)*BUCKETS);
82
83 int rounds = 1+(bits-1)/MAX_RADIX;
84 int rbits = 1+(bits-1)/rounds;
85 int bitOffset = 0;
86 bool flipped = 0;
87
88 while (bitOffset < bits) {
89 if (bitOffset+rbits > bits) rbits = bits-bitOffset;
90 if (flipped)
91 radixStep(B, A, Tmp, counts, n, 1 << rbits,
92 eBits<E,F>(rbits,bitOffset,f));
93 else
94 radixStep(A, B, Tmp, counts, n, 1 << rbits,
95 eBits<E,F>(rbits,bitOffset,f));
96 bitOffset += rbits;
97 flipped = !flipped;
98 }
99
100 if (flipped)
101 for (int i=0; i < n; i++)
102 A[i] = B[i];
103
104 free(B); free(Tmp); free(counts);
105 }
106}
107
108// ADAM: added inline to remove "defined but not used" warning
109static inline void integerSort(uint32_t *A, int n) {
110 uint32_t maxV = 0;
111 for (int i=0; i<n; i++) maxV = std::max(maxV,A[i]);
113}
114
115template <class T>
116void integerSort(std::pair<uint32_t,T> *A, int n) {
117 uint32_t maxV = 0;
118 for (int i=0; i<n; i++) maxV = std::max(maxV,A[i].first);
120}
121
122#endif // _S_RADIX_INCLUDED
Definition test.cpp:53
#define BUCKETS
Definition radixSort.h:38
#define MAX_RADIX
Definition radixSort.h:37
unsigned char bIndexT
Definition radixSort.h:41
void radixStep(E *A, E *B, bIndexT *Tmp, int *counts, int n, int m, F extract)
Definition radixSort.h:45
void iSort(E *A, int n, int m, F f)
Definition radixSort.h:75
double A
unsigned int uint32_t
Definition stdint.h:80
int operator()(E p)
Definition radixSort.h:70
eBits(int bits, int offset, F f)
Definition radixSort.h:68