mas01mc@292
|
1 #include "lshlib.h"
|
mas01mc@292
|
2
|
mas01mc@292
|
3
|
mas01mc@292
|
4 void err(char*s){cout << s << endl;exit(2);}
|
mas01mc@292
|
5
|
mas01mc@292
|
6 Uns32T get_page_logn(){
|
mas01mc@292
|
7 int pagesz = (int)sysconf(_SC_PAGESIZE);
|
mas01mc@292
|
8 return (Uns32T)log2((double)pagesz);
|
mas01mc@292
|
9 }
|
mas01mc@292
|
10
|
mas01cr@370
|
11 unsigned align_up(unsigned x, unsigned w) { return (((x) + ((1<<w)-1)) & ~((1<<w)-1)); }
|
mas01mc@292
|
12
|
mas01mc@292
|
13 void H::error(const char* a, const char* b, const char *sysFunc) {
|
mas01mc@292
|
14 cerr << a << ": " << b << endl;
|
mas01mc@292
|
15 if (sysFunc) {
|
mas01mc@292
|
16 perror(sysFunc);
|
mas01mc@292
|
17 }
|
mas01mc@292
|
18 exit(1);
|
mas01mc@292
|
19 }
|
mas01mc@292
|
20
|
mas01mc@519
|
21 H::H():
|
mas01mc@519
|
22 multiProbePtr(new MultiProbe()),
|
mas01mc@519
|
23 boundaryDistances(0)
|
mas01mc@519
|
24 {
|
mas01mc@293
|
25 // Delay initialization of lsh functions until we know the parameters
|
mas01mc@293
|
26 }
|
mas01mc@293
|
27
|
mas01mc@293
|
28 H::H(Uns32T kk, Uns32T mm, Uns32T dd, Uns32T NN, Uns32T CC, float ww, float rr):
|
mas01mc@292
|
29 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
30 use_u_functions(true),
|
mas01mc@292
|
31 #else
|
mas01mc@292
|
32 use_u_functions(false),
|
mas01mc@292
|
33 #endif
|
mas01mc@293
|
34 maxp(0),
|
mas01mc@292
|
35 bucketCount(0),
|
mas01mc@292
|
36 pointCount(0),
|
mas01mc@292
|
37 N(NN),
|
mas01mc@292
|
38 C(CC),
|
mas01mc@292
|
39 k(kk),
|
mas01mc@292
|
40 m(mm),
|
mas01mc@293
|
41 L((mm*(mm-1))/2),
|
mas01mc@293
|
42 d(dd),
|
mas01mc@293
|
43 w(ww),
|
mas01mc@519
|
44 radius(rr),
|
mas01mc@519
|
45 multiProbePtr(new MultiProbe()),
|
mas01mc@519
|
46 boundaryDistances(0)
|
mas01mc@292
|
47 {
|
mas01mc@292
|
48
|
mas01mc@292
|
49 if(m<2){
|
mas01mc@292
|
50 m=2;
|
mas01mc@292
|
51 L=1; // check value of L
|
mas01mc@292
|
52 cout << "warning: setting m=2, L=1" << endl;
|
mas01mc@292
|
53 }
|
mas01mc@292
|
54 if(use_u_functions && k%2){
|
mas01mc@292
|
55 k++; // make sure k is even
|
mas01mc@292
|
56 cout << "warning: setting k even" << endl;
|
mas01mc@292
|
57 }
|
mas01mc@293
|
58
|
mas01mc@293
|
59 // We have the necessary parameters, so construct hashfunction datastructures
|
mas01mc@293
|
60 initialize_lsh_functions();
|
mas01mc@292
|
61 }
|
mas01mc@292
|
62
|
mas01mc@293
|
63 void H::initialize_lsh_functions(){
|
mas01mc@292
|
64 H::P = UH_PRIME_DEFAULT;
|
mas01mc@292
|
65
|
mas01mc@292
|
66 /* FIXME: don't use time(); instead use /dev/random or similar */
|
mas01mc@292
|
67 /* FIXME: write out the seed somewhere, so that we can get
|
mas01mc@292
|
68 repeatability */
|
mas01mc@292
|
69 #ifdef MT19937
|
mas01mc@292
|
70 init_genrand(time(NULL));
|
mas01mc@292
|
71 #else
|
mas01mc@292
|
72 srand(time(NULL)); // seed random number generator
|
mas01mc@292
|
73 #endif
|
mas01mc@293
|
74 Uns32T i,j, kk;
|
mas01mc@293
|
75 #ifdef USE_U_FUNCTIONS
|
mas01mc@293
|
76 H::A = new float**[ H::m ]; // m x k x d random projectors
|
mas01mc@293
|
77 H::b = new float*[ H::m ]; // m x k random biases
|
mas01mc@293
|
78 #else
|
mas01mc@293
|
79 H::A = new float**[ H::L ]; // m x k x d random projectors
|
mas01mc@293
|
80 H::b = new float*[ H::L ]; // m x k random biases
|
mas01mc@293
|
81 #endif
|
mas01mc@293
|
82 H::g = new Uns32T*[ H::L ]; // L x k random projections
|
mas01mc@293
|
83 assert( H::g && H::A && H::b ); // failure
|
mas01mc@293
|
84 #ifdef USE_U_FUNCTIONS
|
mas01mc@293
|
85 // Use m \times u_i functions \in R^{(k/2) \times (d)}
|
mas01mc@293
|
86 // Combine to make L=m(m-1)/2 hash functions \in R^{k \times d}
|
mas01mc@293
|
87 for( j = 0; j < H::m ; j++ ){ // m functions u_i(v)
|
mas01mc@293
|
88 H::A[j] = new float*[ H::k/2 ]; // k/2 x d 2-stable distribution coefficients
|
mas01mc@293
|
89 H::b[j] = new float[ H::k/2 ]; // bias
|
mas01mc@293
|
90 assert( H::A[j] && H::b[j] ); // failure
|
mas01mc@293
|
91 for( kk = 0; kk < H::k/2 ; kk++ ){
|
mas01mc@293
|
92 H::A[j][kk] = new float[ H::d ];
|
mas01mc@293
|
93 assert( H::A[j][kk] ); // failure
|
mas01mc@293
|
94 for(Uns32T i = 0 ; i < H::d ; i++ )
|
mas01mc@293
|
95 H::A[j][kk][i] = H::randn(); // Normal
|
mas01mc@293
|
96 H::b[j][kk] = H::ranf()*H::w; // Uniform
|
mas01mc@293
|
97 }
|
mas01mc@293
|
98 }
|
mas01mc@293
|
99 #else
|
mas01mc@293
|
100 // Use m \times u_i functions \in R^{k \times (d)}
|
mas01mc@293
|
101 // Combine to make L=m(m-1)/2 hash functions \in R^{k \times d}
|
mas01mc@293
|
102 for( j = 0; j < H::L ; j++ ){ // m functions u_i(v)
|
mas01mc@293
|
103 H::A[j] = new float*[ H::k ]; // k x d 2-stable distribution coefficients
|
mas01mc@293
|
104 H::b[j] = new float[ H::k ]; // bias
|
mas01mc@293
|
105 assert( H::A[j] && H::b[j] ); // failure
|
mas01mc@293
|
106 for( kk = 0; kk < H::k ; kk++ ){
|
mas01mc@293
|
107 H::A[j][kk] = new float[ H::d ];
|
mas01mc@293
|
108 assert( H::A[j][kk] ); // failure
|
mas01mc@293
|
109 for(Uns32T i = 0 ; i < H::d ; i++ )
|
mas01mc@293
|
110 H::A[j][kk][i] = H::randn(); // Normal
|
mas01mc@293
|
111 H::b[j][kk] = H::ranf()*H::w; // Uniform
|
mas01mc@293
|
112 }
|
mas01mc@293
|
113 }
|
mas01mc@293
|
114 #endif
|
mas01mc@293
|
115
|
mas01mc@293
|
116 // Storage for LSH hash function output (Uns32T)
|
mas01mc@293
|
117 for( j = 0 ; j < H::L ; j++ ){ // L functions g_j(u_a, u_b) a,b \in nchoosek(m,2)
|
mas01mc@293
|
118 H::g[j] = new Uns32T[ H::k ]; // k x 32-bit hash values, gj(v)=[x0 x1 ... xk-1] xk \in Z
|
mas01mc@293
|
119 assert( H::g[j] );
|
mas01mc@292
|
120 }
|
mas01mc@292
|
121
|
mas01mc@293
|
122 // LSH Hash tables
|
mas01mc@293
|
123 H::h = new bucket**[ H::L ];
|
mas01mc@293
|
124 assert( H::h );
|
mas01mc@292
|
125 for( j = 0 ; j < H::L ; j++ ){
|
mas01mc@292
|
126 H::h[j] = new bucket*[ H::N ];
|
mas01mc@292
|
127 assert( H::h[j] );
|
mas01mc@292
|
128 for( i = 0 ; i < H::N ; i++)
|
mas01mc@292
|
129 H::h[j][i] = 0;
|
mas01mc@292
|
130 }
|
mas01mc@293
|
131
|
mas01mc@293
|
132 // Standard hash functions
|
mas01mc@293
|
133 H::r1 = new Uns32T*[ H::L ];
|
mas01mc@293
|
134 H::r2 = new Uns32T*[ H::L ];
|
mas01mc@293
|
135 assert( H::r1 && H::r2 ); // failure
|
mas01mc@293
|
136 for( j = 0 ; j < H::L ; j++ ){
|
mas01mc@293
|
137 H::r1[ j ] = new Uns32T[ H::k ];
|
mas01mc@293
|
138 H::r2[ j ] = new Uns32T[ H::k ];
|
mas01mc@293
|
139 assert( H::r1[j] && H::r2[j] ); // failure
|
mas01mc@293
|
140 for( i = 0; i<H::k; i++){
|
mas01mc@293
|
141 H::r1[j][i] = randr();
|
mas01mc@293
|
142 H::r2[j][i] = randr();
|
mas01mc@293
|
143 }
|
mas01mc@293
|
144 }
|
mas01mc@293
|
145
|
mas01mc@516
|
146 // Storage for whole or partial function evaluation depending on USE_U_FUNCTIONS
|
mas01mc@293
|
147 H::initialize_partial_functions();
|
mas01mc@519
|
148
|
mas01mc@519
|
149 // MultiProbe distance functions, there are 2*k per hashtable
|
mas01mc@519
|
150 H::boundaryDistances = new float*[ H::L ]; // L x 2k boundary distances
|
mas01mc@519
|
151 assert( H::boundaryDistances ); // failure
|
mas01mc@519
|
152 for( j = 0; j < H::L ; j++ ){ // 2*k functions x_i(q)
|
mas01mc@519
|
153 H::boundaryDistances[j] = new float[ 2*H::k ];
|
mas01mc@519
|
154 assert( H::boundaryDistances[j] ); // failure
|
mas01mc@519
|
155 for( kk = 0; kk < 2*H::k ; kk++ )
|
mas01mc@519
|
156 H::boundaryDistances[j][kk] = 0.0f; // initialize with zeros
|
mas01mc@519
|
157 }
|
mas01mc@293
|
158 }
|
mas01mc@293
|
159
|
mas01mc@293
|
160 void H::initialize_partial_functions(){
|
mas01mc@293
|
161
|
mas01mc@293
|
162 #ifdef USE_U_FUNCTIONS
|
mas01mc@293
|
163 H::uu = vector<vector<Uns32T> >(H::m);
|
mas01mc@293
|
164 for( Uns32T aa=0 ; aa < H::m ; aa++ )
|
mas01mc@293
|
165 H::uu[aa] = vector<Uns32T>( H::k/2 );
|
mas01mc@293
|
166 #endif
|
mas01mc@293
|
167 }
|
mas01mc@293
|
168
|
mas01mc@293
|
169
|
mas01mc@293
|
170 // Generate z ~ N(0,1)
|
mas01mc@293
|
171 float H::randn(){
|
mas01mc@293
|
172 // Box-Muller
|
mas01mc@293
|
173 float x1, x2;
|
mas01mc@293
|
174 do{
|
mas01mc@293
|
175 x1 = ranf();
|
mas01mc@293
|
176 } while (x1 == 0); // cannot take log of 0
|
mas01mc@293
|
177 x2 = ranf();
|
mas01mc@293
|
178 float z;
|
mas01mc@293
|
179 z = sqrtf(-2.0 * logf(x1)) * cosf(2.0 * M_PI * x2);
|
mas01mc@293
|
180 return z;
|
mas01mc@293
|
181 }
|
mas01mc@293
|
182
|
mas01mc@293
|
183 float H::ranf(){
|
mas01mc@293
|
184 #ifdef MT19937
|
mas01mc@293
|
185 return (float) genrand_real2();
|
mas01mc@293
|
186 #else
|
mas01mc@293
|
187 return (float)( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
|
mas01mc@293
|
188 #endif
|
mas01mc@293
|
189 }
|
mas01mc@293
|
190
|
mas01mc@293
|
191 // range is 1..2^29
|
mas01mc@293
|
192 /* FIXME: that looks like an ... odd range. Still. */
|
mas01mc@293
|
193 Uns32T H::randr(){
|
mas01mc@293
|
194 #ifdef MT19937
|
mas01mc@293
|
195 return (Uns32T)((genrand_int32() >> 3) + 1);
|
mas01mc@293
|
196 #else
|
mas01mc@293
|
197 return (Uns32T) ((rand() >> 2) + 1);
|
mas01mc@293
|
198 #endif
|
mas01mc@292
|
199 }
|
mas01mc@292
|
200
|
mas01mc@292
|
201 // Destruct hash tables
|
mas01mc@292
|
202 H::~H(){
|
mas01mc@293
|
203 Uns32T i,j,kk;
|
mas01mc@340
|
204 bucket** pp;
|
mas01mc@293
|
205 #ifdef USE_U_FUNCTIONS
|
mas01mc@293
|
206 for( j = 0 ; j < H::m ; j++ ){
|
mas01mc@293
|
207 for( kk = 0 ; kk < H::k/2 ; kk++ )
|
mas01mc@293
|
208 delete[] A[j][kk];
|
mas01mc@293
|
209 delete[] A[j];
|
mas01mc@293
|
210 }
|
mas01mc@293
|
211 delete[] A;
|
mas01mc@293
|
212 for( j = 0 ; j < H::m ; j++ )
|
mas01mc@293
|
213 delete[] b[j];
|
mas01mc@293
|
214 delete[] b;
|
mas01mc@293
|
215 #else
|
mas01mc@293
|
216 for( j = 0 ; j < H::L ; j++ ){
|
mas01mc@293
|
217 for( kk = 0 ; kk < H::k ; kk++ )
|
mas01mc@293
|
218 delete[] A[j][kk];
|
mas01mc@293
|
219 delete[] A[j];
|
mas01mc@293
|
220 }
|
mas01mc@293
|
221 delete[] A;
|
mas01mc@293
|
222 for( j = 0 ; j < H::L ; j++ )
|
mas01mc@293
|
223 delete[] b[j];
|
mas01mc@293
|
224 delete[] b;
|
mas01mc@293
|
225 #endif
|
mas01mc@293
|
226
|
mas01mc@293
|
227 for( j = 0 ; j < H::L ; j++ )
|
mas01mc@293
|
228 delete[] g[j];
|
mas01mc@293
|
229 delete[] g;
|
mas01mc@292
|
230 for( j=0 ; j < H::L ; j++ ){
|
mas01mc@292
|
231 delete[] H::r1[ j ];
|
mas01mc@292
|
232 delete[] H::r2[ j ];
|
mas01mc@340
|
233 for(i = 0; i< H::N ; i++){
|
mas01mc@340
|
234 pp = 0;
|
mas01mc@340
|
235 #ifdef LSH_CORE_ARRAY
|
mas01mc@340
|
236 if(H::h[ j ][ i ])
|
mas01mc@340
|
237 if(H::h[ j ][ i ]->t2 & LSH_CORE_ARRAY_BIT){
|
mas01mc@340
|
238 pp = get_pointer_to_bucket_linked_list(H::h[ j ][ i ]);
|
mas01mc@340
|
239 if(*pp){
|
mas01mc@344
|
240 (*pp)->snext.ptr=0; // Head of list uses snext as a non-pointer value
|
mas01mc@340
|
241 delete *pp; // Now the destructor can do its work properly
|
mas01mc@340
|
242 }
|
mas01mc@340
|
243 free(H::h[ j ][ i ]->next);
|
mas01mc@340
|
244 H::h[ j ][ i ]->next = 0; // Zero next pointer
|
mas01mc@344
|
245 H::h[ j ][ i ]->snext.ptr = 0; // Zero head-of-list pointer as above
|
mas01mc@340
|
246 }
|
mas01mc@340
|
247 #endif
|
mas01mc@292
|
248 delete H::h[ j ][ i ];
|
mas01mc@340
|
249 }
|
mas01mc@292
|
250 delete[] H::h[ j ];
|
mas01mc@292
|
251 }
|
mas01mc@474
|
252 delete[] H::r1;
|
mas01mc@474
|
253 delete[] H::r2;
|
mas01mc@474
|
254 delete[] H::h;
|
mas01mc@519
|
255
|
mas01mc@519
|
256 // MultiProbe cleanup
|
mas01mc@519
|
257 for( j = 0 ; j < H::L ; j++ )
|
mas01mc@519
|
258 delete[] H::boundaryDistances[j];
|
mas01mc@519
|
259 delete[] H::boundaryDistances;
|
mas01mc@519
|
260 delete multiProbePtr;
|
mas01mc@292
|
261 }
|
mas01mc@292
|
262
|
mas01mc@292
|
263
|
mas01mc@293
|
264 // Compute all hash functions for vector v
|
mas01mc@293
|
265 // #ifdef USE_U_FUNCTIONS use Combination of m \times h_i \in R^{(k/2) \times d}
|
mas01mc@293
|
266 // to make L \times g_j functions \in Z^k
|
mas01mc@293
|
267 void H::compute_hash_functions(vector<float>& v){ // v \in R^d
|
mas01mc@293
|
268 float iw = 1. / H::w; // hash bucket width
|
mas01mc@293
|
269 Uns32T aa, kk;
|
mas01mc@293
|
270 if( v.size() != H::d )
|
mas01mc@293
|
271 error("v.size != H::d","","compute_hash_functions"); // check input vector dimensionality
|
mas01mc@293
|
272 double tmp = 0;
|
mas01mc@519
|
273 float *pA, *pb, *bd;
|
mas01mc@293
|
274 Uns32T *pg;
|
mas01mc@293
|
275 int dd;
|
mas01mc@293
|
276 vector<float>::iterator vi;
|
mas01mc@293
|
277 vector<Uns32T>::iterator ui;
|
mas01mc@293
|
278
|
mas01mc@293
|
279 #ifdef USE_U_FUNCTIONS
|
mas01mc@293
|
280 Uns32T bb;
|
mas01mc@293
|
281 // Store m dot products to expand
|
mas01mc@293
|
282 for( aa=0; aa < H::m ; aa++ ){
|
mas01mc@293
|
283 ui = H::uu[aa].begin();
|
mas01mc@293
|
284 for( kk = 0 ; kk < H::k/2 ; kk++ ){
|
mas01mc@293
|
285 pb = *( H::b + aa ) + kk;
|
mas01mc@293
|
286 pA = * ( * ( H::A + aa ) + kk );
|
mas01mc@293
|
287 dd = H::d;
|
mas01mc@293
|
288 tmp = 0.;
|
mas01mc@293
|
289 vi = v.begin();
|
mas01mc@293
|
290 while( dd-- )
|
mas01mc@293
|
291 tmp += *pA++ * *vi++; // project
|
mas01mc@293
|
292 tmp += *pb; // translate
|
mas01mc@293
|
293 tmp *= iw; // scale
|
mas01mc@293
|
294 *ui++ = (Uns32T) floor(tmp); // floor
|
mas01mc@293
|
295 }
|
mas01mc@293
|
296 }
|
mas01mc@293
|
297 // Binomial combinations of functions u_{a,b} \in Z^{(k/2) \times d}
|
mas01mc@293
|
298 Uns32T j;
|
mas01mc@293
|
299 for( aa=0, j=0 ; aa < H::m-1 ; aa++ )
|
mas01mc@293
|
300 for( bb = aa + 1 ; bb < H::m ; bb++, j++ ){
|
mas01mc@293
|
301 pg= *( H::g + j ); // L \times functions g_j(v) \in Z^k
|
mas01mc@293
|
302 // u_1 \in Z^{(k/2) \times d}
|
mas01mc@293
|
303 ui = H::uu[aa].begin();
|
mas01mc@293
|
304 kk=H::k/2;
|
mas01mc@293
|
305 while( kk-- )
|
mas01mc@293
|
306 *pg++ = *ui++; // hash function g_j(v)=[x1 x2 ... x(k/2)]; xk \in Z
|
mas01mc@293
|
307 // u_2 \in Z^{(k/2) \times d}
|
mas01mc@293
|
308 ui = H::uu[bb].begin();
|
mas01mc@293
|
309 kk=H::k/2;
|
mas01mc@293
|
310 while( kk--)
|
mas01mc@293
|
311 *pg++ = *ui++; // hash function g_j(v)=[x(k/2+1) x(k/2+2) ... xk]; xk \in Z
|
mas01mc@293
|
312 }
|
mas01mc@293
|
313 #else
|
mas01mc@293
|
314 for( aa=0; aa < H::L ; aa++ ){
|
mas01mc@514
|
315 pg= *( H::g + aa ); // L \times functions g_j(v) \in Z^k
|
mas01mc@519
|
316 bd= *( H::boundaryDistances + aa);
|
mas01mc@514
|
317 for( kk = 0 ; kk != H::k ; kk++ ){
|
mas01mc@293
|
318 pb = *( H::b + aa ) + kk;
|
mas01mc@293
|
319 pA = * ( * ( H::A + aa ) + kk );
|
mas01mc@293
|
320 dd = H::d;
|
mas01mc@293
|
321 tmp = 0.;
|
mas01mc@293
|
322 vi = v.begin();
|
mas01mc@293
|
323 while( dd-- )
|
mas01mc@293
|
324 tmp += *pA++ * *vi++; // project
|
mas01mc@293
|
325 tmp += *pb; // translate
|
mas01mc@519
|
326 tmp *= iw; // scale
|
mas01mc@521
|
327 tmp = floor(tmp); // handle negative values
|
mas01mc@521
|
328 while(tmp<0) // wrap around 0 to N
|
mas01mc@521
|
329 tmp += H::N;
|
mas01mc@521
|
330 *pg = (Uns32T) tmp; // hash function g_j(v)=[x1 x2 ... xk]; xk \in Z
|
mas01mc@519
|
331 *bd = (tmp - *pg++);//*w; // boundary distance -1
|
mas01mc@519
|
332 *(bd+1) = (1.0f - *bd); //*w; // boundary distance +1
|
mas01mc@519
|
333 bd+=2;
|
mas01mc@293
|
334 }
|
mas01mc@293
|
335 }
|
mas01mc@293
|
336 #endif
|
mas01mc@293
|
337 }
|
mas01mc@293
|
338
|
mas01mc@292
|
339 // make hash value \in Z
|
mas01mc@293
|
340 void H::generate_hash_keys(Uns32T*g, Uns32T* r1, Uns32T* r2){
|
mas01mc@293
|
341 H::t1 = computeProductModDefaultPrime( g, r1, H::k ) % H::N;
|
mas01mc@293
|
342 H::t2 = computeProductModDefaultPrime( g, r2, H::k );
|
mas01mc@292
|
343 }
|
mas01mc@292
|
344
|
mas01mc@519
|
345 // make hash value by purturbating the given hash functions
|
mas01mc@519
|
346 // according the the boundary distances of the current query
|
mas01mc@519
|
347 void H::generate_multiprobe_keys(Uns32T*g, Uns32T* r1, Uns32T* r2){
|
mas01mc@519
|
348 assert(!multiProbePtr->empty()); // Test this for now, until all is stable
|
mas01mc@520
|
349 Uns32T* mpg = new Uns32T[H::k]; // temporary array storage
|
mas01mc@519
|
350
|
mas01mc@519
|
351 // Copy the hash bucket identifiers
|
mas01mc@519
|
352 Uns32T* mpgPtr = mpg;
|
mas01mc@519
|
353 Uns32T kk = H::k;
|
mas01mc@519
|
354 while(kk--)
|
mas01mc@519
|
355 *mpgPtr++ = *g++;
|
mas01mc@519
|
356
|
mas01mc@519
|
357 // Retrieve the next purturbation set
|
mas01mc@519
|
358 perturbation_set ps = multiProbePtr->getNextPerturbationSet();
|
mas01mc@519
|
359 perturbation_set::iterator it = ps.begin();
|
mas01mc@519
|
360
|
mas01mc@519
|
361 // Perturbate the hash functions g
|
mas01mc@519
|
362 while( it != ps.end() ){
|
mas01mc@519
|
363 *(mpg + multiProbePtr->getIndex(it)) += multiProbePtr->getBoundary(it);
|
mas01mc@519
|
364 it++;
|
mas01mc@519
|
365 }
|
mas01mc@519
|
366
|
mas01mc@519
|
367 H::t1 = computeProductModDefaultPrime( mpg, r1, H::k ) % H::N;
|
mas01mc@519
|
368 H::t2 = computeProductModDefaultPrime( mpg, r2, H::k );
|
mas01mc@519
|
369
|
mas01mc@519
|
370 delete[] mpg; // free up temporary storage
|
mas01mc@519
|
371 }
|
mas01mc@519
|
372
|
mas01mc@292
|
373 #define CR_ASSERT(b){if(!(b)){fprintf(stderr, "ASSERT failed on line %d, file %s.\n", __LINE__, __FILE__); exit(1);}}
|
mas01mc@292
|
374
|
mas01mc@292
|
375 // Computes (a.b) mod UH_PRIME_DEFAULT
|
mas01mc@293
|
376 inline Uns32T H::computeProductModDefaultPrime(Uns32T *a, Uns32T *b, IntT size){
|
mas01mc@292
|
377 LongUns64T h = 0;
|
mas01mc@292
|
378
|
mas01mc@292
|
379 for(IntT i = 0; i < size; i++){
|
mas01mc@292
|
380 h = h + (LongUns64T)a[i] * (LongUns64T)b[i];
|
mas01mc@292
|
381 h = (h & TWO_TO_32_MINUS_1) + 5 * (h >> 32);
|
mas01mc@292
|
382 if (h >= UH_PRIME_DEFAULT) {
|
mas01mc@292
|
383 h = h - UH_PRIME_DEFAULT;
|
mas01mc@292
|
384 }
|
mas01mc@292
|
385 CR_ASSERT(h < UH_PRIME_DEFAULT);
|
mas01mc@292
|
386 }
|
mas01mc@292
|
387 return h;
|
mas01mc@292
|
388 }
|
mas01mc@292
|
389
|
mas01mc@292
|
390 Uns32T H::bucket_insert_point(bucket **pp){
|
mas01mc@296
|
391 collisionCount = 0;
|
mas01mc@340
|
392 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@292
|
393 if(!*pp){
|
mas01mc@292
|
394 *pp = new bucket();
|
mas01mc@292
|
395 (*pp)->t2 = 0; // Use t2 as a collision counter for the row
|
mas01mc@292
|
396 (*pp)->next = new bucket();
|
mas01mc@292
|
397 }
|
mas01mc@340
|
398 // The list head holds point collision count
|
mas01mc@340
|
399 if( (*pp)->t2 & LSH_CORE_ARRAY_BIT )
|
mas01mc@340
|
400 bucket_insert_point(get_pointer_to_bucket_linked_list(*pp)); // recurse
|
mas01mc@340
|
401 else{
|
mas01mc@340
|
402 collisionCount = (*pp)->t2;
|
mas01mc@340
|
403 if(collisionCount < H::C){ // Block if row is full
|
mas01mc@340
|
404 (*pp)->t2++; // Increment collision counter (numPoints in row)
|
mas01mc@340
|
405 pointCount++;
|
mas01mc@340
|
406 collisionCount++;
|
mas01mc@340
|
407 // Locate the bucket linked list
|
mas01mc@340
|
408 __bucket_insert_point( (*pp)->next );
|
mas01mc@340
|
409 }
|
mas01mc@292
|
410 }
|
mas01mc@340
|
411 #else // NOT USING LSH_LIST_HEAD_COUNTERS
|
mas01mc@340
|
412 if(!*pp)
|
mas01mc@340
|
413 *pp = new bucket();
|
mas01mc@296
|
414 pointCount++;
|
mas01mc@296
|
415 __bucket_insert_point(*pp); // No collision count storage
|
mas01mc@292
|
416 #endif
|
mas01mc@292
|
417 return collisionCount;
|
mas01mc@292
|
418 }
|
mas01mc@292
|
419
|
mas01mc@340
|
420 // insert points into hashtable row collision chain
|
mas01mc@292
|
421 void H::__bucket_insert_point(bucket* p){
|
mas01mc@292
|
422 if(p->t2 == IFLAG){ // initialization flag, is it in the domain of t2?
|
mas01mc@292
|
423 p->t2 = H::t2;
|
mas01mc@292
|
424 bucketCount++; // Record start of new point-locale collision chain
|
mas01mc@344
|
425 p->snext.ptr = new sbucket();
|
mas01mc@344
|
426 __sbucket_insert_point(p->snext.ptr);
|
mas01mc@292
|
427 return;
|
mas01mc@292
|
428 }
|
mas01mc@292
|
429
|
mas01mc@292
|
430 if(p->t2 == H::t2){
|
mas01mc@344
|
431 __sbucket_insert_point(p->snext.ptr);
|
mas01mc@292
|
432 return;
|
mas01mc@292
|
433 }
|
mas01mc@524
|
434 /*
|
mas01mc@292
|
435 if(p->next){
|
mas01mc@340
|
436 // Construct list in t2 order
|
mas01mc@340
|
437 if(H::t2 < p->next->t2){
|
mas01mc@340
|
438 bucket* tmp = new bucket();
|
mas01mc@340
|
439 tmp->next = p->next;
|
mas01mc@340
|
440 p->next = tmp;
|
mas01mc@340
|
441 __bucket_insert_point(tmp);
|
mas01mc@340
|
442 }
|
mas01mc@524
|
443 */
|
mas01mc@524
|
444 // insert bucket before current bucket
|
mas01mc@524
|
445 if(H::t2 < p->t2){
|
mas01mc@524
|
446 bucket* tmp = new bucket();
|
mas01mc@524
|
447 // copy current bucket contents into new bucket
|
mas01mc@524
|
448 tmp->next = p->next;
|
mas01mc@524
|
449 tmp->t2 = p->t2;
|
mas01mc@524
|
450 tmp->snext.ptr = p->snext.ptr;
|
mas01mc@524
|
451 p->next = tmp;
|
mas01mc@524
|
452 p->t2 = IFLAG;
|
mas01mc@524
|
453 p->snext.ptr=0;
|
mas01mc@524
|
454 __bucket_insert_point(p);
|
mas01mc@524
|
455 return;
|
mas01mc@292
|
456 }
|
mas01mc@524
|
457
|
mas01mc@524
|
458 if(p->next)
|
mas01mc@524
|
459 __bucket_insert_point(p->next);
|
mas01mc@340
|
460 else {
|
mas01mc@292
|
461 p->next = new bucket();
|
mas01mc@292
|
462 __bucket_insert_point(p->next);
|
mas01mc@292
|
463 }
|
mas01mc@292
|
464 }
|
mas01mc@292
|
465
|
mas01mc@340
|
466 // insert points into point-locale collision chain
|
mas01mc@292
|
467 void H::__sbucket_insert_point(sbucket* p){
|
mas01mc@292
|
468 if(p->pointID==IFLAG){
|
mas01mc@292
|
469 p->pointID = H::p;
|
mas01mc@292
|
470 return;
|
mas01mc@292
|
471 }
|
mas01mc@340
|
472
|
mas01mc@292
|
473 // Search for pointID
|
mas01mc@340
|
474 if(p->snext){
|
mas01mc@292
|
475 __sbucket_insert_point(p->snext);
|
mas01mc@292
|
476 }
|
mas01mc@292
|
477 else{
|
mas01mc@340
|
478 // Make new point collision bucket at end of list
|
mas01mc@340
|
479 p->snext = new sbucket();
|
mas01mc@340
|
480 __sbucket_insert_point(p->snext);
|
mas01mc@292
|
481 }
|
mas01mc@292
|
482 }
|
mas01mc@292
|
483
|
mas01mc@293
|
484 inline bucket** H::get_bucket(int j){
|
mas01mc@292
|
485 return *(h+j);
|
mas01mc@292
|
486 }
|
mas01mc@292
|
487
|
mas01mc@340
|
488 // Find the linked-list pointer at the end of the CORE_ARRAY
|
mas01mc@340
|
489 bucket** H::get_pointer_to_bucket_linked_list(bucket* rowPtr){
|
mas01mc@344
|
490 Uns32T numBuckets = rowPtr->snext.numBuckets; // Cast pointer to unsigned int
|
mas01mc@343
|
491 Uns32T numPoints = rowPtr->t2 & 0x7FFFFFFF; // Value is stored in low 31 bits of t2 field
|
mas01mc@343
|
492 bucket** listPtr = reinterpret_cast<bucket**> (reinterpret_cast<unsigned int*>(rowPtr->next)+numPoints+numBuckets+1);
|
mas01mc@343
|
493 return listPtr;
|
mas01mc@340
|
494 }
|
mas01mc@340
|
495
|
mas01mc@293
|
496 // Interface to Locality Sensitive Hashing G
|
mas01mc@293
|
497 G::G(float ww, Uns32T kk,Uns32T mm, Uns32T dd, Uns32T NN, Uns32T CC, float rr):
|
mas01mc@293
|
498 H(kk,mm,dd,NN,CC,ww,rr), // constructor to initialize data structures
|
mas01mc@308
|
499 indexName(0),
|
mas01mc@293
|
500 lshHeader(0),
|
mas01mc@292
|
501 calling_instance(0),
|
mas01mc@293
|
502 add_point_callback(0)
|
mas01mc@292
|
503 {
|
mas01mc@293
|
504
|
mas01mc@292
|
505 }
|
mas01mc@292
|
506
|
mas01mc@292
|
507 // Serialize from file LSH constructor
|
mas01mc@292
|
508 // Read parameters from database file
|
mas01mc@292
|
509 // Load the hash functions, close the database
|
mas01mc@292
|
510 // Optionally load the LSH tables into head-allocated lists in core
|
mas01mc@292
|
511 G::G(char* filename, bool lshInCoreFlag):
|
mas01mc@293
|
512 H(), // default base-class constructor call delays data-structure initialization
|
mas01mc@309
|
513 indexName(0),
|
mas01mc@293
|
514 lshHeader(0),
|
mas01mc@292
|
515 calling_instance(0),
|
mas01mc@292
|
516 add_point_callback(0)
|
mas01mc@292
|
517 {
|
mas01mc@474
|
518 FILE* dbFile = 0;
|
mas01mc@292
|
519 int dbfid = unserialize_lsh_header(filename);
|
mas01mc@519
|
520
|
mas01mc@309
|
521 indexName = new char[O2_INDEX_MAXSTR];
|
mas01mc@309
|
522 strncpy(indexName, filename, O2_INDEX_MAXSTR); // COPY THE CONTENTS TO THE NEW POINTER
|
mas01mc@293
|
523 H::initialize_lsh_functions(); // Base-class data-structure initialization
|
mas01mc@293
|
524 unserialize_lsh_functions(dbfid); // populate with on-disk hashfunction values
|
mas01mc@292
|
525
|
mas01mc@292
|
526 // Format1 only needs unserializing if specifically requested
|
mas01mc@292
|
527 if(!(lshHeader->flags&O2_SERIAL_FILEFORMAT2) && lshInCoreFlag){
|
mas01mc@292
|
528 unserialize_lsh_hashtables_format1(dbfid);
|
mas01mc@292
|
529 }
|
mas01mc@292
|
530
|
mas01mc@292
|
531 // Format2 always needs unserializing
|
mas01mc@292
|
532 if(lshHeader->flags&O2_SERIAL_FILEFORMAT2 && lshInCoreFlag){
|
mas01mc@474
|
533 dbFile = fdopen(dbfid, "rb");
|
mas01mc@336
|
534 if(!dbFile)
|
mas01mc@336
|
535 error("Cannot open LSH file for reading", filename);
|
mas01mc@336
|
536 unserialize_lsh_hashtables_format2(dbFile);
|
mas01mc@292
|
537 }
|
mas01mc@336
|
538 serial_close(dbfid);
|
mas01mc@474
|
539 if(dbFile){
|
mas01mc@474
|
540 fclose(dbFile);
|
mas01mc@474
|
541 dbFile = 0;
|
mas01mc@474
|
542 }
|
mas01mc@336
|
543 }
|
mas01mc@292
|
544
|
mas01mc@292
|
545 G::~G(){
|
mas01mc@292
|
546 delete lshHeader;
|
mas01mc@474
|
547 delete[] indexName;
|
mas01mc@292
|
548 }
|
mas01mc@292
|
549
|
mas01mc@292
|
550 // single point insertion; inserted values are hash value and pointID
|
mas01mc@292
|
551 Uns32T G::insert_point(vector<float>& v, Uns32T pp){
|
mas01mc@292
|
552 Uns32T collisionCount = 0;
|
mas01mc@292
|
553 H::p = pp;
|
mas01mc@299
|
554 if(H::maxp && pp<=H::maxp)
|
mas01mc@296
|
555 error("points must be indexed in strict ascending order", "LSH::insert_point(vector<float>&, Uns32T pointID)");
|
mas01mc@296
|
556 H::maxp=pp; // Store highest pointID in database
|
mas01mc@293
|
557 H::compute_hash_functions( v );
|
mas01mc@292
|
558 for(Uns32T j = 0 ; j < H::L ; j++ ){ // insertion
|
mas01mc@293
|
559 H::generate_hash_keys( *( H::g + j ), *( H::r1 + j ), *( H::r2 + j ) );
|
mas01mc@292
|
560 collisionCount += bucket_insert_point( *(h + j) + t1 );
|
mas01mc@292
|
561 }
|
mas01mc@292
|
562 return collisionCount;
|
mas01mc@292
|
563 }
|
mas01mc@292
|
564
|
mas01mc@292
|
565
|
mas01mc@292
|
566 // batch insert for a point set
|
mas01mc@292
|
567 // inserted values are vector hash value and pointID starting at basePointID
|
mas01mc@292
|
568 void G::insert_point_set(vector<vector<float> >& vv, Uns32T basePointID){
|
mas01mc@292
|
569 for(Uns32T point=0; point<vv.size(); point++)
|
mas01mc@292
|
570 insert_point(vv[point], basePointID+point);
|
mas01mc@292
|
571 }
|
mas01mc@292
|
572
|
mas01mc@292
|
573 // point retrieval routine
|
mas01mc@292
|
574 void G::retrieve_point(vector<float>& v, Uns32T qpos, ReporterCallbackPtr add_point, void* caller){
|
mas01mc@522
|
575 // assert(LSH_MULTI_PROBE_COUNT);
|
mas01mc@292
|
576 calling_instance = caller;
|
mas01mc@292
|
577 add_point_callback = add_point;
|
mas01mc@293
|
578 H::compute_hash_functions( v );
|
mas01mc@292
|
579 for(Uns32T j = 0 ; j < H::L ; j++ ){
|
mas01mc@519
|
580 // MultiProbe loop
|
mas01mc@519
|
581 multiProbePtr->generatePerturbationSets( *( H::boundaryDistances + j ) , 2*H::k, (unsigned)LSH_MULTI_PROBE_COUNT);
|
mas01mc@522
|
582 for(Uns32T multiProbeIdx = 0 ; multiProbeIdx < multiProbePtr->size()+1 ; multiProbeIdx++ ){
|
mas01mc@519
|
583 if(!multiProbeIdx)
|
mas01mc@519
|
584 H::generate_hash_keys( *( H::g + j ), *( H::r1 + j ), *( H::r2 + j ) );
|
mas01mc@519
|
585 else
|
mas01mc@519
|
586 H::generate_multiprobe_keys( *( H::g + j ), *( H::r1 + j ), *( H::r2 + j ) );
|
mas01mc@519
|
587 if( bucket* bPtr = *(get_bucket(j) + get_t1()) ) {
|
mas01mc@340
|
588 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@519
|
589 if(bPtr->t2&LSH_CORE_ARRAY_BIT) {
|
mas01mc@519
|
590 retrieve_from_core_hashtable_array((Uns32T*)(bPtr->next), qpos);
|
mas01mc@519
|
591 } else {
|
mas01mc@519
|
592 bucket_chain_point( bPtr->next, qpos);
|
mas01mc@519
|
593 }
|
mas01mc@519
|
594 #else
|
mas01mc@519
|
595 bucket_chain_point( bPtr , qpos);
|
mas01mc@519
|
596 #endif
|
mas01cr@370
|
597 }
|
mas01cr@370
|
598 }
|
mas01mc@292
|
599 }
|
mas01mc@292
|
600 }
|
mas01mc@292
|
601
|
mas01mc@292
|
602 void G::retrieve_point_set(vector<vector<float> >& vv, ReporterCallbackPtr add_point, void* caller){
|
mas01mc@292
|
603 for(Uns32T qpos = 0 ; qpos < vv.size() ; qpos++ )
|
mas01mc@292
|
604 retrieve_point(vv[qpos], qpos, add_point, caller);
|
mas01mc@292
|
605 }
|
mas01mc@292
|
606
|
mas01mc@292
|
607 // export lsh tables to table structure on disk
|
mas01mc@292
|
608 //
|
mas01mc@292
|
609 // LSH TABLE STRUCTURE
|
mas01mc@292
|
610 // ---header 64 bytes ---
|
mas01mc@292
|
611 // [magic #tables #rows #cols elementSize databaseSize version flags dim #funs 0 0 0 0 0 0]
|
mas01mc@292
|
612 //
|
mas01mc@292
|
613 // ---random projections L x k x d float ---
|
mas01mc@292
|
614 // A[0][0][0] A[0][0][1] ... A[0][0][d-1]
|
mas01mc@292
|
615 // A[0][1][0] A[0][1][1] ... A[1][1][d-1]
|
mas01mc@292
|
616 // ...
|
mas01mc@292
|
617 // A[0][K-1][0] A[0][1][1] ... A[0][k-1][d-1]
|
mas01mc@292
|
618 // ...
|
mas01mc@292
|
619 // ...
|
mas01mc@292
|
620 // A[L-1][0][0] A[M-1][0][1] ... A[L-1][0][d-1]
|
mas01mc@292
|
621 // A[L-1][1][0] A[M-1][1][1] ... A[L-1][1][d-1]
|
mas01mc@292
|
622 // ...
|
mas01mc@292
|
623 // A[L-1][k-1][0] A[M-1][1][1] ... A[L-1][k-1][d-1]
|
mas01mc@292
|
624 //
|
mas01mc@292
|
625 // ---bias L x k float ---
|
mas01mc@292
|
626 // b[0][0] b[0][1] ... b[0][k-1]
|
mas01mc@292
|
627 // b[1][0] b[1][1] ... b[1][k-1]
|
mas01mc@292
|
628 // ...
|
mas01mc@292
|
629 // b[L-1][0] b[L-1][1] ... b[L-1][k-1]
|
mas01mc@292
|
630 //
|
mas01mc@292
|
631 // ---random r1 L x k float ---
|
mas01mc@292
|
632 // r1[0][0] r1[0][1] ... r1[0][k-1]
|
mas01mc@292
|
633 // r1[1][0] r1[1][1] ... r1[1][k-1]
|
mas01mc@292
|
634 // ...
|
mas01mc@292
|
635 // r1[L-1][0] r1[L-1][1] ... r1[L-1][k-1]
|
mas01mc@292
|
636 //
|
mas01mc@292
|
637 // ---random r2 L x k float ---
|
mas01mc@292
|
638 // r2[0][0] r2[0][1] ... r2[0][k-1]
|
mas01mc@292
|
639 // r2[1][0] r2[1][1] ... r2[1][k-1]
|
mas01mc@292
|
640 // ...
|
mas01mc@292
|
641 // r2[L-1][0] r2[L-1][1] ... r2[L-1][k-1]
|
mas01mc@292
|
642 //
|
mas01mc@293
|
643 // ******* HASHTABLES FORMAT1 (optimized for LSH_ON_DISK retrieval) *******
|
mas01mc@292
|
644 // ---hash table 0: N x C x 8 ---
|
mas01mc@292
|
645 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
646 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
647 // ...
|
mas01mc@292
|
648 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
649 //
|
mas01mc@292
|
650 // ---hash table 1: N x C x 8 ---
|
mas01mc@292
|
651 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
652 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
653 // ...
|
mas01mc@292
|
654 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
655 //
|
mas01mc@292
|
656 // ...
|
mas01mc@292
|
657 //
|
mas01mc@292
|
658 // ---hash table L-1: N x C x 8 ---
|
mas01mc@292
|
659 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
660 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
661 // ...
|
mas01mc@292
|
662 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
663 //
|
mas01mc@293
|
664 // ******* HASHTABLES FORMAT2 (optimized for LSH_IN_CORE retrieval) *******
|
mas01mc@293
|
665 //
|
mas01mc@293
|
666 // State machine controlled by regular expression.
|
mas01mc@293
|
667 // legend:
|
mas01mc@293
|
668 //
|
mas01mc@306
|
669 // O2_SERIAL_TOKEN_T1 = 0xFFFFFFFCU
|
mas01mc@306
|
670 // O2_SERIAL_TOKEN_T2 = 0xFFFFFFFDU
|
mas01mc@306
|
671 // O2_SERIAL_TOKEN_ENDTABLE = 0xFFFFFFFEU
|
mas01mc@293
|
672 //
|
mas01mc@306
|
673 // T1 - T1 hash token
|
mas01mc@306
|
674 // t1 - t1 hash key (t1 range 0..2^29-1)
|
mas01mc@524
|
675 // %buckets+points% numElements in row for ARRAY encoding
|
mas01mc@306
|
676 // T2 - T2 token
|
mas01mc@293
|
677 // t2 - t2 hash key (range 1..2^32-6)
|
mas01mc@293
|
678 // p - point identifier (range 0..2^32-1)
|
mas01mc@306
|
679 // E - end hash table token
|
mas01mc@293
|
680 // {...} required arguments
|
mas01mc@293
|
681 // [...] optional arguments
|
mas01mc@293
|
682 // * - match zero or more occurences
|
mas01mc@293
|
683 // + - match one or more occurences
|
mas01mc@293
|
684 // {...}^L - repeat argument L times
|
mas01mc@293
|
685 //
|
mas01mc@293
|
686 // FORMAT2 Regular expression:
|
mas01mc@524
|
687 // { [T1 t1 %buckets+points% [T2 t2 p+]+ ]* E }^L
|
mas01mc@293
|
688 //
|
mas01mc@292
|
689
|
mas01mc@292
|
690 // Serial header constructors
|
mas01mc@292
|
691 SerialHeader::SerialHeader(){;}
|
mas01mc@296
|
692 SerialHeader::SerialHeader(float W, Uns32T L, Uns32T N, Uns32T C, Uns32T k, Uns32T d, float r, Uns32T p, Uns32T FMT, Uns32T pc):
|
mas01mc@292
|
693 lshMagic(O2_SERIAL_MAGIC),
|
mas01mc@292
|
694 binWidth(W),
|
mas01mc@292
|
695 numTables(L),
|
mas01mc@292
|
696 numRows(N),
|
mas01mc@292
|
697 numCols(C),
|
mas01mc@292
|
698 elementSize(O2_SERIAL_ELEMENT_SIZE),
|
mas01mc@296
|
699 version(O2_SERIAL_VERSION),
|
mas01mc@296
|
700 size(0), // we are deprecating this value
|
mas01mc@292
|
701 flags(FMT),
|
mas01mc@292
|
702 dataDim(d),
|
mas01mc@292
|
703 numFuns(k),
|
mas01mc@292
|
704 radius(r),
|
mas01mc@296
|
705 maxp(p),
|
mas01mc@296
|
706 size_long((unsigned long long)L * align_up((unsigned long long)N * C * O2_SERIAL_ELEMENT_SIZE, get_page_logn()) // hash tables
|
mas01mc@296
|
707 + align_up(O2_SERIAL_HEADER_SIZE + // header + hash functions
|
mas01mc@296
|
708 (unsigned long long)L*k*( sizeof(float)*d+2*sizeof(Uns32T)+sizeof(float)),get_page_logn())),
|
mas01mc@296
|
709 pointCount(pc){
|
mas01mc@296
|
710
|
mas01mc@296
|
711 if(FMT==O2_SERIAL_FILEFORMAT2)
|
mas01mc@296
|
712 size_long = (unsigned long long)align_up(O2_SERIAL_HEADER_SIZE
|
mas01mc@296
|
713 + (unsigned long long)L*k*(sizeof(float)*d+2+sizeof(Uns32T)
|
mas01mc@296
|
714 +sizeof(float)) + (unsigned long long)pc*16UL,get_page_logn());
|
mas01mc@296
|
715 } // header
|
mas01mc@292
|
716
|
mas01mc@292
|
717 float* G::get_serial_hashfunction_base(char* db){
|
mas01mc@292
|
718 if(db&&lshHeader)
|
mas01mc@292
|
719 return (float*)(db+O2_SERIAL_HEADER_SIZE);
|
mas01mc@292
|
720 else return NULL;
|
mas01mc@292
|
721 }
|
mas01mc@292
|
722
|
mas01mc@292
|
723 SerialElementT* G::get_serial_hashtable_base(char* db){
|
mas01mc@292
|
724 if(db&&lshHeader)
|
mas01mc@292
|
725 return (SerialElementT*)(db+get_serial_hashtable_offset());
|
mas01mc@292
|
726 else
|
mas01mc@292
|
727 return NULL;
|
mas01mc@292
|
728 }
|
mas01mc@292
|
729
|
mas01mc@292
|
730 Uns32T G::get_serial_hashtable_offset(){
|
mas01mc@292
|
731 if(lshHeader)
|
mas01mc@292
|
732 return align_up(O2_SERIAL_HEADER_SIZE +
|
mas01mc@292
|
733 L*lshHeader->numFuns*( sizeof(float)*lshHeader->dataDim+2*sizeof(Uns32T)+sizeof(float)),get_page_logn());
|
mas01mc@292
|
734 else
|
mas01mc@292
|
735 return 0;
|
mas01mc@292
|
736 }
|
mas01mc@292
|
737
|
mas01mc@292
|
738 void G::serialize(char* filename, Uns32T serialFormat){
|
mas01mc@292
|
739 int dbfid;
|
mas01mc@292
|
740 char* db;
|
mas01mc@292
|
741 int dbIsNew=0;
|
mas01mc@474
|
742 FILE* dbFile = 0;
|
mas01mc@292
|
743 // Check requested serialFormat
|
mas01mc@292
|
744 if(!(serialFormat==O2_SERIAL_FILEFORMAT1 || serialFormat==O2_SERIAL_FILEFORMAT2))
|
mas01mc@292
|
745 error("Unrecognized serial file format request: ", "serialize()");
|
mas01mc@296
|
746
|
mas01mc@292
|
747 // Test to see if file exists
|
mas01cr@370
|
748 if((dbfid = open (filename, O_RDONLY)) < 0) {
|
mas01mc@292
|
749 // If it doesn't, then create the file (CREATE)
|
mas01cr@370
|
750 if(errno == ENOENT) {
|
mas01mc@292
|
751 // Create the file
|
mas01mc@292
|
752 std::cout << "Creating new serialized LSH database:" << filename << "...";
|
mas01mc@292
|
753 std::cout.flush();
|
mas01mc@292
|
754 serial_create(filename, serialFormat);
|
mas01mc@292
|
755 dbIsNew=1;
|
mas01cr@370
|
756 } else {
|
mas01mc@292
|
757 // The file can't be opened
|
mas01mc@292
|
758 error("Can't open the file", filename, "open");
|
mas01cr@370
|
759 }
|
mas01cr@370
|
760 }
|
mas01mc@292
|
761
|
mas01mc@292
|
762 // Load the on-disk header into core
|
mas01mc@292
|
763 dbfid = serial_open(filename, 1); // open for write
|
mas01mc@292
|
764 db = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 1);// get database pointer
|
mas01mc@292
|
765 serial_get_header(db); // read header
|
mas01mc@292
|
766 serial_munmap(db, O2_SERIAL_HEADER_SIZE); // drop mmap
|
mas01mc@292
|
767
|
mas01mc@292
|
768 // Check compatibility of core and disk data structures
|
mas01mc@292
|
769 if( !serial_can_merge(serialFormat) )
|
mas01mc@292
|
770 error("Incompatible core and serial LSH, data structure dimensions mismatch.");
|
mas01mc@292
|
771
|
mas01mc@292
|
772 // For new LSH databases write the hashfunctions
|
mas01mc@292
|
773 if(dbIsNew)
|
mas01mc@292
|
774 serialize_lsh_hashfunctions(dbfid);
|
mas01mc@292
|
775 // Write the hashtables in the requested format
|
mas01mc@292
|
776 if(serialFormat == O2_SERIAL_FILEFORMAT1)
|
mas01mc@292
|
777 serialize_lsh_hashtables_format1(dbfid, !dbIsNew);
|
mas01mc@336
|
778 else{
|
mas01mc@474
|
779 dbFile = fdopen(dbfid, "r+b");
|
mas01mc@336
|
780 if(!dbFile)
|
mas01mc@336
|
781 error("Cannot open LSH file for writing",filename);
|
mas01mc@336
|
782 serialize_lsh_hashtables_format2(dbFile, !dbIsNew);
|
mas01mc@336
|
783 fflush(dbFile);
|
mas01mc@336
|
784 }
|
mas01mc@292
|
785
|
mas01mc@336
|
786 if(!dbIsNew) {
|
mas01mc@292
|
787 db = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 1);// get database pointer
|
mas01mc@292
|
788 //serial_get_header(db); // read header
|
mas01mc@293
|
789 cout << "maxp = " << H::maxp << endl;
|
mas01mc@293
|
790 lshHeader->maxp=H::maxp;
|
mas01mc@292
|
791 // Default to FILEFORMAT1
|
mas01mc@292
|
792 if(!(lshHeader->flags&O2_SERIAL_FILEFORMAT2))
|
mas01mc@294
|
793 lshHeader->flags|=O2_SERIAL_FILEFORMAT1;
|
mas01mc@292
|
794 memcpy((char*)db, (char*)lshHeader, sizeof(SerialHeaderT));
|
mas01mc@292
|
795 serial_munmap(db, O2_SERIAL_HEADER_SIZE); // drop mmap
|
mas01mc@336
|
796 }
|
mas01mc@336
|
797 serial_close(dbfid);
|
mas01mc@474
|
798 if(dbFile){
|
mas01mc@474
|
799 fclose(dbFile);
|
mas01mc@474
|
800 dbFile = 0;
|
mas01mc@474
|
801 }
|
mas01mc@292
|
802 }
|
mas01mc@292
|
803
|
mas01mc@292
|
804 // Test to see if core structure and requested format is
|
mas01mc@292
|
805 // compatible with currently opened database
|
mas01mc@292
|
806 int G::serial_can_merge(Uns32T format){
|
mas01mc@292
|
807 SerialHeaderT* that = lshHeader;
|
mas01mc@292
|
808 if( (format==O2_SERIAL_FILEFORMAT2 && !that->flags&O2_SERIAL_FILEFORMAT2)
|
mas01mc@292
|
809 || (format!=O2_SERIAL_FILEFORMAT2 && that->flags&O2_SERIAL_FILEFORMAT2)
|
mas01mc@292
|
810 || !( this->w == that->binWidth &&
|
mas01mc@296
|
811 this->L == that->numTables &&
|
mas01mc@296
|
812 this->N == that->numRows &&
|
mas01mc@296
|
813 this->k == that->numFuns &&
|
mas01mc@296
|
814 this->d == that->dataDim &&
|
mas01mc@296
|
815 sizeof(SerialElementT) == that->elementSize &&
|
mas01mc@296
|
816 this->radius == that->radius)){
|
mas01mc@292
|
817 serial_print_header(format);
|
mas01mc@292
|
818 return 0;
|
mas01mc@292
|
819 }
|
mas01mc@292
|
820 else
|
mas01mc@292
|
821 return 1;
|
mas01mc@292
|
822 }
|
mas01mc@292
|
823
|
mas01mc@292
|
824 // Used as an error message for serial_can_merge()
|
mas01mc@292
|
825 void G::serial_print_header(Uns32T format){
|
mas01mc@292
|
826 std::cout << "Fc:" << format << " Fs:" << lshHeader->flags << endl;
|
mas01mc@292
|
827 std::cout << "Wc:" << w << " Ls:" << lshHeader->binWidth << endl;
|
mas01mc@292
|
828 std::cout << "Lc:" << L << " Ls:" << lshHeader->numTables << endl;
|
mas01mc@292
|
829 std::cout << "Nc:" << N << " Ns:" << lshHeader->numRows << endl;
|
mas01mc@292
|
830 std::cout << "kc:" << k << " ks:" << lshHeader->numFuns << endl;
|
mas01mc@292
|
831 std::cout << "dc:" << d << " ds:" << lshHeader->dataDim << endl;
|
mas01mc@292
|
832 std::cout << "sc:" << sizeof(SerialElementT) << " ss:" << lshHeader->elementSize << endl;
|
mas01mc@292
|
833 std::cout << "rc:" << this->radius << " rs:" << lshHeader->radius << endl;
|
mas01mc@292
|
834 }
|
mas01mc@292
|
835
|
mas01mc@292
|
836 int G::serialize_lsh_hashfunctions(int fid){
|
mas01mc@292
|
837 float* pf;
|
mas01mc@292
|
838 Uns32T *pu;
|
mas01mc@292
|
839 Uns32T x,y,z;
|
mas01mc@292
|
840
|
mas01mc@293
|
841 char* db = serial_mmap(fid, get_serial_hashtable_offset(), 1);// get database pointer
|
mas01mc@292
|
842 pf = get_serial_hashfunction_base(db);
|
mas01mc@292
|
843
|
mas01mc@292
|
844 // HASH FUNCTIONS
|
mas01mc@292
|
845 // Write the random projectors A[][][]
|
mas01mc@292
|
846 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
847 for( x = 0 ; x < H::m ; x++ )
|
mas01mc@292
|
848 for( y = 0 ; y < H::k/2 ; y++ )
|
mas01mc@292
|
849 #else
|
mas01mc@292
|
850 for( x = 0 ; x < H::L ; x++ )
|
mas01mc@292
|
851 for( y = 0 ; y < H::k ; y++ )
|
mas01mc@292
|
852 #endif
|
mas01mc@292
|
853 for( z = 0 ; z < d ; z++ )
|
mas01mc@293
|
854 *pf++ = H::A[x][y][z];
|
mas01mc@292
|
855
|
mas01mc@292
|
856 // Write the random biases b[][]
|
mas01mc@292
|
857 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
858 for( x = 0 ; x < H::m ; x++ )
|
mas01mc@292
|
859 for( y = 0 ; y < H::k/2 ; y++ )
|
mas01mc@292
|
860 #else
|
mas01mc@292
|
861 for( x = 0 ; x < H::L ; x++ )
|
mas01mc@292
|
862 for( y = 0 ; y < H::k ; y++ )
|
mas01mc@292
|
863 #endif
|
mas01mc@293
|
864 *pf++ = H::b[x][y];
|
mas01mc@292
|
865
|
mas01mc@292
|
866 pu = (Uns32T*)pf;
|
mas01mc@292
|
867
|
mas01mc@292
|
868 // Write the Z projectors r1[][]
|
mas01mc@292
|
869 for( x = 0 ; x < H::L ; x++)
|
mas01mc@292
|
870 for( y = 0 ; y < H::k ; y++)
|
mas01mc@293
|
871 *pu++ = H::r1[x][y];
|
mas01mc@292
|
872
|
mas01mc@292
|
873 // Write the Z projectors r2[][]
|
mas01mc@292
|
874 for( x = 0 ; x < H::L ; x++)
|
mas01mc@292
|
875 for( y = 0; y < H::k ; y++)
|
mas01mc@293
|
876 *pu++ = H::r2[x][y];
|
mas01mc@292
|
877
|
mas01mc@292
|
878 serial_munmap(db, get_serial_hashtable_offset());
|
mas01mc@292
|
879 return 1;
|
mas01mc@292
|
880 }
|
mas01mc@292
|
881
|
mas01mc@292
|
882 int G::serialize_lsh_hashtables_format1(int fid, int merge){
|
mas01mc@292
|
883 SerialElementT *pe, *pt;
|
mas01mc@292
|
884 Uns32T x,y;
|
mas01mc@292
|
885
|
mas01mc@292
|
886 if( merge && !serial_can_merge(O2_SERIAL_FILEFORMAT1) )
|
mas01mc@292
|
887 error("Cannot merge core and serial LSH, data structure dimensions mismatch.");
|
mas01mc@292
|
888
|
mas01mc@292
|
889 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
890 Uns32T colCount, meanColCount, colCountN, maxColCount, minColCount;
|
mas01mc@292
|
891 // Write the hash tables
|
mas01mc@292
|
892 for( x = 0 ; x < H::L ; x++ ){
|
mas01mc@292
|
893 std::cout << (merge ? "merging":"writing") << " hash table " << x << " FORMAT1...";
|
mas01mc@292
|
894 std::cout.flush();
|
mas01mc@292
|
895 // memory map a single hash table for sequential access
|
mas01mc@292
|
896 // Align each hash table to page boundary
|
mas01mc@292
|
897 char* dbtable = serial_mmap(fid, hashTableSize, 1,
|
mas01mc@292
|
898 align_up(get_serial_hashtable_offset()+x*hashTableSize, get_page_logn()));
|
mas01mc@324
|
899 #ifdef __CYGWIN__
|
mas01mc@324
|
900 // No madvise in CYGWIN
|
mas01mc@324
|
901 #else
|
mas01mc@292
|
902 if(madvise(dbtable, hashTableSize, MADV_SEQUENTIAL)<0)
|
mas01mc@292
|
903 error("could not advise hashtable memory","","madvise");
|
mas01mc@324
|
904 #endif
|
mas01mc@292
|
905 maxColCount=0;
|
mas01mc@292
|
906 minColCount=O2_SERIAL_MAX_COLS;
|
mas01mc@292
|
907 meanColCount=0;
|
mas01mc@292
|
908 colCountN=0;
|
mas01mc@292
|
909 pt=(SerialElementT*)dbtable;
|
mas01mc@292
|
910 for( y = 0 ; y < H::N ; y++ ){
|
mas01mc@292
|
911 // Move disk pointer to beginning of row
|
mas01mc@292
|
912 pe=pt+y*lshHeader->numCols;
|
mas01mc@292
|
913
|
mas01mc@292
|
914 colCount=0;
|
mas01cr@370
|
915 if(bucket* bPtr = h[x][y]) {
|
mas01cr@370
|
916 if(merge) {
|
mas01mc@340
|
917 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@292
|
918 serial_merge_hashtable_row_format1(pe, bPtr->next, colCount); // skip collision counter bucket
|
mas01cr@370
|
919 } else {
|
mas01mc@292
|
920 serial_write_hashtable_row_format1(pe, bPtr->next, colCount); // skip collision counter bucket
|
mas01mc@292
|
921 #else
|
mas01cr@370
|
922 serial_merge_hashtable_row_format1(pe, bPtr, colCount);
|
mas01cr@370
|
923 } else {
|
mas01cr@370
|
924 serial_write_hashtable_row_format1(pe, bPtr, colCount);
|
mas01mc@292
|
925 #endif
|
mas01cr@370
|
926 }
|
mas01cr@370
|
927 }
|
mas01mc@292
|
928 if(colCount){
|
mas01mc@292
|
929 if(colCount<minColCount)
|
mas01mc@292
|
930 minColCount=colCount;
|
mas01mc@292
|
931 if(colCount>maxColCount)
|
mas01mc@292
|
932 maxColCount=colCount;
|
mas01mc@292
|
933 meanColCount+=colCount;
|
mas01mc@292
|
934 colCountN++;
|
mas01mc@292
|
935 }
|
mas01mc@292
|
936 }
|
mas01mc@292
|
937 if(colCountN)
|
mas01mc@292
|
938 std::cout << "#rows with collisions =" << colCountN << ", mean = " << meanColCount/(float)colCountN
|
mas01mc@292
|
939 << ", min = " << minColCount << ", max = " << maxColCount
|
mas01mc@292
|
940 << endl;
|
mas01mc@292
|
941 serial_munmap(dbtable, hashTableSize);
|
mas01mc@292
|
942 }
|
mas01mc@292
|
943
|
mas01mc@292
|
944 // We're done writing
|
mas01mc@292
|
945 return 1;
|
mas01mc@292
|
946 }
|
mas01mc@292
|
947
|
mas01mc@292
|
948 void G::serial_merge_hashtable_row_format1(SerialElementT* pr, bucket* b, Uns32T& colCount){
|
mas01mc@292
|
949 while(b && b->t2!=IFLAG){
|
mas01mc@292
|
950 SerialElementT*pe=pr; // reset disk pointer to beginning of row
|
mas01mc@344
|
951 serial_merge_element_format1(pe, b->snext.ptr, b->t2, colCount);
|
mas01mc@292
|
952 b=b->next;
|
mas01mc@292
|
953 }
|
mas01mc@292
|
954 }
|
mas01mc@292
|
955
|
mas01mc@292
|
956 void G::serial_merge_element_format1(SerialElementT* pe, sbucket* sb, Uns32T t2, Uns32T& colCount){
|
mas01mc@292
|
957 while(sb){
|
mas01mc@292
|
958 if(colCount==lshHeader->numCols){
|
mas01mc@292
|
959 std::cout << "!point-chain full " << endl;
|
mas01mc@292
|
960 return;
|
mas01mc@292
|
961 }
|
mas01mc@292
|
962 Uns32T c=0;
|
mas01mc@292
|
963 // Merge collision chains
|
mas01mc@292
|
964 while(c<lshHeader->numCols){
|
mas01mc@292
|
965 if( (pe+c)->hashValue==IFLAG){
|
mas01mc@292
|
966 (pe+c)->hashValue=t2;
|
mas01mc@292
|
967 (pe+c)->pointID=sb->pointID;
|
mas01mc@292
|
968 colCount=c+1;
|
mas01mc@292
|
969 if(c+1<lshHeader->numCols)
|
mas01mc@292
|
970 (pe+c+1)->hashValue=IFLAG;
|
mas01mc@292
|
971 break;
|
mas01mc@292
|
972 }
|
mas01mc@292
|
973 c++;
|
mas01mc@292
|
974 }
|
mas01mc@292
|
975 sb=sb->snext;
|
mas01mc@292
|
976 }
|
mas01mc@292
|
977 return;
|
mas01mc@292
|
978 }
|
mas01mc@292
|
979
|
mas01mc@292
|
980 void G::serial_write_hashtable_row_format1(SerialElementT*& pe, bucket* b, Uns32T& colCount){
|
mas01mc@292
|
981 pe->hashValue=IFLAG;
|
mas01mc@292
|
982 while(b && b->t2!=IFLAG){
|
mas01mc@344
|
983 serial_write_element_format1(pe, b->snext.ptr, b->t2, colCount);
|
mas01mc@292
|
984 b=b->next;
|
mas01mc@292
|
985 }
|
mas01mc@292
|
986 }
|
mas01mc@292
|
987
|
mas01mc@292
|
988 void G::serial_write_element_format1(SerialElementT*& pe, sbucket* sb, Uns32T t2, Uns32T& colCount){
|
mas01mc@292
|
989 while(sb){
|
mas01mc@292
|
990 if(colCount==lshHeader->numCols){
|
mas01mc@292
|
991 std::cout << "!point-chain full " << endl;
|
mas01mc@292
|
992 return;
|
mas01mc@292
|
993 }
|
mas01mc@292
|
994 pe->hashValue=t2;
|
mas01mc@292
|
995 pe->pointID=sb->pointID;
|
mas01mc@292
|
996 pe++;
|
mas01mc@292
|
997 colCount++;
|
mas01mc@292
|
998 sb=sb->snext;
|
mas01mc@292
|
999 }
|
mas01mc@292
|
1000 pe->hashValue=IFLAG;
|
mas01mc@292
|
1001 return;
|
mas01mc@292
|
1002 }
|
mas01mc@292
|
1003
|
mas01mc@336
|
1004 int G::serialize_lsh_hashtables_format2(FILE* dbFile, int merge){
|
mas01mc@292
|
1005 Uns32T x,y;
|
mas01mc@292
|
1006
|
mas01mc@292
|
1007 if( merge && !serial_can_merge(O2_SERIAL_FILEFORMAT2) )
|
mas01mc@292
|
1008 error("Cannot merge core and serial LSH, data structure dimensions mismatch.");
|
mas01mc@292
|
1009
|
mas01mc@340
|
1010 // We must pereform FORMAT2 merges in core FORMAT1 (dynamic list structure)
|
mas01mc@292
|
1011 if(merge)
|
mas01mc@340
|
1012 unserialize_lsh_hashtables_format2(dbFile, merge);
|
mas01mc@292
|
1013
|
mas01mc@292
|
1014 Uns32T colCount, meanColCount, colCountN, maxColCount, minColCount, t1;
|
mas01mc@336
|
1015 if(fseek(dbFile, get_serial_hashtable_offset(), SEEK_SET)){
|
mas01mc@336
|
1016 fclose(dbFile);
|
mas01mc@336
|
1017 error("fSeek error in serialize_lsh_hashtables_format2");
|
mas01mc@336
|
1018 }
|
mas01mc@292
|
1019
|
mas01mc@292
|
1020 // Write the hash tables
|
mas01mc@292
|
1021 for( x = 0 ; x < H::L ; x++ ){
|
mas01mc@292
|
1022 std::cout << (merge ? "merging":"writing") << " hash table " << x << " FORMAT2...";
|
mas01mc@292
|
1023 std::cout.flush();
|
mas01mc@292
|
1024 maxColCount=0;
|
mas01mc@292
|
1025 minColCount=O2_SERIAL_MAX_COLS;
|
mas01mc@292
|
1026 meanColCount=0;
|
mas01mc@292
|
1027 colCountN=0;
|
mas01mc@523
|
1028 H::tablesPointCount = 0;
|
mas01mc@292
|
1029 for( y = 0 ; y < H::N ; y++ ){
|
mas01mc@292
|
1030 colCount=0;
|
mas01mc@292
|
1031 if(bucket* bPtr = h[x][y]){
|
mas01mc@306
|
1032 // Check for empty row (even though row was allocated)
|
mas01mc@340
|
1033 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@306
|
1034 if(bPtr->next->t2==IFLAG){
|
mas01mc@336
|
1035 fclose(dbFile);
|
mas01mc@306
|
1036 error("b->next->t2==IFLAG","serialize_lsh_hashtables_format2()");
|
mas01mc@306
|
1037 }
|
mas01mc@306
|
1038 #else
|
mas01mc@306
|
1039 if(bPtr->t2==IFLAG){
|
mas01mc@336
|
1040 fclose(dbFile);
|
mas01mc@306
|
1041 error("b->t2==IFLAG","serialize_lsh_hashtables_format2()");
|
mas01mc@306
|
1042 }
|
mas01mc@306
|
1043 #endif
|
mas01mc@306
|
1044 t1 = O2_SERIAL_TOKEN_T1;
|
mas01mc@340
|
1045 WRITE_UNS32(&t1, "[T1]");
|
mas01mc@306
|
1046 t1 = y;
|
mas01mc@340
|
1047 WRITE_UNS32(&t1, "[t1]");
|
mas01mc@340
|
1048 #ifdef LSH_CORE_ARRAY
|
mas01mc@340
|
1049 t1 = count_buckets_and_points_hashtable_row(bPtr);
|
mas01mc@340
|
1050 WRITE_UNS32(&t1,"[count]"); // write numElements
|
mas01mc@340
|
1051 #endif
|
mas01mc@340
|
1052 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@336
|
1053 serial_write_hashtable_row_format2(dbFile, bPtr->next, colCount); // skip collision counter bucket
|
mas01mc@292
|
1054 #else
|
mas01mc@340
|
1055 serial_write_hashtable_row_format2(dbFile, bPtr, colCount);
|
mas01mc@292
|
1056 #endif
|
mas01mc@292
|
1057 }
|
mas01mc@292
|
1058 if(colCount){
|
mas01mc@292
|
1059 if(colCount<minColCount)
|
mas01mc@292
|
1060 minColCount=colCount;
|
mas01mc@292
|
1061 if(colCount>maxColCount)
|
mas01mc@292
|
1062 maxColCount=colCount;
|
mas01mc@292
|
1063 meanColCount+=colCount;
|
mas01mc@292
|
1064 colCountN++;
|
mas01mc@292
|
1065 }
|
mas01mc@523
|
1066 H::tablesPointCount+=colCount;
|
mas01mc@292
|
1067 }
|
mas01mc@292
|
1068 // Write END of table marker
|
mas01mc@306
|
1069 t1 = O2_SERIAL_TOKEN_ENDTABLE;
|
mas01mc@340
|
1070 WRITE_UNS32(&t1,"[end]");
|
mas01mc@292
|
1071 if(colCountN)
|
mas01mc@523
|
1072 std::cout << "#points: " << H::tablesPointCount << " #rows with collisions =" << colCountN << ", mean = " << meanColCount/(float)colCountN
|
mas01mc@292
|
1073 << ", min = " << minColCount << ", max = " << maxColCount
|
mas01mc@292
|
1074 << endl;
|
mas01mc@340
|
1075 }
|
mas01mc@292
|
1076 // We're done writing
|
mas01mc@292
|
1077 return 1;
|
mas01mc@292
|
1078 }
|
mas01mc@292
|
1079
|
mas01mc@340
|
1080 Uns32T G::count_buckets_and_points_hashtable_row(bucket* bPtr){
|
mas01mc@340
|
1081 Uns32T total_count = 0;
|
mas01mc@340
|
1082 bucket* p = 0;
|
mas01mc@340
|
1083
|
mas01mc@340
|
1084 // count points
|
mas01mc@340
|
1085 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@340
|
1086 total_count = bPtr->t2; // points already counted
|
mas01mc@340
|
1087 p = bPtr->next;
|
mas01mc@340
|
1088 #else
|
mas01mc@340
|
1089 total_count = count_points_hashtable_row(bPtr);
|
mas01mc@340
|
1090 p = bPtr;
|
mas01mc@340
|
1091 #endif
|
mas01mc@340
|
1092
|
mas01mc@340
|
1093 // count buckets
|
mas01mc@340
|
1094 do{
|
mas01mc@340
|
1095 total_count++;
|
mas01mc@340
|
1096 }while((p=p->next));
|
mas01mc@340
|
1097
|
mas01mc@340
|
1098 return total_count;
|
mas01mc@340
|
1099 }
|
mas01mc@340
|
1100
|
mas01mc@340
|
1101 Uns32T G::count_points_hashtable_row(bucket* bPtr){
|
mas01mc@340
|
1102 Uns32T point_count = 0;
|
mas01mc@340
|
1103 bucket* p = bPtr;
|
mas01mc@340
|
1104 sbucket* s = 0;
|
mas01mc@340
|
1105 while(p){
|
mas01mc@344
|
1106 s = p->snext.ptr;
|
mas01mc@340
|
1107 while(s){
|
mas01mc@340
|
1108 point_count++;
|
mas01mc@340
|
1109 s=s->snext;
|
mas01mc@340
|
1110 }
|
mas01mc@340
|
1111 p=p->next;
|
mas01mc@340
|
1112 }
|
mas01mc@340
|
1113 return point_count;
|
mas01mc@340
|
1114 }
|
mas01mc@340
|
1115
|
mas01mc@336
|
1116 void G::serial_write_hashtable_row_format2(FILE* dbFile, bucket* b, Uns32T& colCount){
|
mas01mc@524
|
1117 #ifdef _LSH_DEBUG_
|
mas01mc@524
|
1118 Uns32T last_t2 = 0;
|
mas01mc@524
|
1119 #endif
|
mas01mc@292
|
1120 while(b && b->t2!=IFLAG){
|
mas01mc@344
|
1121 if(!b->snext.ptr){
|
mas01mc@336
|
1122 fclose(dbFile);
|
mas01mc@306
|
1123 error("Empty collision chain in serial_write_hashtable_row_format2()");
|
mas01mc@306
|
1124 }
|
mas01mc@306
|
1125 t2 = O2_SERIAL_TOKEN_T2;
|
mas01mc@524
|
1126
|
mas01mc@336
|
1127 if( fwrite(&t2, sizeof(Uns32T), 1, dbFile) != 1 ){
|
mas01mc@336
|
1128 fclose(dbFile);
|
mas01mc@292
|
1129 error("write error in serial_write_hashtable_row_format2()");
|
mas01mc@292
|
1130 }
|
mas01mc@292
|
1131 t2 = b->t2;
|
mas01mc@524
|
1132 #ifdef _LSH_DEBUG_
|
mas01mc@524
|
1133 if(t2 < last_t2){
|
mas01mc@524
|
1134 fclose(dbFile);
|
mas01mc@524
|
1135 error("t2<last_t2 in serial_write_hashtable_row_format2()");
|
mas01mc@524
|
1136 }
|
mas01mc@524
|
1137 last_t2 = t2;
|
mas01mc@524
|
1138 #endif
|
mas01mc@524
|
1139
|
mas01mc@336
|
1140 if( fwrite(&t2, sizeof(Uns32T), 1, dbFile) != 1 ){
|
mas01mc@336
|
1141 fclose(dbFile);
|
mas01mc@292
|
1142 error("write error in serial_write_hashtable_row_format2()");
|
mas01mc@292
|
1143 }
|
mas01mc@344
|
1144 serial_write_element_format2(dbFile, b->snext.ptr, colCount);
|
mas01mc@292
|
1145 b=b->next;
|
mas01mc@292
|
1146 }
|
mas01mc@292
|
1147 }
|
mas01mc@292
|
1148
|
mas01mc@336
|
1149 void G::serial_write_element_format2(FILE* dbFile, sbucket* sb, Uns32T& colCount){
|
mas01mc@292
|
1150 while(sb){
|
mas01mc@336
|
1151 if(fwrite(&sb->pointID, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@336
|
1152 fclose(dbFile);
|
mas01mc@292
|
1153 error("Write error in serial_write_element_format2()");
|
mas01mc@292
|
1154 }
|
mas01mc@292
|
1155 colCount++;
|
mas01mc@292
|
1156 sb=sb->snext;
|
mas01mc@292
|
1157 }
|
mas01mc@292
|
1158 }
|
mas01mc@292
|
1159
|
mas01mc@292
|
1160
|
mas01mc@292
|
1161 int G::serial_create(char* filename, Uns32T FMT){
|
mas01mc@292
|
1162 return serial_create(filename, w, L, N, C, k, d, FMT);
|
mas01mc@292
|
1163 }
|
mas01mc@292
|
1164
|
mas01mc@292
|
1165
|
mas01mc@292
|
1166 int G::serial_create(char* filename, float binWidth, Uns32T numTables, Uns32T numRows, Uns32T numCols,
|
mas01mc@292
|
1167 Uns32T numFuns, Uns32T dim, Uns32T FMT){
|
mas01mc@292
|
1168
|
mas01mc@292
|
1169 if(numTables > O2_SERIAL_MAX_TABLES || numRows > O2_SERIAL_MAX_ROWS
|
mas01mc@292
|
1170 || numCols > O2_SERIAL_MAX_COLS || numFuns > O2_SERIAL_MAX_FUNS
|
mas01mc@292
|
1171 || dim>O2_SERIAL_MAX_DIM){
|
mas01mc@292
|
1172 error("LSH parameters out of bounds for serialization");
|
mas01mc@292
|
1173 }
|
mas01mc@292
|
1174
|
mas01mc@292
|
1175 int dbfid;
|
mas01mc@292
|
1176 if ((dbfid = open (filename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
|
mas01mc@292
|
1177 error("Can't create serial file", filename, "open");
|
mas01mc@292
|
1178 get_lock(dbfid, 1);
|
mas01mc@292
|
1179
|
mas01mc@292
|
1180 // Make header first to get size of serialized database
|
mas01mc@296
|
1181 lshHeader = new SerialHeaderT(binWidth, numTables, numRows, numCols, numFuns, dim, radius, maxp, FMT, pointCount);
|
mas01mc@296
|
1182
|
mas01mc@296
|
1183 cout << "file size: <=" << lshHeader->get_size()/1024UL << "KB" << endl;
|
mas01mc@296
|
1184 if(lshHeader->get_size()>O2_SERIAL_MAXFILESIZE)
|
mas01mc@296
|
1185 error("Maximum size of LSH file exceded: > 4000MB");
|
mas01mc@296
|
1186
|
mas01mc@292
|
1187 // go to the location corresponding to the last byte
|
mas01mc@292
|
1188 if (lseek (dbfid, lshHeader->get_size() - 1, SEEK_SET) == -1)
|
mas01mc@292
|
1189 error("lseek error in db file", "", "lseek");
|
mas01mc@292
|
1190
|
mas01mc@292
|
1191 // write a dummy byte at the last location
|
mas01mc@292
|
1192 if (write (dbfid, "", 1) != 1)
|
mas01mc@292
|
1193 error("write error", "", "write");
|
mas01mc@292
|
1194
|
mas01mc@293
|
1195 char* db = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 1);
|
mas01mc@296
|
1196
|
mas01mc@292
|
1197 memcpy (db, lshHeader, O2_SERIAL_HEADER_SIZE);
|
mas01mc@296
|
1198
|
mas01mc@292
|
1199 serial_munmap(db, O2_SERIAL_HEADER_SIZE);
|
mas01mc@296
|
1200
|
mas01mc@292
|
1201 close(dbfid);
|
mas01mc@292
|
1202
|
mas01mc@292
|
1203 std::cout << "done initializing tables." << endl;
|
mas01mc@292
|
1204
|
mas01mc@292
|
1205 return 1;
|
mas01mc@292
|
1206 }
|
mas01mc@292
|
1207
|
mas01mc@292
|
1208 char* G::serial_mmap(int dbfid, Uns32T memSize, Uns32T forWrite, off_t offset){
|
mas01mc@293
|
1209 char* db;
|
mas01mc@292
|
1210 if(forWrite){
|
mas01mc@292
|
1211 if ((db = (char*) mmap(0, memSize, PROT_READ | PROT_WRITE,
|
mas01mc@292
|
1212 MAP_SHARED, dbfid, offset)) == (caddr_t) -1)
|
mas01mc@292
|
1213 error("mmap error in request for writable serialized database", "", "mmap");
|
mas01mc@292
|
1214 }
|
mas01mc@292
|
1215 else if ((db = (char*) mmap(0, memSize, PROT_READ, MAP_SHARED, dbfid, offset)) == (caddr_t) -1)
|
mas01mc@292
|
1216 error("mmap error in read-only serialized database", "", "mmap");
|
mas01mc@292
|
1217
|
mas01mc@292
|
1218 return db;
|
mas01mc@292
|
1219 }
|
mas01mc@292
|
1220
|
mas01mc@292
|
1221 SerialHeaderT* G::serial_get_header(char* db){
|
mas01mc@292
|
1222 lshHeader = new SerialHeaderT();
|
mas01mc@292
|
1223 memcpy((char*)lshHeader, db, sizeof(SerialHeaderT));
|
mas01mc@292
|
1224
|
mas01mc@292
|
1225 if(lshHeader->lshMagic!=O2_SERIAL_MAGIC)
|
mas01mc@292
|
1226 error("Not an LSH database file");
|
mas01mc@292
|
1227
|
mas01mc@292
|
1228 return lshHeader;
|
mas01mc@292
|
1229 }
|
mas01mc@292
|
1230
|
mas01mc@292
|
1231 void G::serial_munmap(char* db, Uns32T N){
|
mas01mc@292
|
1232 munmap(db, N);
|
mas01mc@292
|
1233 }
|
mas01mc@292
|
1234
|
mas01mc@292
|
1235 int G::serial_open(char* filename, int writeFlag){
|
mas01mc@292
|
1236 int dbfid;
|
mas01mc@292
|
1237 if(writeFlag){
|
mas01mc@292
|
1238 if ((dbfid = open (filename, O_RDWR)) < 0)
|
mas01mc@292
|
1239 error("Can't open serial file for read/write", filename, "open");
|
mas01mc@292
|
1240 get_lock(dbfid, writeFlag);
|
mas01mc@292
|
1241 }
|
mas01mc@292
|
1242 else{
|
mas01mc@292
|
1243 if ((dbfid = open (filename, O_RDONLY)) < 0)
|
mas01mc@292
|
1244 error("Can't open serial file for read", filename, "open");
|
mas01mc@292
|
1245 get_lock(dbfid, 0);
|
mas01mc@292
|
1246 }
|
mas01mc@292
|
1247
|
mas01mc@292
|
1248 return dbfid;
|
mas01mc@292
|
1249 }
|
mas01mc@292
|
1250
|
mas01mc@292
|
1251 void G::serial_close(int dbfid){
|
mas01mc@292
|
1252
|
mas01mc@292
|
1253 release_lock(dbfid);
|
mas01mc@292
|
1254 close(dbfid);
|
mas01mc@292
|
1255 }
|
mas01mc@292
|
1256
|
mas01mc@292
|
1257 int G::unserialize_lsh_header(char* filename){
|
mas01mc@292
|
1258
|
mas01mc@292
|
1259 int dbfid;
|
mas01mc@292
|
1260 char* db;
|
mas01mc@292
|
1261 // Test to see if file exists
|
mas01mc@292
|
1262 if((dbfid = open (filename, O_RDONLY)) < 0)
|
mas01mc@292
|
1263 error("Can't open the file", filename, "open");
|
mas01mc@292
|
1264 close(dbfid);
|
mas01mc@292
|
1265 dbfid = serial_open(filename, 0); // open for read
|
mas01mc@292
|
1266 db = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 0);// get database pointer
|
mas01mc@292
|
1267 serial_get_header(db); // read header
|
mas01mc@292
|
1268 serial_munmap(db, O2_SERIAL_HEADER_SIZE); // drop mmap
|
mas01mc@292
|
1269
|
mas01mc@292
|
1270 // Unserialize header parameters
|
mas01mc@292
|
1271 H::L = lshHeader->numTables;
|
mas01mc@292
|
1272 H::m = (Uns32T)( (1.0 + sqrt(1 + 8.0*(int)H::L)) / 2.0);
|
mas01mc@292
|
1273 H::N = lshHeader->numRows;
|
mas01mc@292
|
1274 H::C = lshHeader->numCols;
|
mas01mc@292
|
1275 H::k = lshHeader->numFuns;
|
mas01mc@292
|
1276 H::d = lshHeader->dataDim;
|
mas01mc@293
|
1277 H::w = lshHeader->binWidth;
|
mas01mc@293
|
1278 H::radius = lshHeader->radius;
|
mas01mc@293
|
1279 H::maxp = lshHeader->maxp;
|
mas01mc@296
|
1280 H::pointCount = lshHeader->pointCount;
|
mas01mc@292
|
1281
|
mas01mc@292
|
1282 return dbfid;
|
mas01mc@292
|
1283 }
|
mas01mc@292
|
1284
|
mas01mc@292
|
1285 // unserialize the LSH parameters
|
mas01mc@292
|
1286 // we leave the LSH tree on disk as a flat file
|
mas01mc@292
|
1287 // it is this flat file that we search by memory mapping
|
mas01mc@292
|
1288 void G::unserialize_lsh_functions(int dbfid){
|
mas01mc@292
|
1289 Uns32T j, kk;
|
mas01mc@292
|
1290 float* pf;
|
mas01mc@292
|
1291 Uns32T* pu;
|
mas01mc@292
|
1292
|
mas01mc@292
|
1293 // Load the hash functions into core
|
mas01mc@292
|
1294 char* db = serial_mmap(dbfid, get_serial_hashtable_offset(), 0);// get database pointer again
|
mas01mc@292
|
1295
|
mas01mc@292
|
1296 pf = get_serial_hashfunction_base(db);
|
mas01mc@292
|
1297
|
mas01mc@292
|
1298 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
1299 for( j = 0 ; j < H::m ; j++ ){ // L functions gj(v)
|
mas01mc@292
|
1300 for( kk = 0 ; kk < H::k/2 ; kk++ ){ // Normally distributed hash functions
|
mas01mc@292
|
1301 #else
|
mas01mc@292
|
1302 for( j = 0 ; j < H::L ; j++ ){ // L functions gj(v)
|
mas01mc@292
|
1303 for( kk = 0 ; kk < H::k ; kk++ ){ // Normally distributed hash functions
|
mas01mc@292
|
1304 #endif
|
mas01mc@292
|
1305 for(Uns32T i = 0 ; i < H::d ; i++ )
|
mas01mc@293
|
1306 H::A[j][kk][i] = *pf++; // Normally distributed random vectors
|
mas01mc@292
|
1307 }
|
mas01mc@292
|
1308 }
|
mas01mc@292
|
1309 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
1310 for( j = 0 ; j < H::m ; j++ ) // biases b
|
mas01mc@292
|
1311 for( kk = 0 ; kk < H::k/2 ; kk++ )
|
mas01mc@292
|
1312 #else
|
mas01mc@292
|
1313 for( j = 0 ; j < H::L ; j++ ) // biases b
|
mas01mc@292
|
1314 for( kk = 0 ; kk < H::k ; kk++ )
|
mas01mc@292
|
1315 #endif
|
mas01mc@293
|
1316 H::b[j][kk] = *pf++;
|
mas01mc@292
|
1317
|
mas01mc@292
|
1318 pu = (Uns32T*)pf;
|
mas01mc@292
|
1319 for( j = 0 ; j < H::L ; j++ ) // Z projectors r1
|
mas01mc@292
|
1320 for( kk = 0 ; kk < H::k ; kk++ )
|
mas01mc@292
|
1321 H::r1[j][kk] = *pu++;
|
mas01mc@292
|
1322
|
mas01mc@292
|
1323 for( j = 0 ; j < H::L ; j++ ) // Z projectors r2
|
mas01mc@292
|
1324 for( kk = 0 ; kk < H::k ; kk++ )
|
mas01mc@292
|
1325 H::r2[j][kk] = *pu++;
|
mas01mc@292
|
1326
|
mas01mc@293
|
1327 serial_munmap(db, get_serial_hashtable_offset());
|
mas01mc@292
|
1328 }
|
mas01mc@292
|
1329
|
mas01mc@292
|
1330 void G::unserialize_lsh_hashtables_format1(int fid){
|
mas01mc@292
|
1331 SerialElementT *pe, *pt;
|
mas01mc@292
|
1332 Uns32T x,y;
|
mas01mc@292
|
1333 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
1334 // Read the hash tables into core
|
mas01mc@292
|
1335 for( x = 0 ; x < H::L ; x++ ){
|
mas01mc@292
|
1336 // memory map a single hash table
|
mas01mc@292
|
1337 // Align each hash table to page boundary
|
mas01mc@292
|
1338 char* dbtable = serial_mmap(fid, hashTableSize, 0,
|
mas01mc@292
|
1339 align_up(get_serial_hashtable_offset()+x*hashTableSize, get_page_logn()));
|
mas01mc@324
|
1340 #ifdef __CYGWIN__
|
mas01mc@324
|
1341 // No madvise in CYGWIN
|
mas01mc@324
|
1342 #else
|
mas01mc@292
|
1343 if(madvise(dbtable, hashTableSize, MADV_SEQUENTIAL)<0)
|
mas01mc@292
|
1344 error("could not advise hashtable memory","","madvise");
|
mas01mc@324
|
1345 #endif
|
mas01mc@292
|
1346 pt=(SerialElementT*)dbtable;
|
mas01mc@292
|
1347 for( y = 0 ; y < H::N ; y++ ){
|
mas01mc@292
|
1348 // Move disk pointer to beginning of row
|
mas01mc@292
|
1349 pe=pt+y*lshHeader->numCols;
|
mas01mc@292
|
1350 unserialize_hashtable_row_format1(pe, h[x]+y);
|
mas01mc@340
|
1351 #ifdef LSH_DUMP_CORE_TABLES
|
mas01mc@292
|
1352 printf("S[%d,%d]", x, y);
|
mas01mc@292
|
1353 serial_bucket_dump(pe);
|
mas01mc@292
|
1354 printf("C[%d,%d]", x, y);
|
mas01mc@292
|
1355 dump_hashtable_row(h[x][y]);
|
mas01mc@292
|
1356 #endif
|
mas01mc@292
|
1357 }
|
mas01mc@292
|
1358 serial_munmap(dbtable, hashTableSize);
|
mas01mc@292
|
1359 }
|
mas01mc@292
|
1360 }
|
mas01mc@292
|
1361
|
mas01mc@292
|
1362 void G::unserialize_hashtable_row_format1(SerialElementT* pe, bucket** b){
|
mas01mc@292
|
1363 Uns32T colCount = 0;
|
mas01mc@292
|
1364 while(colCount!=lshHeader->numCols && pe->hashValue !=IFLAG){
|
mas01mc@292
|
1365 H::p = pe->pointID; // current point ID
|
mas01mc@292
|
1366 t2 = pe->hashValue;
|
mas01mc@292
|
1367 bucket_insert_point(b);
|
mas01mc@292
|
1368 pe++;
|
mas01mc@292
|
1369 colCount++;
|
mas01mc@292
|
1370 }
|
mas01mc@292
|
1371 }
|
mas01mc@292
|
1372
|
mas01mc@340
|
1373 void G::unserialize_lsh_hashtables_format2(FILE* dbFile, bool forMerge){
|
mas01mc@292
|
1374 Uns32T x=0,y=0;
|
mas01mc@523
|
1375 #ifdef _LSH_DEBUG_
|
mas01mc@523
|
1376 cout << "Loading hashtables..." << endl;
|
mas01mc@523
|
1377 cout << "header pointCount = " << pointCount << endl;
|
mas01mc@523
|
1378 cout << "forMerge = " << forMerge << endl;
|
mas01mc@523
|
1379 Uns32T sumTablesPointCount = 0;
|
mas01mc@523
|
1380 #endif
|
mas01mc@292
|
1381 // Seek to hashtable base offset
|
mas01mc@336
|
1382 if(fseek(dbFile, get_serial_hashtable_offset(), SEEK_SET)){
|
mas01mc@336
|
1383 fclose(dbFile);
|
mas01mc@336
|
1384 error("fSeek error in unserialize_lsh_hashtables_format2");
|
mas01mc@292
|
1385 }
|
mas01mc@292
|
1386
|
mas01mc@292
|
1387 // Read the hash tables into core (structure is given in header)
|
mas01mc@292
|
1388 while( x < H::L){
|
mas01mc@523
|
1389 tablesPointCount=0;
|
mas01mc@336
|
1390 if(fread(&(H::t1), sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@336
|
1391 fclose(dbFile);
|
mas01mc@292
|
1392 error("Read error","unserialize_lsh_hashtables_format2()");
|
mas01mc@292
|
1393 }
|
mas01mc@306
|
1394 if(H::t1==O2_SERIAL_TOKEN_ENDTABLE)
|
mas01mc@292
|
1395 x++; // End of table
|
mas01mc@292
|
1396 else
|
mas01mc@292
|
1397 while(y < H::N){
|
mas01mc@306
|
1398 // Read a row and move file pointer to beginning of next row or _bittable
|
mas01mc@306
|
1399 if(!(H::t1==O2_SERIAL_TOKEN_T1)){
|
mas01mc@336
|
1400 fclose(dbFile);
|
mas01mc@306
|
1401 error("State matchine error T1","unserialize_lsh_hashtables_format2()");
|
mas01mc@292
|
1402 }
|
mas01mc@336
|
1403 if(fread(&(H::t1), sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@336
|
1404 fclose(dbFile);
|
mas01mc@306
|
1405 error("Read error: t1","unserialize_lsh_hashtables_format2()");
|
mas01mc@306
|
1406 }
|
mas01mc@306
|
1407 y = H::t1;
|
mas01mc@292
|
1408 if(y>=H::N){
|
mas01mc@336
|
1409 fclose(dbFile);
|
mas01mc@292
|
1410 error("Unserialized hashtable row pointer out of range","unserialize_lsh_hashtables_format2()");
|
mas01mc@292
|
1411 }
|
mas01mc@340
|
1412 Uns32T token = 0;
|
mas01mc@340
|
1413 #ifdef LSH_CORE_ARRAY
|
mas01mc@340
|
1414 Uns32T numElements;
|
mas01mc@340
|
1415 if(fread(&numElements, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@340
|
1416 fclose(dbFile);
|
mas01mc@340
|
1417 error("Read error: numElements","unserialize_lsh_hashtables_format2()");
|
mas01mc@340
|
1418 }
|
mas01mc@525
|
1419
|
mas01mc@525
|
1420 /*
|
mas01mc@524
|
1421 #ifdef _LSH_DEBUG_
|
mas01mc@524
|
1422 cout << "[" << x << "," << y << "] numElements(disk) = " << numElements;
|
mas01mc@524
|
1423 #endif
|
mas01mc@525
|
1424 */
|
mas01mc@340
|
1425 // BACKWARD COMPATIBILITY: check to see if T2 or END token was read
|
mas01mc@340
|
1426 if(numElements==O2_SERIAL_TOKEN_T2 || numElements==O2_SERIAL_TOKEN_ENDTABLE ){
|
mas01mc@340
|
1427 forMerge=true; // Force use of dynamic linked list core format
|
mas01mc@340
|
1428 token = numElements;
|
mas01mc@340
|
1429 }
|
mas01mc@340
|
1430
|
mas01mc@524
|
1431 if(forMerge) // FOR INDEXING use dynamic linked list data structure
|
mas01mc@340
|
1432 // Use linked list CORE format
|
mas01mc@340
|
1433 token = unserialize_hashtable_row_format2(dbFile, h[x]+y, token);
|
mas01mc@524
|
1434 else // FOR QUERY use static array data structure
|
mas01mc@340
|
1435 // Use ARRAY CORE format with numElements counter
|
mas01mc@340
|
1436 token = unserialize_hashtable_row_to_array(dbFile, h[x]+y, numElements);
|
mas01mc@340
|
1437 #else
|
mas01mc@523
|
1438 token = unserialize_hashtable_row_format2(dbFile, h[x]+y);
|
mas01mc@513
|
1439 #endif
|
mas01mc@292
|
1440 // Check that token is valid
|
mas01mc@306
|
1441 if( !(token==O2_SERIAL_TOKEN_T1 || token==O2_SERIAL_TOKEN_ENDTABLE) ){
|
mas01mc@336
|
1442 fclose(dbFile);
|
mas01mc@292
|
1443 error("State machine error end of row/table", "unserialize_lsh_hashtables_format2()");
|
mas01mc@292
|
1444 }
|
mas01mc@292
|
1445 // Check for end of table flag
|
mas01mc@306
|
1446 if(token==O2_SERIAL_TOKEN_ENDTABLE){
|
mas01mc@292
|
1447 x++;
|
mas01mc@292
|
1448 break;
|
mas01mc@292
|
1449 }
|
mas01mc@292
|
1450 // Check for new row flag
|
mas01mc@306
|
1451 if(token==O2_SERIAL_TOKEN_T1)
|
mas01mc@292
|
1452 H::t1 = token;
|
mas01mc@292
|
1453 }
|
mas01mc@523
|
1454 #ifdef _LSH_DEBUG_
|
mas01mc@525
|
1455 cout << "[T " << x-1 << "] pointCount = " << tablesPointCount << endl;
|
mas01mc@523
|
1456 sumTablesPointCount+=tablesPointCount;
|
mas01mc@523
|
1457 #endif
|
mas01mc@292
|
1458 }
|
mas01mc@523
|
1459 #ifdef _LSH_DEBUG_
|
mas01mc@523
|
1460 cout << "TOTAL pointCount = " << sumTablesPointCount << endl;
|
mas01mc@523
|
1461 #endif
|
mas01mc@513
|
1462 #ifdef LSH_DUMP_CORE_TABLES
|
mas01mc@513
|
1463 dump_hashtables();
|
mas01mc@513
|
1464 #endif
|
mas01mc@306
|
1465 }
|
mas01mc@292
|
1466
|
mas01mc@340
|
1467 Uns32T G::unserialize_hashtable_row_format2(FILE* dbFile, bucket** b, Uns32T token){
|
mas01mc@292
|
1468 bool pointFound = false;
|
mas01mc@340
|
1469
|
mas01mc@340
|
1470 if(token)
|
mas01mc@340
|
1471 H::t2 = token;
|
mas01mc@340
|
1472 else if(fread(&(H::t2), sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@340
|
1473 fclose(dbFile);
|
mas01mc@340
|
1474 error("Read error T2 token","unserialize_hashtable_row_format2");
|
mas01mc@340
|
1475 }
|
mas01mc@340
|
1476
|
mas01mc@306
|
1477 if( !(H::t2==O2_SERIAL_TOKEN_ENDTABLE || H::t2==O2_SERIAL_TOKEN_T2)){
|
mas01mc@336
|
1478 fclose(dbFile);
|
mas01mc@292
|
1479 error("State machine error: expected E or T2");
|
mas01mc@292
|
1480 }
|
mas01mc@340
|
1481
|
mas01mc@306
|
1482 while(!(H::t2==O2_SERIAL_TOKEN_ENDTABLE || H::t2==O2_SERIAL_TOKEN_T1)){
|
mas01mc@292
|
1483 pointFound=false;
|
mas01mc@292
|
1484 // Check for T2 token
|
mas01mc@306
|
1485 if(H::t2!=O2_SERIAL_TOKEN_T2){
|
mas01mc@336
|
1486 fclose(dbFile);
|
mas01mc@292
|
1487 error("State machine error T2 token", "unserialize_hashtable_row_format2()");
|
mas01mc@306
|
1488 }
|
mas01mc@292
|
1489 // Read t2 value
|
mas01mc@336
|
1490 if(fread(&(H::t2), sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@336
|
1491 fclose(dbFile);
|
mas01mc@292
|
1492 error("Read error t2","unserialize_hashtable_row_format2");
|
mas01mc@292
|
1493 }
|
mas01mc@336
|
1494 if(fread(&(H::p), sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@336
|
1495 fclose(dbFile);
|
mas01mc@292
|
1496 error("Read error H::p","unserialize_hashtable_row_format2");
|
mas01mc@292
|
1497 }
|
mas01mc@306
|
1498 while(!(H::p==O2_SERIAL_TOKEN_ENDTABLE || H::p==O2_SERIAL_TOKEN_T1 || H::p==O2_SERIAL_TOKEN_T2 )){
|
mas01mc@292
|
1499 pointFound=true;
|
mas01mc@292
|
1500 bucket_insert_point(b);
|
mas01mc@523
|
1501 tablesPointCount++;
|
mas01mc@336
|
1502 if(fread(&(H::p), sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@336
|
1503 fclose(dbFile);
|
mas01mc@292
|
1504 error("Read error H::p","unserialize_hashtable_row_format2");
|
mas01mc@292
|
1505 }
|
mas01mc@292
|
1506 }
|
mas01mc@292
|
1507 if(!pointFound)
|
mas01mc@292
|
1508 error("State machine error: point", "unserialize_hashtable_row_format2()");
|
mas01mc@306
|
1509 H::t2 = H::p; // Copy last found token to t2
|
mas01mc@560
|
1510 }
|
mas01mc@292
|
1511 return H::t2; // holds current token
|
mas01mc@292
|
1512 }
|
mas01mc@292
|
1513
|
mas01mc@340
|
1514 // Unserialize format2 hashtable row to a CORE_ARRAY pointed to
|
mas01mc@340
|
1515 // by the hashtable row pointer: rowPtr
|
mas01mc@340
|
1516 //
|
mas01mc@340
|
1517 // numElements is the total number of t2 buckets plus points
|
mas01mc@340
|
1518 // memory required is numElements+1+sizeof(hashtable ptr)
|
mas01mc@340
|
1519 //
|
mas01mc@340
|
1520 // numElements = numPoints + numBuckets
|
mas01mc@340
|
1521 //
|
mas01mc@340
|
1522 // During inserts (merges) new hashtable entries are appened at rowPtr+numPoints+numBuckets+1
|
mas01mc@340
|
1523 //
|
mas01mc@340
|
1524 // ASSUME: that LSH_LIST_HEAD_COUNTERS is set so that the first hashtable bucket is used to count
|
mas01mc@340
|
1525 // point and bucket entries
|
mas01mc@340
|
1526 //
|
mas01mc@340
|
1527 // We store the values of numPoints and numBuckets in separate fields of the first bucket
|
mas01mc@340
|
1528 // rowPtr->t2 // numPoints
|
mas01mc@524
|
1529 // (rowPtr->snext.numBuckets) // numBuckets
|
mas01mc@340
|
1530 //
|
mas01mc@340
|
1531 // We cast the rowPtr->next pointer to (Uns32*) malloc(numElements*sizeof(Uns32T) + sizeof(bucket*))
|
mas01mc@340
|
1532 // To get to the fist bucket, we use
|
mas01mc@340
|
1533 //
|
mas01mc@340
|
1534
|
mas01mc@340
|
1535 #define READ_UNS32T(VAL,TOKENSTR) if(fread(VAL, sizeof(Uns32T), 1, dbFile) != 1){\
|
mas01mc@340
|
1536 fclose(dbFile);error("Read error unserialize_hashtable_format2",TOKENSTR);}
|
mas01mc@340
|
1537
|
mas01mc@340
|
1538 #define TEST_TOKEN(TEST, TESTSTR) if(TEST){fclose(dbFile);error("State machine error: ", TESTSTR);}
|
mas01mc@340
|
1539
|
mas01mc@340
|
1540 #define SKIP_BITS_LEFT_SHIFT_MSB (30)
|
mas01mc@340
|
1541
|
mas01mc@340
|
1542 #define SKIP_BITS_RIGHT_SHIFT_MSB (28)
|
mas01mc@340
|
1543 #define SKIP_BITS_RIGHT_SHIFT_LSB (30)
|
mas01mc@340
|
1544
|
mas01mc@340
|
1545 #define MAX_POINTS_IN_BUCKET_CORE_ARRAY (16)
|
mas01mc@340
|
1546 #define LSH_CORE_ARRAY_END_ROW_TOKEN (0xFFFFFFFD)
|
mas01mc@340
|
1547
|
mas01mc@340
|
1548 // Encode the skip bits. Zero if only one point, MAX 8 (plus first == 9)
|
mas01mc@340
|
1549 #define ENCODE_POINT_SKIP_BITS TEST_TOKEN(!numPointsThisBucket, "no points found");\
|
mas01mc@340
|
1550 if(numPointsThisBucket==1){\
|
mas01mc@340
|
1551 secondPtr=ap++;\
|
mas01mc@340
|
1552 *secondPtr=0;\
|
mas01mc@340
|
1553 numPoints++;\
|
mas01mc@525
|
1554 numSingletons++;\
|
mas01mc@340
|
1555 }\
|
mas01mc@340
|
1556 if(numPointsThisBucket>1){\
|
mas01mc@340
|
1557 *firstPtr |= ( (numPointsThisBucket-1) & 0x3 ) << SKIP_BITS_LEFT_SHIFT_MSB;\
|
mas01mc@340
|
1558 *secondPtr |= ( ( (numPointsThisBucket-1) & 0xC) >> 2 ) << SKIP_BITS_LEFT_SHIFT_MSB;}
|
mas01mc@340
|
1559
|
mas01mc@340
|
1560 Uns32T G::unserialize_hashtable_row_to_array(FILE* dbFile, bucket** rowPP, Uns32T numElements){
|
mas01mc@340
|
1561 Uns32T numPointsThisBucket = 0;
|
mas01mc@340
|
1562 Uns32T numBuckets = 0;
|
mas01mc@340
|
1563 Uns32T numPoints = 0;
|
mas01mc@340
|
1564 Uns32T* firstPtr = 0;
|
mas01mc@340
|
1565 Uns32T* secondPtr = 0;
|
mas01mc@525
|
1566 Uns32T numSingletons = 0; // Count single point puckets because we encode them with 2 points (for skip)
|
mas01mc@340
|
1567
|
mas01mc@340
|
1568 // Initialize new row
|
mas01mc@340
|
1569 if(!*rowPP){
|
mas01mc@340
|
1570 *rowPP = new bucket();
|
mas01mc@340
|
1571 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@340
|
1572 (*rowPP)->t2 = 0; // Use t2 as a collision counter for the row
|
mas01mc@340
|
1573 (*rowPP)->next = 0;
|
mas01mc@340
|
1574 #endif
|
mas01mc@340
|
1575 }
|
mas01mc@340
|
1576 bucket* rowPtr = *rowPP;
|
mas01mc@524
|
1577 Uns32T last_t2 = 0;
|
mas01mc@340
|
1578 READ_UNS32T(&(H::t2),"t2");
|
mas01mc@340
|
1579 TEST_TOKEN(!(H::t2==O2_SERIAL_TOKEN_ENDTABLE || H::t2==O2_SERIAL_TOKEN_T2), "expected E or T2");
|
mas01mc@340
|
1580 // Because we encode points in 16-point blocks, we sometimes allocate repeated t2 elements
|
mas01mc@340
|
1581 // So over-allocate by a factor of two and realloc later to actual numElements
|
mas01mc@340
|
1582 CR_ASSERT(rowPtr->next = (bucket*) malloc((2*numElements+1)*sizeof(Uns32T)+sizeof(bucket**)));
|
mas01mc@340
|
1583 Uns32T* ap = reinterpret_cast<Uns32T*>(rowPtr->next); // Cast pointer to Uns32T* for array format
|
mas01mc@340
|
1584 while( !(H::t2==O2_SERIAL_TOKEN_ENDTABLE || H::t2==O2_SERIAL_TOKEN_T1) ){
|
mas01mc@340
|
1585 numPointsThisBucket = 0;// reset bucket point counter
|
mas01mc@340
|
1586 secondPtr = 0; // reset second-point pointer
|
mas01mc@524
|
1587 TEST_TOKEN(H::t2!=O2_SERIAL_TOKEN_T2, "expected T2");
|
mas01mc@524
|
1588 READ_UNS32T(&(H::t2), "Read error t2");
|
mas01mc@524
|
1589 if(H::t2<last_t2)
|
mas01mc@524
|
1590 cout << "last_t2=" << last_t2 << ", t2=" << H::t2 << endl;
|
mas01mc@524
|
1591 TEST_TOKEN(H::t2<last_t2, "t2 tokens not in ascending order");
|
mas01mc@524
|
1592 last_t2 = H::t2;
|
mas01mc@525
|
1593 /*
|
mas01mc@525
|
1594 #ifdef _LSH_DEBUG_
|
mas01mc@525
|
1595 cout << "+" << H::t2 << "+";
|
mas01mc@525
|
1596 #endif
|
mas01mc@525
|
1597 */
|
mas01mc@340
|
1598 *ap++ = H::t2; // Insert t2 value into array
|
mas01mc@340
|
1599 numBuckets++;
|
mas01mc@340
|
1600 READ_UNS32T(&(H::p), "Read error H::p");
|
mas01mc@340
|
1601 while(!(H::p==O2_SERIAL_TOKEN_ENDTABLE || H::p==O2_SERIAL_TOKEN_T1 || H::p==O2_SERIAL_TOKEN_T2 )){
|
mas01mc@340
|
1602 if(numPointsThisBucket==MAX_POINTS_IN_BUCKET_CORE_ARRAY){
|
mas01mc@340
|
1603 ENCODE_POINT_SKIP_BITS;
|
mas01mc@525
|
1604 /*
|
mas01mc@525
|
1605 #ifdef _LSH_DEBUG_
|
mas01mc@525
|
1606 cout << "*" << H::t2 << "*";
|
mas01mc@525
|
1607 #endif
|
mas01mc@525
|
1608 */
|
mas01mc@340
|
1609 *ap++ = H::t2; // Extra element
|
mas01mc@340
|
1610 numBuckets++; // record this as a new bucket
|
mas01mc@340
|
1611 numPointsThisBucket=0; // reset bucket point counter
|
mas01mc@340
|
1612 secondPtr = 0; // reset second-point pointer
|
mas01mc@340
|
1613 }
|
mas01mc@340
|
1614 if( ++numPointsThisBucket == 1 )
|
mas01mc@340
|
1615 firstPtr = ap; // store pointer to first point to insert skip bits later on
|
mas01mc@340
|
1616 else if( numPointsThisBucket == 2 )
|
mas01mc@340
|
1617 secondPtr = ap; // store pointer to first point to insert skip bits later on
|
mas01mc@340
|
1618 numPoints++;
|
mas01mc@525
|
1619 /*
|
mas01mc@525
|
1620 #ifdef _LSH_DEBUG_
|
mas01mc@525
|
1621 cout << "(" << H::p << ":" << numPoints << ")";
|
mas01mc@525
|
1622 #endif
|
mas01mc@525
|
1623 */
|
mas01mc@525
|
1624
|
mas01mc@340
|
1625 *ap++ = H::p;
|
mas01mc@340
|
1626 READ_UNS32T(&(H::p), "Read error H::p");
|
mas01mc@340
|
1627 }
|
mas01mc@340
|
1628 ENCODE_POINT_SKIP_BITS;
|
mas01mc@340
|
1629 H::t2 = H::p; // Copy last found token to t2
|
mas01mc@525
|
1630 }
|
mas01mc@340
|
1631 // Reallocate the row to its actual size
|
mas01mc@340
|
1632 CR_ASSERT(rowPtr->next = (bucket*) realloc(rowPtr->next, (numBuckets+numPoints+1)*sizeof(Uns32T)+sizeof(bucket**)));
|
mas01mc@340
|
1633 // Record the sizes at the head of the row
|
mas01mc@344
|
1634 rowPtr->snext.numBuckets = numBuckets;
|
mas01mc@340
|
1635 rowPtr->t2 = numPoints;
|
mas01mc@340
|
1636 // Place end of row marker
|
mas01mc@340
|
1637 *ap++ = LSH_CORE_ARRAY_END_ROW_TOKEN;
|
mas01mc@340
|
1638 // Set the LSH_CORE_ARRAY_BIT to identify data structure for insertion and retrieval
|
mas01mc@340
|
1639 rowPtr->t2 |= LSH_CORE_ARRAY_BIT;
|
mas01mc@340
|
1640 // Allocate a new dynamic list head at the end of the array
|
mas01mc@340
|
1641 bucket** listPtr = reinterpret_cast<bucket**> (ap);
|
mas01mc@340
|
1642 *listPtr = 0;
|
mas01mc@525
|
1643 /*
|
mas01mc@524
|
1644 #ifdef _LSH_DEBUG_
|
mas01mc@525
|
1645 cout << " numBuckets=" << numBuckets << " numPoints=" << numPoints - numSingletons << " numElements(array) " << numBuckets+numPoints - numSingletons << " " << endl;
|
mas01mc@524
|
1646 #endif
|
mas01mc@525
|
1647 */
|
mas01mc@525
|
1648 H::tablesPointCount += numPoints - numSingletons;
|
mas01mc@340
|
1649 // Return current token
|
mas01mc@340
|
1650 return H::t2; // return H::t2 which holds current token [E or T1]
|
mas01mc@340
|
1651 }
|
mas01mc@340
|
1652
|
mas01mc@340
|
1653
|
mas01mc@340
|
1654
|
mas01mc@340
|
1655 // *p is a pointer to the beginning of a hashtable row array
|
mas01mc@340
|
1656 // The array consists of t2 hash keys and one or more point identifiers p for each hash key
|
mas01mc@340
|
1657 // Retrieval is performed by generating a hash key query_t2 for query point q
|
mas01mc@340
|
1658 // We identify the row that t2 is stored in using a secondary hash t1, this row is the entry
|
mas01mc@340
|
1659 // point for retrieve_from_core_hashtable_array
|
mas01mc@558
|
1660 #define SKIP_BITS (0xC0000000U)
|
mas01mc@340
|
1661 void G::retrieve_from_core_hashtable_array(Uns32T* p, Uns32T qpos){
|
mas01mc@340
|
1662 Uns32T skip;
|
mas01mc@340
|
1663 Uns32T t2;
|
mas01mc@340
|
1664 Uns32T p1;
|
mas01mc@340
|
1665 Uns32T p2;
|
mas01mc@340
|
1666
|
mas01mc@340
|
1667 CR_ASSERT(p);
|
mas01mc@340
|
1668
|
mas01mc@340
|
1669 do{
|
mas01mc@340
|
1670 t2 = *p++;
|
mas01mc@340
|
1671 if( t2 > H::t2 )
|
mas01mc@340
|
1672 return;
|
mas01mc@340
|
1673 p1 = *p++;
|
mas01mc@340
|
1674 p2 = *p++;
|
mas01mc@340
|
1675 skip = (( p1 & SKIP_BITS ) >> SKIP_BITS_RIGHT_SHIFT_LSB) + (( p2 & SKIP_BITS ) >> SKIP_BITS_RIGHT_SHIFT_MSB);
|
mas01mc@340
|
1676 if( t2 == H::t2 ){
|
mas01mc@561
|
1677 add_point_callback(calling_instance, p1 & ~SKIP_BITS, qpos, radius);
|
mas01mc@340
|
1678 if(skip--){
|
mas01mc@561
|
1679 add_point_callback(calling_instance, p2 & ~SKIP_BITS, qpos, radius);
|
mas01mc@340
|
1680 while(skip-- )
|
mas01mc@340
|
1681 add_point_callback(calling_instance, *p++, qpos, radius);
|
mas01mc@340
|
1682 }
|
mas01mc@340
|
1683 }
|
mas01mc@340
|
1684 else
|
mas01mc@340
|
1685 if(*p != LSH_CORE_ARRAY_END_ROW_TOKEN)
|
mas01mc@340
|
1686 p = p + skip;
|
mas01mc@340
|
1687 }while( *p != LSH_CORE_ARRAY_END_ROW_TOKEN );
|
mas01mc@340
|
1688 }
|
mas01mc@513
|
1689
|
mas01mc@513
|
1690 void G::dump_hashtables(){
|
mas01mc@513
|
1691 for(Uns32T x = 0; x < H::L ; x++)
|
mas01mc@513
|
1692 for(Uns32T y = 0; y < H::N ; y++){
|
mas01mc@513
|
1693 bucket* bPtr = h[x][y];
|
mas01mc@513
|
1694 if(bPtr){
|
mas01mc@513
|
1695 printf("C[%d,%d]", x, y);
|
mas01mc@513
|
1696 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@513
|
1697 printf("[numBuckets=%d]",bPtr->snext.numBuckets);
|
mas01mc@513
|
1698 if(bPtr->t2&LSH_CORE_ARRAY_BIT) {
|
mas01mc@513
|
1699 dump_core_hashtable_array((Uns32T*)(bPtr->next));
|
mas01mc@513
|
1700 }
|
mas01mc@513
|
1701 else {
|
mas01mc@513
|
1702 dump_hashtable_row(bPtr->next);
|
mas01mc@513
|
1703 }
|
mas01mc@513
|
1704 #else
|
mas01mc@513
|
1705 dump_hashtable_row(bPtr);
|
mas01mc@513
|
1706 #endif
|
mas01mc@513
|
1707 printf("\n");
|
mas01mc@513
|
1708 fflush(stdout);
|
mas01mc@513
|
1709 }
|
mas01mc@513
|
1710 }
|
mas01mc@513
|
1711 }
|
mas01mc@513
|
1712
|
mas01mc@559
|
1713 void G::dump_core_row(Uns32T n){
|
mas01mc@559
|
1714 if(!(n<H::N)){
|
mas01mc@561
|
1715 printf("ROW OUT OF RANGE:%d (MAX:%d)\n", n, H::N-1);
|
mas01mc@559
|
1716 return;
|
mas01mc@559
|
1717 }
|
mas01mc@559
|
1718 for(Uns32T j = 0 ; j < H::L ; j++ ){
|
mas01mc@559
|
1719 bucket* bPtr = h[j][n];
|
mas01mc@559
|
1720 if(bPtr){
|
mas01mc@559
|
1721 printf("C[%d,%d]", j, n);
|
mas01mc@559
|
1722 #ifdef LSH_LIST_HEAD_COUNTERS
|
mas01mc@559
|
1723 printf("[numBuckets=%d]",bPtr->snext.numBuckets);
|
mas01mc@559
|
1724 if(bPtr->t2&LSH_CORE_ARRAY_BIT) {
|
mas01mc@559
|
1725 dump_core_hashtable_array((Uns32T*)(bPtr->next));
|
mas01mc@559
|
1726 }
|
mas01mc@559
|
1727 else {
|
mas01mc@559
|
1728 dump_hashtable_row(bPtr->next);
|
mas01mc@559
|
1729 }
|
mas01mc@559
|
1730 #else
|
mas01mc@559
|
1731 dump_hashtable_row(bPtr);
|
mas01mc@559
|
1732 #endif
|
mas01mc@559
|
1733 printf("\n");
|
mas01mc@559
|
1734 }
|
mas01mc@559
|
1735 }
|
mas01mc@559
|
1736 }
|
mas01mc@559
|
1737
|
mas01mc@560
|
1738 void G::dump_disk_row(char* filename, Uns32T n){
|
mas01mc@560
|
1739 int dbfid = unserialize_lsh_header(filename);
|
mas01mc@560
|
1740 if(dbfid<0){
|
mas01mc@560
|
1741 cerr << "Could not read header from " << filename << endl;
|
mas01mc@560
|
1742 return;
|
mas01mc@560
|
1743 }
|
mas01mc@560
|
1744 FILE* dbFile = 0;
|
mas01mc@560
|
1745 dbFile = fdopen(dbfid, "rb");
|
mas01mc@560
|
1746 if(!dbFile){
|
mas01mc@560
|
1747 cerr << "Could not create FILE pointer from file:" << filename << " with fid:" << dbfid << endl;
|
mas01mc@560
|
1748 close(dbfid);
|
mas01mc@560
|
1749 return;
|
mas01mc@560
|
1750 }
|
mas01mc@559
|
1751
|
mas01mc@560
|
1752 Uns32T x=0,y=0;
|
mas01mc@560
|
1753
|
mas01mc@560
|
1754 // Seek to hashtable base offset
|
mas01mc@560
|
1755 if(fseek(dbFile, get_serial_hashtable_offset(), SEEK_SET)){
|
mas01mc@560
|
1756 fclose(dbFile);
|
mas01mc@560
|
1757 error("fSeek error in unserialize_lsh_hashtables_format2");
|
mas01mc@560
|
1758 }
|
mas01mc@561
|
1759 Uns32T token = 0;
|
mas01mc@560
|
1760 Uns32T pointID;
|
mas01mc@560
|
1761
|
mas01mc@560
|
1762 // Read the hash tables into core (structure is given in header)
|
mas01mc@560
|
1763 while( x < H::L){
|
mas01mc@560
|
1764 y=0;
|
mas01mc@560
|
1765 if(fread(&token, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@560
|
1766 fclose(dbFile);
|
mas01mc@561
|
1767 error("Read error T1","unserialize_lsh_hashtables_format2()");
|
mas01mc@560
|
1768 }
|
mas01mc@561
|
1769 while(token != O2_SERIAL_TOKEN_ENDTABLE){
|
mas01mc@561
|
1770 if(token == O2_SERIAL_TOKEN_T1){
|
mas01mc@561
|
1771 if(fread(&token, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@560
|
1772 fclose(dbFile);
|
mas01mc@561
|
1773 error("Read error t1","unserialize_lsh_hashtables_format2()");
|
mas01mc@561
|
1774 }
|
mas01mc@561
|
1775 y=token;
|
mas01mc@561
|
1776 if(y==n){
|
mas01mc@561
|
1777 printf("D[%d,%d]", x, y);
|
mas01mc@560
|
1778 if(fread(&token, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@560
|
1779 fclose(dbFile);
|
mas01mc@560
|
1780 error("Read error 2","unserialize_lsh_hashtables_format2()");
|
mas01mc@560
|
1781 }
|
mas01mc@561
|
1782 printf("[numElements=%d]", token);
|
mas01mc@561
|
1783 if(fread(&token, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@561
|
1784 fclose(dbFile);
|
mas01mc@561
|
1785 error("Read error 3","unserialize_lsh_hashtables_format2()");
|
mas01mc@561
|
1786 }
|
mas01mc@561
|
1787 while(!(token==O2_SERIAL_TOKEN_ENDTABLE || token==O2_SERIAL_TOKEN_T1)){
|
mas01mc@561
|
1788 // Check for T2 token
|
mas01mc@561
|
1789 if(token!=O2_SERIAL_TOKEN_T2){
|
mas01mc@561
|
1790 printf("t2=%d",token);
|
mas01mc@561
|
1791 fclose(dbFile);
|
mas01mc@561
|
1792 error("State machine error T2 token", "unserialize_hashtable_row_format2()");
|
mas01mc@561
|
1793 }
|
mas01mc@561
|
1794 // Read t2 value
|
mas01mc@560
|
1795 if(fread(&token, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@560
|
1796 fclose(dbFile);
|
mas01mc@561
|
1797 error("Read error t2","unserialize_hashtable_row_format2");
|
mas01mc@560
|
1798 }
|
mas01mc@561
|
1799 if(fread(&pointID, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@561
|
1800 fclose(dbFile);
|
mas01mc@561
|
1801 error("Read error pointID","unserialize_hashtable_row_format2");
|
mas01mc@561
|
1802 }
|
mas01mc@561
|
1803 while(!(pointID==O2_SERIAL_TOKEN_ENDTABLE || pointID==O2_SERIAL_TOKEN_T1 || pointID==O2_SERIAL_TOKEN_T2 )){
|
mas01mc@561
|
1804 printf("(%0X,%u)", token, pointID);
|
mas01mc@561
|
1805 if(fread(&pointID, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@560
|
1806 fclose(dbFile);
|
mas01mc@561
|
1807 error("Read error H::p","unserialize_hashtable_row_format2");
|
mas01mc@560
|
1808 }
|
mas01mc@561
|
1809 }
|
mas01mc@561
|
1810 token = pointID; // Copy last found token
|
mas01mc@561
|
1811 }
|
mas01mc@561
|
1812 printf("\n");
|
mas01mc@561
|
1813 }
|
mas01mc@561
|
1814 else{ // gobble up rest of row
|
mas01mc@561
|
1815 while(!(token==O2_SERIAL_TOKEN_T1 || token==O2_SERIAL_TOKEN_ENDTABLE)){
|
mas01mc@561
|
1816 if(fread(&token, sizeof(Uns32T), 1, dbFile) != 1){
|
mas01mc@561
|
1817 fclose(dbFile);
|
mas01mc@561
|
1818 error("Read error 4","unserialize_lsh_hashtables_format2()");
|
mas01mc@560
|
1819 }
|
mas01mc@560
|
1820 }
|
mas01mc@560
|
1821 }
|
mas01mc@560
|
1822 }
|
mas01mc@560
|
1823 }
|
mas01mc@561
|
1824 if(token==O2_SERIAL_TOKEN_ENDTABLE){
|
mas01mc@561
|
1825 x++;
|
mas01mc@561
|
1826 }
|
mas01mc@560
|
1827 }
|
mas01mc@560
|
1828 close(dbfid);
|
mas01mc@559
|
1829 }
|
mas01mc@559
|
1830
|
mas01mc@513
|
1831 void G::dump_core_hashtable_array(Uns32T* p){
|
mas01mc@513
|
1832 Uns32T skip;
|
mas01mc@513
|
1833 Uns32T t2;
|
mas01mc@513
|
1834 Uns32T p1;
|
mas01mc@513
|
1835 Uns32T p2;
|
mas01mc@513
|
1836 CR_ASSERT(p);
|
mas01mc@513
|
1837 do{
|
mas01mc@513
|
1838 t2 = *p++;
|
mas01mc@513
|
1839 p1 = *p++;
|
mas01mc@513
|
1840 p2 = *p++;
|
mas01mc@513
|
1841 skip = (( p1 & SKIP_BITS ) >> SKIP_BITS_RIGHT_SHIFT_LSB) + (( p2 & SKIP_BITS ) >> SKIP_BITS_RIGHT_SHIFT_MSB);
|
mas01mc@561
|
1842 printf("(%0X, %u)", t2, p1 & ~SKIP_BITS);
|
mas01mc@513
|
1843 if(skip--){
|
mas01mc@561
|
1844 printf("(%0X, %u)", t2, p2 & ~SKIP_BITS);
|
mas01mc@513
|
1845 while(skip-- )
|
mas01mc@560
|
1846 printf("(%0X, %u)", t2, *p++);
|
mas01mc@513
|
1847 }
|
mas01mc@513
|
1848 }while( *p != LSH_CORE_ARRAY_END_ROW_TOKEN );
|
mas01mc@513
|
1849 }
|
mas01mc@340
|
1850
|
mas01mc@292
|
1851 void G::dump_hashtable_row(bucket* p){
|
mas01mc@292
|
1852 while(p && p->t2!=IFLAG){
|
mas01mc@344
|
1853 sbucket* sbp = p->snext.ptr;
|
mas01mc@292
|
1854 while(sbp){
|
mas01mc@292
|
1855 printf("(%0X,%u)", p->t2, sbp->pointID);
|
mas01mc@292
|
1856 fflush(stdout);
|
mas01mc@292
|
1857 sbp=sbp->snext;
|
mas01mc@292
|
1858 }
|
mas01mc@292
|
1859 p=p->next;
|
mas01mc@292
|
1860 }
|
mas01mc@292
|
1861 printf("\n");
|
mas01mc@292
|
1862 }
|
mas01mc@292
|
1863
|
mas01mc@292
|
1864
|
mas01mc@292
|
1865 // G::serial_retrieve_point( ... )
|
mas01mc@292
|
1866 // retrieves (pointID) from a serialized LSH database
|
mas01mc@292
|
1867 //
|
mas01mc@292
|
1868 // inputs:
|
mas01mc@292
|
1869 // filename - file name of serialized LSH database
|
mas01mc@292
|
1870 // vv - query point set
|
mas01mc@292
|
1871 //
|
mas01mc@292
|
1872 // outputs:
|
mas01mc@292
|
1873 // inserts retrieved points into add_point() callback method
|
mas01mc@292
|
1874 void G::serial_retrieve_point_set(char* filename, vector<vector<float> >& vv, ReporterCallbackPtr add_point, void* caller)
|
mas01mc@292
|
1875 {
|
mas01mc@292
|
1876 int dbfid = serial_open(filename, 0); // open for read
|
mas01mc@292
|
1877 char* dbheader = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 0);// get database pointer
|
mas01mc@292
|
1878 serial_get_header(dbheader); // read header
|
mas01mc@292
|
1879 serial_munmap(dbheader, O2_SERIAL_HEADER_SIZE); // drop header mmap
|
mas01mc@292
|
1880
|
mas01mc@292
|
1881 if((lshHeader->flags & O2_SERIAL_FILEFORMAT2)){
|
mas01mc@336
|
1882 serial_close(dbfid);
|
mas01mc@292
|
1883 error("serial_retrieve_point_set is for SERIAL_FILEFORMAT1 only");
|
mas01mc@292
|
1884 }
|
mas01mc@292
|
1885
|
mas01mc@292
|
1886 // size of each hash table
|
mas01mc@292
|
1887 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
1888 calling_instance = caller; // class instance variable used in ...bucket_chain_point()
|
mas01mc@292
|
1889 add_point_callback = add_point;
|
mas01mc@292
|
1890
|
mas01mc@292
|
1891 for(Uns32T j=0; j<L; j++){
|
mas01mc@292
|
1892 // memory map a single hash table for random access
|
mas01mc@292
|
1893 char* db = serial_mmap(dbfid, hashTableSize, 0,
|
mas01mc@292
|
1894 align_up(get_serial_hashtable_offset()+j*hashTableSize,get_page_logn()));
|
mas01mc@324
|
1895 #ifdef __CYGWIN__
|
mas01mc@324
|
1896 // No madvise in CYGWIN
|
mas01mc@324
|
1897 #else
|
mas01mc@292
|
1898 if(madvise(db, hashTableSize, MADV_RANDOM)<0)
|
mas01mc@292
|
1899 error("could not advise local hashtable memory","","madvise");
|
mas01mc@324
|
1900 #endif
|
mas01mc@292
|
1901 SerialElementT* pe = (SerialElementT*)db ;
|
mas01mc@292
|
1902 for(Uns32T qpos=0; qpos<vv.size(); qpos++){
|
mas01mc@293
|
1903 H::compute_hash_functions(vv[qpos]);
|
mas01mc@293
|
1904 H::generate_hash_keys(*(g+j),*(r1+j),*(r2+j));
|
mas01mc@292
|
1905 serial_bucket_chain_point(pe+t1*lshHeader->numCols, qpos); // Point to correct row
|
mas01mc@292
|
1906 }
|
mas01mc@292
|
1907 serial_munmap(db, hashTableSize); // drop hashtable mmap
|
mas01mc@292
|
1908 }
|
mas01mc@292
|
1909 serial_close(dbfid);
|
mas01mc@292
|
1910 }
|
mas01mc@292
|
1911
|
mas01mc@292
|
1912 void G::serial_retrieve_point(char* filename, vector<float>& v, Uns32T qpos, ReporterCallbackPtr add_point, void* caller){
|
mas01mc@292
|
1913 int dbfid = serial_open(filename, 0); // open for read
|
mas01mc@292
|
1914 char* dbheader = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 0);// get database pointer
|
mas01mc@292
|
1915 serial_get_header(dbheader); // read header
|
mas01mc@292
|
1916 serial_munmap(dbheader, O2_SERIAL_HEADER_SIZE); // drop header mmap
|
mas01mc@292
|
1917
|
mas01mc@292
|
1918 if((lshHeader->flags & O2_SERIAL_FILEFORMAT2)){
|
mas01mc@336
|
1919 serial_close(dbfid);
|
mas01mc@292
|
1920 error("serial_retrieve_point is for SERIAL_FILEFORMAT1 only");
|
mas01mc@292
|
1921 }
|
mas01mc@292
|
1922
|
mas01mc@292
|
1923 // size of each hash table
|
mas01mc@292
|
1924 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
1925 calling_instance = caller;
|
mas01mc@292
|
1926 add_point_callback = add_point;
|
mas01mc@293
|
1927 H::compute_hash_functions(v);
|
mas01mc@292
|
1928 for(Uns32T j=0; j<L; j++){
|
mas01mc@292
|
1929 // memory map a single hash table for random access
|
mas01mc@292
|
1930 char* db = serial_mmap(dbfid, hashTableSize, 0,
|
mas01mc@292
|
1931 align_up(get_serial_hashtable_offset()+j*hashTableSize,get_page_logn()));
|
mas01mc@324
|
1932 #ifdef __CYGWIN__
|
mas01mc@324
|
1933 // No madvise in CYGWIN
|
mas01mc@324
|
1934 #else
|
mas01mc@292
|
1935 if(madvise(db, hashTableSize, MADV_RANDOM)<0)
|
mas01mc@292
|
1936 error("could not advise local hashtable memory","","madvise");
|
mas01mc@324
|
1937 #endif
|
mas01mc@292
|
1938 SerialElementT* pe = (SerialElementT*)db ;
|
mas01mc@293
|
1939 H::generate_hash_keys(*(g+j),*(r1+j),*(r2+j));
|
mas01mc@292
|
1940 serial_bucket_chain_point(pe+t1*lshHeader->numCols, qpos); // Point to correct row
|
mas01mc@292
|
1941 serial_munmap(db, hashTableSize); // drop hashtable mmap
|
mas01mc@292
|
1942 }
|
mas01mc@292
|
1943 serial_close(dbfid);
|
mas01mc@292
|
1944 }
|
mas01mc@292
|
1945
|
mas01mc@292
|
1946 void G::serial_dump_tables(char* filename){
|
mas01mc@292
|
1947 int dbfid = serial_open(filename, 0); // open for read
|
mas01mc@292
|
1948 char* dbheader = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 0);// get database pointer
|
mas01mc@292
|
1949 serial_get_header(dbheader); // read header
|
mas01mc@292
|
1950 serial_munmap(dbheader, O2_SERIAL_HEADER_SIZE); // drop header mmap
|
mas01mc@292
|
1951 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
1952 for(Uns32T j=0; j<L; j++){
|
mas01mc@292
|
1953 // memory map a single hash table for random access
|
mas01mc@292
|
1954 char* db = serial_mmap(dbfid, hashTableSize, 0,
|
mas01mc@292
|
1955 align_up(get_serial_hashtable_offset()+j*hashTableSize,get_page_logn()));
|
mas01mc@324
|
1956 #ifdef __CYGWIN__
|
mas01mc@324
|
1957 // No madvise in CYGWIN
|
mas01mc@324
|
1958 #else
|
mas01mc@292
|
1959 if(madvise(db, hashTableSize, MADV_SEQUENTIAL)<0)
|
mas01mc@292
|
1960 error("could not advise local hashtable memory","","madvise");
|
mas01mc@324
|
1961 #endif
|
mas01mc@292
|
1962 SerialElementT* pe = (SerialElementT*)db ;
|
mas01mc@292
|
1963 printf("*********** TABLE %d ***************\n", j);
|
mas01mc@292
|
1964 fflush(stdout);
|
mas01mc@292
|
1965 int count=0;
|
mas01mc@292
|
1966 do{
|
mas01mc@292
|
1967 printf("[%d,%d]", j, count++);
|
mas01mc@292
|
1968 fflush(stdout);
|
mas01mc@292
|
1969 serial_bucket_dump(pe);
|
mas01mc@292
|
1970 pe+=lshHeader->numCols;
|
mas01mc@292
|
1971 }while(pe<(SerialElementT*)db+lshHeader->numRows*lshHeader->numCols);
|
mas01mc@292
|
1972 }
|
mas01mc@292
|
1973
|
mas01mc@292
|
1974 }
|
mas01mc@292
|
1975
|
mas01mc@292
|
1976 void G::serial_bucket_dump(SerialElementT* pe){
|
mas01mc@292
|
1977 SerialElementT* pend = pe+lshHeader->numCols;
|
mas01mc@292
|
1978 while( !(pe->hashValue==IFLAG || pe==pend ) ){
|
mas01mc@292
|
1979 printf("(%0X,%u)",pe->hashValue,pe->pointID);
|
mas01mc@292
|
1980 pe++;
|
mas01mc@292
|
1981 }
|
mas01mc@292
|
1982 printf("\n");
|
mas01mc@292
|
1983 fflush(stdout);
|
mas01mc@292
|
1984 }
|
mas01mc@292
|
1985
|
mas01mc@292
|
1986 void G::serial_bucket_chain_point(SerialElementT* pe, Uns32T qpos){
|
mas01mc@292
|
1987 SerialElementT* pend = pe+lshHeader->numCols;
|
mas01mc@292
|
1988 while( !(pe->hashValue==IFLAG || pe==pend ) ){
|
mas01mc@292
|
1989 if(pe->hashValue==t2){ // new match
|
mas01mc@292
|
1990 add_point_callback(calling_instance, pe->pointID, qpos, radius);
|
mas01mc@292
|
1991 }
|
mas01mc@292
|
1992 pe++;
|
mas01mc@292
|
1993 }
|
mas01mc@292
|
1994 }
|
mas01mc@292
|
1995
|
mas01mc@292
|
1996 void G::bucket_chain_point(bucket* p, Uns32T qpos){
|
mas01mc@292
|
1997 if(!p || p->t2==IFLAG)
|
mas01mc@292
|
1998 return;
|
mas01mc@292
|
1999 if(p->t2==t2){ // match
|
mas01mc@344
|
2000 sbucket_chain_point(p->snext.ptr, qpos); // add to reporter
|
mas01mc@292
|
2001 }
|
mas01mc@292
|
2002 if(p->next){
|
mas01mc@292
|
2003 bucket_chain_point(p->next, qpos); // recurse
|
mas01mc@292
|
2004 }
|
mas01mc@292
|
2005 }
|
mas01mc@292
|
2006
|
mas01mc@292
|
2007 void G::sbucket_chain_point(sbucket* p, Uns32T qpos){
|
mas01mc@292
|
2008 add_point_callback(calling_instance, p->pointID, qpos, radius);
|
mas01mc@292
|
2009 if(p->snext){
|
mas01mc@292
|
2010 sbucket_chain_point(p->snext, qpos);
|
mas01mc@292
|
2011 }
|
mas01mc@292
|
2012 }
|
mas01mc@292
|
2013
|
mas01mc@292
|
2014 void G::get_lock(int fd, bool exclusive) {
|
mas01mc@292
|
2015 struct flock lock;
|
mas01mc@292
|
2016 int status;
|
mas01mc@292
|
2017 lock.l_type = exclusive ? F_WRLCK : F_RDLCK;
|
mas01mc@292
|
2018 lock.l_whence = SEEK_SET;
|
mas01mc@292
|
2019 lock.l_start = 0;
|
mas01mc@292
|
2020 lock.l_len = 0; /* "the whole file" */
|
mas01mc@292
|
2021 retry:
|
mas01mc@292
|
2022 do {
|
mas01mc@292
|
2023 status = fcntl(fd, F_SETLKW, &lock);
|
mas01mc@292
|
2024 } while (status != 0 && errno == EINTR);
|
mas01mc@292
|
2025 if (status) {
|
mas01mc@292
|
2026 if (errno == EAGAIN) {
|
mas01mc@292
|
2027 sleep(1);
|
mas01mc@292
|
2028 goto retry;
|
mas01mc@292
|
2029 } else {
|
mas01mc@292
|
2030 error("fcntl lock error", "", "fcntl");
|
mas01mc@292
|
2031 }
|
mas01mc@292
|
2032 }
|
mas01mc@292
|
2033 }
|
mas01mc@292
|
2034
|
mas01mc@292
|
2035 void G::release_lock(int fd) {
|
mas01mc@292
|
2036 struct flock lock;
|
mas01mc@292
|
2037 int status;
|
mas01mc@292
|
2038
|
mas01mc@292
|
2039 lock.l_type = F_UNLCK;
|
mas01mc@292
|
2040 lock.l_whence = SEEK_SET;
|
mas01mc@292
|
2041 lock.l_start = 0;
|
mas01mc@292
|
2042 lock.l_len = 0;
|
mas01mc@292
|
2043
|
mas01mc@292
|
2044 status = fcntl(fd, F_SETLKW, &lock);
|
mas01mc@292
|
2045
|
mas01mc@292
|
2046 if (status)
|
mas01mc@292
|
2047 error("fcntl unlock error", "", "fcntl");
|
mas01mc@292
|
2048 }
|