-
Notifications
You must be signed in to change notification settings - Fork 25
/
LSHReservoirSampler.cpp
576 lines (514 loc) · 22 KB
/
LSHReservoirSampler.cpp
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
#include "LSHReservoirSampler.h"
#include "misc.h"
#include "LSHReservoirSampler_config.h"
void LSHReservoirSampler::add(int numInputEntries, int* dataIdx, float* dataVal, int* dataMarker) {
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add]" << std::endl;
#endif
#ifdef PROFILE_READ
float transfer_time = 0;
#endif
const int numProbePerTb = numInputEntries * _hashingProbes;
if ((unsigned) numInputEntries > _maxSamples) {
printf("[LSHReservoirSampler::add] Input length %d is too large! \n", numInputEntries);
pause();
return;
}
#if defined OPENCL_HASHING || defined OPENCL_HASHTABLE
cl_mem allprobsHash_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
_numTables * numInputEntries * _hashingProbes * sizeof(unsigned int), NULL, &_err);
cl_mem allprobsIdx_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
_numTables * numInputEntries * _hashingProbes * sizeof(unsigned int), NULL, &_err);
#endif
#if defined CPU_HASHING || defined CPU_TB
unsigned int* allprobsHash = new unsigned int[_numTables * numInputEntries * _hashingProbes];
unsigned int* allprobsIdx = new unsigned int[_numTables * numInputEntries * _hashingProbes];
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Started hashing. " << std::endl;
#endif
#if defined OPENCL_HASHING
#ifdef PROFILE_READ
auto transfer_begin = Clock::now();
#endif
cl_mem dataIdx_obj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numInputEntries * _dimension * sizeof(int), NULL, &_err);
_err = clEnqueueWriteBuffer(command_queue_gpu, dataIdx_obj, CL_TRUE, 0,
numInputEntries * _dimension * sizeof(int), dataIdx, 0, NULL, NULL);
cl_mem dataVal_obj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numInputEntries * _dimension * sizeof(float), NULL, &_err);
_err |= clEnqueueWriteBuffer(command_queue_gpu, dataVal_obj, CL_TRUE, 0,
numInputEntries * _dimension * sizeof(float), dataVal, 0, NULL, NULL);
cl_mem dataMarker_obj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
(numInputEntries + 1) * sizeof(int), NULL, &_err);
_err |= clEnqueueWriteBuffer(command_queue_gpu, dataMarker_obj, CL_TRUE, 0,
(numInputEntries + 1) * sizeof(int), dataMarker, 0, NULL, NULL);
clCheckError(_err, "Failed to write sparse input data to memobj!");
#ifdef PROFILE_READ
auto transfer_end = Clock::now();
transfer_time += GETTIME_MS(transfer_begin, transfer_end);
#endif
_hashFamily->getHash(&allprobsHash_gpuobj, &allprobsIdx_gpuobj,
&dataIdx_obj, &dataVal_obj, &dataMarker_obj, numInputEntries, _hashingProbes);
clReleaseMemObject(dataIdx_obj);
clReleaseMemObject(dataVal_obj);
clReleaseMemObject(dataMarker_obj);
#elif defined CPU_HASHING
_hashFamily->getHash(allprobsHash, allprobsIdx,
dataIdx, dataVal, dataMarker, numInputEntries, _hashingProbes);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Completed hashing. " << std::endl;
#endif
/* Extra memory transfer for cross device processing. */
#ifdef PROFILE_READ
auto transfer_begin_1 = Clock::now();
#endif
#if defined OPENCL_HASHING && defined CPU_TB
memCpy_uint_g2c(allprobsHash, &allprobsHash_gpuobj, _numTables * numInputEntries * _hashingProbes);
memCpy_uint_g2c(allprobsIdx, &allprobsIdx_gpuobj, _numTables * numInputEntries * _hashingProbes);
#endif
#if defined CPU_HASHING && defined OPENCL_HASHTABLE
memCpy_uint_c2g(&allprobsHash_gpuobj, allprobsHash, _numTables * numInputEntries * _hashingProbes);
memCpy_uint_c2g(&allprobsIdx_gpuobj, allprobsIdx, _numTables * numInputEntries * _hashingProbes);
#endif
#ifdef PROFILE_READ
auto transfer_end_1 = Clock::now();
transfer_time += GETTIME_MS(transfer_begin_1, transfer_end_1);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Adding to table. " << std::endl;
#endif
#ifdef OPENCL_HASHTABLE
HashAddGPUTB(&allprobsHash_gpuobj, &allprobsIdx_gpuobj, numProbePerTb, numInputEntries);
#elif defined CPU_TB
HashAddCPUTB(allprobsHash, allprobsIdx, numProbePerTb, numInputEntries);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Added to table. " << std::endl;
#endif
#if defined OPENCL_HASHING || defined OPENCL_HASHTABLE
clReleaseMemObject(allprobsHash_gpuobj);
clReleaseMemObject(allprobsIdx_gpuobj);
#endif
#if defined CPU_HASHING || defined CPU_TB
delete[] allprobsHash;
delete[] allprobsIdx;
#endif
_sequentialIDCounter_kernel += numInputEntries;
#ifdef PROFILE_READ
printf("[LSHReservoirSampler::add] MemTransfer %5.3f ms\n", transfer_time);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Exit. " << std::endl;
#endif
}
void LSHReservoirSampler::add(int numInputEntries, float* input) {
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add]" << std::endl;
#endif
const int numProbePerTb = numInputEntries * _hashingProbes;
if ((unsigned) numInputEntries > _maxSamples) { // TODO: Better limit available?
printf("[LSHReservoirSampler::add] Input length %d is too large! \n", numInputEntries);
pause();
return;
}
#ifdef PROFILE_READ
float transfer_time = 0;
#endif
#if defined OPENCL_HASHING || defined OPENCL_HASHTABLE
cl_mem allprobsHash_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
_numTables * numInputEntries * _hashingProbes * sizeof(unsigned int), NULL, &_err);
cl_mem allprobsIdx_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
_numTables * numInputEntries * _hashingProbes * sizeof(unsigned int), NULL, &_err);
#endif
#if defined CPU_HASHING || defined CPU_TB
unsigned int* allprobsHash = new unsigned int[_numTables * numInputEntries * _hashingProbes];
unsigned int* allprobsIdx = new unsigned int[_numTables * numInputEntries * _hashingProbes];
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Started hashing. " << std::endl;
#endif
#if defined OPENCL_HASHING
#ifdef PROFILE_READ
auto transfer_begin = Clock::now();
#endif
cl_mem input_obj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numInputEntries * _dimension * sizeof(float), NULL, &_err);
_err = clEnqueueWriteBuffer(command_queue_gpu, input_obj, CL_TRUE, 0,
numInputEntries * _dimension * sizeof(float), input, 0, NULL, NULL);
clCheckError(_err, "Failed to write input data to memobj!");
#ifdef PROFILE_READ
auto transfer_end = Clock::now();
transfer_time += GETTIME_MS(transfer_begin, transfer_end);
#endif
_hashFamily->getHash(&allprobsHash_gpuobj, &allprobsIdx_gpuobj,
&input_obj, numInputEntries, _hashingProbes);
clReleaseMemObject(input_obj);
#elif defined CPU_HASHING
_hashFamily->getHash(allprobsHash, allprobsIdx, input, numInputEntries, _hashingProbes);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Completed hashing. " << std::endl;
#endif
/* Extra memory transfer for cross device processing. */
#ifdef PROFILE_READ
auto transfer_begin_1 = Clock::now();
#endif
#if defined OPENCL_HASHING && defined CPU_TB
memCpy_uint_g2c(allprobsHash, &allprobsHash_gpuobj, _numTables * numInputEntries * _hashingProbes);
memCpy_uint_g2c(allprobsIdx, &allprobsIdx_gpuobj, _numTables * numInputEntries * _hashingProbes);
#endif
#if defined CPU_HASHING && defined OPENCL_HASHTABLE
memCpy_uint_c2g(&allprobsHash_gpuobj, allprobsHash, _numTables * numInputEntries * _hashingProbes);
memCpy_uint_c2g(&allprobsIdx_gpuobj, allprobsIdx, _numTables * numInputEntries * _hashingProbes);
#endif
#ifdef PROFILE_READ
auto transfer_end_1 = Clock::now();
transfer_time += GETTIME_MS(transfer_begin_1, transfer_end_1);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Adding to table. " << std::endl;
#endif
#ifdef OPENCL_HASHTABLE
HashAddGPUTB(&allprobsHash_gpuobj, &allprobsIdx_gpuobj, numProbePerTb, numInputEntries);
#elif defined CPU_TB
HashAddCPUTB(allprobsHash, allprobsIdx, numProbePerTb, numInputEntries);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Added to table. " << std::endl;
#endif
#if defined OPENCL_HASHING || defined OPENCL_HASHTABLE
clReleaseMemObject(allprobsHash_gpuobj);
clReleaseMemObject(allprobsIdx_gpuobj);
#endif
#if defined CPU_HASHING || defined CPU_TB
delete[] allprobsHash;
delete[] allprobsIdx;
#endif
_sequentialIDCounter_kernel += numInputEntries;
#ifdef PROFILE_READ
printf("[LSHReservoirSampler::add] MemTransfer %5.3f ms\n", transfer_time);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::add] Exit. " << std::endl;
#endif
}
void LSHReservoirSampler::lossy_ann(int numQueryEntries, int* dataIdx, float* dataVal, int* dataMarker, unsigned int* outputs, int k) {
#if defined USE_OPENCL
std::cout << "[LSHReservoirSampler::lossy_ann] Does not have GPU implementation. " << std::endl;
return;
#endif
unsigned int* allprobsHash = new unsigned int[_numTables * numQueryEntries * _queryProbes];
unsigned int* allprobsIdx = new unsigned int[_numTables * numQueryEntries * _queryProbes];
#if defined DEBUG
std::cout << "[LSHReservoirSampler::lossy_ann] Started hashing. " << std::endl;
#endif
_hashFamily->getHash(allprobsHash, allprobsIdx,
dataIdx, dataVal, dataMarker, numQueryEntries, _queryProbes);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::lossy_ann] Completed hashing. " << std::endl;
std::cout << "[LSHReservoirSampler::lossy_ann] Lossy K-selection. " << std::endl;
#endif
query_frequentitem_cpu_openmp(numQueryEntries, outputs, allprobsHash, k);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::lossy_ann] Lossy K-selection completes. " << std::endl;
#endif
delete[] allprobsHash;
delete[] allprobsIdx;
}
void LSHReservoirSampler::ann(int numQueryEntries, float* queries, unsigned int* outputs, int topk) {
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann]" << std::endl;
#endif
if ((unsigned) topk > _reservoirSize * _numTables) {
printf("[LSHReservoirSampler::ann] Maximum k exceeded! %d\n", topk);
pause();
return;
}
#if !defined USE_OPENCL
unsigned int* allprobsHash = new unsigned int[_numTables * numQueryEntries * _queryProbes];
unsigned int* allprobsIdx = new unsigned int[_numTables * numQueryEntries * _queryProbes];
int segmentSize = _numTables * _queryProbes * _reservoirSize;
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Started hashing. " << std::endl;
#endif
_hashFamily->getHash(allprobsHash, allprobsIdx, queries, numQueryEntries, _queryProbes);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Completed hashing. " << std::endl;
#endif
unsigned int* tally = new unsigned int[numQueryEntries * segmentSize];
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Extracting rows. " << std::endl;
#endif
query_extractRows_cpu_openmp(numQueryEntries, segmentSize, tally, allprobsHash);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Extracted rows. " << std::endl;
std::cout << "[LSHReservoirSampler::ann] Started k-selection. " << std::endl;
#endif
kSelect(tally, outputs, segmentSize, numQueryEntries, topk);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] K-selection completed. " << std::endl;
#endif
delete[] allprobsHash;
delete[] allprobsIdx;
delete[] tally;
#else
#ifdef PROFILE_READ
float transfer_time = 0;
#endif
int segmentSize = _numTables * _queryProbes * _reservoirSize;
int segmentSizePow2 = smallestPow2(segmentSize); // Pow2 required by sorting.
#if defined OPENCL_HASHING || defined OPENCL_HASHTABLE
cl_mem allprobsHash_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
_numTables * numQueryEntries * _queryProbes * sizeof(unsigned int), NULL, &_err);
cl_mem allprobsIdx_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
_numTables * numQueryEntries * _queryProbes * sizeof(unsigned int), NULL, &_err);
#endif
#if defined CPU_HASHING || defined CPU_TB
unsigned int* allprobsHash = new unsigned int[_numTables * numQueryEntries * _queryProbes];
unsigned int* allprobsIdx = new unsigned int[_numTables * numQueryEntries * _queryProbes];
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Started hashing. " << std::endl;
#endif
#if defined OPENCL_HASHING
#ifdef PROFILE_READ
auto transfer_begin = Clock::now();
#endif
cl_mem queries_obj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numQueryEntries * _dimension * sizeof(float), NULL, &_err);
_err = clEnqueueWriteBuffer(command_queue_gpu, queries_obj, CL_TRUE, 0,
numQueryEntries * _dimension * sizeof(float), queries, 0, NULL, NULL);
clCheckError(_err, "Failed to write queries data to memobj!");
#ifdef PROFILE_READ
auto transfer_end = Clock::now();
transfer_time += GETTIME_MS(transfer_begin, transfer_end);
#endif
_hashFamily->getHash(&allprobsHash_gpuobj, &allprobsIdx_gpuobj,
&queries_obj, numQueryEntries, _queryProbes);
clReleaseMemObject(queries_obj);
#elif defined CPU_HASHING
_hashFamily->getHash(allprobsHash, allprobsIdx, queries, numQueryEntries, _queryProbes);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Completed hashing. " << std::endl;
#endif
/* Extra memory transfer for cross device processing. */
#ifdef PROFILE_READ
auto transfer_begin_1 = Clock::now();
#endif
#if defined OPENCL_HASHING && defined CPU_TB
memCpy_uint_g2c(allprobsHash, &allprobsHash_gpuobj, _numTables * numQueryEntries * _queryProbes);
#endif
#if defined CPU_HASHING && defined OPENCL_HASHTABLE
memCpy_uint_c2g(&allprobsHash_gpuobj, allprobsHash, _numTables * numQueryEntries * _queryProbes);
#endif
#ifdef PROFILE_READ
auto transfer_end_1 = Clock::now();
transfer_time += GETTIME_MS(transfer_begin_1, transfer_end_1);
#endif
cl_mem tally_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numQueryEntries * segmentSizePow2 * sizeof(unsigned int), NULL, &_err);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Extracting rows. " << std::endl;
#endif
#if defined OPENCL_HASHTABLE
RowsAggregationGPUTB(&allprobsHash_gpuobj, &tally_gpuobj, segmentSizePow2, numQueryEntries);
#elif defined CPU_TB
RowsAggregationCPUTB(allprobsHash, &tally_gpuobj, segmentSizePow2, numQueryEntries);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Extracted rows. " << std::endl;
std::cout << "[LSHReservoirSampler::ann] Started k-selection. " << std::endl;
#endif
#if defined OPENCL_KSELECT
kSelect(&tally_gpuobj, outputs, segmentSize, segmentSizePow2, numQueryEntries, topk);
#elif defined CPU_KSELECT
unsigned int *tally = new unsigned int[numQueryEntries * segmentSizePow2];
memCpy_uint_g2c(tally, &tally_gpuobj, numQueryEntries * segmentSizePow2);
kSelect(tally, outputs, segmentSizePow2, numQueryEntries, topk);
delete[] tally;
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] K-selection completed. " << std::endl;
#endif
clReleaseMemObject(tally_gpuobj);
#if defined OPENCL_HASHTABLE || defined OPENCL_HASHING
clReleaseMemObject(allprobsHash_gpuobj);
clReleaseMemObject(allprobsIdx_gpuobj);
#endif
#if defined CPU_TB || defined CPU_HASHING
delete[] allprobsHash;
delete[] allprobsIdx;
#endif
#ifdef PROFILE_READ
printf("[LSHReservoirSampler::ann] MemTransfer %5.3f ms\n", transfer_time);
#endif
#endif // NO_GPU
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Exit. " << std::endl;
#endif
}
void LSHReservoirSampler::ann(int numQueryEntries, int* dataIdx, float* dataVal, int* dataMarker, unsigned int* outputs, int topk) {
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann]" << std::endl;
#endif
if ((unsigned) topk > _reservoirSize * _numTables) {
printf("Error: Maximum k exceeded! %d\n", topk);
pause();
return;
}
#if !defined USE_OPENCL
unsigned int* allprobsHash = new unsigned int[_numTables * numQueryEntries * _queryProbes];
unsigned int* allprobsIdx = new unsigned int[_numTables * numQueryEntries * _queryProbes];
int segmentSize = _numTables * _queryProbes * _reservoirSize;
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Started hashing. " << std::endl;
#endif
_hashFamily->getHash(allprobsHash, allprobsIdx,
dataIdx, dataVal, dataMarker, numQueryEntries, _queryProbes);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Completed hashing. " << std::endl;
#endif
unsigned int* tally = new unsigned int[numQueryEntries * segmentSize];
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Extracting rows. " << std::endl;
#endif
query_extractRows_cpu_openmp(numQueryEntries, segmentSize, tally, allprobsHash);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Extracted rows. " << std::endl;
std::cout << "[LSHReservoirSampler::ann] Started k-selection. " << std::endl;
#endif
kSelect(tally, outputs, segmentSize, numQueryEntries, topk);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Completed k-selection. " << std::endl;
#endif
delete[] allprobsHash;
delete[] allprobsIdx;
delete[] tally;
#else
#ifdef PROFILE_READ
float transfer_time = 0;
#endif
int segmentSize = _numTables * _queryProbes * _reservoirSize;
int segmentSizePow2 = smallestPow2(segmentSize); // Pow2 required by sorting.
#if defined OPENCL_HASHING || defined OPENCL_HASHTABLE
cl_mem allprobsHash_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
_numTables * numQueryEntries * _queryProbes * sizeof(unsigned int), NULL, &_err);
cl_mem allprobsIdx_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
_numTables * numQueryEntries * _queryProbes * sizeof(unsigned int), NULL, &_err);
#endif
#if defined CPU_HASHING || defined CPU_TB
unsigned int* allprobsHash = new unsigned int[_numTables * numQueryEntries * _queryProbes];
unsigned int* allprobsIdx = new unsigned int[_numTables * numQueryEntries * _queryProbes];
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Started hashing. " << std::endl;
#endif
#if defined OPENCL_HASHING
#ifdef PROFILE_READ
auto transfer_begin = Clock::now();
#endif
cl_mem dataIdx_obj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numQueryEntries * _dimension * sizeof(int), NULL, &_err);
_err = clEnqueueWriteBuffer(command_queue_gpu, dataIdx_obj, CL_TRUE, 0,
numQueryEntries * _dimension * sizeof(int), dataIdx, 0, NULL, NULL);
cl_mem dataVal_obj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numQueryEntries * _dimension * sizeof(float), NULL, &_err);
_err |= clEnqueueWriteBuffer(command_queue_gpu, dataVal_obj, CL_TRUE, 0,
numQueryEntries * _dimension * sizeof(float), dataVal, 0, NULL, NULL);
cl_mem dataMarker_obj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
(numQueryEntries + 1) * sizeof(int), NULL, &_err);
_err |= clEnqueueWriteBuffer(command_queue_gpu, dataMarker_obj, CL_TRUE, 0,
(numQueryEntries + 1) * sizeof(int), dataMarker, 0, NULL, NULL);
clCheckError(_err, "Failed to write sparse input data to memobj!");
#ifdef PROFILE_READ
auto transfer_end = Clock::now();
transfer_time += GETTIME_MS(transfer_begin, transfer_end);
#endif
_hashFamily->getHash(&allprobsHash_gpuobj, &allprobsIdx_gpuobj,
&dataIdx_obj, &dataVal_obj, &dataMarker_obj, numQueryEntries, _queryProbes);
clReleaseMemObject(dataIdx_obj);
clReleaseMemObject(dataVal_obj);
clReleaseMemObject(dataMarker_obj);
#elif defined CPU_HASHING
_hashFamily->getHash(allprobsHash, allprobsIdx,
dataIdx, dataVal, dataMarker, numQueryEntries, _queryProbes);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Completed hashing. " << std::endl;
#endif
/* Extra memory transfer for cross device processing. */
#ifdef PROFILE_READ
auto transfer_begin_1 = Clock::now();
#endif
#if defined OPENCL_HASHING && defined CPU_TB
memCpy_uint_g2c(allprobsHash, &allprobsHash_gpuobj, _numTables * numQueryEntries * _queryProbes);
#endif
#if defined CPU_HASHING && defined OPENCL_HASHTABLE
memCpy_uint_c2g(&allprobsHash_gpuobj, allprobsHash, _numTables * numQueryEntries * _queryProbes);
#endif
#ifdef PROFILE_READ
auto transfer_end_1 = Clock::now();
transfer_time += GETTIME_MS(transfer_begin_1, transfer_end_1);
#endif
cl_mem tally_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numQueryEntries * segmentSizePow2 * sizeof(unsigned int), NULL, &_err);
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Extracting rows. " << std::endl;
#endif
#if defined OPENCL_HASHTABLE
RowsAggregationGPUTB(&allprobsHash_gpuobj, &tally_gpuobj, segmentSizePow2, numQueryEntries);
#elif defined CPU_TB
RowsAggregationCPUTB(allprobsHash, &tally_gpuobj, segmentSizePow2, numQueryEntries);
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Extracted rows. " << std::endl;
std::cout << "[LSHReservoirSampler::ann] Started k-selection. " << std::endl;
#endif
#if defined OPENCL_KSELECT
kSelect(&tally_gpuobj, outputs, segmentSize, segmentSizePow2, numQueryEntries, topk);
#elif defined CPU_KSELECT
unsigned int *tally = new unsigned int[numQueryEntries * segmentSizePow2];
memCpy_uint_g2c(tally, &tally_gpuobj, numQueryEntries * segmentSizePow2);
kSelect(tally, outputs, segmentSizePow2, numQueryEntries, topk);
delete[] tally;
#endif
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Completed k-selection. " << std::endl;
#endif
clReleaseMemObject(tally_gpuobj);
#if defined OPENCL_HASHTABLE || defined OPENCL_HASHING
clReleaseMemObject(allprobsHash_gpuobj);
clReleaseMemObject(allprobsIdx_gpuobj);
#endif
#if defined CPU_TB || defined CPU_HASHING
delete[] allprobsHash;
delete[] allprobsIdx;
#endif
#ifdef PROFILE_READ
printf("[LSHReservoirSampler::ann] MemTransfer %5.3f ms\n", transfer_time);
#endif
#endif // NO_GPU
#if defined DEBUG
std::cout << "[LSHReservoirSampler::ann] Exit" << std::endl;
#endif
}
void LSHReservoirSampler::ann_debug(int numQueryEntries, int* dataIdx, float* dataVal, int* dataMarker, int topk) {
int segmentSize = _numTables * _queryProbes * _reservoirSize;
int segmentSizePow2 = smallestPow2(segmentSize); // Pow2 required by sorting.
unsigned int* allprobsHash = new unsigned int[_numTables * numQueryEntries * _queryProbes];
unsigned int* allprobsIdx = new unsigned int[_numTables * numQueryEntries * _queryProbes];
_hashFamily->getHash(allprobsHash, allprobsIdx,
dataIdx, dataVal, dataMarker, numQueryEntries, _queryProbes);
cl_mem tally_gpuobj = clCreateBuffer(context_gpu, CL_MEM_READ_WRITE,
numQueryEntries * segmentSizePow2 * sizeof(unsigned int), NULL, &_err);
RowsAggregationCPUTB(allprobsHash, &tally_gpuobj, segmentSizePow2, numQueryEntries);
unsigned int* tally = new unsigned int[numQueryEntries * segmentSizePow2];
memCpy_uint_g2c(tally, &tally_gpuobj, numQueryEntries * segmentSizePow2);
kSelect_debug(&tally_gpuobj, tally, segmentSize, segmentSizePow2, numQueryEntries, topk);
delete[] tally;
delete[] allprobsHash;
delete[] allprobsIdx;
}