-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrandomizedBucketSelect.cu
881 lines (701 loc) · 31.5 KB
/
randomizedBucketSelect.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/* 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/sort.h>
#include <thrust/transform_reduce.h>
#include <thrust/random.h>
#include <thrust/functional.h>
namespace RandomizedBucketSelect{
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)
/// ***********************************************************
/// ***********************************************************
/// **** HELPER CPU FUNCTIONS
/// ***********************************************************
/// ***********************************************************
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));
}
/// ***********************************************************
/// ***********************************************************
/// **** HELPER GPU FUNCTIONS-KERNELS
/// ***********************************************************
/// ***********************************************************
//this function assigns elements to buckets based off of a randomized sampling of the elements in the vector
template <typename T>
__global__ void assignSmartBucket(T * d_vector, int length, int numBuckets, double * slopes, T * pivots, int numPivots, uint* elementToBucket, uint* bucketCount, int offset){
int index = blockDim.x * blockIdx.x + threadIdx.x;
uint bucketIndex;
int threadIndex = threadIdx.x;
//variables in shared memory for fast access
__shared__ int sharedNumSmallBuckets;
if (threadIndex < 1)
sharedNumSmallBuckets = numBuckets / (numPivots-1);
extern __shared__ uint array[];
uint * sharedBuckets = (uint *)array;
double * sharedSlopes = (double *)&sharedBuckets[numBuckets];
T * sharedPivots = (T *)&sharedSlopes[numPivots-1];
/*
uint * sharedBuckets = (uint *)array;
double * sharedSlopes = (double *)&sharedBuckets[numBuckets];
T * sharedPivots = (T *)&sharedSlopes[numPivots-1];
// statically allocating the array gives faster results
__shared__ double sharedSlopes[NUM_PIVOTS-1];
__shared__ T sharedPivots[NUM_PIVOTS];
*/
//reading bucket counts into shared memory where increments will be performed
for (int i = 0; i < (numBuckets / MAX_THREADS_PER_BLOCK); i++)
if (threadIndex < numBuckets)
sharedBuckets[i * MAX_THREADS_PER_BLOCK + threadIndex] = 0;
if(threadIndex < numPivots) {
*(sharedPivots + threadIndex) = *(pivots + threadIndex);
if(threadIndex < numPivots-1)
sharedSlopes[threadIndex] = slopes[threadIndex];
}
syncthreads();
//assigning elements to buckets and incrementing the bucket counts
if(index < length) {
int i;
for(i = index; i < length; i += offset) {
T num = d_vector[i];
int minPivotIndex = 0;
int maxPivotIndex = numPivots-1;
int midPivotIndex;
// find the index of the pivot that is the greatest s.t. lower than or equal to num using binary search
//while (maxPivotIndex > minPivotIndex+1) {
for(int j = 1; j < numPivots - 1; j*=2) {
midPivotIndex = (maxPivotIndex + minPivotIndex) / 2;
if (num >= sharedPivots[midPivotIndex])
minPivotIndex = midPivotIndex;
else
maxPivotIndex = midPivotIndex;
}
bucketIndex = (minPivotIndex * sharedNumSmallBuckets) + (int) ((num - sharedPivots[minPivotIndex]) * sharedSlopes[minPivotIndex]);
elementToBucket[i] = bucketIndex;
// hashmap implementation set[bucketindex]=add.i;
//bucketCount[blockIdx.x * numBuckets + bucketIndex]++;
atomicInc (sharedBuckets + bucketIndex, length);
}
}
syncthreads();
//reading bucket counts from shared memory back to global memory
for (int i = 0; i < (numBuckets / MAX_THREADS_PER_BLOCK); i++)
if (threadIndex < numBuckets)
//atomicAdd(bucketCount + blockIdx.x * numBuckets + i * MAX_THREADS_PER_BLOCK + threadIndex, sharedBuckets[i * MAX_THREADS_PER_BLOCK + threadIndex]);
*(bucketCount + blockIdx.x * numBuckets + i * MAX_THREADS_PER_BLOCK + threadIndex) = *(sharedBuckets + i * MAX_THREADS_PER_BLOCK + threadIndex);
}
//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]);
}
}
//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 findKBucket(uint * d_bucketCount, uint * h_bucketCount, int numBuckets, int k, uint * sum, int numBlocks){
int sumsRowIndex= numBuckets * (numBlocks-1);
/*
for(int j=0; j<numBuckets; j++)
CUDA_CALL(cudaMemcpy(h_bucketCount + j, d_bucketCount + sumsRowIndex + j, sizeof(uint), cudaMemcpyDeviceToHost));
*/
CUDA_CALL(cudaMemcpy(h_bucketCount, d_bucketCount + sumsRowIndex, sizeof(uint) * numBuckets, cudaMemcpyDeviceToHost));
int kBucket = 0;
uint scanner = h_bucketCount[0];
while ((scanner < k) & (kBucket < numBuckets - 1)) {
kBucket++;
scanner += h_bucketCount[kBucket];
}
*(sum) = scanner - h_bucketCount[kBucket];
return kBucket;
}
__global__ void sumCounts(uint * d_bucketCount, const int numBuckets, const int numBlocks) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
for(int j=1; j<numBlocks; j++)
d_bucketCount[index + numBuckets*j] += d_bucketCount[index + numBuckets*(j-1)];
}
//copy elements in the kth bucket to a new array
template <typename T>
__global__ void copyElements (T* d_vector, int length, uint* elementToBucket, const int bucket, T* newArray, uint offset, uint * d_bucketCount, int numTotalBuckets){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int threadIndex = threadIdx.x;
__shared__ uint sharedBucket;
__shared__ uint sharedBucketCount;
if(threadIndex < 1) {
sharedBucket = bucket;
sharedBucketCount = d_bucketCount[blockIdx.x * numTotalBuckets + bucket];
}
syncthreads();
if(idx < length) {
for(int i=idx; i<length; i+=offset) {
if (elementToBucket[i] == sharedBucket)
//newArray[atomicDec(d_bucketCount + blockIdx.x * numTotalBuckets + temp, length)-1] = d_vector[i];
newArray[atomicDec(&sharedBucketCount, length) - 1] = d_vector[i];
}
}
}
//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];
}
}
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];
}
}
}
/// ***********************************************************
/// ***********************************************************
/// **** GENERATE PIVOTS
/// ***********************************************************
/// ***********************************************************
__host__ __device__
unsigned int hash(unsigned int a) {
a = (a+0x7ed55d16) + (a<<12);
a = (a^0xc761c23c) ^ (a>>19);
a = (a+0x165667b1) + (a<<5);
a = (a+0xd3a2646c) ^ (a<<9);
a = (a+0xfd7046c5) + (a<<3);
a = (a^0xb55a4f09) ^ (a>>16);
return a;
}
struct RandomNumberFunctor :
public thrust::unary_function<unsigned int, float> {
unsigned int mainSeed;
RandomNumberFunctor(unsigned int _mainSeed) :
mainSeed(_mainSeed) {}
__host__ __device__
float operator()(unsigned int threadIdx)
{
unsigned int seed = hash(threadIdx) * mainSeed;
thrust::default_random_engine rng(seed);
rng.discard(threadIdx);
thrust::uniform_real_distribution<float> u(0, 1);
return u(rng);
}
};
template <typename T>
void createRandomVector(T * d_vec, int size) {
timeval t1;
uint seed;
gettimeofday(&t1, NULL);
seed = t1.tv_usec * t1.tv_sec;
thrust::device_ptr<T> d_ptr(d_vec);
thrust::transform (thrust::counting_iterator<uint>(0),thrust::counting_iterator<uint>(size), d_ptr, RandomNumberFunctor(seed));
}
template <typename T>
__global__ void enlargeIndexAndGetElements (T * in, T * list, int size) {
*(in + blockIdx.x*blockDim.x + threadIdx.x) = *(list + ((int) (*(in + blockIdx.x * blockDim.x + threadIdx.x) * size)));
}
__global__ void enlargeIndexAndGetElements (float * in, uint * out, uint * list, int size) {
*(out + blockIdx.x * blockDim.x + threadIdx.x) = (uint) *(list + ((int) (*(in + blockIdx.x * blockDim.x + threadIdx.x) * size)));
}
template <typename T>
void generatePivots (T * pivots, double * slopes, T * d_list, int sizeOfVector, int numPivots, int sizeOfSample, int totalSmallBuckets, T min, T max) {
T * d_randoms;
int endOffset = 22;
int pivotOffset = (sizeOfSample - endOffset * 2) / (numPivots - 3);
int numSmallBuckets = totalSmallBuckets / (numPivots - 1);
cudaMalloc (&d_randoms, sizeof (T) * sizeOfSample);
createRandomVector (d_randoms, sizeOfSample);
// converts randoms floats into elements from necessary indices
enlargeIndexAndGetElements<<<(sizeOfSample/MAX_THREADS_PER_BLOCK), MAX_THREADS_PER_BLOCK>>>(d_randoms, d_list, sizeOfVector);
pivots[0] = min;
pivots[numPivots - 1] = max;
thrust::device_ptr<T>randoms_ptr(d_randoms);
thrust::sort(randoms_ptr, randoms_ptr + sizeOfSample);
cudaThreadSynchronize();
// set the pivots which are endOffset away from the min and max pivots
cudaMemcpy (pivots + 1, d_randoms + endOffset - 1, sizeof (T), cudaMemcpyDeviceToHost);
cudaMemcpy (pivots + numPivots - 2, d_randoms + sizeOfSample - endOffset - 1, sizeof (T), cudaMemcpyDeviceToHost);
slopes[0] = numSmallBuckets / (double) (pivots[1] - pivots[0]);
for (register int i = 2; i < numPivots - 2; i++) {
cudaMemcpy (pivots + i, d_randoms + pivotOffset * (i - 1) + endOffset - 1, sizeof (T), cudaMemcpyDeviceToHost);
slopes[i - 1] = numSmallBuckets / (double) (pivots[i] - pivots[i - 1]);
}
slopes[numPivots - 3] = numSmallBuckets / (double) (pivots[numPivots - 2] - pivots[numPivots - 3]);
slopes[numPivots - 2] = numSmallBuckets / (double) (pivots[numPivots - 1] - pivots[numPivots - 2]);
cudaFree(d_randoms);
}
/************************************************************************/
/************************************************************************/
//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("original 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;
}
/************************* BEGIN MAIN FUNCTIONS FOR RANDOMIZEDBLOCKEDBUCKETSELECT ************************/
/************************* BEGIN MAIN FUNCTIONS FOR RANDOMIZEDBLOCKEDBUCKETSELECT ************************/
/************************* BEGIN MAIN FUNCTIONS FOR RANDOMIZEDBLOCKEDBUCKETSELECT ************************/
/// ***********************************************************
/// ***********************************************************
/// **** MAIN FUNCTION
/// ***********************************************************
/// ***********************************************************
/* this function finds the kth-largest element from the input array */
template <typename T>
T phaseOneR(T* d_vector, int length, int K, int blocks, int threads, int pass = 0){
/// ***********************************************************
/// ****STEP 1: Find Min and Max of the whole vector
/// ****We don't need to go through the rest of the algorithm if it's flat
/// ***********************************************************
T 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;
}
/// ***********************************************************
/// ****STEP 2: Declare variables and allocate memory
/// **** Declare Variables
/// ***********************************************************
//declaring variables for kernel launches
int threadsPerBlock = threads;
int numBlocks = blocks;
int numBuckets = 4096;
int offset = blocks * threads;
// variables for the randomized selection
int numPivots = NUM_PIVOTS;
int sampleSize = MAX_THREADS_PER_BLOCK;
// pivot variables
double slopes[numPivots - 1];
double * d_slopes;
T pivots[numPivots];
T * d_pivots;
//Allocate memory to store bucket assignments
size_t size = length * sizeof(uint);
uint* d_elementToBucket; //array showing what bucket every element is in
CUDA_CALL(cudaMalloc(&d_elementToBucket, size));
//Allocate memory to store bucket counts
size_t totalBucketSize = numBlocks * numBuckets * sizeof(uint);
uint h_bucketCount[numBuckets]; //array showing the number of elements in each bucket
uint * d_bucketCount;
CUDA_CALL(cudaMalloc(&d_bucketCount, totalBucketSize));
// bucket counters
int kthBucket;
uint kthBucketScanner = 0;
// variable to store the end result
int newInputLength;
T* newInput;
T kthValue = 0;
/// ***********************************************************
/// ****STEP 3: Generate Pivots and Slopes
/// Declare slopes and pivots
/// ***********************************************************
CUDA_CALL(cudaMalloc(&d_slopes, (numPivots - 1) * sizeof(double)));
CUDA_CALL(cudaMalloc(&d_pivots, numPivots * sizeof(T)));
//Find bucket sizes using a randomized selection
generatePivots<T>(pivots, slopes, d_vector, length, numPivots, sampleSize, numBuckets, minimum, maximum);
// make any slopes that were infinity due to division by zero (due to no
// difference between the two associated pivots) into zero, so all the
// values which use that slope are projected into a single bucket
for (register int i = 0; i < numPivots - 1; i++)
if (isinf(slopes[i]))
slopes[i] = 0;
CUDA_CALL(cudaMemcpy(d_slopes, slopes, (numPivots - 1) * sizeof(double), cudaMemcpyHostToDevice));
CUDA_CALL(cudaMemcpy(d_pivots, pivots, numPivots * sizeof(T), cudaMemcpyHostToDevice));
/// ***********************************************************
/// ****STEP 4: Assign elements to buckets
///
/// ***********************************************************
//Distribute elements into their respective buckets
assignSmartBucket<T><<<numBlocks, threadsPerBlock, numPivots * sizeof(T) + (numPivots-1) * sizeof(double) + numBuckets * sizeof(uint)>>>(d_vector, length, numBuckets, d_slopes, d_pivots, numPivots, d_elementToBucket, d_bucketCount, offset);
sumCounts<<<numBuckets/threadsPerBlock, threadsPerBlock>>>(d_bucketCount, numBuckets, numBlocks);
/// ***********************************************************
/// ****STEP 5: Find the kth buckets
/// and their respective update indices
/// ***********************************************************
kthBucket = findKBucket(d_bucketCount, h_bucketCount, numBuckets, K, &kthBucketScanner, numBlocks);
newInputLength = h_bucketCount[kthBucket];
K -= kthBucketScanner;
printf("original kthBucketCount = %d\n", newInputLength);
/// ***********************************************************
/// ****STEP 6: Copy the kth buckets
/// only unique ones
/// ***********************************************************
// allocate memories
CUDA_CALL(cudaMalloc(&newInput, newInputLength * sizeof(T)));
copyElements<T><<<numBlocks, threadsPerBlock>>>(d_vector, length, d_elementToBucket, kthBucket, newInput, offset, d_bucketCount, numBuckets);
//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(d_bucketCount);
cudaFree(d_elementToBucket);
cudaFree(d_pivots);
cudaFree(d_slopes);
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(d_bucketCount);
cudaFree(d_elementToBucket);
cudaFree(d_pivots);
cudaFree(d_slopes);
kthValue = phaseOne(newInput, newInputLength, K, blocks, threads,pass + 1);
}
else{
// find boundaries of kth bucket
int pivotOffset = numBuckets / (numPivots - 1);
int pivotIndex = kthBucket/pivotOffset;
int pivotInnerindex = kthBucket - pivotOffset * pivotIndex;
minimum = max(minimum, (T) (pivots[pivotIndex] + pivotInnerindex / slopes[pivotIndex]));
maximum = min(maximum, (T) (pivots[pivotIndex] + (pivotInnerindex+1) / slopes[pivotIndex]));
if (newInputLength<33000) {
thrust::device_ptr<T>newInput_ptr(newInput);
thrust::sort(newInput_ptr, newInput_ptr + newInputLength);
cudaMemcpy (&kthValue, newInput + K - 1, sizeof (T), cudaMemcpyDeviceToHost);
} else
kthValue = phaseTwo(newInput,newInputLength, K, blocks, threads,maximum, minimum);
}
//free all used memory
cudaFree(d_elementToBucket);
cudaFree(d_bucketCount);
cudaFree(d_slopes);
cudaFree(d_pivots);
cudaFree(newInput);
return kthValue;
}
/**************************************************************************/
/**************************************************************************/
//THIS IS THE RANDOMIZEDBUCKETSELECT FUNCTION WRAPPER THAT CHOOSES THE CORRECT
//VERSION OF BUCKET SELECT TO RUN BASED ON THE INPUT LENGTH
/**************************************************************************/
template <typename T>
T randomizedBucketSelectWrapper(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
{
//printf("Call PhaseOneR in parent function.\n");
kthValue = phaseOneR(d_vector, length, K, blocks, threads);
// printf("After Call PhaseOneR in parent function, kthvalue = %f.\n", kthValue);
return kthValue;
}
}
}