-
Notifications
You must be signed in to change notification settings - Fork 5
/
bucketSelect.cu
473 lines (385 loc) · 15.5 KB
/
bucketSelect.cu
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* Copyright 2011 Russel Steinbach, Jeffrey Blanchard, Bradley Gordon,
* and Toluwaloju Alabi
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/random.h>
#include <thrust/sort.h>
#include <thrust/transform_reduce.h>
namespace BucketSelect{
using namespace std;
#define MAX_THREADS_PER_BLOCK 1024
#define CUTOFF_POINT 200000
#define NUM_PIVOTS 17
#define CUDA_CALL(x) do { if((x) != cudaSuccess) { \
printf("Error at %s:%d\n",__FILE__,__LINE__); \
return EXIT_FAILURE;}} while(0)
cudaEvent_t start, stop;
float time;
void timing(int selection, int ind){
if(selection==0) {
//****//
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start,0);
//****//
}
else {
//****//
cudaThreadSynchronize();
cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
printf("Time %d: %lf \n", ind, time);
//****//
}
}
template<typename T>
void cleanup(uint *h_c, T* d_k, int *etb, uint *bc){
free(h_c);
cudaFree(d_k);
cudaFree(etb);
cudaFree(bc);
}
//This function initializes a vector to all zeros on the host (CPU)
void setToAllZero(uint* deviceVector, int length){
cudaMemset(deviceVector, 0, length * sizeof(uint));
}
//this function assigns elements to buckets
template <typename T>
__global__ void assignBucket(T* d_vector, int length, int bucketNumbers, double slope, double minimum, int* bucket, uint* bucketCount, int offset){
int idx = blockDim.x * blockIdx.x + threadIdx.x;
int bucketIndex;
extern __shared__ uint sharedBuckets[];
int index = threadIdx.x;
//variables in shared memory for fast access
__shared__ int sbucketNums;
__shared__ double sMin;
sbucketNums = bucketNumbers;
sMin = minimum;
//reading bucket counts into shared memory where increments will be performed
for(int i=0; i < (bucketNumbers/1024); i++)
if(index < bucketNumbers)
sharedBuckets[i*1024+index] = 0;
syncthreads();
//assigning elements to buckets and incrementing the bucket counts
if(idx < length) {
int i;
for(i=idx; i< length; i+=offset){
//calculate the bucketIndex for each element
bucketIndex = (d_vector[i] - sMin) * slope;
//if it goes beyond the number of buckets, put it in the last bucket
if(bucketIndex >= sbucketNums){
bucketIndex = sbucketNums - 1;
}
bucket[i] = bucketIndex;
atomicInc(&sharedBuckets[bucketIndex], length);
}
}
syncthreads();
//reading bucket counts from shared memory back to global memory
for(int i=0; i < (bucketNumbers/1024); i++)
if(index < bucketNumbers)
atomicAdd(&bucketCount[i*1024+index], sharedBuckets[i*1024+index]);
}
//this function reassigns elements to buckets
template <typename T>
__global__ void reassignBucket(T* d_vector, int *bucket, uint *bucketCount, const int bucketNumbers, const int length, const double slope, const double maximum, const double minimum, int offset, int Kbucket){
int idx = blockDim.x * blockIdx.x + threadIdx.x;
extern __shared__ uint sharedBuckets[];
int index = threadIdx.x;
int bucketIndex;
//reading bucket counts to shared memory where increments will be performed
if(index < bucketNumbers){
sharedBuckets[index] =0;
}
syncthreads();
//assigning elements to buckets and incrementing the bucket counts
if (idx < length){
int i;
for(i=idx; i<length; i+=offset){
if(bucket[i] != Kbucket){
bucket[i] = bucketNumbers+1;
}
else{
//calculate the bucketIndex for each element
bucketIndex = (d_vector[i] - minimum) * slope;
//if it goes beyond the number of buckets, put it in the last bucket
if(bucketIndex >= bucketNumbers){
bucketIndex = bucketNumbers - 1;
}
bucket[i] = bucketIndex;
atomicInc(&sharedBuckets[bucketIndex], length);
}
}
}
syncthreads();
//reading bucket counts from shared memory back to global memory
if(index < bucketNumbers){
atomicAdd(&bucketCount[index], sharedBuckets[index]);
}
}
//copy elements in the kth bucket to a new array
template <typename T>
__global__ void copyElement(T* d_vector, int length, int* elementToBucket, int bucket, T* newArray, uint* count, int offset){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx < length){
for(int i=idx; i<length; i+=offset)
//copy elements in the kth bucket to the new array
if(elementToBucket[i] == bucket)
newArray[atomicInc(count, length)] = d_vector[i];
}
}
//this function finds the bin containing the kth element we are looking for (works on the host)
inline int FindKBucket(uint *d_counter, uint *h_counter, const int numBuckets, const int k, uint * sum){
cudaMemcpy(sum, d_counter, sizeof(uint), cudaMemcpyDeviceToHost);
int Kbucket = 0;
if (*sum<k){
cudaMemcpy(h_counter, d_counter, numBuckets * sizeof(uint), cudaMemcpyDeviceToHost);
while ( (*sum<k) & (Kbucket<numBuckets-1)){
Kbucket++;
*sum += h_counter[Kbucket];
}
}
else{
cudaMemcpy(h_counter, d_counter, sizeof(uint), cudaMemcpyDeviceToHost);
}
return Kbucket;
}
/*
//this function finds the bin containing the kth element we are looking for (works on the host)
inline int FindSmartKBucket(uint *d_counter, uint *h_counter, const int num_buckets, int k, uint * sum){
cudaMemcpy(sum, d_counter, sizeof(uint), cudaMemcpyDeviceToHost);
int Kbucket = 0;
int warp_size = 32;
if (*sum<k){
while ( (*sum<k) & (Kbucket<num_buckets-1)) {
Kbucket++;
if (!((Kbucket-1)%32))
cudaMemcpy(h_counter + Kbucket, d_counter + Kbucket, warp_size * sizeof(uint), cudaMemcpyDeviceToHost);
*sum += h_counter[Kbucket];
}
}
else{
cudaMemcpy(h_counter, d_counter, sizeof(uint), cudaMemcpyDeviceToHost);
}
return Kbucket;
}
*/
template <typename T>
__global__ void GetKvalue(T* d_vector, int * d_bucket, const int Kbucket, const int n, T* Kvalue, int offset )
{
uint xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if (xIndex < n) {
int i;
for(i=xIndex; i<n; i+=offset){
if ( d_bucket[i] == Kbucket )
Kvalue[0] = d_vector[i];
}
}
}
/************************************************************************/
/************************************************************************/
//THIS IS THE PHASE TWO FUNCTION WHICH WILL BE CALLED IF THE INPUT
//LENGTH IS LESS THAN THE CUTOFF OF 2MILLION 200 THOUSAND
/************************************************************************/
template <typename T>
T phaseTwo(T* d_vector, int length, int K, int blocks, int threads, double maxValue = 0, double minValue = 0){
//declaring and initializing variables for kernel launches
int threadsPerBlock = threads;
int numBlocks = blocks;
int numBuckets = 1024;
int offset = blocks * threads;
uint sum=0, Kbucket=0, iter=0;
int Kbucket_count = 0;
//initializing variables for kernel launches
if(length < 1024){
numBlocks = 1;
}
//variable to store the end result
T kthValue =0;
//declaring and initializing other variables
size_t size = length * sizeof(int);
size_t totalBucketSize = numBuckets * sizeof(uint);
//allocate memory to store bucket assignments and to count elements in buckets
int* elementToBucket;
uint* d_bucketCount;
cudaMalloc(&elementToBucket, size);
cudaMalloc(&d_bucketCount, totalBucketSize);
uint * h_bucketCount = (uint*)malloc(totalBucketSize);
T* d_Kth_val;
cudaMalloc(&d_Kth_val, sizeof(T));
thrust::device_ptr<T>dev_ptr(d_vector);
//if max == min, then we know that it must not have had the values passed in.
if(maxValue == minValue){
thrust::pair<thrust::device_ptr<T>, thrust::device_ptr<T> > result = thrust::minmax_element(dev_ptr, dev_ptr + length);
minValue = *result.first;
maxValue = *result.second;
}
double slope = (numBuckets - 1)/(maxValue - minValue);
//first check is max is equal to min
if(maxValue == minValue){
cleanup(h_bucketCount, d_Kth_val, elementToBucket,d_bucketCount);
return maxValue;
}
//make all entries of this vector equal to zero
setToAllZero(d_bucketCount, numBuckets);
//distribute elements to bucket
assignBucket<<<numBlocks, threadsPerBlock, numBuckets*sizeof(uint)>>>(d_vector, length, numBuckets, slope, minValue, elementToBucket, d_bucketCount, offset);
//find the bucket containing the kth element we want
Kbucket = FindKBucket(d_bucketCount, h_bucketCount, numBuckets, K, &sum);
Kbucket_count = h_bucketCount[Kbucket];
while ( (Kbucket_count > 1) && (iter < 1000)){
minValue = max(minValue, minValue + Kbucket/slope);
maxValue = min(maxValue, minValue + 1/slope);
K = K - sum + Kbucket_count;
if ( maxValue - minValue > 0.0f ){
slope = (numBuckets - 1)/(maxValue-minValue);
setToAllZero(d_bucketCount, numBuckets);
reassignBucket<<< numBlocks, threadsPerBlock, numBuckets * sizeof(uint) >>>(d_vector, elementToBucket, d_bucketCount, numBuckets,length, slope, maxValue, minValue, offset, Kbucket);
sum = 0;
Kbucket = FindKBucket(d_bucketCount, h_bucketCount, numBuckets, K, &sum);
Kbucket_count = h_bucketCount[Kbucket];
iter++;
}
else{
//if the max and min are the same, then we are done
cleanup(h_bucketCount, d_Kth_val, elementToBucket, d_bucketCount);
return maxValue;
}
}
GetKvalue<<<numBlocks, threadsPerBlock >>>(d_vector, elementToBucket, Kbucket, length, d_Kth_val, offset);
cudaMemcpy(&kthValue, d_Kth_val, sizeof(T), cudaMemcpyDeviceToHost);
cudaThreadSynchronize();
cleanup(h_bucketCount, d_Kth_val, elementToBucket, d_bucketCount);
return kthValue;
}
/* this function finds the kth-largest element from the input array */
template <typename T>
T phaseOne(T* d_vector, int length, int K, int blocks, int threads, int pass = 0){
//declaring variables for kernel launches
int threadsPerBlock = threads;
int numBlocks = blocks;
int numBuckets = 1024;
int offset = blocks * threads;
int kthBucket, kthBucketCount;
int newInputLength;
int* elementToBucket; //array showing what bucket every element is in
//declaring and initializing other variables
uint *d_bucketCount, *count; //array showing the number of elements in each bucket
uint kthBucketScanner = 0;
size_t size = length * sizeof(int);
//variable to store the end result
T kthValue = 0;
T* newInput;
//find max and min with thrust
double maximum, minimum;
thrust::device_ptr<T>dev_ptr(d_vector);
thrust::pair<thrust::device_ptr<T>, thrust::device_ptr<T> > result = thrust::minmax_element(dev_ptr, dev_ptr + length);
minimum = *result.first;
maximum = *result.second;
//if the max and the min are the same, then we are done
if(maximum == minimum){
return maximum;
}
//if we want the max or min just return it
if(K == 1){
return minimum;
}
if(K == length){
return maximum;
}
//Allocate memory to store bucket assignments
CUDA_CALL(cudaMalloc(&elementToBucket, size));
//Allocate memory to store bucket counts
size_t totalBucketSize = numBuckets * sizeof(uint);
CUDA_CALL(cudaMalloc(&d_bucketCount, totalBucketSize));
uint* h_bucketCount = (uint*)malloc(totalBucketSize);
//Calculate max-min
double range = maximum - minimum;
//Calculate the slope, i.e numBuckets/range
double slope = (numBuckets - 1)/range;
cudaMalloc(&count, sizeof(uint));
//Set the bucket count vector to all zeros
setToAllZero(d_bucketCount, numBuckets);
//Distribute elements into their respective buckets
assignBucket<<<numBlocks, threadsPerBlock, numBuckets*sizeof(uint)>>>(d_vector, length, numBuckets, slope, minimum, elementToBucket, d_bucketCount, offset);
kthBucket = FindKBucket(d_bucketCount, h_bucketCount, numBuckets, K, & kthBucketScanner);
kthBucketCount = h_bucketCount[kthBucket];
printf("naive kthBucketCount = %d\n", kthBucketCount);
//we must update K since we have reduced the problem size to elements in the kth bucket
if(kthBucket != 0){
K = kthBucketCount - (kthBucketScanner - K);
}
//copy elements in the kth bucket to a new array
cudaMalloc(&newInput, kthBucketCount * sizeof(T));
setToAllZero(count, 1);
copyElement<<<numBlocks, threadsPerBlock>>>(d_vector, length, elementToBucket, kthBucket, newInput, count, offset);
//store the length of the newly copied elements
newInputLength = kthBucketCount;
//if we only copied one element, then we are done
if(newInputLength == 1){
thrust::device_ptr<T>new_ptr(newInput);
kthValue = new_ptr[0];
//free all used memory
cudaFree(elementToBucket); cudaFree(d_bucketCount); cudaFree(count); cudaFree(newInput);
return kthValue;
}
/*********************************************************************/
//END OF FIRST PASS, NOW WE PROCEED TO SUBSEQUENT PASSES
/*********************************************************************/
//if the new length is greater than the CUTOFF, run the regular phaseOne again
if(newInputLength > CUTOFF_POINT && pass < 1){
if(pass > 0){
cudaFree(d_vector);
}
cudaFree(elementToBucket); cudaFree(d_bucketCount); cudaFree(count);
kthValue = phaseOne(newInput, newInputLength, K, blocks, threads,pass + 1);
}
else{
minimum = max(minimum, minimum + kthBucket/slope);
maximum = min(maximum, minimum + 1/slope);
kthValue = phaseTwo(newInput,newInputLength, K, blocks, threads,maximum, minimum);
}
//free all used memory
cudaFree(elementToBucket); cudaFree(d_bucketCount); cudaFree(newInput); cudaFree(count);
return kthValue;
}
/**************************************************************************/
/**************************************************************************/
//THIS IS THE BUCKETSELECT FUNCTION WRAPPER THAT CHOOSES THE CORRECT VERSION
//OF BUCKET SELECT TO RUN BASED ON THE INPUT LENGTH
/**************************************************************************/
template <typename T>
T bucketSelectWrapper(T* d_vector, int length, int K, int blocks, int threads)
{
T kthValue;
//change K to be the kth smallest
K = length - K + 1;
if(length <= CUTOFF_POINT)
{
kthValue = phaseTwo(d_vector, length, K, blocks, threads);
return kthValue;
}
else
{
kthValue = phaseOne(d_vector, length, K, blocks, threads);
return kthValue;
}
}
}