mas01mc@292
|
1 #include "lshlib.h"
|
mas01mc@292
|
2
|
mas01mc@292
|
3 //#define __LSH_DUMP_CORE_TABLES__
|
mas01mc@292
|
4 //#define USE_U_FUNCTIONS
|
mas01mc@292
|
5 //#define LSH_BLOCK_FULL_ROWS
|
mas01mc@292
|
6
|
mas01mc@292
|
7 void err(char*s){cout << s << endl;exit(2);}
|
mas01mc@292
|
8
|
mas01mc@292
|
9 Uns32T get_page_logn(){
|
mas01mc@292
|
10 int pagesz = (int)sysconf(_SC_PAGESIZE);
|
mas01mc@292
|
11 return (Uns32T)log2((double)pagesz);
|
mas01mc@292
|
12 }
|
mas01mc@292
|
13
|
mas01mc@292
|
14 unsigned align_up(unsigned x, unsigned w){ return ((x) + ((1<<w)-1) & ~((1<<w)-1)); }
|
mas01mc@292
|
15
|
mas01mc@292
|
16 void H::error(const char* a, const char* b, const char *sysFunc) {
|
mas01mc@292
|
17 cerr << a << ": " << b << endl;
|
mas01mc@292
|
18 if (sysFunc) {
|
mas01mc@292
|
19 perror(sysFunc);
|
mas01mc@292
|
20 }
|
mas01mc@292
|
21 exit(1);
|
mas01mc@292
|
22 }
|
mas01mc@292
|
23
|
mas01mc@292
|
24 H::H(Uns32T kk, Uns32T mm, Uns32T dd, Uns32T NN, Uns32T CC):
|
mas01mc@292
|
25 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
26 use_u_functions(true),
|
mas01mc@292
|
27 #else
|
mas01mc@292
|
28 use_u_functions(false),
|
mas01mc@292
|
29 #endif
|
mas01mc@292
|
30 bucketCount(0),
|
mas01mc@292
|
31 pointCount(0),
|
mas01mc@292
|
32 N(NN),
|
mas01mc@292
|
33 C(CC),
|
mas01mc@292
|
34 k(kk),
|
mas01mc@292
|
35 m(mm),
|
mas01mc@292
|
36 L(mm*(mm-1)/2),
|
mas01mc@292
|
37 d(dd)
|
mas01mc@292
|
38 {
|
mas01mc@292
|
39 Uns32T j;
|
mas01mc@292
|
40 cout << "file size: ~" << (((unsigned long long)L*N*C*sizeof(SerialElementT))/1000000UL) << "MB" << endl;
|
mas01mc@292
|
41 if(((unsigned long long)L*N*C*sizeof(SerialElementT))>4000000000UL)
|
mas01mc@292
|
42 error("Maximum size of LSH file exceded: 12*L*N*C > 4000MB");
|
mas01mc@292
|
43 else if(((unsigned long long)N*C*sizeof(SerialElementT))>1000000000UL)
|
mas01mc@292
|
44 cout << "warning: hash tables exceed 1000MB." << endl;
|
mas01mc@292
|
45
|
mas01mc@292
|
46 if(m<2){
|
mas01mc@292
|
47 m=2;
|
mas01mc@292
|
48 L=1; // check value of L
|
mas01mc@292
|
49 cout << "warning: setting m=2, L=1" << endl;
|
mas01mc@292
|
50 }
|
mas01mc@292
|
51 if(use_u_functions && k%2){
|
mas01mc@292
|
52 k++; // make sure k is even
|
mas01mc@292
|
53 cout << "warning: setting k even" << endl;
|
mas01mc@292
|
54 }
|
mas01mc@292
|
55 __initialize_data_structures();
|
mas01mc@292
|
56 for(j=0; j<L; j++)
|
mas01mc@292
|
57 for(kk=0; kk<k; kk++) {
|
mas01mc@292
|
58 r1[j][kk]=__randr(); // random 1..2^29
|
mas01mc@292
|
59 r2[j][kk]=__randr(); // random 1..2^29
|
mas01mc@292
|
60 }
|
mas01mc@292
|
61 }
|
mas01mc@292
|
62
|
mas01mc@292
|
63 // Post constructor initialization
|
mas01mc@292
|
64 void H::__initialize_data_structures(){
|
mas01mc@292
|
65 H::P = UH_PRIME_DEFAULT;
|
mas01mc@292
|
66
|
mas01mc@292
|
67 /* FIXME: don't use time(); instead use /dev/random or similar */
|
mas01mc@292
|
68 /* FIXME: write out the seed somewhere, so that we can get
|
mas01mc@292
|
69 repeatability */
|
mas01mc@292
|
70 #ifdef MT19937
|
mas01mc@292
|
71 init_genrand(time(NULL));
|
mas01mc@292
|
72 #else
|
mas01mc@292
|
73 srand(time(NULL)); // seed random number generator
|
mas01mc@292
|
74 #endif
|
mas01mc@292
|
75 Uns32T i,j;
|
mas01mc@292
|
76 H::h = new bucket**[ H::L ];
|
mas01mc@292
|
77 H::r1 = new Uns32T*[ H::L ];
|
mas01mc@292
|
78 H::r2 = new Uns32T*[ H::L ];
|
mas01mc@292
|
79 assert( H::h && H::r1 && H::r2 ); // failure
|
mas01mc@292
|
80 for( j = 0 ; j < H::L ; j++ ){
|
mas01mc@292
|
81 H::r1[ j ] = new Uns32T[ H::k ];
|
mas01mc@292
|
82 H::r2[ j ] = new Uns32T[ H::k ];
|
mas01mc@292
|
83 assert( H::r1[j] && H::r2[j] ); // failure
|
mas01mc@292
|
84 }
|
mas01mc@292
|
85
|
mas01mc@292
|
86 for( j = 0 ; j < H::L ; j++ ){
|
mas01mc@292
|
87 H::h[j] = new bucket*[ H::N ];
|
mas01mc@292
|
88 assert( H::h[j] );
|
mas01mc@292
|
89 for( i = 0 ; i < H::N ; i++)
|
mas01mc@292
|
90 H::h[j][i] = 0;
|
mas01mc@292
|
91 }
|
mas01mc@292
|
92 }
|
mas01mc@292
|
93
|
mas01mc@292
|
94 // Destruct hash tables
|
mas01mc@292
|
95 H::~H(){
|
mas01mc@292
|
96 Uns32T i,j;
|
mas01mc@292
|
97 for( j=0 ; j < H::L ; j++ ){
|
mas01mc@292
|
98 delete[] H::r1[ j ];
|
mas01mc@292
|
99 delete[] H::r2[ j ];
|
mas01mc@292
|
100 for(i = 0; i< H::N ; i++)
|
mas01mc@292
|
101 delete H::h[ j ][ i ];
|
mas01mc@292
|
102 delete[] H::h[ j ];
|
mas01mc@292
|
103 }
|
mas01mc@292
|
104 delete[] H::r1;
|
mas01mc@292
|
105 delete[] H::r2;
|
mas01mc@292
|
106 delete[] H::h;
|
mas01mc@292
|
107 }
|
mas01mc@292
|
108
|
mas01mc@292
|
109
|
mas01mc@292
|
110 // make hash value \in Z
|
mas01mc@292
|
111 void H::__generate_hash_keys(Uns32T*g,Uns32T* r1, Uns32T* r2){
|
mas01mc@292
|
112 H::t1 = __computeProductModDefaultPrime( g, r1, H::k ) % H::N;
|
mas01mc@292
|
113 H::t2 = __computeProductModDefaultPrime( g, r2, H::k );
|
mas01mc@292
|
114
|
mas01mc@292
|
115 }
|
mas01mc@292
|
116
|
mas01mc@292
|
117 #define CR_ASSERT(b){if(!(b)){fprintf(stderr, "ASSERT failed on line %d, file %s.\n", __LINE__, __FILE__); exit(1);}}
|
mas01mc@292
|
118
|
mas01mc@292
|
119 // Computes (a.b) mod UH_PRIME_DEFAULT
|
mas01mc@292
|
120 inline Uns32T H::__computeProductModDefaultPrime(Uns32T *a, Uns32T *b, IntT size){
|
mas01mc@292
|
121 LongUns64T h = 0;
|
mas01mc@292
|
122
|
mas01mc@292
|
123 for(IntT i = 0; i < size; i++){
|
mas01mc@292
|
124 h = h + (LongUns64T)a[i] * (LongUns64T)b[i];
|
mas01mc@292
|
125 h = (h & TWO_TO_32_MINUS_1) + 5 * (h >> 32);
|
mas01mc@292
|
126 if (h >= UH_PRIME_DEFAULT) {
|
mas01mc@292
|
127 h = h - UH_PRIME_DEFAULT;
|
mas01mc@292
|
128 }
|
mas01mc@292
|
129 CR_ASSERT(h < UH_PRIME_DEFAULT);
|
mas01mc@292
|
130 }
|
mas01mc@292
|
131 return h;
|
mas01mc@292
|
132 }
|
mas01mc@292
|
133
|
mas01mc@292
|
134 Uns32T H::bucket_insert_point(bucket **pp){
|
mas01mc@292
|
135 Uns32T collisionCount = 0;
|
mas01mc@292
|
136 if(!*pp){
|
mas01mc@292
|
137 *pp = new bucket();
|
mas01mc@292
|
138 #ifdef LSH_BLOCK_FULL_ROWS
|
mas01mc@292
|
139 (*pp)->t2 = 0; // Use t2 as a collision counter for the row
|
mas01mc@292
|
140 (*pp)->next = new bucket();
|
mas01mc@292
|
141 #endif
|
mas01mc@292
|
142 }
|
mas01mc@292
|
143 #ifdef LSH_BLOCK_FULL_ROWS
|
mas01mc@292
|
144 collisionCount = (*pp)->t2;
|
mas01mc@292
|
145 if(collisionCount < H::C){ // Block if row is full
|
mas01mc@292
|
146 (*pp)->t2++; // Increment collision counter
|
mas01mc@292
|
147 pointCount++;
|
mas01mc@292
|
148 collisionCount++;
|
mas01mc@292
|
149 __bucket_insert_point((*pp)->next); // First bucket holds collision count
|
mas01mc@292
|
150 }
|
mas01mc@292
|
151 #else
|
mas01mc@292
|
152 pointCount++;
|
mas01mc@292
|
153 __bucket_insert_point(*pp); // No collision count storage
|
mas01mc@292
|
154 #endif
|
mas01mc@292
|
155 return collisionCount;
|
mas01mc@292
|
156 }
|
mas01mc@292
|
157
|
mas01mc@292
|
158 void H::__bucket_insert_point(bucket* p){
|
mas01mc@292
|
159 if(p->t2 == IFLAG){ // initialization flag, is it in the domain of t2?
|
mas01mc@292
|
160 p->t2 = H::t2;
|
mas01mc@292
|
161 bucketCount++; // Record start of new point-locale collision chain
|
mas01mc@292
|
162 p->snext = new sbucket();
|
mas01mc@292
|
163 __sbucket_insert_point(p->snext);
|
mas01mc@292
|
164 return;
|
mas01mc@292
|
165 }
|
mas01mc@292
|
166
|
mas01mc@292
|
167 if(p->t2 == H::t2){
|
mas01mc@292
|
168 __sbucket_insert_point(p->snext);
|
mas01mc@292
|
169 return;
|
mas01mc@292
|
170 }
|
mas01mc@292
|
171
|
mas01mc@292
|
172 if(p->next){
|
mas01mc@292
|
173 __bucket_insert_point(p->next);
|
mas01mc@292
|
174 }
|
mas01mc@292
|
175
|
mas01mc@292
|
176 else{
|
mas01mc@292
|
177 p->next = new bucket();
|
mas01mc@292
|
178 __bucket_insert_point(p->next);
|
mas01mc@292
|
179 }
|
mas01mc@292
|
180
|
mas01mc@292
|
181 }
|
mas01mc@292
|
182
|
mas01mc@292
|
183 void H::__sbucket_insert_point(sbucket* p){
|
mas01mc@292
|
184 if(p->pointID==IFLAG){
|
mas01mc@292
|
185 p->pointID = H::p;
|
mas01mc@292
|
186 return;
|
mas01mc@292
|
187 }
|
mas01mc@292
|
188
|
mas01mc@292
|
189 // Search for pointID
|
mas01mc@292
|
190 if(p->snext){
|
mas01mc@292
|
191 __sbucket_insert_point(p->snext);
|
mas01mc@292
|
192 }
|
mas01mc@292
|
193 else{
|
mas01mc@292
|
194 // Make new point collision bucket at end of list
|
mas01mc@292
|
195 p->snext = new sbucket();
|
mas01mc@292
|
196 __sbucket_insert_point(p->snext);
|
mas01mc@292
|
197 }
|
mas01mc@292
|
198 }
|
mas01mc@292
|
199
|
mas01mc@292
|
200 inline bucket** H::__get_bucket(int j){
|
mas01mc@292
|
201 return *(h+j);
|
mas01mc@292
|
202 }
|
mas01mc@292
|
203
|
mas01mc@292
|
204 // hash functions G
|
mas01mc@292
|
205 G::G(float ww, Uns32T kk,Uns32T mm, Uns32T dd, Uns32T NN, Uns32T CC, float r):
|
mas01mc@292
|
206 H(kk,mm,dd,NN,CC),
|
mas01mc@292
|
207 w(ww),
|
mas01mc@292
|
208 radius(r),
|
mas01mc@292
|
209 maxp(0),
|
mas01mc@292
|
210 calling_instance(0),
|
mas01mc@292
|
211 add_point_callback(0),
|
mas01mc@292
|
212 lshHeader(0)
|
mas01mc@292
|
213 {
|
mas01mc@292
|
214 Uns32T j;
|
mas01mc@292
|
215 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
216 G::A = new float**[ H::m ]; // m x k x d random projectors
|
mas01mc@292
|
217 G::b = new float*[ H::m ]; // m x k random biases
|
mas01mc@292
|
218 #else
|
mas01mc@292
|
219 G::A = new float**[ H::L ]; // m x k x d random projectors
|
mas01mc@292
|
220 G::b = new float*[ H::L ]; // m x k random biases
|
mas01mc@292
|
221 #endif
|
mas01mc@292
|
222 G::g = new Uns32T*[ H::L ]; // L x k random projections
|
mas01mc@292
|
223 assert( G::g && G::A && G::b ); // failure
|
mas01mc@292
|
224 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
225 // Use m \times u_i functions \in R^{(k/2) \times (d)}
|
mas01mc@292
|
226 // Combine to make L=m(m-1)/2 hash functions \in R^{k \times d}
|
mas01mc@292
|
227 for( j = 0; j < H::m ; j++ ){ // m functions u_i(v)
|
mas01mc@292
|
228 G::A[j] = new float*[ H::k/2 ]; // k/2 x d 2-stable distribution coefficients
|
mas01mc@292
|
229 G::b[j] = new float[ H::k/2 ]; // bias
|
mas01mc@292
|
230 assert( G::A[j] && G::b[j] ); // failure
|
mas01mc@292
|
231 for( kk = 0; kk < H::k/2 ; kk++ ){
|
mas01mc@292
|
232 G::A[j][kk] = new float[ H::d ];
|
mas01mc@292
|
233 assert( G::A[j][kk] ); // failure
|
mas01mc@292
|
234 for(Uns32T i = 0 ; i < H::d ; i++ )
|
mas01mc@292
|
235 G::A[j][kk][i] = randn(); // Normal
|
mas01mc@292
|
236 G::b[j][kk] = ranf()*G::w; // Uniform
|
mas01mc@292
|
237 }
|
mas01mc@292
|
238 }
|
mas01mc@292
|
239 #else
|
mas01mc@292
|
240 // Use m \times u_i functions \in R^{k \times (d)}
|
mas01mc@292
|
241 // Combine to make L=m(m-1)/2 hash functions \in R^{k \times d}
|
mas01mc@292
|
242 for( j = 0; j < H::L ; j++ ){ // m functions u_i(v)
|
mas01mc@292
|
243 G::A[j] = new float*[ H::k ]; // k x d 2-stable distribution coefficients
|
mas01mc@292
|
244 G::b[j] = new float[ H::k ]; // bias
|
mas01mc@292
|
245 assert( G::A[j] && G::b[j] ); // failure
|
mas01mc@292
|
246 for( kk = 0; kk < H::k ; kk++ ){
|
mas01mc@292
|
247 G::A[j][kk] = new float[ H::d ];
|
mas01mc@292
|
248 assert( G::A[j][kk] ); // failure
|
mas01mc@292
|
249 for(Uns32T i = 0 ; i < H::d ; i++ )
|
mas01mc@292
|
250 G::A[j][kk][i] = randn(); // Normal
|
mas01mc@292
|
251 G::b[j][kk] = ranf()*G::w; // Uniform
|
mas01mc@292
|
252 }
|
mas01mc@292
|
253 }
|
mas01mc@292
|
254 #endif
|
mas01mc@292
|
255
|
mas01mc@292
|
256 for( j = 0 ; j < H::L ; j++ ){ // L functions g_j(u_a, u_b) a,b \in nchoosek(m,2)
|
mas01mc@292
|
257 G::g[j] = new Uns32T[ H::k ]; // k x 32-bit hash values, gj(v)=[x0 x1 ... xk-1] xk \in Z
|
mas01mc@292
|
258 assert( G::g[j] );
|
mas01mc@292
|
259 }
|
mas01mc@292
|
260
|
mas01mc@292
|
261 initialize_partial_functions(); // m partially evaluated hash functions
|
mas01mc@292
|
262 }
|
mas01mc@292
|
263
|
mas01mc@292
|
264 // Serialize from file LSH constructor
|
mas01mc@292
|
265 // Read parameters from database file
|
mas01mc@292
|
266 // Load the hash functions, close the database
|
mas01mc@292
|
267 // Optionally load the LSH tables into head-allocated lists in core
|
mas01mc@292
|
268 G::G(char* filename, bool lshInCoreFlag):
|
mas01mc@292
|
269 calling_instance(0),
|
mas01mc@292
|
270 add_point_callback(0)
|
mas01mc@292
|
271 {
|
mas01mc@292
|
272 int dbfid = unserialize_lsh_header(filename);
|
mas01mc@292
|
273 unserialize_lsh_functions(dbfid);
|
mas01mc@292
|
274 initialize_partial_functions();
|
mas01mc@292
|
275
|
mas01mc@292
|
276 // Format1 only needs unserializing if specifically requested
|
mas01mc@292
|
277 if(!(lshHeader->flags&O2_SERIAL_FILEFORMAT2) && lshInCoreFlag){
|
mas01mc@292
|
278 unserialize_lsh_hashtables_format1(dbfid);
|
mas01mc@292
|
279 }
|
mas01mc@292
|
280
|
mas01mc@292
|
281 // Format2 always needs unserializing
|
mas01mc@292
|
282 if(lshHeader->flags&O2_SERIAL_FILEFORMAT2 && lshInCoreFlag){
|
mas01mc@292
|
283 unserialize_lsh_hashtables_format2(dbfid);
|
mas01mc@292
|
284 }
|
mas01mc@292
|
285
|
mas01mc@292
|
286 close(dbfid);
|
mas01mc@292
|
287 }
|
mas01mc@292
|
288
|
mas01mc@292
|
289 void G::initialize_partial_functions(){
|
mas01mc@292
|
290
|
mas01mc@292
|
291 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
292 uu = vector<vector<Uns32T> >(H::m);
|
mas01mc@292
|
293 for( Uns32T aa=0 ; aa < H::m ; aa++ )
|
mas01mc@292
|
294 uu[aa] = vector<Uns32T>( H::k/2 );
|
mas01mc@292
|
295 #else
|
mas01mc@292
|
296 uu = vector<vector<Uns32T> >(H::L);
|
mas01mc@292
|
297 for( Uns32T aa=0 ; aa < H::L ; aa++ )
|
mas01mc@292
|
298 uu[aa] = vector<Uns32T>( H::k );
|
mas01mc@292
|
299 #endif
|
mas01mc@292
|
300 }
|
mas01mc@292
|
301
|
mas01mc@292
|
302
|
mas01mc@292
|
303 // Generate z ~ N(0,1)
|
mas01mc@292
|
304 float G::randn(){
|
mas01mc@292
|
305 // Box-Muller
|
mas01mc@292
|
306 float x1, x2;
|
mas01mc@292
|
307 do{
|
mas01mc@292
|
308 x1 = ranf();
|
mas01mc@292
|
309 } while (x1 == 0); // cannot take log of 0
|
mas01mc@292
|
310 x2 = ranf();
|
mas01mc@292
|
311 float z;
|
mas01mc@292
|
312 z = sqrtf(-2.0 * logf(x1)) * cosf(2.0 * M_PI * x2);
|
mas01mc@292
|
313 return z;
|
mas01mc@292
|
314 }
|
mas01mc@292
|
315
|
mas01mc@292
|
316 float G::ranf(){
|
mas01mc@292
|
317 #ifdef MT19937
|
mas01mc@292
|
318 return (float) genrand_real2();
|
mas01mc@292
|
319 #else
|
mas01mc@292
|
320 return (float)( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
|
mas01mc@292
|
321 #endif
|
mas01mc@292
|
322 }
|
mas01mc@292
|
323
|
mas01mc@292
|
324 // range is 1..2^29
|
mas01mc@292
|
325 /* FIXME: that looks like an ... odd range. Still. */
|
mas01mc@292
|
326 Uns32T H::__randr(){
|
mas01mc@292
|
327 #ifdef MT19937
|
mas01mc@292
|
328 return (Uns32T)((genrand_int32() >> 3) + 1);
|
mas01mc@292
|
329 #else
|
mas01mc@292
|
330 return (Uns32T) ((rand() >> 2) + 1);
|
mas01mc@292
|
331 #endif
|
mas01mc@292
|
332 }
|
mas01mc@292
|
333
|
mas01mc@292
|
334 G::~G(){
|
mas01mc@292
|
335 Uns32T j,kk;
|
mas01mc@292
|
336 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
337 for( j = 0 ; j < H::m ; j++ ){
|
mas01mc@292
|
338 for( kk = 0 ; kk < H::k/2 ; kk++ )
|
mas01mc@292
|
339 delete[] A[j][kk];
|
mas01mc@292
|
340 delete[] A[j];
|
mas01mc@292
|
341 }
|
mas01mc@292
|
342 delete[] A;
|
mas01mc@292
|
343 for( j = 0 ; j < H::m ; j++ )
|
mas01mc@292
|
344 delete[] b[j];
|
mas01mc@292
|
345 delete[] b;
|
mas01mc@292
|
346 #else
|
mas01mc@292
|
347 for( j = 0 ; j < H::L ; j++ ){
|
mas01mc@292
|
348 for( kk = 0 ; kk < H::k ; kk++ )
|
mas01mc@292
|
349 delete[] A[j][kk];
|
mas01mc@292
|
350 delete[] A[j];
|
mas01mc@292
|
351 }
|
mas01mc@292
|
352 delete[] A;
|
mas01mc@292
|
353 for( j = 0 ; j < H::L ; j++ )
|
mas01mc@292
|
354 delete[] b[j];
|
mas01mc@292
|
355 delete[] b;
|
mas01mc@292
|
356 #endif
|
mas01mc@292
|
357
|
mas01mc@292
|
358 for( j = 0 ; j < H::L ; j++ )
|
mas01mc@292
|
359 delete[] g[j];
|
mas01mc@292
|
360 delete[] g;
|
mas01mc@292
|
361 delete lshHeader;
|
mas01mc@292
|
362 }
|
mas01mc@292
|
363
|
mas01mc@292
|
364 // Compute all hash functions for vector v
|
mas01mc@292
|
365 // #ifdef USE_U_FUNCTIONS use Combination of m \times h_i \in R^{(k/2) \times d}
|
mas01mc@292
|
366 // to make L \times g_j functions \in Z^k
|
mas01mc@292
|
367 void G::compute_hash_functions(vector<float>& v){ // v \in R^d
|
mas01mc@292
|
368 float iw = 1. / G::w; // hash bucket width
|
mas01mc@292
|
369 Uns32T aa, kk;
|
mas01mc@292
|
370 if( v.size() != H::d )
|
mas01mc@292
|
371 error("v.size != H::d","","compute_hash_functions"); // check input vector dimensionality
|
mas01mc@292
|
372 double tmp = 0;
|
mas01mc@292
|
373 float *pA, *pb;
|
mas01mc@292
|
374 Uns32T *pg;
|
mas01mc@292
|
375 int dd;
|
mas01mc@292
|
376 vector<float>::iterator vi;
|
mas01mc@292
|
377 vector<Uns32T>::iterator ui;
|
mas01mc@292
|
378
|
mas01mc@292
|
379 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
380 Uns32T bb;
|
mas01mc@292
|
381 // Store m dot products to expand
|
mas01mc@292
|
382 for( aa=0; aa < H::m ; aa++ ){
|
mas01mc@292
|
383 ui = uu[aa].begin();
|
mas01mc@292
|
384 for( kk = 0 ; kk < H::k/2 ; kk++ ){
|
mas01mc@292
|
385 pb = *( G::b + aa ) + kk;
|
mas01mc@292
|
386 pA = * ( * ( G::A + aa ) + kk );
|
mas01mc@292
|
387 dd = H::d;
|
mas01mc@292
|
388 tmp = 0.;
|
mas01mc@292
|
389 vi = v.begin();
|
mas01mc@292
|
390 while( dd-- )
|
mas01mc@292
|
391 tmp += *pA++ * *vi++; // project
|
mas01mc@292
|
392 tmp += *pb; // translate
|
mas01mc@292
|
393 tmp *= iw; // scale
|
mas01mc@292
|
394 *ui++ = (Uns32T) floor(tmp); // floor
|
mas01mc@292
|
395 }
|
mas01mc@292
|
396 }
|
mas01mc@292
|
397 // Binomial combinations of functions u_{a,b} \in Z^{(k/2) \times d}
|
mas01mc@292
|
398 Uns32T j;
|
mas01mc@292
|
399 for( aa=0, j=0 ; aa < H::m-1 ; aa++ )
|
mas01mc@292
|
400 for( bb = aa + 1 ; bb < H::m ; bb++, j++ ){
|
mas01mc@292
|
401 pg= *( G::g + j ); // L \times functions g_j(v) \in Z^k
|
mas01mc@292
|
402 // u_1 \in Z^{(k/2) \times d}
|
mas01mc@292
|
403 ui = uu[aa].begin();
|
mas01mc@292
|
404 kk=H::k/2;
|
mas01mc@292
|
405 while( kk-- )
|
mas01mc@292
|
406 *pg++ = *ui++; // hash function g_j(v)=[x1 x2 ... x(k/2)]; xk \in Z
|
mas01mc@292
|
407 // u_2 \in Z^{(k/2) \times d}
|
mas01mc@292
|
408 ui = uu[bb].begin();
|
mas01mc@292
|
409 kk=H::k/2;
|
mas01mc@292
|
410 while( kk--)
|
mas01mc@292
|
411 *pg++ = *ui++; // hash function g_j(v)=[x(k/2+1) x(k/2+2) ... xk]; xk \in Z
|
mas01mc@292
|
412 }
|
mas01mc@292
|
413 #else
|
mas01mc@292
|
414 for( aa=0; aa < H::L ; aa++ ){
|
mas01mc@292
|
415 ui = uu[aa].begin();
|
mas01mc@292
|
416 for( kk = 0 ; kk < H::k ; kk++ ){
|
mas01mc@292
|
417 pb = *( G::b + aa ) + kk;
|
mas01mc@292
|
418 pA = * ( * ( G::A + aa ) + kk );
|
mas01mc@292
|
419 dd = H::d;
|
mas01mc@292
|
420 tmp = 0.;
|
mas01mc@292
|
421 vi = v.begin();
|
mas01mc@292
|
422 while( dd-- )
|
mas01mc@292
|
423 tmp += *pA++ * *vi++; // project
|
mas01mc@292
|
424 tmp += *pb; // translate
|
mas01mc@292
|
425 tmp *= iw; // scale
|
mas01mc@292
|
426 *ui++ = (Uns32T) (floor(tmp)); // floor
|
mas01mc@292
|
427 }
|
mas01mc@292
|
428 }
|
mas01mc@292
|
429 // Compute hash functions
|
mas01mc@292
|
430 for( aa=0 ; aa < H::L ; aa++ ){
|
mas01mc@292
|
431 pg= *( G::g + aa ); // L \times functions g_j(v) \in Z^k
|
mas01mc@292
|
432 // u_1 \in Z^{k \times d}
|
mas01mc@292
|
433 ui = uu[aa].begin();
|
mas01mc@292
|
434 kk=H::k;
|
mas01mc@292
|
435 while( kk-- )
|
mas01mc@292
|
436 *pg++ = *ui++; // hash function g_j(v)=[x1 x2 ... xk]; xk \in Z
|
mas01mc@292
|
437 }
|
mas01mc@292
|
438 #endif
|
mas01mc@292
|
439
|
mas01mc@292
|
440 }
|
mas01mc@292
|
441
|
mas01mc@292
|
442
|
mas01mc@292
|
443 // single point insertion; inserted values are hash value and pointID
|
mas01mc@292
|
444 Uns32T G::insert_point(vector<float>& v, Uns32T pp){
|
mas01mc@292
|
445 Uns32T collisionCount = 0;
|
mas01mc@292
|
446 H::p = pp;
|
mas01mc@292
|
447 if(pp>G::maxp)
|
mas01mc@292
|
448 G::maxp=pp; // Store highest pointID in database
|
mas01mc@292
|
449 compute_hash_functions( v );
|
mas01mc@292
|
450 for(Uns32T j = 0 ; j < H::L ; j++ ){ // insertion
|
mas01mc@292
|
451 __generate_hash_keys( *( G::g + j ), *( H::r1 + j ), *( H::r2 + j ) );
|
mas01mc@292
|
452 collisionCount += bucket_insert_point( *(h + j) + t1 );
|
mas01mc@292
|
453 }
|
mas01mc@292
|
454 return collisionCount;
|
mas01mc@292
|
455 }
|
mas01mc@292
|
456
|
mas01mc@292
|
457
|
mas01mc@292
|
458 // batch insert for a point set
|
mas01mc@292
|
459 // inserted values are vector hash value and pointID starting at basePointID
|
mas01mc@292
|
460 void G::insert_point_set(vector<vector<float> >& vv, Uns32T basePointID){
|
mas01mc@292
|
461 for(Uns32T point=0; point<vv.size(); point++)
|
mas01mc@292
|
462 insert_point(vv[point], basePointID+point);
|
mas01mc@292
|
463 }
|
mas01mc@292
|
464
|
mas01mc@292
|
465 // point retrieval routine
|
mas01mc@292
|
466 void G::retrieve_point(vector<float>& v, Uns32T qpos, ReporterCallbackPtr add_point, void* caller){
|
mas01mc@292
|
467 calling_instance = caller;
|
mas01mc@292
|
468 add_point_callback = add_point;
|
mas01mc@292
|
469 compute_hash_functions( v );
|
mas01mc@292
|
470 for(Uns32T j = 0 ; j < H::L ; j++ ){
|
mas01mc@292
|
471 __generate_hash_keys( *( G::g + j ), *( H::r1 + j ), *( H::r2 + j ) );
|
mas01mc@292
|
472 if( bucket* bPtr = *(__get_bucket(j) + get_t1()) )
|
mas01mc@292
|
473 #ifdef LSH_BLOCK_FULL_ROWS
|
mas01mc@292
|
474 bucket_chain_point( bPtr->next, qpos);
|
mas01mc@292
|
475 #else
|
mas01mc@292
|
476 bucket_chain_point( bPtr , qpos);
|
mas01mc@292
|
477 #endif
|
mas01mc@292
|
478 }
|
mas01mc@292
|
479 }
|
mas01mc@292
|
480
|
mas01mc@292
|
481 void G::retrieve_point_set(vector<vector<float> >& vv, ReporterCallbackPtr add_point, void* caller){
|
mas01mc@292
|
482 for(Uns32T qpos = 0 ; qpos < vv.size() ; qpos++ )
|
mas01mc@292
|
483 retrieve_point(vv[qpos], qpos, add_point, caller);
|
mas01mc@292
|
484 }
|
mas01mc@292
|
485
|
mas01mc@292
|
486 // export lsh tables to table structure on disk
|
mas01mc@292
|
487 //
|
mas01mc@292
|
488 // LSH TABLE STRUCTURE
|
mas01mc@292
|
489 // ---header 64 bytes ---
|
mas01mc@292
|
490 // [magic #tables #rows #cols elementSize databaseSize version flags dim #funs 0 0 0 0 0 0]
|
mas01mc@292
|
491 //
|
mas01mc@292
|
492 // ---random projections L x k x d float ---
|
mas01mc@292
|
493 // A[0][0][0] A[0][0][1] ... A[0][0][d-1]
|
mas01mc@292
|
494 // A[0][1][0] A[0][1][1] ... A[1][1][d-1]
|
mas01mc@292
|
495 // ...
|
mas01mc@292
|
496 // A[0][K-1][0] A[0][1][1] ... A[0][k-1][d-1]
|
mas01mc@292
|
497 // ...
|
mas01mc@292
|
498 // ...
|
mas01mc@292
|
499 // A[L-1][0][0] A[M-1][0][1] ... A[L-1][0][d-1]
|
mas01mc@292
|
500 // A[L-1][1][0] A[M-1][1][1] ... A[L-1][1][d-1]
|
mas01mc@292
|
501 // ...
|
mas01mc@292
|
502 // A[L-1][k-1][0] A[M-1][1][1] ... A[L-1][k-1][d-1]
|
mas01mc@292
|
503 //
|
mas01mc@292
|
504 // ---bias L x k float ---
|
mas01mc@292
|
505 // b[0][0] b[0][1] ... b[0][k-1]
|
mas01mc@292
|
506 // b[1][0] b[1][1] ... b[1][k-1]
|
mas01mc@292
|
507 // ...
|
mas01mc@292
|
508 // b[L-1][0] b[L-1][1] ... b[L-1][k-1]
|
mas01mc@292
|
509 //
|
mas01mc@292
|
510 // ---random r1 L x k float ---
|
mas01mc@292
|
511 // r1[0][0] r1[0][1] ... r1[0][k-1]
|
mas01mc@292
|
512 // r1[1][0] r1[1][1] ... r1[1][k-1]
|
mas01mc@292
|
513 // ...
|
mas01mc@292
|
514 // r1[L-1][0] r1[L-1][1] ... r1[L-1][k-1]
|
mas01mc@292
|
515 //
|
mas01mc@292
|
516 // ---random r2 L x k float ---
|
mas01mc@292
|
517 // r2[0][0] r2[0][1] ... r2[0][k-1]
|
mas01mc@292
|
518 // r2[1][0] r2[1][1] ... r2[1][k-1]
|
mas01mc@292
|
519 // ...
|
mas01mc@292
|
520 // r2[L-1][0] r2[L-1][1] ... r2[L-1][k-1]
|
mas01mc@292
|
521 //
|
mas01mc@292
|
522 // ---hash table 0: N x C x 8 ---
|
mas01mc@292
|
523 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
524 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
525 // ...
|
mas01mc@292
|
526 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
527 //
|
mas01mc@292
|
528 // ---hash table 1: N x C x 8 ---
|
mas01mc@292
|
529 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
530 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
531 // ...
|
mas01mc@292
|
532 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
533 //
|
mas01mc@292
|
534 // ...
|
mas01mc@292
|
535 //
|
mas01mc@292
|
536 // ---hash table L-1: N x C x 8 ---
|
mas01mc@292
|
537 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
538 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
539 // ...
|
mas01mc@292
|
540 // [t2 pointID][t2 pointID]...[t2 pointID]
|
mas01mc@292
|
541 //
|
mas01mc@292
|
542
|
mas01mc@292
|
543 // Serial header constructors
|
mas01mc@292
|
544 SerialHeader::SerialHeader(){;}
|
mas01mc@292
|
545 SerialHeader::SerialHeader(float W, Uns32T L, Uns32T N, Uns32T C, Uns32T k, Uns32T d, float r, Uns32T p, Uns32T FMT):
|
mas01mc@292
|
546 lshMagic(O2_SERIAL_MAGIC),
|
mas01mc@292
|
547 binWidth(W),
|
mas01mc@292
|
548 numTables(L),
|
mas01mc@292
|
549 numRows(N),
|
mas01mc@292
|
550 numCols(C),
|
mas01mc@292
|
551 elementSize(O2_SERIAL_ELEMENT_SIZE),
|
mas01mc@292
|
552 version(O2_SERIAL_VERSION),
|
mas01mc@292
|
553 size(L * align_up(N * C * O2_SERIAL_ELEMENT_SIZE, get_page_logn()) // hash tables
|
mas01mc@292
|
554 + align_up(O2_SERIAL_HEADER_SIZE + // header + hash functions
|
mas01mc@292
|
555 L*k*( sizeof(float)*d+2*sizeof(Uns32T)+sizeof(float)),get_page_logn())),
|
mas01mc@292
|
556 flags(FMT),
|
mas01mc@292
|
557 dataDim(d),
|
mas01mc@292
|
558 numFuns(k),
|
mas01mc@292
|
559 radius(r),
|
mas01mc@292
|
560 maxp(p){;} // header
|
mas01mc@292
|
561
|
mas01mc@292
|
562 float* G::get_serial_hashfunction_base(char* db){
|
mas01mc@292
|
563 if(db&&lshHeader)
|
mas01mc@292
|
564 return (float*)(db+O2_SERIAL_HEADER_SIZE);
|
mas01mc@292
|
565 else return NULL;
|
mas01mc@292
|
566 }
|
mas01mc@292
|
567
|
mas01mc@292
|
568 SerialElementT* G::get_serial_hashtable_base(char* db){
|
mas01mc@292
|
569 if(db&&lshHeader)
|
mas01mc@292
|
570 return (SerialElementT*)(db+get_serial_hashtable_offset());
|
mas01mc@292
|
571 else
|
mas01mc@292
|
572 return NULL;
|
mas01mc@292
|
573 }
|
mas01mc@292
|
574
|
mas01mc@292
|
575 Uns32T G::get_serial_hashtable_offset(){
|
mas01mc@292
|
576 if(lshHeader)
|
mas01mc@292
|
577 return align_up(O2_SERIAL_HEADER_SIZE +
|
mas01mc@292
|
578 L*lshHeader->numFuns*( sizeof(float)*lshHeader->dataDim+2*sizeof(Uns32T)+sizeof(float)),get_page_logn());
|
mas01mc@292
|
579 else
|
mas01mc@292
|
580 return 0;
|
mas01mc@292
|
581 }
|
mas01mc@292
|
582
|
mas01mc@292
|
583 void G::serialize(char* filename, Uns32T serialFormat){
|
mas01mc@292
|
584 int dbfid;
|
mas01mc@292
|
585 char* db;
|
mas01mc@292
|
586 int dbIsNew=0;
|
mas01mc@292
|
587
|
mas01mc@292
|
588 // Check requested serialFormat
|
mas01mc@292
|
589 if(!(serialFormat==O2_SERIAL_FILEFORMAT1 || serialFormat==O2_SERIAL_FILEFORMAT2))
|
mas01mc@292
|
590 error("Unrecognized serial file format request: ", "serialize()");
|
mas01mc@292
|
591
|
mas01mc@292
|
592 // Test to see if file exists
|
mas01mc@292
|
593 if((dbfid = open (filename, O_RDONLY)) < 0)
|
mas01mc@292
|
594 // If it doesn't, then create the file (CREATE)
|
mas01mc@292
|
595 if(errno == ENOENT){
|
mas01mc@292
|
596 // Create the file
|
mas01mc@292
|
597 std::cout << "Creating new serialized LSH database:" << filename << "...";
|
mas01mc@292
|
598 std::cout.flush();
|
mas01mc@292
|
599 serial_create(filename, serialFormat);
|
mas01mc@292
|
600 dbIsNew=1;
|
mas01mc@292
|
601 }
|
mas01mc@292
|
602 else
|
mas01mc@292
|
603 // The file can't be opened
|
mas01mc@292
|
604 error("Can't open the file", filename, "open");
|
mas01mc@292
|
605
|
mas01mc@292
|
606 // Load the on-disk header into core
|
mas01mc@292
|
607 dbfid = serial_open(filename, 1); // open for write
|
mas01mc@292
|
608 db = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 1);// get database pointer
|
mas01mc@292
|
609 serial_get_header(db); // read header
|
mas01mc@292
|
610 serial_munmap(db, O2_SERIAL_HEADER_SIZE); // drop mmap
|
mas01mc@292
|
611
|
mas01mc@292
|
612 // Check compatibility of core and disk data structures
|
mas01mc@292
|
613 if( !serial_can_merge(serialFormat) )
|
mas01mc@292
|
614 error("Incompatible core and serial LSH, data structure dimensions mismatch.");
|
mas01mc@292
|
615
|
mas01mc@292
|
616 // For new LSH databases write the hashfunctions
|
mas01mc@292
|
617 if(dbIsNew)
|
mas01mc@292
|
618 serialize_lsh_hashfunctions(dbfid);
|
mas01mc@292
|
619 // Write the hashtables in the requested format
|
mas01mc@292
|
620 if(serialFormat == O2_SERIAL_FILEFORMAT1)
|
mas01mc@292
|
621 serialize_lsh_hashtables_format1(dbfid, !dbIsNew);
|
mas01mc@292
|
622 else
|
mas01mc@292
|
623 serialize_lsh_hashtables_format2(dbfid, !dbIsNew);
|
mas01mc@292
|
624
|
mas01mc@292
|
625 if(!dbIsNew){
|
mas01mc@292
|
626 db = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 1);// get database pointer
|
mas01mc@292
|
627 //serial_get_header(db); // read header
|
mas01mc@292
|
628 cout << "maxp = " << G::maxp << endl;
|
mas01mc@292
|
629 lshHeader->maxp=G::maxp;
|
mas01mc@292
|
630 // Default to FILEFORMAT1
|
mas01mc@292
|
631 if(!(lshHeader->flags&O2_SERIAL_FILEFORMAT2))
|
mas01mc@292
|
632 lshHeader->flags|=O2_SERIAL_FILEFORMAT2;
|
mas01mc@292
|
633 memcpy((char*)db, (char*)lshHeader, sizeof(SerialHeaderT));
|
mas01mc@292
|
634 serial_munmap(db, O2_SERIAL_HEADER_SIZE); // drop mmap
|
mas01mc@292
|
635 }
|
mas01mc@292
|
636
|
mas01mc@292
|
637 serial_close(dbfid);
|
mas01mc@292
|
638 }
|
mas01mc@292
|
639
|
mas01mc@292
|
640 // Test to see if core structure and requested format is
|
mas01mc@292
|
641 // compatible with currently opened database
|
mas01mc@292
|
642 int G::serial_can_merge(Uns32T format){
|
mas01mc@292
|
643 SerialHeaderT* that = lshHeader;
|
mas01mc@292
|
644 if( (format==O2_SERIAL_FILEFORMAT2 && !that->flags&O2_SERIAL_FILEFORMAT2)
|
mas01mc@292
|
645 || (format!=O2_SERIAL_FILEFORMAT2 && that->flags&O2_SERIAL_FILEFORMAT2)
|
mas01mc@292
|
646 || !( this->w == that->binWidth &&
|
mas01mc@292
|
647 this->L == that->numTables &&
|
mas01mc@292
|
648 this->N == that->numRows &&
|
mas01mc@292
|
649 this->k == that->numFuns &&
|
mas01mc@292
|
650 this->d == that->dataDim &&
|
mas01mc@292
|
651 sizeof(SerialElementT) == that->elementSize &&
|
mas01mc@292
|
652 this->radius == that->radius)){
|
mas01mc@292
|
653 serial_print_header(format);
|
mas01mc@292
|
654 return 0;
|
mas01mc@292
|
655 }
|
mas01mc@292
|
656 else
|
mas01mc@292
|
657 return 1;
|
mas01mc@292
|
658 }
|
mas01mc@292
|
659
|
mas01mc@292
|
660 // Used as an error message for serial_can_merge()
|
mas01mc@292
|
661 void G::serial_print_header(Uns32T format){
|
mas01mc@292
|
662 std::cout << "Fc:" << format << " Fs:" << lshHeader->flags << endl;
|
mas01mc@292
|
663 std::cout << "Wc:" << w << " Ls:" << lshHeader->binWidth << endl;
|
mas01mc@292
|
664 std::cout << "Lc:" << L << " Ls:" << lshHeader->numTables << endl;
|
mas01mc@292
|
665 std::cout << "Nc:" << N << " Ns:" << lshHeader->numRows << endl;
|
mas01mc@292
|
666 std::cout << "kc:" << k << " ks:" << lshHeader->numFuns << endl;
|
mas01mc@292
|
667 std::cout << "dc:" << d << " ds:" << lshHeader->dataDim << endl;
|
mas01mc@292
|
668 std::cout << "sc:" << sizeof(SerialElementT) << " ss:" << lshHeader->elementSize << endl;
|
mas01mc@292
|
669 std::cout << "rc:" << this->radius << " rs:" << lshHeader->radius << endl;
|
mas01mc@292
|
670 }
|
mas01mc@292
|
671
|
mas01mc@292
|
672 int G::serialize_lsh_hashfunctions(int fid){
|
mas01mc@292
|
673 float* pf;
|
mas01mc@292
|
674 Uns32T *pu;
|
mas01mc@292
|
675 Uns32T x,y,z;
|
mas01mc@292
|
676
|
mas01mc@292
|
677 db = serial_mmap(fid, get_serial_hashtable_offset(), 1);// get database pointer
|
mas01mc@292
|
678 pf = get_serial_hashfunction_base(db);
|
mas01mc@292
|
679
|
mas01mc@292
|
680 // HASH FUNCTIONS
|
mas01mc@292
|
681 // Write the random projectors A[][][]
|
mas01mc@292
|
682 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
683 for( x = 0 ; x < H::m ; x++ )
|
mas01mc@292
|
684 for( y = 0 ; y < H::k/2 ; y++ )
|
mas01mc@292
|
685 #else
|
mas01mc@292
|
686 for( x = 0 ; x < H::L ; x++ )
|
mas01mc@292
|
687 for( y = 0 ; y < H::k ; y++ )
|
mas01mc@292
|
688 #endif
|
mas01mc@292
|
689 for( z = 0 ; z < d ; z++ )
|
mas01mc@292
|
690 *pf++ = A[x][y][z];
|
mas01mc@292
|
691
|
mas01mc@292
|
692 // Write the random biases b[][]
|
mas01mc@292
|
693 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
694 for( x = 0 ; x < H::m ; x++ )
|
mas01mc@292
|
695 for( y = 0 ; y < H::k/2 ; y++ )
|
mas01mc@292
|
696 #else
|
mas01mc@292
|
697 for( x = 0 ; x < H::L ; x++ )
|
mas01mc@292
|
698 for( y = 0 ; y < H::k ; y++ )
|
mas01mc@292
|
699 #endif
|
mas01mc@292
|
700 *pf++=b[x][y];
|
mas01mc@292
|
701
|
mas01mc@292
|
702 pu = (Uns32T*)pf;
|
mas01mc@292
|
703
|
mas01mc@292
|
704 // Write the Z projectors r1[][]
|
mas01mc@292
|
705 for( x = 0 ; x < H::L ; x++)
|
mas01mc@292
|
706 for( y = 0 ; y < H::k ; y++)
|
mas01mc@292
|
707 *pu++ = r1[x][y];
|
mas01mc@292
|
708
|
mas01mc@292
|
709 // Write the Z projectors r2[][]
|
mas01mc@292
|
710 for( x = 0 ; x < H::L ; x++)
|
mas01mc@292
|
711 for( y = 0; y < H::k ; y++)
|
mas01mc@292
|
712 *pu++ = r2[x][y];
|
mas01mc@292
|
713
|
mas01mc@292
|
714 serial_munmap(db, get_serial_hashtable_offset());
|
mas01mc@292
|
715 return 1;
|
mas01mc@292
|
716 }
|
mas01mc@292
|
717
|
mas01mc@292
|
718 int G::serialize_lsh_hashtables_format1(int fid, int merge){
|
mas01mc@292
|
719 SerialElementT *pe, *pt;
|
mas01mc@292
|
720 Uns32T x,y;
|
mas01mc@292
|
721
|
mas01mc@292
|
722 if( merge && !serial_can_merge(O2_SERIAL_FILEFORMAT1) )
|
mas01mc@292
|
723 error("Cannot merge core and serial LSH, data structure dimensions mismatch.");
|
mas01mc@292
|
724
|
mas01mc@292
|
725 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
726 Uns32T colCount, meanColCount, colCountN, maxColCount, minColCount;
|
mas01mc@292
|
727 // Write the hash tables
|
mas01mc@292
|
728 for( x = 0 ; x < H::L ; x++ ){
|
mas01mc@292
|
729 std::cout << (merge ? "merging":"writing") << " hash table " << x << " FORMAT1...";
|
mas01mc@292
|
730 std::cout.flush();
|
mas01mc@292
|
731 // memory map a single hash table for sequential access
|
mas01mc@292
|
732 // Align each hash table to page boundary
|
mas01mc@292
|
733 char* dbtable = serial_mmap(fid, hashTableSize, 1,
|
mas01mc@292
|
734 align_up(get_serial_hashtable_offset()+x*hashTableSize, get_page_logn()));
|
mas01mc@292
|
735 if(madvise(dbtable, hashTableSize, MADV_SEQUENTIAL)<0)
|
mas01mc@292
|
736 error("could not advise hashtable memory","","madvise");
|
mas01mc@292
|
737
|
mas01mc@292
|
738 maxColCount=0;
|
mas01mc@292
|
739 minColCount=O2_SERIAL_MAX_COLS;
|
mas01mc@292
|
740 meanColCount=0;
|
mas01mc@292
|
741 colCountN=0;
|
mas01mc@292
|
742 pt=(SerialElementT*)dbtable;
|
mas01mc@292
|
743 for( y = 0 ; y < H::N ; y++ ){
|
mas01mc@292
|
744 // Move disk pointer to beginning of row
|
mas01mc@292
|
745 pe=pt+y*lshHeader->numCols;
|
mas01mc@292
|
746
|
mas01mc@292
|
747 colCount=0;
|
mas01mc@292
|
748 if(bucket* bPtr = h[x][y])
|
mas01mc@292
|
749 if(merge)
|
mas01mc@292
|
750 #ifdef LSH_BLOCK_FULL_ROWS
|
mas01mc@292
|
751 serial_merge_hashtable_row_format1(pe, bPtr->next, colCount); // skip collision counter bucket
|
mas01mc@292
|
752 else
|
mas01mc@292
|
753 serial_write_hashtable_row_format1(pe, bPtr->next, colCount); // skip collision counter bucket
|
mas01mc@292
|
754 #else
|
mas01mc@292
|
755 serial_merge_hashtable_row_format1(pe, bPtr, colCount);
|
mas01mc@292
|
756 else
|
mas01mc@292
|
757 serial_write_hashtable_row_format1(pe, bPtr, colCount);
|
mas01mc@292
|
758 #endif
|
mas01mc@292
|
759 if(colCount){
|
mas01mc@292
|
760 if(colCount<minColCount)
|
mas01mc@292
|
761 minColCount=colCount;
|
mas01mc@292
|
762 if(colCount>maxColCount)
|
mas01mc@292
|
763 maxColCount=colCount;
|
mas01mc@292
|
764 meanColCount+=colCount;
|
mas01mc@292
|
765 colCountN++;
|
mas01mc@292
|
766 }
|
mas01mc@292
|
767 }
|
mas01mc@292
|
768 if(colCountN)
|
mas01mc@292
|
769 std::cout << "#rows with collisions =" << colCountN << ", mean = " << meanColCount/(float)colCountN
|
mas01mc@292
|
770 << ", min = " << minColCount << ", max = " << maxColCount
|
mas01mc@292
|
771 << endl;
|
mas01mc@292
|
772 serial_munmap(dbtable, hashTableSize);
|
mas01mc@292
|
773 }
|
mas01mc@292
|
774
|
mas01mc@292
|
775 // We're done writing
|
mas01mc@292
|
776 return 1;
|
mas01mc@292
|
777 }
|
mas01mc@292
|
778
|
mas01mc@292
|
779 void G::serial_merge_hashtable_row_format1(SerialElementT* pr, bucket* b, Uns32T& colCount){
|
mas01mc@292
|
780 while(b && b->t2!=IFLAG){
|
mas01mc@292
|
781 SerialElementT*pe=pr; // reset disk pointer to beginning of row
|
mas01mc@292
|
782 serial_merge_element_format1(pe, b->snext, b->t2, colCount);
|
mas01mc@292
|
783 b=b->next;
|
mas01mc@292
|
784 }
|
mas01mc@292
|
785 }
|
mas01mc@292
|
786
|
mas01mc@292
|
787 void G::serial_merge_element_format1(SerialElementT* pe, sbucket* sb, Uns32T t2, Uns32T& colCount){
|
mas01mc@292
|
788 while(sb){
|
mas01mc@292
|
789 if(colCount==lshHeader->numCols){
|
mas01mc@292
|
790 std::cout << "!point-chain full " << endl;
|
mas01mc@292
|
791 return;
|
mas01mc@292
|
792 }
|
mas01mc@292
|
793 Uns32T c=0;
|
mas01mc@292
|
794 // Merge collision chains
|
mas01mc@292
|
795 while(c<lshHeader->numCols){
|
mas01mc@292
|
796 if( (pe+c)->hashValue==IFLAG){
|
mas01mc@292
|
797 (pe+c)->hashValue=t2;
|
mas01mc@292
|
798 (pe+c)->pointID=sb->pointID;
|
mas01mc@292
|
799 colCount=c+1;
|
mas01mc@292
|
800 if(c+1<lshHeader->numCols)
|
mas01mc@292
|
801 (pe+c+1)->hashValue=IFLAG;
|
mas01mc@292
|
802 break;
|
mas01mc@292
|
803 }
|
mas01mc@292
|
804 c++;
|
mas01mc@292
|
805 }
|
mas01mc@292
|
806 sb=sb->snext;
|
mas01mc@292
|
807 }
|
mas01mc@292
|
808 return;
|
mas01mc@292
|
809 }
|
mas01mc@292
|
810
|
mas01mc@292
|
811 void G::serial_write_hashtable_row_format1(SerialElementT*& pe, bucket* b, Uns32T& colCount){
|
mas01mc@292
|
812 pe->hashValue=IFLAG;
|
mas01mc@292
|
813 while(b && b->t2!=IFLAG){
|
mas01mc@292
|
814 serial_write_element_format1(pe, b->snext, b->t2, colCount);
|
mas01mc@292
|
815 b=b->next;
|
mas01mc@292
|
816 }
|
mas01mc@292
|
817 }
|
mas01mc@292
|
818
|
mas01mc@292
|
819 void G::serial_write_element_format1(SerialElementT*& pe, sbucket* sb, Uns32T t2, Uns32T& colCount){
|
mas01mc@292
|
820 while(sb){
|
mas01mc@292
|
821 if(colCount==lshHeader->numCols){
|
mas01mc@292
|
822 std::cout << "!point-chain full " << endl;
|
mas01mc@292
|
823 return;
|
mas01mc@292
|
824 }
|
mas01mc@292
|
825 pe->hashValue=t2;
|
mas01mc@292
|
826 pe->pointID=sb->pointID;
|
mas01mc@292
|
827 pe++;
|
mas01mc@292
|
828 colCount++;
|
mas01mc@292
|
829 sb=sb->snext;
|
mas01mc@292
|
830 }
|
mas01mc@292
|
831 pe->hashValue=IFLAG;
|
mas01mc@292
|
832 return;
|
mas01mc@292
|
833 }
|
mas01mc@292
|
834
|
mas01mc@292
|
835 int G::serialize_lsh_hashtables_format2(int fid, int merge){
|
mas01mc@292
|
836 Uns32T x,y;
|
mas01mc@292
|
837
|
mas01mc@292
|
838 if( merge && !serial_can_merge(O2_SERIAL_FILEFORMAT2) )
|
mas01mc@292
|
839 error("Cannot merge core and serial LSH, data structure dimensions mismatch.");
|
mas01mc@292
|
840
|
mas01mc@292
|
841 // We must pereform FORMAT1 merges in core
|
mas01mc@292
|
842 if(merge)
|
mas01mc@292
|
843 unserialize_lsh_hashtables_format2(fid);
|
mas01mc@292
|
844
|
mas01mc@292
|
845 Uns32T colCount, meanColCount, colCountN, maxColCount, minColCount, t1;
|
mas01mc@292
|
846 lseek(fid, get_serial_hashtable_offset(), SEEK_SET);
|
mas01mc@292
|
847
|
mas01mc@292
|
848 // Write the hash tables
|
mas01mc@292
|
849 for( x = 0 ; x < H::L ; x++ ){
|
mas01mc@292
|
850 std::cout << (merge ? "merging":"writing") << " hash table " << x << " FORMAT2...";
|
mas01mc@292
|
851 std::cout.flush();
|
mas01mc@292
|
852 maxColCount=0;
|
mas01mc@292
|
853 minColCount=O2_SERIAL_MAX_COLS;
|
mas01mc@292
|
854 meanColCount=0;
|
mas01mc@292
|
855 colCountN=0;
|
mas01mc@292
|
856 for( y = 0 ; y < H::N ; y++ ){
|
mas01mc@292
|
857 colCount=0;
|
mas01mc@292
|
858 if(bucket* bPtr = h[x][y]){
|
mas01mc@292
|
859 t1 = y | O2_SERIAL_FLAGS_T1_BIT;
|
mas01mc@292
|
860 if( write(fid, &t1, sizeof(Uns32T)) != sizeof(Uns32T) ){
|
mas01mc@292
|
861 close(fid);
|
mas01mc@292
|
862 error("write error in serial_write_hashtable_format2() [t1]");
|
mas01mc@292
|
863 }
|
mas01mc@292
|
864 #ifdef LSH_BLOCK_FULL_ROWS
|
mas01mc@292
|
865 serial_write_hashtable_row_format2(fid, bPtr->next, colCount); // skip collision counter bucket
|
mas01mc@292
|
866 #else
|
mas01mc@292
|
867 serial_write_hashtable_row_format2(fid, bPtr, colCount);
|
mas01mc@292
|
868 #endif
|
mas01mc@292
|
869 }
|
mas01mc@292
|
870 if(colCount){
|
mas01mc@292
|
871 if(colCount<minColCount)
|
mas01mc@292
|
872 minColCount=colCount;
|
mas01mc@292
|
873 if(colCount>maxColCount)
|
mas01mc@292
|
874 maxColCount=colCount;
|
mas01mc@292
|
875 meanColCount+=colCount;
|
mas01mc@292
|
876 colCountN++;
|
mas01mc@292
|
877 }
|
mas01mc@292
|
878 }
|
mas01mc@292
|
879 // Write END of table marker
|
mas01mc@292
|
880 t1 = O2_SERIAL_FLAGS_END_BIT;
|
mas01mc@292
|
881 if( write(fid, &t1, sizeof(Uns32T)) != sizeof(Uns32T) ){
|
mas01mc@292
|
882 close(fid);
|
mas01mc@292
|
883 error("write error in serial_write_hashtable_format2() [end]");
|
mas01mc@292
|
884 }
|
mas01mc@292
|
885
|
mas01mc@292
|
886 if(colCountN)
|
mas01mc@292
|
887 std::cout << "#rows with collisions =" << colCountN << ", mean = " << meanColCount/(float)colCountN
|
mas01mc@292
|
888 << ", min = " << minColCount << ", max = " << maxColCount
|
mas01mc@292
|
889 << endl;
|
mas01mc@292
|
890 }
|
mas01mc@292
|
891
|
mas01mc@292
|
892 // We're done writing
|
mas01mc@292
|
893 return 1;
|
mas01mc@292
|
894 }
|
mas01mc@292
|
895
|
mas01mc@292
|
896 void G::serial_write_hashtable_row_format2(int fid, bucket* b, Uns32T& colCount){
|
mas01mc@292
|
897 while(b && b->t2!=IFLAG){
|
mas01mc@292
|
898 t2 = O2_SERIAL_FLAGS_T2_BIT;
|
mas01mc@292
|
899 if( write(fid, &t2, sizeof(Uns32T)) != sizeof(Uns32T) ){
|
mas01mc@292
|
900 close(fid);
|
mas01mc@292
|
901 error("write error in serial_write_hashtable_row_format2()");
|
mas01mc@292
|
902 }
|
mas01mc@292
|
903 t2 = b->t2;
|
mas01mc@292
|
904 if( write(fid, &t2, sizeof(Uns32T)) != sizeof(Uns32T) ){
|
mas01mc@292
|
905 close(fid);
|
mas01mc@292
|
906 error("write error in serial_write_hashtable_row_format2()");
|
mas01mc@292
|
907 }
|
mas01mc@292
|
908 serial_write_element_format2(fid, b->snext, colCount);
|
mas01mc@292
|
909 b=b->next;
|
mas01mc@292
|
910 }
|
mas01mc@292
|
911 }
|
mas01mc@292
|
912
|
mas01mc@292
|
913 void G::serial_write_element_format2(int fid, sbucket* sb, Uns32T& colCount){
|
mas01mc@292
|
914 while(sb){
|
mas01mc@292
|
915 if(write(fid, &sb->pointID, sizeof(Uns32T))!=sizeof(Uns32T)){
|
mas01mc@292
|
916 close(fid);
|
mas01mc@292
|
917 error("Write error in serial_write_element_format2()");
|
mas01mc@292
|
918 }
|
mas01mc@292
|
919 colCount++;
|
mas01mc@292
|
920 sb=sb->snext;
|
mas01mc@292
|
921 }
|
mas01mc@292
|
922 }
|
mas01mc@292
|
923
|
mas01mc@292
|
924
|
mas01mc@292
|
925 int G::serial_create(char* filename, Uns32T FMT){
|
mas01mc@292
|
926 return serial_create(filename, w, L, N, C, k, d, FMT);
|
mas01mc@292
|
927 }
|
mas01mc@292
|
928
|
mas01mc@292
|
929
|
mas01mc@292
|
930 int G::serial_create(char* filename, float binWidth, Uns32T numTables, Uns32T numRows, Uns32T numCols,
|
mas01mc@292
|
931 Uns32T numFuns, Uns32T dim, Uns32T FMT){
|
mas01mc@292
|
932
|
mas01mc@292
|
933 if(numTables > O2_SERIAL_MAX_TABLES || numRows > O2_SERIAL_MAX_ROWS
|
mas01mc@292
|
934 || numCols > O2_SERIAL_MAX_COLS || numFuns > O2_SERIAL_MAX_FUNS
|
mas01mc@292
|
935 || dim>O2_SERIAL_MAX_DIM){
|
mas01mc@292
|
936 error("LSH parameters out of bounds for serialization");
|
mas01mc@292
|
937 }
|
mas01mc@292
|
938
|
mas01mc@292
|
939 int dbfid;
|
mas01mc@292
|
940 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
|
941 error("Can't create serial file", filename, "open");
|
mas01mc@292
|
942 get_lock(dbfid, 1);
|
mas01mc@292
|
943
|
mas01mc@292
|
944 // Make header first to get size of serialized database
|
mas01mc@292
|
945 lshHeader = new SerialHeaderT(binWidth, numTables, numRows, numCols, numFuns, dim, radius, maxp, FMT);
|
mas01mc@292
|
946
|
mas01mc@292
|
947 // go to the location corresponding to the last byte
|
mas01mc@292
|
948 if (lseek (dbfid, lshHeader->get_size() - 1, SEEK_SET) == -1)
|
mas01mc@292
|
949 error("lseek error in db file", "", "lseek");
|
mas01mc@292
|
950
|
mas01mc@292
|
951 // write a dummy byte at the last location
|
mas01mc@292
|
952 if (write (dbfid, "", 1) != 1)
|
mas01mc@292
|
953 error("write error", "", "write");
|
mas01mc@292
|
954
|
mas01mc@292
|
955 db = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 1);
|
mas01mc@292
|
956
|
mas01mc@292
|
957 memcpy (db, lshHeader, O2_SERIAL_HEADER_SIZE);
|
mas01mc@292
|
958
|
mas01mc@292
|
959 serial_munmap(db, O2_SERIAL_HEADER_SIZE);
|
mas01mc@292
|
960
|
mas01mc@292
|
961 close(dbfid);
|
mas01mc@292
|
962
|
mas01mc@292
|
963 std::cout << "done initializing tables." << endl;
|
mas01mc@292
|
964
|
mas01mc@292
|
965 return 1;
|
mas01mc@292
|
966 }
|
mas01mc@292
|
967
|
mas01mc@292
|
968 char* G::serial_mmap(int dbfid, Uns32T memSize, Uns32T forWrite, off_t offset){
|
mas01mc@292
|
969 if(forWrite){
|
mas01mc@292
|
970 if ((db = (char*) mmap(0, memSize, PROT_READ | PROT_WRITE,
|
mas01mc@292
|
971 MAP_SHARED, dbfid, offset)) == (caddr_t) -1)
|
mas01mc@292
|
972 error("mmap error in request for writable serialized database", "", "mmap");
|
mas01mc@292
|
973 }
|
mas01mc@292
|
974 else if ((db = (char*) mmap(0, memSize, PROT_READ, MAP_SHARED, dbfid, offset)) == (caddr_t) -1)
|
mas01mc@292
|
975 error("mmap error in read-only serialized database", "", "mmap");
|
mas01mc@292
|
976
|
mas01mc@292
|
977 return db;
|
mas01mc@292
|
978 }
|
mas01mc@292
|
979
|
mas01mc@292
|
980 SerialHeaderT* G::serial_get_header(char* db){
|
mas01mc@292
|
981 lshHeader = new SerialHeaderT();
|
mas01mc@292
|
982 memcpy((char*)lshHeader, db, sizeof(SerialHeaderT));
|
mas01mc@292
|
983
|
mas01mc@292
|
984 if(lshHeader->lshMagic!=O2_SERIAL_MAGIC)
|
mas01mc@292
|
985 error("Not an LSH database file");
|
mas01mc@292
|
986
|
mas01mc@292
|
987 return lshHeader;
|
mas01mc@292
|
988 }
|
mas01mc@292
|
989
|
mas01mc@292
|
990 void G::serial_munmap(char* db, Uns32T N){
|
mas01mc@292
|
991 munmap(db, N);
|
mas01mc@292
|
992 }
|
mas01mc@292
|
993
|
mas01mc@292
|
994 int G::serial_open(char* filename, int writeFlag){
|
mas01mc@292
|
995 int dbfid;
|
mas01mc@292
|
996 if(writeFlag){
|
mas01mc@292
|
997 if ((dbfid = open (filename, O_RDWR)) < 0)
|
mas01mc@292
|
998 error("Can't open serial file for read/write", filename, "open");
|
mas01mc@292
|
999 get_lock(dbfid, writeFlag);
|
mas01mc@292
|
1000 }
|
mas01mc@292
|
1001 else{
|
mas01mc@292
|
1002 if ((dbfid = open (filename, O_RDONLY)) < 0)
|
mas01mc@292
|
1003 error("Can't open serial file for read", filename, "open");
|
mas01mc@292
|
1004 get_lock(dbfid, 0);
|
mas01mc@292
|
1005 }
|
mas01mc@292
|
1006
|
mas01mc@292
|
1007 return dbfid;
|
mas01mc@292
|
1008 }
|
mas01mc@292
|
1009
|
mas01mc@292
|
1010 void G::serial_close(int dbfid){
|
mas01mc@292
|
1011
|
mas01mc@292
|
1012 release_lock(dbfid);
|
mas01mc@292
|
1013 close(dbfid);
|
mas01mc@292
|
1014 }
|
mas01mc@292
|
1015
|
mas01mc@292
|
1016 int G::unserialize_lsh_header(char* filename){
|
mas01mc@292
|
1017
|
mas01mc@292
|
1018 int dbfid;
|
mas01mc@292
|
1019 char* db;
|
mas01mc@292
|
1020 // Test to see if file exists
|
mas01mc@292
|
1021 if((dbfid = open (filename, O_RDONLY)) < 0)
|
mas01mc@292
|
1022 error("Can't open the file", filename, "open");
|
mas01mc@292
|
1023 close(dbfid);
|
mas01mc@292
|
1024 dbfid = serial_open(filename, 0); // open for read
|
mas01mc@292
|
1025 db = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 0);// get database pointer
|
mas01mc@292
|
1026 serial_get_header(db); // read header
|
mas01mc@292
|
1027 serial_munmap(db, O2_SERIAL_HEADER_SIZE); // drop mmap
|
mas01mc@292
|
1028
|
mas01mc@292
|
1029 // Unserialize header parameters
|
mas01mc@292
|
1030 H::L = lshHeader->numTables;
|
mas01mc@292
|
1031 H::m = (Uns32T)( (1.0 + sqrt(1 + 8.0*(int)H::L)) / 2.0);
|
mas01mc@292
|
1032 H::N = lshHeader->numRows;
|
mas01mc@292
|
1033 H::C = lshHeader->numCols;
|
mas01mc@292
|
1034 H::k = lshHeader->numFuns;
|
mas01mc@292
|
1035 H::d = lshHeader->dataDim;
|
mas01mc@292
|
1036 G::w = lshHeader->binWidth;
|
mas01mc@292
|
1037 G::radius = lshHeader->radius;
|
mas01mc@292
|
1038 G::maxp = lshHeader->maxp;
|
mas01mc@292
|
1039
|
mas01mc@292
|
1040 return dbfid;
|
mas01mc@292
|
1041 }
|
mas01mc@292
|
1042
|
mas01mc@292
|
1043 // unserialize the LSH parameters
|
mas01mc@292
|
1044 // we leave the LSH tree on disk as a flat file
|
mas01mc@292
|
1045 // it is this flat file that we search by memory mapping
|
mas01mc@292
|
1046 void G::unserialize_lsh_functions(int dbfid){
|
mas01mc@292
|
1047 Uns32T j, kk;
|
mas01mc@292
|
1048 float* pf;
|
mas01mc@292
|
1049 Uns32T* pu;
|
mas01mc@292
|
1050
|
mas01mc@292
|
1051 // Load the hash functions into core
|
mas01mc@292
|
1052 char* db = serial_mmap(dbfid, get_serial_hashtable_offset(), 0);// get database pointer again
|
mas01mc@292
|
1053
|
mas01mc@292
|
1054 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
1055 G::A = new float**[ H::m ]; // m x k x d random projectors
|
mas01mc@292
|
1056 G::b = new float*[ H::m ]; // m x k random biases
|
mas01mc@292
|
1057 #else
|
mas01mc@292
|
1058 G::A = new float**[ H::L ]; // m x k x d random projectors
|
mas01mc@292
|
1059 G::b = new float*[ H::L ]; // m x k random biases
|
mas01mc@292
|
1060 #endif
|
mas01mc@292
|
1061 G::g = new Uns32T*[ H::L ]; // L x k random projections
|
mas01mc@292
|
1062 assert(g&&A&&b); // failure
|
mas01mc@292
|
1063
|
mas01mc@292
|
1064 pf = get_serial_hashfunction_base(db);
|
mas01mc@292
|
1065
|
mas01mc@292
|
1066 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
1067 for( j = 0 ; j < H::m ; j++ ){ // L functions gj(v)
|
mas01mc@292
|
1068 G::A[j] = new float*[ H::k/2 ]; // k x d 2-stable distribution coefficients
|
mas01mc@292
|
1069 G::b[j] = new float[ H::k/2 ]; // bias
|
mas01mc@292
|
1070 assert( G::A[j] && G::b[j] ); // failure
|
mas01mc@292
|
1071 for( kk = 0 ; kk < H::k/2 ; kk++ ){ // Normally distributed hash functions
|
mas01mc@292
|
1072 #else
|
mas01mc@292
|
1073 for( j = 0 ; j < H::L ; j++ ){ // L functions gj(v)
|
mas01mc@292
|
1074 G::A[j] = new float*[ H::k ]; // k x d 2-stable distribution coefficients
|
mas01mc@292
|
1075 G::b[j] = new float[ H::k ]; // bias
|
mas01mc@292
|
1076 assert( G::A[j] && G::b[j] ); // failure
|
mas01mc@292
|
1077 for( kk = 0 ; kk < H::k ; kk++ ){ // Normally distributed hash functions
|
mas01mc@292
|
1078 #endif
|
mas01mc@292
|
1079 G::A[j][kk] = new float[ H::d ];
|
mas01mc@292
|
1080 assert( G::A[j][kk] ); // failure
|
mas01mc@292
|
1081 for(Uns32T i = 0 ; i < H::d ; i++ )
|
mas01mc@292
|
1082 G::A[j][kk][i] = *pf++; // Normally distributed random vectors
|
mas01mc@292
|
1083 }
|
mas01mc@292
|
1084 }
|
mas01mc@292
|
1085 #ifdef USE_U_FUNCTIONS
|
mas01mc@292
|
1086 for( j = 0 ; j < H::m ; j++ ) // biases b
|
mas01mc@292
|
1087 for( kk = 0 ; kk < H::k/2 ; kk++ )
|
mas01mc@292
|
1088 #else
|
mas01mc@292
|
1089 for( j = 0 ; j < H::L ; j++ ) // biases b
|
mas01mc@292
|
1090 for( kk = 0 ; kk < H::k ; kk++ )
|
mas01mc@292
|
1091 #endif
|
mas01mc@292
|
1092 G::b[j][kk] = *pf++;
|
mas01mc@292
|
1093
|
mas01mc@292
|
1094 for( j = 0 ; j < H::L ; j++){ // 32-bit hash values, gj(v)=[x0 x1 ... xk-1] xk \in Z
|
mas01mc@292
|
1095 G::g[j] = new Uns32T[ H::k ];
|
mas01mc@292
|
1096 assert( G::g[j] );
|
mas01mc@292
|
1097 }
|
mas01mc@292
|
1098
|
mas01mc@292
|
1099
|
mas01mc@292
|
1100 H::__initialize_data_structures();
|
mas01mc@292
|
1101
|
mas01mc@292
|
1102 pu = (Uns32T*)pf;
|
mas01mc@292
|
1103 for( j = 0 ; j < H::L ; j++ ) // Z projectors r1
|
mas01mc@292
|
1104 for( kk = 0 ; kk < H::k ; kk++ )
|
mas01mc@292
|
1105 H::r1[j][kk] = *pu++;
|
mas01mc@292
|
1106
|
mas01mc@292
|
1107 for( j = 0 ; j < H::L ; j++ ) // Z projectors r2
|
mas01mc@292
|
1108 for( kk = 0 ; kk < H::k ; kk++ )
|
mas01mc@292
|
1109 H::r2[j][kk] = *pu++;
|
mas01mc@292
|
1110
|
mas01mc@292
|
1111 serial_munmap(db, get_serial_hashtable_offset());
|
mas01mc@292
|
1112 }
|
mas01mc@292
|
1113
|
mas01mc@292
|
1114 void G::unserialize_lsh_hashtables_format1(int fid){
|
mas01mc@292
|
1115 SerialElementT *pe, *pt;
|
mas01mc@292
|
1116 Uns32T x,y;
|
mas01mc@292
|
1117 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
1118 // Read the hash tables into core
|
mas01mc@292
|
1119 for( x = 0 ; x < H::L ; x++ ){
|
mas01mc@292
|
1120 // memory map a single hash table
|
mas01mc@292
|
1121 // Align each hash table to page boundary
|
mas01mc@292
|
1122 char* dbtable = serial_mmap(fid, hashTableSize, 0,
|
mas01mc@292
|
1123 align_up(get_serial_hashtable_offset()+x*hashTableSize, get_page_logn()));
|
mas01mc@292
|
1124 if(madvise(dbtable, hashTableSize, MADV_SEQUENTIAL)<0)
|
mas01mc@292
|
1125 error("could not advise hashtable memory","","madvise");
|
mas01mc@292
|
1126 pt=(SerialElementT*)dbtable;
|
mas01mc@292
|
1127 for( y = 0 ; y < H::N ; y++ ){
|
mas01mc@292
|
1128 // Move disk pointer to beginning of row
|
mas01mc@292
|
1129 pe=pt+y*lshHeader->numCols;
|
mas01mc@292
|
1130 unserialize_hashtable_row_format1(pe, h[x]+y);
|
mas01mc@292
|
1131 #ifdef __LSH_DUMP_CORE_TABLES__
|
mas01mc@292
|
1132 printf("S[%d,%d]", x, y);
|
mas01mc@292
|
1133 serial_bucket_dump(pe);
|
mas01mc@292
|
1134 printf("C[%d,%d]", x, y);
|
mas01mc@292
|
1135 dump_hashtable_row(h[x][y]);
|
mas01mc@292
|
1136 #endif
|
mas01mc@292
|
1137 }
|
mas01mc@292
|
1138 serial_munmap(dbtable, hashTableSize);
|
mas01mc@292
|
1139 }
|
mas01mc@292
|
1140 }
|
mas01mc@292
|
1141
|
mas01mc@292
|
1142 void G::unserialize_hashtable_row_format1(SerialElementT* pe, bucket** b){
|
mas01mc@292
|
1143 Uns32T colCount = 0;
|
mas01mc@292
|
1144 while(colCount!=lshHeader->numCols && pe->hashValue !=IFLAG){
|
mas01mc@292
|
1145 H::p = pe->pointID; // current point ID
|
mas01mc@292
|
1146 t2 = pe->hashValue;
|
mas01mc@292
|
1147 bucket_insert_point(b);
|
mas01mc@292
|
1148 pe++;
|
mas01mc@292
|
1149 colCount++;
|
mas01mc@292
|
1150 }
|
mas01mc@292
|
1151 }
|
mas01mc@292
|
1152
|
mas01mc@292
|
1153 void G::unserialize_lsh_hashtables_format2(int fid){
|
mas01mc@292
|
1154 Uns32T x=0,y=0;
|
mas01mc@292
|
1155
|
mas01mc@292
|
1156 // Seek to hashtable base offset
|
mas01mc@292
|
1157 if(lseek(fid, get_serial_hashtable_offset(), SEEK_SET)!=get_serial_hashtable_offset()){
|
mas01mc@292
|
1158 close(fid);
|
mas01mc@292
|
1159 error("Seek error in unserialize_lsh_hashtables_format2");
|
mas01mc@292
|
1160 }
|
mas01mc@292
|
1161
|
mas01mc@292
|
1162 // Read the hash tables into core (structure is given in header)
|
mas01mc@292
|
1163 while( x < H::L){
|
mas01mc@292
|
1164 if(read(fid, &(H::t1), sizeof(Uns32T))!=sizeof(Uns32T)){
|
mas01mc@292
|
1165 close(fid);
|
mas01mc@292
|
1166 error("Read error","unserialize_lsh_hashtables_format2()");
|
mas01mc@292
|
1167 }
|
mas01mc@292
|
1168 if(H::t1&O2_SERIAL_FLAGS_END_BIT)
|
mas01mc@292
|
1169 x++; // End of table
|
mas01mc@292
|
1170 else
|
mas01mc@292
|
1171 while(y < H::N){
|
mas01mc@292
|
1172 // Read a row and move file pointer to beginning of next row or table
|
mas01mc@292
|
1173 if(!(H::t1&O2_SERIAL_FLAGS_T1_BIT)){
|
mas01mc@292
|
1174 close(fid);
|
mas01mc@292
|
1175 error("State matchine error t1","unserialize_lsh_hashtables_format2()");
|
mas01mc@292
|
1176 }
|
mas01mc@292
|
1177 y = H::t1 ^ O2_SERIAL_FLAGS_T1_BIT;
|
mas01mc@292
|
1178 if(y>=H::N){
|
mas01mc@292
|
1179 close(fid);
|
mas01mc@292
|
1180 error("Unserialized hashtable row pointer out of range","unserialize_lsh_hashtables_format2()");
|
mas01mc@292
|
1181 }
|
mas01mc@292
|
1182 Uns32T token = unserialize_hashtable_row_format2(fid, h[x]+y);
|
mas01mc@292
|
1183
|
mas01mc@292
|
1184 #ifdef __LSH_DUMP_CORE_TABLES__
|
mas01mc@292
|
1185 printf("C[%d,%d]", x, y);
|
mas01mc@292
|
1186 dump_hashtable_row(h[x][y]);
|
mas01mc@292
|
1187 #endif
|
mas01mc@292
|
1188 // Check that token is valid
|
mas01mc@292
|
1189 if( !(token&O2_SERIAL_FLAGS_T1_BIT || token&O2_SERIAL_FLAGS_END_BIT) ){
|
mas01mc@292
|
1190 close(fid);
|
mas01mc@292
|
1191 error("State machine error end of row/table", "unserialize_lsh_hashtables_format2()");
|
mas01mc@292
|
1192 }
|
mas01mc@292
|
1193 // Check for end of table flag
|
mas01mc@292
|
1194 if(token&O2_SERIAL_FLAGS_END_BIT){
|
mas01mc@292
|
1195 x++;
|
mas01mc@292
|
1196 break;
|
mas01mc@292
|
1197 }
|
mas01mc@292
|
1198 // Check for new row flag
|
mas01mc@292
|
1199 if(token&O2_SERIAL_FLAGS_T1_BIT)
|
mas01mc@292
|
1200 H::t1 = token;
|
mas01mc@292
|
1201 }
|
mas01mc@292
|
1202 }
|
mas01mc@292
|
1203 }
|
mas01mc@292
|
1204
|
mas01mc@292
|
1205 Uns32T G::unserialize_hashtable_row_format2(int fid, bucket** b){
|
mas01mc@292
|
1206 bool pointFound = false;
|
mas01mc@292
|
1207 if(read(fid, &(H::t2), sizeof(Uns32T)) != sizeof(Uns32T)){
|
mas01mc@292
|
1208 close(fid);
|
mas01mc@292
|
1209 error("Read error T2 token","unserialize_hashtable_row_format2");
|
mas01mc@292
|
1210 }
|
mas01mc@292
|
1211 if( !(H::t2==O2_SERIAL_FLAGS_END_BIT || H::t2==O2_SERIAL_FLAGS_T2_BIT)){
|
mas01mc@292
|
1212 close(fid);
|
mas01mc@292
|
1213 error("State machine error: expected E or T2");
|
mas01mc@292
|
1214 }
|
mas01mc@292
|
1215 while(!(H::t2==O2_SERIAL_FLAGS_END_BIT || H::t2&O2_SERIAL_FLAGS_T1_BIT)){
|
mas01mc@292
|
1216 pointFound=false;
|
mas01mc@292
|
1217 // Check for T2 token
|
mas01mc@292
|
1218 if(H::t2!=O2_SERIAL_FLAGS_T2_BIT)
|
mas01mc@292
|
1219 error("State machine error T2 token", "unserialize_hashtable_row_format2()");
|
mas01mc@292
|
1220 // Read t2 value
|
mas01mc@292
|
1221 if(read(fid, &(H::t2), sizeof(Uns32T)) != sizeof(Uns32T)){
|
mas01mc@292
|
1222 close(fid);
|
mas01mc@292
|
1223 error("Read error t2","unserialize_hashtable_row_format2");
|
mas01mc@292
|
1224 }
|
mas01mc@292
|
1225 if(read(fid, &(H::p), sizeof(Uns32T)) != sizeof(Uns32T)){
|
mas01mc@292
|
1226 close(fid);
|
mas01mc@292
|
1227 error("Read error H::p","unserialize_hashtable_row_format2");
|
mas01mc@292
|
1228 }
|
mas01mc@292
|
1229 while(!(H::p==O2_SERIAL_FLAGS_END_BIT || H::p&O2_SERIAL_FLAGS_T1_BIT || H::p==O2_SERIAL_FLAGS_T2_BIT )){
|
mas01mc@292
|
1230 pointFound=true;
|
mas01mc@292
|
1231 bucket_insert_point(b);
|
mas01mc@292
|
1232 if(read(fid, &(H::p), sizeof(Uns32T)) != sizeof(Uns32T)){
|
mas01mc@292
|
1233 close(fid);
|
mas01mc@292
|
1234 error("Read error H::p","unserialize_hashtable_row_format2");
|
mas01mc@292
|
1235 }
|
mas01mc@292
|
1236 }
|
mas01mc@292
|
1237 H::t2 = H::p; // Copy last found token to t2
|
mas01mc@292
|
1238 if(!pointFound)
|
mas01mc@292
|
1239 error("State machine error: point", "unserialize_hashtable_row_format2()");
|
mas01mc@292
|
1240 }
|
mas01mc@292
|
1241 return H::t2; // holds current token
|
mas01mc@292
|
1242 }
|
mas01mc@292
|
1243
|
mas01mc@292
|
1244 void G::dump_hashtable_row(bucket* p){
|
mas01mc@292
|
1245 while(p && p->t2!=IFLAG){
|
mas01mc@292
|
1246 sbucket* sbp = p->snext;
|
mas01mc@292
|
1247 while(sbp){
|
mas01mc@292
|
1248 printf("(%0X,%u)", p->t2, sbp->pointID);
|
mas01mc@292
|
1249 fflush(stdout);
|
mas01mc@292
|
1250 sbp=sbp->snext;
|
mas01mc@292
|
1251 }
|
mas01mc@292
|
1252 p=p->next;
|
mas01mc@292
|
1253 }
|
mas01mc@292
|
1254 printf("\n");
|
mas01mc@292
|
1255 }
|
mas01mc@292
|
1256
|
mas01mc@292
|
1257
|
mas01mc@292
|
1258 // G::serial_retrieve_point( ... )
|
mas01mc@292
|
1259 // retrieves (pointID) from a serialized LSH database
|
mas01mc@292
|
1260 //
|
mas01mc@292
|
1261 // inputs:
|
mas01mc@292
|
1262 // filename - file name of serialized LSH database
|
mas01mc@292
|
1263 // vv - query point set
|
mas01mc@292
|
1264 //
|
mas01mc@292
|
1265 // outputs:
|
mas01mc@292
|
1266 // inserts retrieved points into add_point() callback method
|
mas01mc@292
|
1267 void G::serial_retrieve_point_set(char* filename, vector<vector<float> >& vv, ReporterCallbackPtr add_point, void* caller)
|
mas01mc@292
|
1268 {
|
mas01mc@292
|
1269 int dbfid = serial_open(filename, 0); // open for read
|
mas01mc@292
|
1270 char* dbheader = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 0);// get database pointer
|
mas01mc@292
|
1271 serial_get_header(dbheader); // read header
|
mas01mc@292
|
1272 serial_munmap(dbheader, O2_SERIAL_HEADER_SIZE); // drop header mmap
|
mas01mc@292
|
1273
|
mas01mc@292
|
1274 if((lshHeader->flags & O2_SERIAL_FILEFORMAT2)){
|
mas01mc@292
|
1275 close(dbfid);
|
mas01mc@292
|
1276 error("serial_retrieve_point_set is for SERIAL_FILEFORMAT1 only");
|
mas01mc@292
|
1277 }
|
mas01mc@292
|
1278
|
mas01mc@292
|
1279 // size of each hash table
|
mas01mc@292
|
1280 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
1281 calling_instance = caller; // class instance variable used in ...bucket_chain_point()
|
mas01mc@292
|
1282 add_point_callback = add_point;
|
mas01mc@292
|
1283
|
mas01mc@292
|
1284 for(Uns32T j=0; j<L; j++){
|
mas01mc@292
|
1285 // memory map a single hash table for random access
|
mas01mc@292
|
1286 char* db = serial_mmap(dbfid, hashTableSize, 0,
|
mas01mc@292
|
1287 align_up(get_serial_hashtable_offset()+j*hashTableSize,get_page_logn()));
|
mas01mc@292
|
1288 if(madvise(db, hashTableSize, MADV_RANDOM)<0)
|
mas01mc@292
|
1289 error("could not advise local hashtable memory","","madvise");
|
mas01mc@292
|
1290 SerialElementT* pe = (SerialElementT*)db ;
|
mas01mc@292
|
1291 for(Uns32T qpos=0; qpos<vv.size(); qpos++){
|
mas01mc@292
|
1292 compute_hash_functions(vv[qpos]);
|
mas01mc@292
|
1293 __generate_hash_keys(*(g+j),*(r1+j),*(r2+j));
|
mas01mc@292
|
1294 serial_bucket_chain_point(pe+t1*lshHeader->numCols, qpos); // Point to correct row
|
mas01mc@292
|
1295 }
|
mas01mc@292
|
1296 serial_munmap(db, hashTableSize); // drop hashtable mmap
|
mas01mc@292
|
1297 }
|
mas01mc@292
|
1298 serial_close(dbfid);
|
mas01mc@292
|
1299 }
|
mas01mc@292
|
1300
|
mas01mc@292
|
1301 void G::serial_retrieve_point(char* filename, vector<float>& v, Uns32T qpos, ReporterCallbackPtr add_point, void* caller){
|
mas01mc@292
|
1302 int dbfid = serial_open(filename, 0); // open for read
|
mas01mc@292
|
1303 char* dbheader = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 0);// get database pointer
|
mas01mc@292
|
1304 serial_get_header(dbheader); // read header
|
mas01mc@292
|
1305 serial_munmap(dbheader, O2_SERIAL_HEADER_SIZE); // drop header mmap
|
mas01mc@292
|
1306
|
mas01mc@292
|
1307 if((lshHeader->flags & O2_SERIAL_FILEFORMAT2)){
|
mas01mc@292
|
1308 close(dbfid);
|
mas01mc@292
|
1309 error("serial_retrieve_point is for SERIAL_FILEFORMAT1 only");
|
mas01mc@292
|
1310 }
|
mas01mc@292
|
1311
|
mas01mc@292
|
1312 // size of each hash table
|
mas01mc@292
|
1313 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
1314 calling_instance = caller;
|
mas01mc@292
|
1315 add_point_callback = add_point;
|
mas01mc@292
|
1316 compute_hash_functions(v);
|
mas01mc@292
|
1317 for(Uns32T j=0; j<L; j++){
|
mas01mc@292
|
1318 // memory map a single hash table for random access
|
mas01mc@292
|
1319 char* db = serial_mmap(dbfid, hashTableSize, 0,
|
mas01mc@292
|
1320 align_up(get_serial_hashtable_offset()+j*hashTableSize,get_page_logn()));
|
mas01mc@292
|
1321 if(madvise(db, hashTableSize, MADV_RANDOM)<0)
|
mas01mc@292
|
1322 error("could not advise local hashtable memory","","madvise");
|
mas01mc@292
|
1323 SerialElementT* pe = (SerialElementT*)db ;
|
mas01mc@292
|
1324 __generate_hash_keys(*(g+j),*(r1+j),*(r2+j));
|
mas01mc@292
|
1325 serial_bucket_chain_point(pe+t1*lshHeader->numCols, qpos); // Point to correct row
|
mas01mc@292
|
1326 serial_munmap(db, hashTableSize); // drop hashtable mmap
|
mas01mc@292
|
1327 }
|
mas01mc@292
|
1328 serial_close(dbfid);
|
mas01mc@292
|
1329 }
|
mas01mc@292
|
1330
|
mas01mc@292
|
1331 void G::serial_dump_tables(char* filename){
|
mas01mc@292
|
1332 int dbfid = serial_open(filename, 0); // open for read
|
mas01mc@292
|
1333 char* dbheader = serial_mmap(dbfid, O2_SERIAL_HEADER_SIZE, 0);// get database pointer
|
mas01mc@292
|
1334 serial_get_header(dbheader); // read header
|
mas01mc@292
|
1335 serial_munmap(dbheader, O2_SERIAL_HEADER_SIZE); // drop header mmap
|
mas01mc@292
|
1336 Uns32T hashTableSize=sizeof(SerialElementT)*lshHeader->numRows*lshHeader->numCols;
|
mas01mc@292
|
1337 for(Uns32T j=0; j<L; j++){
|
mas01mc@292
|
1338 // memory map a single hash table for random access
|
mas01mc@292
|
1339 char* db = serial_mmap(dbfid, hashTableSize, 0,
|
mas01mc@292
|
1340 align_up(get_serial_hashtable_offset()+j*hashTableSize,get_page_logn()));
|
mas01mc@292
|
1341 if(madvise(db, hashTableSize, MADV_SEQUENTIAL)<0)
|
mas01mc@292
|
1342 error("could not advise local hashtable memory","","madvise");
|
mas01mc@292
|
1343 SerialElementT* pe = (SerialElementT*)db ;
|
mas01mc@292
|
1344 printf("*********** TABLE %d ***************\n", j);
|
mas01mc@292
|
1345 fflush(stdout);
|
mas01mc@292
|
1346 int count=0;
|
mas01mc@292
|
1347 do{
|
mas01mc@292
|
1348 printf("[%d,%d]", j, count++);
|
mas01mc@292
|
1349 fflush(stdout);
|
mas01mc@292
|
1350 serial_bucket_dump(pe);
|
mas01mc@292
|
1351 pe+=lshHeader->numCols;
|
mas01mc@292
|
1352 }while(pe<(SerialElementT*)db+lshHeader->numRows*lshHeader->numCols);
|
mas01mc@292
|
1353 }
|
mas01mc@292
|
1354
|
mas01mc@292
|
1355 }
|
mas01mc@292
|
1356
|
mas01mc@292
|
1357 void G::serial_bucket_dump(SerialElementT* pe){
|
mas01mc@292
|
1358 SerialElementT* pend = pe+lshHeader->numCols;
|
mas01mc@292
|
1359 while( !(pe->hashValue==IFLAG || pe==pend ) ){
|
mas01mc@292
|
1360 printf("(%0X,%u)",pe->hashValue,pe->pointID);
|
mas01mc@292
|
1361 pe++;
|
mas01mc@292
|
1362 }
|
mas01mc@292
|
1363 printf("\n");
|
mas01mc@292
|
1364 fflush(stdout);
|
mas01mc@292
|
1365 }
|
mas01mc@292
|
1366
|
mas01mc@292
|
1367 void G::serial_bucket_chain_point(SerialElementT* pe, Uns32T qpos){
|
mas01mc@292
|
1368 SerialElementT* pend = pe+lshHeader->numCols;
|
mas01mc@292
|
1369 while( !(pe->hashValue==IFLAG || pe==pend ) ){
|
mas01mc@292
|
1370 if(pe->hashValue==t2){ // new match
|
mas01mc@292
|
1371 add_point_callback(calling_instance, pe->pointID, qpos, radius);
|
mas01mc@292
|
1372 }
|
mas01mc@292
|
1373 pe++;
|
mas01mc@292
|
1374 }
|
mas01mc@292
|
1375 }
|
mas01mc@292
|
1376
|
mas01mc@292
|
1377 void G::bucket_chain_point(bucket* p, Uns32T qpos){
|
mas01mc@292
|
1378 if(!p || p->t2==IFLAG)
|
mas01mc@292
|
1379 return;
|
mas01mc@292
|
1380 if(p->t2==t2){ // match
|
mas01mc@292
|
1381 sbucket_chain_point(p->snext, qpos); // add to reporter
|
mas01mc@292
|
1382 }
|
mas01mc@292
|
1383 if(p->next){
|
mas01mc@292
|
1384 bucket_chain_point(p->next, qpos); // recurse
|
mas01mc@292
|
1385 }
|
mas01mc@292
|
1386 }
|
mas01mc@292
|
1387
|
mas01mc@292
|
1388 void G::sbucket_chain_point(sbucket* p, Uns32T qpos){
|
mas01mc@292
|
1389 add_point_callback(calling_instance, p->pointID, qpos, radius);
|
mas01mc@292
|
1390 if(p->snext){
|
mas01mc@292
|
1391 sbucket_chain_point(p->snext, qpos);
|
mas01mc@292
|
1392 }
|
mas01mc@292
|
1393 }
|
mas01mc@292
|
1394
|
mas01mc@292
|
1395 void G::get_lock(int fd, bool exclusive) {
|
mas01mc@292
|
1396 struct flock lock;
|
mas01mc@292
|
1397 int status;
|
mas01mc@292
|
1398 lock.l_type = exclusive ? F_WRLCK : F_RDLCK;
|
mas01mc@292
|
1399 lock.l_whence = SEEK_SET;
|
mas01mc@292
|
1400 lock.l_start = 0;
|
mas01mc@292
|
1401 lock.l_len = 0; /* "the whole file" */
|
mas01mc@292
|
1402 retry:
|
mas01mc@292
|
1403 do {
|
mas01mc@292
|
1404 status = fcntl(fd, F_SETLKW, &lock);
|
mas01mc@292
|
1405 } while (status != 0 && errno == EINTR);
|
mas01mc@292
|
1406 if (status) {
|
mas01mc@292
|
1407 if (errno == EAGAIN) {
|
mas01mc@292
|
1408 sleep(1);
|
mas01mc@292
|
1409 goto retry;
|
mas01mc@292
|
1410 } else {
|
mas01mc@292
|
1411 error("fcntl lock error", "", "fcntl");
|
mas01mc@292
|
1412 }
|
mas01mc@292
|
1413 }
|
mas01mc@292
|
1414 }
|
mas01mc@292
|
1415
|
mas01mc@292
|
1416 void G::release_lock(int fd) {
|
mas01mc@292
|
1417 struct flock lock;
|
mas01mc@292
|
1418 int status;
|
mas01mc@292
|
1419
|
mas01mc@292
|
1420 lock.l_type = F_UNLCK;
|
mas01mc@292
|
1421 lock.l_whence = SEEK_SET;
|
mas01mc@292
|
1422 lock.l_start = 0;
|
mas01mc@292
|
1423 lock.l_len = 0;
|
mas01mc@292
|
1424
|
mas01mc@292
|
1425 status = fcntl(fd, F_SETLKW, &lock);
|
mas01mc@292
|
1426
|
mas01mc@292
|
1427 if (status)
|
mas01mc@292
|
1428 error("fcntl unlock error", "", "fcntl");
|
mas01mc@292
|
1429 }
|