-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathLSHReservoirSampler_misc.cpp
185 lines (160 loc) · 6.95 KB
/
LSHReservoirSampler_misc.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
#include "LSHReservoirSampler.h"
#include "indexing.h"
#include "misc.h"
#include "LSHReservoirSampler_config.h"
void LSHReservoirSampler::kernelBandWidth(const std::string& kernelName, float br, float bw, float time) {
float actual = ((br + bw) / 1000000000) / (time / 1000);
std::cout << "[Bandwidth] " << kernelName;
printf(" %3.2f GBps. \n", actual);
return;
}
//void LSHReservoirSampler::clMemCpy_uint_g2c(cl_mem *dst, cl_mem *src, unsigned int size) {
// unsigned int *buffer = new unsigned int[size];
// _err = clEnqueueReadBuffer(command_queue_gpu, *src, CL_TRUE, 0, size * sizeof(unsigned int), buffer, 0, NULL, NULL);
// clCheckError(_err, "[clMemCpy_uint_g2c] Failed to read from gpu.");
// clFinish(command_queue_gpu);
// _err = clEnqueueWriteBuffer(command_queue_cpu, *dst, CL_TRUE, 0, size * sizeof(unsigned int), buffer, 0, NULL, NULL);
// clCheckError(_err, "[clMemCpy_uint_g2c] Failed to write to cpu.");
// clFinish(command_queue_cpu);
// delete[] buffer;
//}
//void LSHReservoirSampler::clMemCpy_uint_c2g(cl_mem *dst, cl_mem *src, unsigned int size) {
// unsigned int *buffer = new unsigned int[size];
// _err = clEnqueueReadBuffer(command_queue_cpu, *src, CL_TRUE, 0, size * sizeof(unsigned int), buffer, 0, NULL, NULL);
// clCheckError(_err, "[clMemCpy_uint_c2g] Failed to read from cpu.");
// clFinish(command_queue_cpu);
// _err |= clEnqueueWriteBuffer(command_queue_gpu, *dst, CL_TRUE, 0, size * sizeof(unsigned int), buffer, 0, NULL, NULL);
// clCheckError(_err, "[clMemCpy_uint_c2g] Failed to write to gpu.");
// clFinish(command_queue_gpu);
// delete[] buffer;
//}
void LSHReservoirSampler::memCpy_uint_g2c(unsigned int *dst, cl_mem *src, unsigned int size) {
_err = clEnqueueReadBuffer(command_queue_gpu, *src, CL_TRUE, 0, size * sizeof(unsigned int), dst, 0, NULL, NULL);
clCheckError(_err, "[memCpy_uint_g2c] Failed to read from gpu.");
clFinish(command_queue_gpu);
}
void LSHReservoirSampler::memCpy_uint_c2g(cl_mem *dst, unsigned int *src, unsigned int size) {
_err |= clEnqueueWriteBuffer(command_queue_gpu, *dst, CL_TRUE, 0, size * sizeof(unsigned int), src, 0, NULL, NULL);
clCheckError(_err, "[memCpy_uint_c2g] Failed to write to gpu.");
clFinish(command_queue_gpu);
}
void LSHReservoirSampler::checkTableMemLoad() {
#if defined OPENCL_HASHTABLE
_tableMemAllocator = new unsigned int[_numTables * sizeof(unsigned int)];
_err = clEnqueueReadBuffer(command_queue_gpu, _tableMemAllocator_obj, CL_TRUE, 0,
_numTables * sizeof(unsigned int), _tableMemAllocator, 0, NULL, NULL);
#endif
unsigned int maxx = 0;
unsigned int minn = _numReservoirs;
unsigned int tt = 0;
for (unsigned int i = 0; i < _numTables; i++) {
if (_tableMemAllocator[i] < minn) {
minn = _tableMemAllocator[i];
}
if (_tableMemAllocator[i] > maxx) {
maxx = _tableMemAllocator[i];
}
tt += _tableMemAllocator[i];
}
printf("Table Mem Usage ranges from %f to %f, average %f\n",
((float)minn) / (float)_aggNumReservoirs,
((float)maxx) / (float)_aggNumReservoirs,
((float)tt) / (float)(_numTables * _aggNumReservoirs));
#if defined OPENCL_HASHTABLE
delete[] _tableMemAllocator;
#endif
}
void LSHReservoirSampler::clCheckError(cl_int code, const char* msg) {
if (code != CL_SUCCESS) {
printf(msg);
printf("\nError Code: %d\n", code);
pause();
exit(1);
}
}
void LSHReservoirSampler::clCheckErrorNoExit(cl_int code, const char* msg) {
if (code != CL_SUCCESS) {
printf(msg);
printf("\nError Code: %d\n", code);
pause();
}
}
void LSHReservoirSampler::clTestAlloc(long long numInts, cl_context* testContext, cl_command_queue* testQueue) {
int *bufferArray = new int[numInts];
/* Allocation. */
auto begin = Clock::now();
cl_mem buffer = clCreateBuffer(*testContext, CL_MEM_READ_WRITE, numInts * sizeof(int), NULL, &_err);
clCheckErrorNoExit(_err, "[clTestAlloc] Failed to declare buffer.");
clFinish(*testQueue);
auto end = Clock::now();
float etime = (end - begin).count() / (float)1000000;
printf("[clTestAlloc] Allocation took %5.3f ms\n", etime);
begin = Clock::now();
_err = clEnqueueWriteBuffer(*testQueue, buffer, CL_TRUE, 0, numInts * sizeof(int), bufferArray, 0, NULL, NULL);
clCheckErrorNoExit(_err, "[clTestAlloc] Failed to Write-init.");
clFinish(*testQueue);
end = Clock::now();
etime = (end - begin).count() / (float)1000000;
float io = (float)numInts * (float)sizeof(int);
printf("[clTestAlloc] Write-init took %5.3f ms. Avg Bandwidth %5.3f Gib/s \n",
etime, (io / (float)1000000000) / (etime / 1000));
clReleaseMemObject(buffer);
delete[] bufferArray;
pause();
}
void LSHReservoirSampler::showParams() {
printf("\n");
printf("<<< LSHR Parameters >>>\n");
std::cout << "_rangePow " << _rangePow << "\n";
std::cout << "_rangePow_Rehashed " << _numSecHash << "\n";
std::cout << "_numTables " << _numTables << "\n";
std::cout << "_reservoirSize " << _reservoirSize << "\n";
std::cout << "_queryProbes " << _queryProbes << "\n";
std::cout << "_hashingProbes " << _hashingProbes << "\n";
std::cout << "_dimension " << _dimension << "\n";
std::cout << "_maxSamples " << _maxSamples << "\n";
std::cout << "_tableAllocFraction " << _tableAllocFraction << "\n";
std::cout << "_segmentSizeModulor " << _segmentSizeModulor << "\n";
std::cout << "_segmentSizeBitShiftDivisor " << _segmentSizeBitShiftDivisor << "\n";
std::cout << "_numReservoirs " << _numReservoirs << "\n";
std::cout << "_numReservoirsHashed " << _numReservoirsHashed << "\n";
std::cout << "_aggNumReservoirs " << _aggNumReservoirs << "\n";
std::cout << "_maxReservoirRand " << _maxReservoirRand << "\n";
printf("\n");
}
void LSHReservoirSampler::viewTables() {
#if defined OPENCL_HASHTABLE
_tablePointers = new unsigned int[_numReservoirsHashed * _numTables];
_tableMem = new unsigned int[_tableMemMax];
_err = clEnqueueReadBuffer(command_queue_gpu, _tablePointers_obj, CL_TRUE, 0,
_numReservoirsHashed * _numTables * sizeof(unsigned int), _tablePointers, 0, NULL, NULL);
_err = clEnqueueReadBuffer(command_queue_gpu, _tableMem_obj, CL_TRUE, 0,
_tableMemMax * sizeof(unsigned int), _tableMem, 0, NULL, NULL);
#endif
for (unsigned int which = 0; which < std::min(_numTables, (unsigned int) DEBUGTB); which++) {
printf("\n");
printf("<<< Table %d Content >>>\n", which);
unsigned int maxResShow = 0;
for (unsigned int t = 0; t < _numReservoirs; t++) {
if (_tablePointers[tablePointersIdx(_numReservoirsHashed, t, which, _sechash_a, _sechash_b)] != _tableNull && maxResShow < DEBUGENTRIES) {
unsigned int allocIdx = _tablePointers[tablePointersIdx(_numReservoirsHashed, t, which, _sechash_a, _sechash_b)];
printf("Reservoir %d (%u): ", t, _tableMem[tableMemCtIdx(which, allocIdx, _aggNumReservoirs)]);
for (unsigned int h = 0; h < std::min(_reservoirSize, (unsigned)DEBUGENTRIES); h++) {
printf("%u ", _tableMem[tableMemResIdx(which, allocIdx, _aggNumReservoirs) + h]);
}
printf("\n");
maxResShow++;
}
}
printf("\n");
}
pause();
#if defined OPENCL_HASHTABLE
delete[] _tableMem;
#endif
}
void LSHReservoirSampler::pause() {
#ifdef VISUAL_STUDIO
system("pause");
#endif
}