-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPU.cu
executable file
·1775 lines (1437 loc) · 66.7 KB
/
GPU.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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//precompute direct neighbors with the GPU:
#include "GPU.h"
#include "kernel.h"
#include "SortByWorkload.h"
#include "structs.h"
#include "params.h"
#include "WorkQueue.h"
#include <cuda_runtime.h>
#include <cuda.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <unistd.h>
#include "omp.h"
// //thrust
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/device_ptr.h>
#include <thrust/system/cuda/execution_policy.h> // for streams for thrust (added with Thrust v1.8)
//
// //for warming up GPU:
#include <thrust/copy.h>
#include <thrust/fill.h>
#include <thrust/sequence.h>
//elements for the result set
//FOR A SINGLE KERNEL INVOCATION
//NOT FOR THE BATCHED ONE
#define BUFFERELEM 300000000 //400000000-original (when removing the data from the device before putting it back for the sort)
//FOR THE BATCHED EXECUTION:
//#define BATCHTOTALELEM 1200000000 //THE TOTAL SIZE ALLOCATED ON THE HOST
//THE NUMBER OF BATCHES AND THE SIZE OF THE BUFFER FOR EACH KERNEL EXECUTION ARE NOT RELATED TO THE TOTAL NUMBER
//OF ELEMENTS (ABOVE).
// #define NUMBATCHES 20
// #define BATCHBUFFERELEM 100000000 //THE SMALLER SIZE ALLOCATED ON THE DEVICE FOR EACH KERNEL EXECUTION
// #define GPUSTREAMS 1 //number of concurrent gpu streams, now defined in params.h
using std::cout;
using std::endl;
//sort ascending
bool compareByPointValue(const key_val_sort &a, const key_val_sort &b)
{
return a.value_at_dim < b.value_at_dim;
}
uint64_t getLinearID_nDimensions2(unsigned int * indexes, unsigned int * dimLen, unsigned int nDimensions) {
uint64_t index = 0;
uint64_t multiplier = 1;
for (int i = 0; i<nDimensions; i++){
index += (uint64_t)indexes[i] * multiplier;
multiplier *= dimLen[i];
}
return index;
}
////////////////////////////////////////////////////////////////////////////////
void gridIndexingGPU(
unsigned int * DBSIZE,
uint64_t totalCells,
DTYPE * database,
DTYPE ** dev_database,
DTYPE * epsilon,
DTYPE ** dev_epsilon,
DTYPE * minArr,
DTYPE ** dev_minArr,
struct grid ** index,
struct grid ** dev_index,
unsigned int * indexLookupArr,
unsigned int ** dev_indexLookupArr,
struct gridCellLookup ** gridCellLookupArr,
struct gridCellLookup ** dev_gridCellLookupArr,
unsigned int * nNonEmptyCells,
unsigned int ** dev_nNonEmptyCells,
unsigned int * nCells,
unsigned int ** dev_nCells)
{
cudaError_t errCode;
double tStartAllocGPU = omp_get_wtime();
errCode = cudaMalloc( (void**)dev_database, sizeof(DTYPE) * (GPUNUMDIM) * (*DBSIZE));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Alloc database -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMalloc( (void**)dev_epsilon, sizeof(DTYPE));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Alloc epsilon -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMalloc((void**)dev_minArr, sizeof(DTYPE) * (NUMINDEXEDDIM));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Alloc minArr -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMalloc( (void**)dev_indexLookupArr, sizeof(unsigned int) * (*DBSIZE));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: lookup array allocation -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMalloc((void**)dev_nNonEmptyCells, sizeof(unsigned int));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Alloc nNonEmptyCells -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMalloc((void**)dev_nCells, sizeof(unsigned int) * (NUMINDEXEDDIM));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Alloc nCells -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
uint64_t * dev_pointCellArr;
errCode = cudaMalloc((void**)&dev_pointCellArr, sizeof(uint64_t) * (*DBSIZE));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: point cell array alloc -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
unsigned int * dev_databaseVal;
errCode = cudaMalloc((void**)&dev_databaseVal, sizeof(unsigned int) * (*DBSIZE));
if (errCode != cudaSuccess) {
cout << "[INDEX] ~ Error: Alloc databaseVal -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
unsigned int * N = new unsigned int;
unsigned int * dev_N;
errCode = cudaMalloc((void**)&dev_N, sizeof(unsigned int) * GPUSTREAMS);
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Alloc dev_N -- error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
double tEndAllocGPU = omp_get_wtime();
cout << "[INDEX] ~ Time to allocate on the GPU: " << tEndAllocGPU - tStartAllocGPU << "\n\n";
cout.flush();
////////////////////////////////////////////////////////////////////////////
double tStartCopyGPU = omp_get_wtime();
errCode = cudaMemcpy( (*dev_database), database, sizeof(DTYPE) * (GPUNUMDIM) * (*DBSIZE), cudaMemcpyHostToDevice );
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: database copy to device -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy( (*dev_epsilon), epsilon, sizeof(DTYPE), cudaMemcpyHostToDevice );
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: epsilon copy to device -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy( (*dev_minArr), minArr, sizeof(DTYPE) * (NUMINDEXEDDIM), cudaMemcpyHostToDevice );
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Copy minArr to device -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy( (*dev_nCells), nCells, sizeof(unsigned int) * (NUMINDEXEDDIM), cudaMemcpyHostToDevice );
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Copy nCells to device -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy(dev_N, DBSIZE, sizeof(unsigned int), cudaMemcpyHostToDevice);
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: database size Got error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
double tEndCopyGPU = omp_get_wtime();
cout << "[INDEX] ~ Time to copy to the GPU: " << tEndCopyGPU - tStartCopyGPU << "\n\n";
cout.flush();
////////////////////////////////////////////////////////////////////////////
const int TOTALBLOCKS = ceil((1.0 * (*DBSIZE)) / (1.0 * BLOCKSIZE));
printf("[INDEX] ~ Total blocks: %d\n",TOTALBLOCKS);
kernelIndexComputeNonemptyCells<<<TOTALBLOCKS, BLOCKSIZE>>>((*dev_database), dev_N, (*dev_epsilon), (*dev_minArr),
(*dev_nCells), dev_pointCellArr, nullptr, false);
cudaDeviceSynchronize();
thrust::device_ptr<uint64_t> dev_pointCellArr_ptr(dev_pointCellArr);
thrust::device_ptr<uint64_t> dev_new_end;
try
{
//first sort
thrust::sort(thrust::device, dev_pointCellArr_ptr, dev_pointCellArr_ptr + (*DBSIZE)); //, thrust::greater<uint64_t>()
//then unique
dev_new_end = thrust::unique(thrust::device, dev_pointCellArr_ptr, dev_pointCellArr_ptr + (*DBSIZE));
}
catch(std::bad_alloc &e)
{
std::cerr << "[INDEX] ~ Ran out of memory while sorting" << std::endl;
exit(-1);
}
uint64_t * new_end = thrust::raw_pointer_cast(dev_new_end);
uint64_t numNonEmptyCells = std::distance(dev_pointCellArr_ptr, dev_new_end);
printf("[INDEX] ~ Number of full cells (non-empty): %lu\n", numNonEmptyCells);
*nNonEmptyCells = numNonEmptyCells;
(*gridCellLookupArr) = new struct gridCellLookup[numNonEmptyCells];
uint64_t * pointCellArrTmp = new uint64_t[numNonEmptyCells];
errCode = cudaMemcpy(pointCellArrTmp, dev_pointCellArr, sizeof(uint64_t) * numNonEmptyCells, cudaMemcpyDeviceToHost);
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: pointCellArrTmp memcpy Got error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
for (uint64_t i = 0; i < numNonEmptyCells; ++i)
{
(*gridCellLookupArr)[i].idx = i;
(*gridCellLookupArr)[i].gridLinearID = pointCellArrTmp[i];
}
kernelIndexComputeNonemptyCells<<<TOTALBLOCKS, BLOCKSIZE>>>((*dev_database), dev_N, (*dev_epsilon), (*dev_minArr),
(*dev_nCells), dev_pointCellArr, dev_databaseVal, true);
try
{
thrust::sort_by_key(thrust::device, dev_pointCellArr, dev_pointCellArr + (*DBSIZE), dev_databaseVal);
}
catch(std::bad_alloc &e)
{
std::cerr << "[INDEX] ~ Ran out of memory while sorting key/value pairs" << std::endl;
exit(-1);
}
uint64_t * cellKey = new uint64_t[(*DBSIZE)];
errCode = cudaMemcpy(cellKey, dev_pointCellArr, sizeof(uint64_t) * (*DBSIZE), cudaMemcpyDeviceToHost);
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: pointCellArr memcpy Got error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy(indexLookupArr, dev_databaseVal, sizeof(unsigned int) * (*DBSIZE), cudaMemcpyDeviceToHost);
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: databaseIDValue memcpy Got error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
(*index) = new grid[numNonEmptyCells];
(*index)[0].indexmin = 0;
uint64_t cnt=0;
for (uint64_t i = 1; i < (*DBSIZE); ++i)
{
if (cellKey[i - 1] != cellKey[i])
{
//grid index
cnt++;
(*index)[cnt].indexmin = i;
(*index)[cnt - 1].indexmax = i - 1;
}
}
(*index)[numNonEmptyCells - 1].indexmax = (*DBSIZE) - 1;
printf("[INDEX] ~ Full cells: %d (%f, fraction full)\n", (unsigned int)numNonEmptyCells, numNonEmptyCells * 1.0 / double(totalCells));
printf("[INDEX] ~ Empty cells: %ld (%f, fraction empty)\n", totalCells - (unsigned int)numNonEmptyCells, (totalCells - numNonEmptyCells * 1.0) / double(totalCells));
printf("[INDEX] ~ Size of index that would be sent to GPU (GiB) -- (if full index sent), excluding the data lookup arr: %f\n",
(double)sizeof(struct grid) * (totalCells) / (1024.0 * 1024.0 * 1024.0));
printf("[INDEX] ~ Size of compressed index to be sent to GPU (GiB), excluding the data and grid lookup arr: %f\n",
(double)sizeof(struct grid) * (numNonEmptyCells * 1.0) / (1024.0 * 1024.0 * 1024.0));
printf("[INDEX] ~ When copying from entire index to compressed index: number of non-empty cells: %lu\n", numNonEmptyCells);
////////////////////////////////////////////////////////////////////////////
errCode = cudaMalloc( (void**)dev_index, sizeof(struct grid) * (*nNonEmptyCells));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: Alloc grid index -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMalloc( (void**)dev_gridCellLookupArr, sizeof(struct gridCellLookup) * (*nNonEmptyCells));
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: copy grid cell lookup array allocation -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
////////////////////////////////////////////////////////////////////////////
errCode = cudaMemcpy( (*dev_nNonEmptyCells), nNonEmptyCells, sizeof(unsigned int), cudaMemcpyHostToDevice );
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: nNonEmptyCells copy to device -- error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy((*dev_index), (*index), sizeof(struct grid) * numNonEmptyCells, cudaMemcpyHostToDevice);
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: index copy to the GPU error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy((*dev_indexLookupArr), indexLookupArr, sizeof(unsigned int) * (*DBSIZE), cudaMemcpyHostToDevice);
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: index lookup array copy to the GPU error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy((*dev_gridCellLookupArr), (*gridCellLookupArr), sizeof(struct gridCellLookup) * numNonEmptyCells, cudaMemcpyHostToDevice);
if (errCode != cudaSuccess)
{
cout << "[INDEX] ~ Error: grid lookup array copy to the GPU error with code " << errCode << '\n';
cout << "[INDEX] ~ Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
////////////////////////////////////////////////////////////////////////////
delete N;
delete[] pointCellArrTmp;
cudaFree(dev_pointCellArr);
cudaFree(dev_databaseVal);
cudaFree(dev_N);
double tEndIndexGPU = omp_get_wtime();
cout << "[INDEX] ~ Time to index using the GPU (including allocating and transfering memory): " << tEndIndexGPU - tStartAllocGPU << '\n';
cout.flush();
}
unsigned long long GPUBatchEst_v2(
int searchMode,
unsigned int * DBSIZE,
float staticPartition,
DTYPE * dev_database,
unsigned int * dev_originPointIndex,
DTYPE * dev_epsilon,
struct grid * dev_grid,
unsigned int * dev_indexLookupArr,
struct gridCellLookup * dev_gridCellLookupArr,
DTYPE * dev_minArr,
unsigned int * dev_nCells,
unsigned int * dev_nNonEmptyCells,
unsigned int * retNumBatches,
unsigned int * retGPUBufferSize,
std::vector< std::pair<unsigned int, unsigned int> > * batches)
{
cudaError_t errCode;
cout << "[GPU] ~ Estimating batches\n";
// Parameters for the batch size estimation.
double sampleRate = 0.10;
int offsetRate = 1.0 / sampleRate;
cout << "[GPU] ~ Sample rate: " << sampleRate << ", offset: " << offsetRate << '\n';
/////////////////
// N GPU threads
////////////////
unsigned int * dev_N_batchEst;
unsigned int * N_batchEst = new unsigned int;
unsigned int partitionedDBSIZE = (*DBSIZE) * staticPartition;
if (SM_HYBRID_STATIC == searchMode && STATIC_SPLIT_QUERIES)
{
// Split the worked based on the number of queries, so also reduce the number of queries to estimate
(*N_batchEst) = partitionedDBSIZE * sampleRate;
} else {
// Searchmode is either GPU alone, dynamic hybrid, or the workload is statically split
// based on the number of candidate points to refine, and so we estimate all the query points
// in all mentionned cases
(*N_batchEst) = (*DBSIZE) * sampleRate;
}
errCode = cudaMalloc((void**)&dev_N_batchEst, sizeof(unsigned int));
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: dev_N_batchEst Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy(dev_N_batchEst, N_batchEst, sizeof(unsigned int), cudaMemcpyHostToDevice);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: N batchEST Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
/////////////
// count the result set size
////////////
unsigned int * dev_cnt_batchEst;
unsigned int * cnt_batchEst = new unsigned int;
(*cnt_batchEst) = 0;
errCode = cudaMalloc((void**)&dev_cnt_batchEst, sizeof(unsigned int));
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: dev_cnt_batchEst Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy(dev_cnt_batchEst, cnt_batchEst, sizeof(unsigned int), cudaMemcpyHostToDevice);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: dev_cnt_batchEst Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
//////////////////
// Sample offset - To sample the data to estimate the total number of key/value pairs
/////////////////
unsigned int * dev_sampleOffset;
unsigned int * sampleOffset = new unsigned int;
(*sampleOffset) = offsetRate;
errCode = cudaMalloc((void**)&dev_sampleOffset, sizeof(unsigned int));
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: sample offset Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
errCode = cudaMemcpy(dev_sampleOffset, sampleOffset, sizeof(unsigned int), cudaMemcpyHostToDevice);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: dev_sampleOffset Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
//////////////////
// To save the estimated number of neighbors of points
//////////////////
unsigned int * dev_estimatedResult;
unsigned int * estimatedResult = new unsigned int[(*N_batchEst)];
errCode = cudaMalloc((void**)&dev_estimatedResult, (*N_batchEst) * sizeof(unsigned int));
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: estimated result Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
int nbBlockTmp;
if (searchMode == SM_HYBRID_STATIC)
{
#if STATIC_SPLIT_QUERIES
nbBlockTmp = ceil((1.0 * partitionedDBSIZE * sampleRate) / (1.0 * BLOCKSIZE));
#else
nbBlockTmp = ceil((1.0 * (*DBSIZE) * sampleRate) / (1.0 * BLOCKSIZE));
#endif
} else {
nbBlockTmp = ceil((1.0 * (*DBSIZE) * sampleRate) / (1.0 * BLOCKSIZE));
}
cout << "[GPU] ~ Total blocks: " << nbBlockTmp << '\n';
cout.flush();
cout << "[GPU] ~ Estimating batch without using pattern\n";
cout.flush();
const int TOTALBLOCKSBATCHEST = nbBlockTmp;
#if SORT_BY_WORKLOAD
kernelNDGridIndexBatchEstimator_v2<<<TOTALBLOCKSBATCHEST, BLOCKSIZE>>>(dev_N_batchEst, dev_sampleOffset,
dev_database, dev_originPointIndex, dev_epsilon, dev_grid, dev_indexLookupArr, dev_gridCellLookupArr, dev_minArr,
dev_nCells, dev_cnt_batchEst, dev_nNonEmptyCells, dev_estimatedResult);
#else
kernelNDGridIndexBatchEstimator_v2<<<TOTALBLOCKSBATCHEST, BLOCKSIZE>>>(dev_N_batchEst, dev_sampleOffset,
dev_database, nullptr, dev_epsilon, dev_grid, dev_indexLookupArr, dev_gridCellLookupArr, dev_minArr,
dev_nCells, dev_cnt_batchEst, dev_nNonEmptyCells, dev_estimatedResult);
#endif
cout << "[GPU] ~ ERROR FROM KERNEL LAUNCH OF BATCH ESTIMATOR: " << cudaGetLastError() << '\n';
cout.flush();
errCode = cudaMemcpy(cnt_batchEst, dev_cnt_batchEst, sizeof(unsigned int), cudaMemcpyDeviceToHost);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: getting cnt for batch estimate from GPU Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
} else {
cout << "[GPU] ~ Result set size for estimating the number of batches (sampled): " << *cnt_batchEst << '\n';
cout.flush();
}
errCode = cudaMemcpy(estimatedResult, dev_estimatedResult, (*N_batchEst) * sizeof(unsigned int), cudaMemcpyDeviceToHost);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: getting estimated results for batch estimate from GPU Got error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
unsigned int GPUBufferSize = 50000000;
// unsigned int GPUBufferSize = 100000000;
// uint64_t estimatedNeighbors = (uint64_t)*cnt_batchEst * (uint64_t)offsetRate;
// cout << "[GPU] ~ From GPU cnt: " << *cnt_batchEst <<", offset rate: " << offsetRate << '\n';
// cout.flush();
unsigned long long fullEst = 0;
unsigned int * estimatedFull;
unsigned int nbUnestimatedSequences;
if (SM_HYBRID_STATIC == searchMode)
{
#if STATIC_SPLIT_QUERIES
nbUnestimatedSequences = partitionedDBSIZE / (*sampleOffset);
estimatedFull = new unsigned int[partitionedDBSIZE];
#else
nbUnestimatedSequences = (*DBSIZE) / (*sampleOffset);
estimatedFull = new unsigned int[(*DBSIZE)];
#endif
} else {
nbUnestimatedSequences = (*DBSIZE) / (*sampleOffset);
estimatedFull = new unsigned int[(*DBSIZE)];
}
for (int i = 0; i < nbUnestimatedSequences - 1; ++i)
{
unsigned int nbEstBefore = estimatedResult[i];
unsigned int nbEstAfter = estimatedResult[i + 1];
unsigned int maxEst = (nbEstBefore < nbEstAfter) ? nbEstAfter : nbEstBefore;
unsigned int estBefore = i * (*sampleOffset);
unsigned int estAfter = (i + 1) * (*sampleOffset);
estimatedFull[estBefore] = nbEstBefore;
fullEst += nbEstBefore;
for (int j = estBefore + 1; j < estAfter; ++j)
{
#if SORT_BY_WORKLOAD
estimatedFull[j] = maxEst;
fullEst += maxEst;
#else
// If we do not sort by workload, then we can not assume that the work is in non-increasing order,
// and thus that the used estimator is "correct", so we overestimate the estimation to compensate,
// similarly as in the original algorithm
estimatedFull[j] = maxEst + maxEst * sampleRate;
fullEst += maxEst + maxEst * sampleRate;
#endif
}
}
cout << "[GPU | RESULT] ~ Total estimated workload: " << fullEst << '\n';
if (searchMode == SM_HYBRID_STATIC)
{
// Not enough work to fill at least GPUSTREAMS batches, so reducing GPUBufferSize so the
// GPU can fully use its GPUSTREAMS streams
// Used if the work is statically partitioned, as the CPU will always have some work reserved
if (fullEst < (GPUBufferSize * GPUSTREAMS))
{
GPUBufferSize = fullEst / (GPUSTREAMS);
cout << "[GPU] ~ Too few batches, reducing GPUBufferSize to " << GPUBufferSize << '\n';
}
} else {
// Not enough work to fill at least 6 batches (2 * GPUSTREAMS)
// So we force to have at least 6 batches so all streams can be used, and the CPU as well
// Used if the work is dynamically partitioned (work queue), so the CPU can have some work
if (fullEst < (GPUBufferSize * GPUSTREAMS * 2))
{
GPUBufferSize = fullEst / (GPUSTREAMS * 2);
cout << "[GPU] ~ Too few batches, reducing GPUBufferSize to " << GPUBufferSize << '\n';
}
}
unsigned int batchBegin = 0;
unsigned int batchEnd = 0;
unsigned long long runningEst = 0;
// Keeping 5% of margin to avoid a potential overflow of the buffer
unsigned int reserveBuffer = GPUBufferSize * 0.05;
if (searchMode == SM_HYBRID_STATIC)
{
#if STATIC_SPLIT_QUERIES
for (int i = 0; i < partitionedDBSIZE; ++i)
{
runningEst += estimatedFull[i];
// fullEst += estimatedFull[i];
if ((GPUBufferSize - reserveBuffer) <= runningEst)
{
batchEnd = i;
batches->push_back(std::make_pair(batchBegin, batchEnd));
batchBegin = i;
runningEst = 0;
} else {
// The last batch may not fulfill the above condition of filling a result buffer
if (partitionedDBSIZE - 1 == i)
{
batchEnd = partitionedDBSIZE;
batches->push_back(std::make_pair(batchBegin, batchEnd));
}
}
}
printf("[GPU | RESULT] ~ %u query points allocated to the GPU, with %llu estimated candidates\n", partitionedDBSIZE, runningEst);
printf("[GPU | RESULT] ~ %u query points allocated to the CPU, with %llu estimated candidates\n", (*DBSIZE) - partitionedDBSIZE, fullEst - runningEst);
setQueueIndex(partitionedDBSIZE);
#else // Static partitioning based on the number candidate points to refine
// unsigned long long partitionedCandidates = fullEst * staticPartition;
// runningEst = 0;
// unsigned long long runningEstBatch = 0;
// unsigned int queryPoint = 0;
// while (runningEst < partitionedCandidates)
// {
// runningEst += estimatedFull[queryPoint];
// runningEstBatch += estimatedFull[queryPoint];
// if ((GPUBufferSize - reserveBuffer) <= runningEstBatch)
// {
// batchEnd = queryPoint;
// batches->push_back(std::make_pair(batchBegin, batchEnd));
// batchBegin = queryPoint;
// runningEstBatch = 0;
// }
// queryPoint++;
// }
// batchEnd = queryPoint;
// batches->push_back(std::make_pair(batchBegin, batchEnd));
for (int i = 0; i < (*DBSIZE); ++i)
{
runningEst += estimatedFull[i];
// fullEst += estimatedFull[i];
if ((GPUBufferSize - reserveBuffer) <= runningEst)
{
batchEnd = i;
batches->push_back(std::make_pair(batchBegin, batchEnd));
batchBegin = i;
runningEst = 0;
} else {
// The last batch may not fulfill the above condition of filling a result buffer
if ((*DBSIZE) - 1 == i)
{
batchEnd = (*DBSIZE);
batches->push_back(std::make_pair(batchBegin, batchEnd));
}
}
}
// printf("[GPU | RESULT] ~ %u query points allocated to the GPU, with %llu estimated candidates\n", queryPoint, runningEst);
// printf("[GPU | RESULT] ~ %u query points allocated to the CPU, with %llu estimated candidates\n", (*DBSIZE) - queryPoint, fullEst - runningEst);
setQueueIndex((*DBSIZE));
#endif
fullEst = runningEst;
} else {
for (int i = 0; i < (*DBSIZE); ++i)
{
runningEst += estimatedFull[i];
// fullEst += estimatedFull[i];
if ((GPUBufferSize - reserveBuffer) <= runningEst)
{
batchEnd = i;
batches->push_back(std::make_pair(batchBegin, batchEnd));
batchBegin = i;
runningEst = 0;
} else {
// The last batch may not fulfill the above condition of filling a result buffer
if ((*DBSIZE) - 1 == i)
{
batchEnd = (*DBSIZE);
batches->push_back(std::make_pair(batchBegin, batchEnd));
}
}
}
// setQueueIndex((batches[GPUSTREAMS]).first);
}
cout << "[GPU] ~ Estimated total result set size: " << fullEst << '\n';
cout << "[GPU] ~ Number of batches: " << batches->size() << '\n';
cout.flush();
(*retNumBatches) = batches->size();
(*retGPUBufferSize) = GPUBufferSize;
cout << "[GPU] ~ Done estimating batches\n";
cudaFree(dev_cnt_batchEst);
cudaFree(dev_N_batchEst);
cudaFree(dev_sampleOffset);
cudaFree(dev_estimatedResult);
delete[] estimatedResult;
delete[] estimatedFull;
delete N_batchEst;
delete cnt_batchEst;
delete sampleOffset;
return fullEst;
}
//modified from: makeDistanceTableGPUGridIndexBatchesAlternateTest
void distanceTableNDGridBatches(
int searchMode,
float staticPartition,
unsigned int * DBSIZE,
DTYPE * epsilon,
DTYPE * dev_epsilon,
DTYPE * database,
DTYPE * dev_database,
struct grid * grid,
struct grid * dev_grid,
unsigned int * indexLookupArr,
unsigned int * dev_indexLookupArr,
struct gridCellLookup * gridCellLookupArr,
struct gridCellLookup * dev_gridCellLookupArr,
DTYPE * minArr,
DTYPE * dev_minArr,
unsigned int * nCells,
unsigned int * dev_nCells,
unsigned int * nNonEmptyCells,
unsigned int * dev_nNonEmptyCells,
// unsigned int * gridCellNDMask,
// unsigned int * dev_gridCellNDMask,
// unsigned int * gridCellNDMaskOffsets,
// unsigned int * dev_gridCellNDMaskOffsets,
// unsigned int * nNDMaskElems,
unsigned int * originPointIndex,
unsigned int * dev_originPointIndex,
struct neighborTableLookup * neighborTable,
std::vector<struct neighborDataPtrs> * pointersToNeighbors,
uint64_t * totalNeighbors,
unsigned int * nbQueriesGPU)
{
double tKernelResultsStart = omp_get_wtime();
//CUDA error code:
cudaError_t errCode;
cout << "\n[GPU] ~ Sometimes the GPU will error on a previous execution and you won't know. \n[GPU] ~ Last error start of function: " << cudaGetLastError() << '\n';
cout.flush();
///////////////////////////////////
//COUNT VALUES -- RESULT SET SIZE FOR EACH KERNEL INVOCATION
///////////////////////////////////
//total size of the result set as it's batched
//this isnt sent to the GPU
unsigned int * totalResultSetCnt = new unsigned int;
*totalResultSetCnt = 0;
//count values - for an individual kernel launch
//need different count values for each stream
unsigned int * cnt;
cnt = (unsigned int*)malloc(sizeof(unsigned int) * GPUSTREAMS);
*cnt = 0;
unsigned int * dev_cnt;
dev_cnt = (unsigned int*)malloc(sizeof(unsigned int) * GPUSTREAMS);
*dev_cnt = 0;
//allocate on the device
errCode = cudaMalloc((void**)&dev_cnt, sizeof(unsigned int) * GPUSTREAMS);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: Alloc cnt -- error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
///////////////////////////////////
//END COUNT VALUES -- RESULT SET SIZE FOR EACH KERNEL INVOCATION
///////////////////////////////////
////////////////////////////////////
//NUMBER OF THREADS PER GPU STREAM
////////////////////////////////////
//THE NUMBER OF THREADS THAT ARE LAUNCHED IN A SINGLE KERNEL INVOCATION
//CAN BE FEWER THAN THE NUMBER OF ELEMENTS IN THE DATABASE IF MORE THAN 1 BATCH
unsigned int * N = new unsigned int[GPUSTREAMS];
unsigned int * dev_N;
// dev_N = (unsigned int*)malloc(sizeof(unsigned int) * GPUSTREAMS);
//allocate on the device
errCode = cudaMalloc((void**)&dev_N, sizeof(unsigned int) * GPUSTREAMS);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: Alloc dev_N -- error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
////////////////////////////////////
//NUMBER OF THREADS PER GPU STREAM
////////////////////////////////////
////////////////////////////////////
//OFFSET INTO THE DATABASE FOR BATCHING THE RESULTS
//BATCH NUMBER
////////////////////////////////////
unsigned int * batchOffset = new unsigned int [GPUSTREAMS];
unsigned int * dev_offset;
// dev_offset = (unsigned int*)malloc(sizeof(unsigned int) * GPUSTREAMS);
//allocate on the device
errCode = cudaMalloc((void**)&dev_offset, sizeof(unsigned int) * GPUSTREAMS);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: Alloc offset -- error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
//Batch number to calculate the point to process (in conjunction with the offset)
//offset into the database when batching the results
unsigned int * batchNumber;
batchNumber = (unsigned int*)malloc(sizeof(unsigned int) * GPUSTREAMS);
unsigned int * dev_batchNumber;
// dev_batchNumber = (unsigned int*)malloc(sizeof(unsigned int) * GPUSTREAMS);
//allocate on the device
errCode = cudaMalloc((void**)&dev_batchNumber, sizeof(unsigned int) * GPUSTREAMS);
if (errCode != cudaSuccess)
{
cout << "[GPU] ~ Error: Alloc batch number -- error with code " << errCode << '\n';
cout << " Details: " << cudaGetErrorString(errCode) << '\n';
cout.flush();
}
////////////////////////////////////
//END OFFSET INTO THE DATABASE FOR BATCHING THE RESULTS
//BATCH NUMBER
////////////////////////////////////
/////////////////////////////////////////////////////////
//BEGIN BATCH ESTIMATOR
/////////////////////////////////////////////////////////
unsigned long long estimatedNeighbors = 0;
unsigned int numBatches = 0;
unsigned int GPUBufferSize = 0;
std::vector< std::pair<unsigned int, unsigned int> > batchesVector;
double tstartbatchest = omp_get_wtime();
if (SM_HYBRID_STATIC == searchMode)
{
#if STATIC_SPLIT_QUERIES
#if SORT_BY_WORKLOAD
estimatedNeighbors = GPUBatchEst_v2(searchMode, DBSIZE, staticPartition, dev_database, dev_originPointIndex, dev_epsilon, dev_grid, dev_indexLookupArr,
dev_gridCellLookupArr, dev_minArr, dev_nCells, dev_nNonEmptyCells, &numBatches, &GPUBufferSize, &batchesVector);
#else
estimatedNeighbors = GPUBatchEst_v2(searchMode, DBSIZE, staticPartition, dev_database, nullptr, dev_epsilon, dev_grid, dev_indexLookupArr,
dev_gridCellLookupArr, dev_minArr, dev_nCells, dev_nNonEmptyCells, &numBatches, &GPUBufferSize, &batchesVector);
#endif
#else
unsigned int nbQueryPointsStatic = getStaticQueryPoint();
cout << "[GPU | DEBUG] ~ Number of queries for the GPU: " << nbQueryPointsStatic << '\n';
#if SORT_BY_WORKLOAD
estimatedNeighbors = GPUBatchEst_v2(searchMode, &nbQueryPointsStatic, staticPartition, dev_database, dev_originPointIndex, dev_epsilon, dev_grid, dev_indexLookupArr,
dev_gridCellLookupArr, dev_minArr, dev_nCells, dev_nNonEmptyCells, &numBatches, &GPUBufferSize, &batchesVector);
#else
estimatedNeighbors = GPUBatchEst_v2(searchMode, &nbQueryPointsStatic, staticPartition, dev_database, nullptr, dev_epsilon, dev_grid, dev_indexLookupArr,
dev_gridCellLookupArr, dev_minArr, dev_nCells, dev_nNonEmptyCells, &numBatches, &GPUBufferSize, &batchesVector);
#endif
#endif
} else {
#if SORT_BY_WORKLOAD
estimatedNeighbors = GPUBatchEst_v2(searchMode, DBSIZE, staticPartition, dev_database, dev_originPointIndex, dev_epsilon, dev_grid, dev_indexLookupArr,
dev_gridCellLookupArr, dev_minArr, dev_nCells, dev_nNonEmptyCells, &numBatches, &GPUBufferSize, &batchesVector);
#else
estimatedNeighbors = GPUBatchEst_v2(searchMode, DBSIZE, staticPartition, dev_database, nullptr, dev_epsilon, dev_grid, dev_indexLookupArr,
dev_gridCellLookupArr, dev_minArr, dev_nCells, dev_nNonEmptyCells, &numBatches, &GPUBufferSize, &batchesVector);
#endif
}
double tendbatchest = omp_get_wtime();
cout << "[GPU] ~ Time to estimate batches: " << tendbatchest - tstartbatchest << '\n';
cout.flush();
cout << "[GPU] ~ In calling function: Estimated neighbors = " << estimatedNeighbors
<< ", num. batches = " << numBatches << ", GPU buffer size = " << GPUBufferSize << '\n';
cout.flush();
// cout << "[GPU] ~ Batches: \n";
// for (int i = 0; i < batchesVector.size(); ++i)
// {
// cout << " [GPU] ~ " << batchesVector[i].first << ", " << batchesVector[i].second << '\n';
// }
// sets the batch size for the queue and the queue index, considering the offset reserved for the GPU
// shouldn't happen anymore as we always have at least 2*GPUSTREAMS batches now
// setQueueIndex(GPUSTREAMS * (*DBSIZE / numBatches));
// if (batchesVector.size() < GPUSTREAMS)
// {
// setQueueIndex((*DBSIZE)); // the GPU reserves all the computation
// } else {
if (searchMode != SM_HYBRID_STATIC)
{
setQueueIndex(batchesVector[GPUSTREAMS].first);
}
// }
// setQueueIndex(0);