-
Notifications
You must be signed in to change notification settings - Fork 25
/
LSH_init.cpp
330 lines (282 loc) · 11.8 KB
/
LSH_init.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
#include "LSH.h"
#ifndef UINT_MAX
#define UINT_MAX 0xffffffff
#endif
#define RANDPROJGROUPSIZE 100
/* Constructor - Optimal Densified Minhash - Type 2. */
LSH::LSH(int hashType, int _K_in, int _L_in, int _rangePow_in) {
_clEnabled = 0;
_hashType = hashType;
_K = _K_in;
_L = _L_in;
_numTables = _L_in; // In densified minhash, _numTables is equivalan to _L. Initialized for general usage just in case.
_rangePow = _rangePow_in;
printf("<<< LSH Parameters >>>\n");
std::cout << "_K " << _K << std::endl;
std::cout << "_L " << _L << std::endl;
std::cout << "_rangePow " << _rangePow_in << std::endl;
std::cout << "_hashType " << _hashType << std::endl;
rand1 = new int[_K * _L];
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned int> dis(1, UINT_MAX);
// Generate rand1 - odd random numbers.
for (int i = 0; i < _K * _L; i++)
{
rand1[i] = dis(gen);
if (rand1[i] % 2 == 0)
rand1[i]++;
}
_numhashes = _K * _L;
_lognumhash = log2(_numhashes);
std::cout << "_lognumhash " << _lognumhash << std::endl;
// _randa and _randHash* are random odd numbers.
_randa = dis(gen);
if (_randa % 2 == 0)
_randa++;
_randHash = new int[2];
_randHash[0] = dis(gen);
if (_randHash[0] % 2 == 0)
_randHash[0]++;
_randHash[1] = dis(gen);
if (_randHash[1] % 2 == 0)
_randHash[1]++;
std::cout << "Optimal Densified Hashing intialized ... \n";
}
/* Constructor - SRP - Type 1. */
LSH::LSH(int hashType, int numHashPerFamily, int numHashFamilies, int dimension, int samFactor) {
_rangePow = numHashPerFamily,
_numTables = numHashFamilies;
_dimension = dimension;
_samSize = (int) floor(dimension / samFactor);;
_samFactor = samFactor;
_clEnabled = 0;
_hashType = hashType;
_groupHashingSize = RANDPROJGROUPSIZE;
printf("<<< LSH Parameters >>>\n");
std::cout << "_rangePow " << _rangePow << std::endl;
std::cout << "_numTables " << _numTables << std::endl;
std::cout << "_dimension " << _dimension << std::endl;
std::cout << "_samSize " << _samSize << std::endl;
std::cout << "_hashType " << _hashType << std::endl;
/* Signed random projection. */
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "Generating random number of srp hashes of dense data ... \n";
// Random number generation for hashing purpose - declarations.
// Random number generation for fast random projection.
// Reference: Anshumali Shrivastava, Rice CS
// randBits - random bits deciding to add or subtract, contain randbits for numTable * _rangePow * samSize.
_randBits = new short[_numTables * _rangePow * _samSize];
// indices - selected indices to perform subtraction. Dimension same as randbits.
_indices = new int[_numTables * _rangePow * _samSize];
int *a = new int[_dimension];
for (int i = 0; i < _dimension; i++) {
a[i] = i;
}
for (int tb = 0; tb < _numTables; tb++) {
srand(time(0));
for (int i = 0; i < _rangePow; i++) {
std::random_shuffle(&a[0], &a[_dimension]);
for (int j = 0; j < _samSize; j++) {
_indices[tb * _rangePow * _samSize + i * _samSize + j] = a[j];
// For 1/2 chance, assign random bit 1, or -1 to randBits.
if (rand() % 2 == 0)
_randBits[tb * _rangePow * _samSize + i * _samSize + j] = 1;
else
_randBits[tb * _rangePow * _samSize + i * _samSize + j] = -1;
}
}
}
delete[] a;
std::cout << "Generating random number of universal hashes of sparse data ... \n";
_hash_a = new unsigned int[_rangePow * _numTables];
_hash_b = new unsigned int[_rangePow * _numTables];
_binhash_a = new unsigned int[_rangePow * _numTables];
_binhash_b = new unsigned int[_rangePow * _numTables];
std::default_random_engine generator0;
for (int i = 0; i < _rangePow * _numTables; i++) {
std::uniform_int_distribution<unsigned int> distribution(0, 0x7FFFFFFF);
_binhash_a[i] = (distribution(generator0)) * 2 + 1;
}
for (int i = 0; i < _rangePow * _numTables; i++) {
std::uniform_int_distribution<unsigned int> distribution(0, 0x7FFFFFFF);
_binhash_b[i] = distribution(generator0);
}
for (int i = 0; i < _rangePow * _numTables; i++) {
std::uniform_int_distribution<unsigned int> distribution(0, 0x7FFFFFFF);
_hash_a[i] = (distribution(generator0)) * 2 + 1;
}
for (int i = 0; i < _rangePow * _numTables; i++) {
std::uniform_int_distribution<unsigned int> distribution(0, 0xFFFFFFFF >> _samFactor);
_hash_b[i] = distribution(generator0);
}
}
LSH::~LSH() {
if (_clEnabled) {
switch (_hashType)
{
case 1:
clReleaseKernel(kernel_randproj_dense);
clReleaseKernel(kernel_randproj_sparse);
clReleaseMemObject(_randBits_obj);
clReleaseMemObject(_indices_obj);
clReleaseMemObject(_hash_a_obj);
clReleaseMemObject(_hash_b_obj);
clReleaseMemObject(_binhash_a_obj);
clReleaseMemObject(_binhash_b_obj);
break;
case 2:
break;
default:
break;
}
}
switch (_hashType)
{
case 1:
delete[] _binhash_a;
delete[] _binhash_b;
delete[] _hash_a;
delete[] _hash_b;
delete[] _randBits;
delete[] _indices;
break;
case 2:
delete[] _randHash;
delete[] rand1;
break;
default:
break;
}
}
void LSH::clLSH(cl_platform_id *platforms_lsh, cl_device_id *devices_lsh, cl_context context_lsh,
cl_program program_lsh, cl_command_queue command_queue_lsh) {
_platforms_lsh = platforms_lsh;
_devices_lsh = devices_lsh;
_context_lsh = context_lsh;
_program_lsh = program_lsh;
_command_queue_lsh = command_queue_lsh;
clProgram_LSH(); // Build .cl source files.
kernel_mult_probes_storeid = clCreateKernel(_program_lsh, "mult_probes_storeid", NULL);
kernel_mult_probes = clCreateKernel(_program_lsh, "mult_probes", NULL);
_err = clSetKernelArg(kernel_mult_probes_storeid, 4, sizeof(int), (void *)&_rangePow);
_err |= clSetKernelArg(kernel_mult_probes_storeid, 5, sizeof(int), (void *)&_numTables);
_err |= clSetKernelArg(kernel_mult_probes, 3, sizeof(int), (void *)&_rangePow);
_err |= clSetKernelArg(kernel_mult_probes, 4, sizeof(int), (void *)&_numTables);
ClCheckError(_err, "[LSH::clLSH] Failed to set kernel_mult_probes_storeid & kernel_mult_probes arguments!");
switch (_hashType)
{
case 1:
_clEnabled = 1;
_randBits_obj = clCreateBuffer(_context_lsh, CL_MEM_READ_WRITE,
_numTables * _rangePow * _samSize * sizeof(short), NULL, &_err);
_indices_obj = clCreateBuffer(_context_lsh, CL_MEM_READ_WRITE,
_numTables * _rangePow * _samSize * sizeof(int), NULL, &_err);
_err = clEnqueueWriteBuffer(_command_queue_lsh, _randBits_obj, CL_TRUE, 0,
_numTables * _rangePow * _samSize * sizeof(short), _randBits, 0, NULL, NULL);
_err |= clEnqueueWriteBuffer(_command_queue_lsh, _indices_obj, CL_TRUE, 0,
_numTables * _rangePow * _samSize * sizeof(int), _indices, 0, NULL, NULL);
kernel_randproj_dense = clCreateKernel(_program_lsh, "dense_rand_proj", NULL);
kernel_randproj_sparse = clCreateKernel(_program_lsh, "sparse_rand_proj", NULL);
if (kernel_randproj_dense == NULL || kernel_randproj_sparse == NULL) {
printf("[LSH::clLSH] One or more CPU kernels failed to be created. \n");
}
_hash_a_obj = clCreateBuffer(_context_lsh, CL_MEM_READ_WRITE,
_rangePow * _numTables * sizeof(unsigned int), NULL, &_err);
_hash_b_obj = clCreateBuffer(_context_lsh, CL_MEM_READ_WRITE,
_rangePow * _numTables * sizeof(unsigned int), NULL, &_err);
_err = clEnqueueWriteBuffer(_command_queue_lsh, _hash_a_obj, CL_TRUE, 0,
_rangePow * _numTables * sizeof(unsigned int), _hash_a, 0, NULL, NULL);
_err |= clEnqueueWriteBuffer(_command_queue_lsh, _hash_b_obj, CL_TRUE, 0,
_rangePow * _numTables * sizeof(unsigned int), _hash_b, 0, NULL, NULL);
_binhash_a_obj = clCreateBuffer(_context_lsh, CL_MEM_READ_WRITE,
_rangePow * _numTables * sizeof(unsigned int), NULL, &_err);
_binhash_b_obj = clCreateBuffer(_context_lsh, CL_MEM_READ_WRITE,
_rangePow * _numTables * sizeof(unsigned int), NULL, &_err);
_err |= clEnqueueWriteBuffer(_command_queue_lsh, _binhash_a_obj, CL_TRUE, 0,
_rangePow * _numTables * sizeof(unsigned int), _binhash_a, 0, NULL, NULL);
_err |= clEnqueueWriteBuffer(_command_queue_lsh, _binhash_b_obj, CL_TRUE, 0,
_rangePow * _numTables * sizeof(unsigned int), _binhash_b, 0, NULL, NULL);
_err = clSetKernelArg(kernel_randproj_dense, 2, sizeof(cl_mem), (void *)&_randBits_obj);
_err |= clSetKernelArg(kernel_randproj_dense, 3, sizeof(cl_mem), (void *)&_indices_obj);
_err |= clSetKernelArg(kernel_randproj_dense, 5, sizeof(int), (void *)&_samSize);
_err |= clSetKernelArg(kernel_randproj_dense, 6, sizeof(int), (void *)&_dimension);
_err |= clSetKernelArg(kernel_randproj_dense, 7, sizeof(int), (void *)&_rangePow);
_err |= clSetKernelArg(kernel_randproj_dense, 8, sizeof(int), (void *)&_groupHashingSize);
_err |= clSetKernelArg(kernel_randproj_dense, 9, _samSize * _rangePow * sizeof(int), NULL);
_err |= clSetKernelArg(kernel_randproj_dense, 10, _samSize * _rangePow * sizeof(short), NULL);
_err |= clSetKernelArg(kernel_randproj_dense, 11, _groupHashingSize * _rangePow * sizeof(int), NULL);
ClCheckError(_err, "[LSH::clLSH] Failed to set kernel_randproj_dense arguments!");
_err = clSetKernelArg(kernel_randproj_sparse, 4, sizeof(cl_mem), (void *)&_hash_a_obj);
_err |= clSetKernelArg(kernel_randproj_sparse, 5, sizeof(cl_mem), (void *)&_hash_b_obj);
_err |= clSetKernelArg(kernel_randproj_sparse, 6, sizeof(cl_mem), (void *)&_binhash_a_obj);
_err |= clSetKernelArg(kernel_randproj_sparse, 7, sizeof(cl_mem), (void *)&_binhash_b_obj);
_err |= clSetKernelArg(kernel_randproj_sparse, 9, sizeof(int), (void *)&_rangePow);
_err |= clSetKernelArg(kernel_randproj_sparse, 10, sizeof(int), (void *)&_samFactor);
_err |= clSetKernelArg(kernel_randproj_sparse, 11, sizeof(int), (void *)&_groupHashingSize);
_err |= clSetKernelArg(kernel_randproj_sparse, 12, _groupHashingSize * _rangePow * sizeof(int), NULL);
ClCheckError(_err, "[LSH::clLSH] Failed to set kernel_randproj_sparse arguments!");
break;
case 2:
std::cout << "[LSH::clLSH] No OpenCL implementation: Optimal Densified MinHash. ";
break;
default:
break;
}
}
void LSH::clProgram_LSH() {
// Load and creat program.
FILE *program_handle;
const char *file_name[] = { CL_KERNEL_FILE_1 };
const char options[] = "-cl-finite-math-only -cl-no-signed-zeros -w -cl-mad-enable -cl-fast-relaxed-math -I ./";
size_t program_size[NUM_CL_KERNEL];
size_t log_size;
char *program_buffer[NUM_CL_KERNEL];
for (int i = 0; i < NUM_CL_KERNEL; i++) {
program_handle = fopen(file_name[i], "r");
if (program_handle == NULL) {
perror("[OpenCL] Couldn't find the program file");
exit(1);
}
fseek(program_handle, 0, SEEK_END);
program_size[i] = ftell(program_handle);
rewind(program_handle);
program_buffer[i] = (char*)malloc(program_size[i] + 1);
program_buffer[i][program_size[i]] = '\0';
fread(program_buffer[i], sizeof(char),
program_size[i], program_handle);
fclose(program_handle);
printf("[OpenCL] Program %d loaded, %d characters. \n", i, (int) program_size[i]);
}
_program_lsh = clCreateProgramWithSource(_context_lsh, NUM_CL_KERNEL,
(const char**)program_buffer, program_size, &_err);
if (_err != 0) {
printf("[LSH] Couldn't create CL program for lsh.");
printf("\nError Code: %d\n", _err);
}
// Build lsh program.
_err = clBuildProgram(_program_lsh, 1, _devices_lsh, options, NULL, NULL);
if (_err < 0) {
clGetProgramBuildInfo(_program_lsh, _devices_lsh[0],
CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
_program_log_lsh = (char*)malloc(log_size + 1);
_program_log_lsh[log_size] = '\0';
clGetProgramBuildInfo(_program_lsh, _devices_lsh[0],
CL_PROGRAM_BUILD_LOG,
log_size + 1, _program_log_lsh, NULL);
printf("%s\n", _program_log_lsh);
free(_program_log_lsh);
system("pause");
exit(1);
}
for (int i = 0; i < NUM_CL_KERNEL; i++) {
free(program_buffer[i]);
}
}
void LSH::ClCheckError(cl_int code, const char* msg) {
if (code != 0) {
printf(msg);
printf("\nError Code: %d\n", code);
exit(1);
}
}