-
Notifications
You must be signed in to change notification settings - Fork 0
/
radixsort.h
298 lines (267 loc) · 12.2 KB
/
radixsort.h
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
/*
* GPU Smoldyn: Smoldyn algorithm ported to the GPU using CUDA 2.2
* Writtern By Lorenzo Dematté, 2010-2011
*
* This file is part of GPU Smoldyn
*
* GPU Smoldyn is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GPU Smoldyn is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*
* Based on algorithm and source code of Smoldyn, written by Steve Andrews, 2003.
*
* Portions taken by code examples in NVIDIA Whitepapers, GPU Gems 2 and 3,
* Copyright 1993-2009 NVIDIA Corporation, Addison-Wesley and the original authors.
*
*/
#ifndef __RADIXSORT_H__
#define __RADIXSORT_H__
// -----------------------------------------------------------------------
// Fast CUDA Radix Sort Class
//
// The parallel radix sort algorithm implemented by this code is described
// in the following paper.
//
// Satish, N., Harris, M., and Garland, M. "Designing Efficient Sorting
// Algorithms for Manycore GPUs". In Proceedings of IEEE International
// Parallel & Distributed Processing Symposium 2009 (IPDPS 2009).
//
// -----------------------------------------------------------------------
#include <cuda_runtime_api.h>
#include <cudpp/cudpp.h>
extern "C" void checkCudaError(const char *msg);
extern "C" void initDeviceParameters();
extern "C"
void radixSort(unsigned int *keys,
unsigned int *values,
unsigned int *tempKeys,
unsigned int *tempValues,
unsigned int *counters,
unsigned int *countersSum,
unsigned int *blockOffsets,
CUDPPHandle scanPlan,
unsigned int numElements,
unsigned int keyBits,
bool flipBits);
extern "C"
void radixSortFloatKeys(float *keys,
unsigned int *values,
float *tempKeys,
unsigned int *tempValues,
unsigned int *counters,
unsigned int *countersSum,
unsigned int *blockOffsets,
CUDPPHandle scanPlan,
unsigned int numElements,
unsigned int keyBits,
bool negativeKeys);
extern "C"
void radixSortKeysOnly(unsigned int *keys,
unsigned int *tempKeys,
unsigned int *counters,
unsigned int *countersSum,
unsigned int *blockOffsets,
CUDPPHandle scanPlan,
unsigned int numElements,
unsigned int keyBits,
bool flipBits);
extern "C"
void radixSortFloatKeysOnly(float *keys,
float *tempKeys,
unsigned int *counters,
unsigned int *countersSum,
unsigned int *blockOffsets,
CUDPPHandle scanPlan,
unsigned int numElements,
unsigned int keyBits,
bool negativeKeys);
//-----------------------------------------------------------------------------
// CUDA Radix Sort class
//
// This class can be used to sort arrays of keys and values (or only keys)
// keys and values are both 32-bits in length, though the keyBits argument
// can be used to sort by only the keyBits least significant bits. Values
// are unsigned integers (common case is assumed to be pointers or array indices).
//
// Use the RadixSort class from host code. The constructor takes the maximum
// number of elements, M, to be sorted, and a boolean flag for whether the sort
// is only on keys or on key-value pairs (the default). Invoke the sort on an
// array of N <= M elements by calling sort(), passing:
// a.) CUDA device pointers to an array of N keys and an (optional) array of
// N values,
// b.) The number of elements N to sort, and
// c.) The number of bits to sort in the keys (must be a multiple of 4 bits).
//
// Note there are two sort() functions: one for unsigned integer keys and one
// for float keys
//
// initialize() (called by the constructor) allocates temporary storage for the
// sort and the prefix sum that it uses. Temporary storage is
// (2*M + 3*8*M/CTA_SIZE) unsigned ints, with a default CTA_SIZE of 256 threads.
// So for example, sorting 128K key/value pairs (1MB) requires 1MB+48KB of
// temporary storage, in addition to the 1MB for the original arrays.
//
// Depends on CUDPP library: http://www.gpgpu.org/developer/cudpp
// (A pre-compiled CUDPP.lib is included with the NVIDIA CUDA SDK
//
//-----------------------------------------------------------------------------
class RadixSort
{
public: // methods
//------------------------------------------------------------------------
// Constructor
// @param maxElements Maximum number of elements to be sorted.
// @param keysOnly true if only keys are to be sorted, false if keys and values
//
// Allocates maxElements * (2 + 3*8/CTA_SIZE) unsigned ints of temp storage.
//------------------------------------------------------------------------
RadixSort(unsigned int maxElements, bool keysOnly = false)
: mScanPlan(0),
mNumElements(0),
mTempKeys(0),
mTempValues(0),
mCounters(0),
mCountersSum(0),
mBlockOffsets(0)
{
// Allocate temporary storage
initialize(maxElements, keysOnly);
}
//------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------
~RadixSort()
{
finalize();
}
//------------------------------------------------------------------------
// Sorts input arrays of unsigned integer keys and (optional) values
//
// @param keys Array of keys for data to be sorted
// @param values Array of values to be sorted
// @param numElements Number of elements to be sorted. Must be <=
// maxElements passed to the constructor
// @param keyBits The number of bits in each key to use for ordering
//------------------------------------------------------------------------
void sort(unsigned int *keys,
unsigned int *values,
unsigned int numElements,
unsigned int keyBits)
{
if (values == 0)
{
radixSortKeysOnly(keys, mTempKeys,
mCounters, mCountersSum, mBlockOffsets,
mScanPlan, numElements, keyBits, false);
}
else
{
radixSort(keys, values, mTempKeys, mTempValues,
mCounters, mCountersSum, mBlockOffsets,
mScanPlan, numElements, keyBits, false);
}
}
//------------------------------------------------------------------------
// Sorts input arrays of float keys and (optional) unsigned integer values
//
// @param keys Array of keys for data to be sorted
// @param values Array of values to be sorted
// @param numElements Number of elements to be sorted. Must be <=
// maxElements passed to the constructor
// @param keyBits The number of bits in each key to use for ordering
// @param negativeKeys False if unsigned float keys, true if signed
//------------------------------------------------------------------------
void sort(float *keys,
unsigned int *values,
unsigned int numElements,
unsigned int keyBits,
bool negativeKeys)
{
if (values == 0)
{
radixSortFloatKeysOnly(keys, (float*)mTempKeys,
mCounters, mCountersSum, mBlockOffsets,
mScanPlan, numElements, keyBits, negativeKeys);
}
else
{
radixSortFloatKeys(keys, values, (float*)mTempKeys, mTempValues,
mCounters, mCountersSum, mBlockOffsets,
mScanPlan, numElements, keyBits, negativeKeys);
}
}
public: // constants
static const unsigned int CTA_SIZE = 256; // Number of threads per block
static const unsigned int WARP_SIZE = 32;
// These are static so that extern "C" functions in the .cu file can access them
/*static unsigned int numCTAs[SORT_KERNEL_COUNT];
static unsigned int persistentCTAThreshold[2];
static unsigned int persistentCTAThresholdFullBlocks[2];*/
protected: // data
CUDPPHandle mScanPlan; // CUDPP plan handle for prefix sum
unsigned int mNumElements; // Number of elements of temp storage allocated
unsigned int *mTempKeys; // Intermediate storage for keys
unsigned int *mTempValues; // Intermediate storage for values
unsigned int *mCounters; // Counter for each radix
unsigned int *mCountersSum; // Prefix sum of radix counters
unsigned int *mBlockOffsets; // Global offsets of each radix in each block
protected: // methods
//------------------------------------------------------------------------
// Initialization. Allocates temporary storage and CUDPP scan plan.
// @param numElements Maximum number of elements to be sorted.
//
// Allocates numElements * (2 + 3*8/CTA_SIZE) unsigned ints of temp storage.
// Note, the scan plan allocates an additional (numElements * 8/CTA_SIZE)/512
// elements of temp storage, but for a 1M element array to be sorted, this
// amounts to only 256 bytes extra.
//------------------------------------------------------------------------
void initialize(unsigned int numElements, bool keysOnly)
{
// initialize parameters based on present CUDA device
initDeviceParameters();
// Allocate temporary storage
mNumElements = numElements;
unsigned int numBlocks = ((numElements % (CTA_SIZE * 4)) == 0) ?
(numElements / (CTA_SIZE * 4)) : (numElements / (CTA_SIZE * 4) + 1);
unsigned int numBlocks2 = ((numElements % (CTA_SIZE * 2)) == 0) ?
(numElements / (CTA_SIZE * 2)) : (numElements / (CTA_SIZE * 2) + 1);
// Initialize scan
CUDPPConfiguration scanConfig;
scanConfig.algorithm = CUDPP_SCAN;
scanConfig.datatype = CUDPP_UINT;
scanConfig.op = CUDPP_ADD;
scanConfig.options = CUDPP_OPTION_EXCLUSIVE | CUDPP_OPTION_FORWARD;
cudppPlan(&mScanPlan, scanConfig, 16 * numBlocks2, 1, 0);
cudaMalloc((void **)&mTempKeys, numElements * sizeof(unsigned int));
if (!keysOnly)
cudaMalloc((void **)&mTempValues, numElements * sizeof(unsigned int));
cudaMalloc((void **)&mCounters, WARP_SIZE * numBlocks * sizeof(unsigned int));
cudaMalloc((void **)&mCountersSum, WARP_SIZE * numBlocks * sizeof(unsigned int));
cudaMalloc((void **)&mBlockOffsets, WARP_SIZE * numBlocks * sizeof(unsigned int));
checkCudaError("RadixSort::initialize()");
}
//------------------------------------------------------------------------
// Deallocate all temporary storage and destroy CUDPP scan plan.
//------------------------------------------------------------------------
void finalize()
{
cudaFree(mTempKeys);
cudaFree(mTempValues);
cudaFree(mCounters);
cudaFree(mCountersSum);
cudaFree(mBlockOffsets);
mCounters = mCountersSum = mBlockOffsets = 0;
checkCudaError("RadixSort::finalize()");
cudppDestroyPlan(mScanPlan);
}
};
#endif // __RADIXSORT_H__