annotate lshlib.h @ 292:d9a88cfd4ab6

Completed merge of lshlib back to current version of the trunk.
author mas01mc
date Tue, 29 Jul 2008 22:01:17 +0000
parents
children 9fd5340faffd
rev   line source
mas01mc@292 1 // lshlib.h - a library for locality sensitive hashtable insertion and retrieval
mas01mc@292 2 //
mas01mc@292 3 // Author: Michael Casey
mas01mc@292 4 // Copyright (c) 2008 Michael Casey, All Rights Reserved
mas01mc@292 5
mas01mc@292 6 /* GNU GENERAL PUBLIC LICENSE
mas01mc@292 7 Version 2, June 1991
mas01mc@292 8 See LICENSE.txt
mas01mc@292 9 */
mas01mc@292 10
mas01mc@292 11 #ifndef __LSHLIB_H
mas01mc@292 12 #define __LSHLIB_H
mas01mc@292 13
mas01mc@292 14 #include <vector>
mas01mc@292 15 #include <queue>
mas01mc@292 16 #include <stdio.h>
mas01mc@292 17 #include <stdlib.h>
mas01mc@292 18 #include <sys/types.h>
mas01mc@292 19 #include <sys/stat.h>
mas01mc@292 20 #include <sys/mman.h>
mas01mc@292 21 #include <fcntl.h>
mas01mc@292 22 #include <string.h>
mas01mc@292 23 #include <iostream>
mas01mc@292 24 #include <fstream>
mas01mc@292 25 #include <math.h>
mas01mc@292 26 #include <sys/time.h>
mas01mc@292 27 #include <assert.h>
mas01mc@292 28 #include <float.h>
mas01mc@292 29 #include <signal.h>
mas01mc@292 30 #include <time.h>
mas01mc@292 31 #include <limits.h>
mas01mc@292 32 #include <errno.h>
mas01mc@292 33 #ifdef MT19937
mas01mc@292 34 #include "mt19937/mt19937ar.h"
mas01mc@292 35 #endif
mas01mc@292 36
mas01mc@292 37 #define IntT int
mas01mc@292 38 #define LongUns64T long long unsigned
mas01mc@292 39 #define Uns32T unsigned
mas01mc@292 40 #define Int32T int
mas01mc@292 41 #define BooleanT int
mas01mc@292 42 #define TRUE 1
mas01mc@292 43 #define FALSE 0
mas01mc@292 44
mas01mc@292 45 // A big number (>> max # of points)
mas01mc@292 46 #define INDEX_START_EMPTY 1000000000U
mas01mc@292 47
mas01mc@292 48 // 4294967291 = 2^32-5
mas01mc@292 49 #define UH_PRIME_DEFAULT 4294967291U
mas01mc@292 50
mas01mc@292 51 // 2^29
mas01mc@292 52 #define MAX_HASH_RND 536870912U
mas01mc@292 53
mas01mc@292 54 // 2^32-1
mas01mc@292 55 #define TWO_TO_32_MINUS_1 4294967295U
mas01mc@292 56
mas01mc@292 57 #define O2_SERIAL_VERSION 1 // Sync with SVN version
mas01mc@292 58 #define O2_SERIAL_HEADER_SIZE sizeof(SerialHeaderT)
mas01mc@292 59 #define O2_SERIAL_ELEMENT_SIZE sizeof(SerialElementT)
mas01mc@292 60 #define O2_SERIAL_MAX_TABLES (200)
mas01mc@292 61 #define O2_SERIAL_MAX_ROWS (1000000)
mas01mc@292 62 #define O2_SERIAL_MAX_COLS (1000)
mas01mc@292 63 #define O2_SERIAL_MAX_DIM (2000)
mas01mc@292 64 #define O2_SERIAL_MAX_FUNS (100)
mas01mc@292 65 #define O2_SERIAL_MAX_BINWIDTH (200)
mas01mc@292 66
mas01mc@292 67 // Flags for Serial Header
mas01mc@292 68 #define O2_SERIAL_FILEFORMAT1 (0x1U) // Optimize for on-disk search
mas01mc@292 69 #define O2_SERIAL_FILEFORMAT2 (0x2U) // Optimize for in-core search
mas01mc@292 70
mas01mc@292 71 // Flags for serialization fileformat2: use high 3 bits of Uns32T
mas01mc@292 72 #define O2_SERIAL_FLAGS_T1_BIT (0x80000000U)
mas01mc@292 73 #define O2_SERIAL_FLAGS_T2_BIT (0x40000000U)
mas01mc@292 74 #define O2_SERIAL_FLAGS_END_BIT (0x20000000U)
mas01mc@292 75
mas01mc@292 76 unsigned align_up(unsigned x, unsigned w);
mas01mc@292 77
mas01mc@292 78 #define O2_SERIAL_FUNCTIONS_SIZE (align_up(sizeof(float) * O2_SERIAL_MAX_TABLES * O2_SERIAL_MAX_FUNS * O2_SERIAL_MAX_DIM \
mas01mc@292 79 + sizeof(float) * O2_SERIAL_MAX_TABLES * O2_SERIAL_MAX_FUNS + \
mas01mc@292 80 + sizeof(Uns32T) * O2_SERIAL_MAX_TABLES * O2_SERIAL_MAX_FUNS * 2 \
mas01mc@292 81 + O2_SERIAL_HEADER_SIZE,get_page_logn()))
mas01mc@292 82
mas01mc@292 83 #define O2_SERIAL_MAX_LSH_SIZE (O2_SERIAL_ELEMENT_SIZE * O2_SERIAL_MAX_TABLES \
mas01mc@292 84 * O2_SERIAL_MAX_ROWS * O2_SERIAL_MAX_COLS + O2_SERIAL_FUNCTIONS_SIZE)
mas01mc@292 85
mas01mc@292 86 #define O2_SERIAL_MAGIC ('o'|'2'<<8|'l'<<16|'s'<<24)
mas01mc@292 87
mas01mc@292 88 using namespace std;
mas01mc@292 89
mas01mc@292 90 Uns32T get_page_logn();
mas01mc@292 91
mas01mc@292 92 // Disk table entry
mas01mc@292 93 typedef class SerialElement SerialElementT;
mas01mc@292 94 class SerialElement {
mas01mc@292 95 public:
mas01mc@292 96 Uns32T hashValue;
mas01mc@292 97 Uns32T pointID;
mas01mc@292 98
mas01mc@292 99 SerialElement(Uns32T h, Uns32T pID):
mas01mc@292 100 hashValue(h),
mas01mc@292 101 pointID(pID){}
mas01mc@292 102 };
mas01mc@292 103
mas01mc@292 104 // Disk header
mas01mc@292 105 typedef class SerialHeader SerialHeaderT;
mas01mc@292 106 class SerialHeader {
mas01mc@292 107 public:
mas01mc@292 108 Uns32T lshMagic; // unique identifier for file header
mas01mc@292 109 float binWidth; // hash-function bin width
mas01mc@292 110 Uns32T numTables; // number of hash tables in file
mas01mc@292 111 Uns32T numRows; // size of each hash table
mas01mc@292 112 Uns32T numCols; // max collisions in each hash table
mas01mc@292 113 Uns32T elementSize; // size of a hash bucket
mas01mc@292 114 Uns32T version; // version number of file format
mas01mc@292 115 Uns32T size; // total size of database (bytes)
mas01mc@292 116 Uns32T flags; // 32 bits of useful information
mas01mc@292 117 Uns32T dataDim; // vector dimensionality
mas01mc@292 118 Uns32T numFuns; // number of independent hash functions
mas01mc@292 119 float radius; // 32-bit floating point radius
mas01mc@292 120 Uns32T maxp; // number of unique IDs in the database
mas01mc@292 121 Uns32T value_14; // spare value
mas01mc@292 122 Uns32T value_15; // spare value
mas01mc@292 123 Uns32T value_16; // spare value
mas01mc@292 124
mas01mc@292 125 SerialHeader();
mas01mc@292 126 SerialHeader(float W, Uns32T L, Uns32T N, Uns32T C, Uns32T k, Uns32T d, float radius, Uns32T p, Uns32T FMT);
mas01mc@292 127
mas01mc@292 128 float get_binWidth(){return binWidth;}
mas01mc@292 129 Uns32T get_numTables(){return numTables;}
mas01mc@292 130 Uns32T get_numRows(){return numRows;}
mas01mc@292 131 Uns32T get_numCols(){return numCols;}
mas01mc@292 132 Uns32T get_elementSize(){return elementSize;}
mas01mc@292 133 Uns32T get_version(){return version;}
mas01mc@292 134 Uns32T get_flags(){return flags;}
mas01mc@292 135 Uns32T get_size(){return size;}
mas01mc@292 136 Uns32T get_dataDim(){return dataDim;}
mas01mc@292 137 Uns32T get_numFuns(){return numFuns;}
mas01mc@292 138 Uns32T get_maxp(){return maxp;}
mas01mc@292 139 };
mas01mc@292 140
mas01mc@292 141 #define IFLAG 0xFFFFFFFF
mas01mc@292 142
mas01mc@292 143 // Point-set collision bucket (sbucket).
mas01mc@292 144 // sbuckets form a collision chain that identifies PointIDs falling in the same locale.
mas01mc@292 145 // sbuckets are chained from a bucket containing the collision list's t2 identifier
mas01mc@292 146 class sbucket {
mas01mc@292 147 friend class bucket;
mas01mc@292 148 friend class H;
mas01mc@292 149 friend class G;
mas01mc@292 150
mas01mc@292 151 public:
mas01mc@292 152 class sbucket* snext;
mas01mc@292 153 unsigned int pointID;
mas01mc@292 154
mas01mc@292 155 sbucket(){
mas01mc@292 156 snext=0;
mas01mc@292 157 pointID=IFLAG;
mas01mc@292 158 }
mas01mc@292 159 ~sbucket(){delete snext;}
mas01mc@292 160 sbucket* get_snext(){return snext;}
mas01mc@292 161 };
mas01mc@292 162
mas01mc@292 163 // bucket structure for a linked list of locales that collide with the same hash value t1
mas01mc@292 164 // different buckets represent different locales, collisions within a locale are chained
mas01mc@292 165 // in sbuckets
mas01mc@292 166 class bucket {
mas01mc@292 167 friend class H;
mas01mc@292 168 friend class G;
mas01mc@292 169 bucket* next;
mas01mc@292 170 sbucket* snext;
mas01mc@292 171 public:
mas01mc@292 172 unsigned int t2;
mas01mc@292 173 bucket(){
mas01mc@292 174 next=0;
mas01mc@292 175 snext=0;
mas01mc@292 176 t2=IFLAG;
mas01mc@292 177 }
mas01mc@292 178 ~bucket(){delete next;delete snext;}
mas01mc@292 179 bucket* get_next(){return next;}
mas01mc@292 180 };
mas01mc@292 181
mas01mc@292 182
mas01mc@292 183 // The hash_functions for locality-sensitive hashing
mas01mc@292 184 class H{
mas01mc@292 185 friend class G;
mas01mc@292 186 private:
mas01mc@292 187 bucket*** h; // hash functions
mas01mc@292 188 Uns32T** r1; // random ints for hashing
mas01mc@292 189 Uns32T** r2; // random ints for hashing
mas01mc@292 190 Uns32T t1; // first hash table key
mas01mc@292 191 Uns32T t2; // second hash table key
mas01mc@292 192 Uns32T P; // hash table prime number
mas01mc@292 193 bool use_u_functions; // flag to optimize computation of hashes
mas01mc@292 194 Uns32T bucketCount; // count of number of point buckets allocated
mas01mc@292 195 Uns32T pointCount; // count of number of points inserted
mas01mc@292 196
mas01mc@292 197 void __initialize_data_structures();
mas01mc@292 198 void __bucket_insert_point(bucket*);
mas01mc@292 199 void __sbucket_insert_point(sbucket*);
mas01mc@292 200 Uns32T __computeProductModDefaultPrime(Uns32T*,Uns32T*,IntT);
mas01mc@292 201 Uns32T __randr();
mas01mc@292 202 bucket** __get_bucket(int j);
mas01mc@292 203 void __generate_hash_keys(Uns32T*,Uns32T*,Uns32T*);
mas01mc@292 204 void error(const char* a, const char* b = "", const char *sysFunc = 0);
mas01mc@292 205
mas01mc@292 206 public:
mas01mc@292 207 Uns32T N; // num rows per table
mas01mc@292 208 Uns32T C; // num collision per row
mas01mc@292 209 Uns32T k; // num projections per hash function
mas01mc@292 210 Uns32T m; // ~sqrt num hash tables
mas01mc@292 211 Uns32T L; // L = m*(m-1)/2, conversely, m = (1 + sqrt(1 + 8.0*L)) / 2.0
mas01mc@292 212 Uns32T d; // dimensions
mas01mc@292 213 Uns32T p; // current point
mas01mc@292 214
mas01mc@292 215 H(){;}
mas01mc@292 216 H(Uns32T k, Uns32T m, Uns32T d, Uns32T N, Uns32T C);
mas01mc@292 217 ~H();
mas01mc@292 218
mas01mc@292 219
mas01mc@292 220 Uns32T bucket_insert_point(bucket**);
mas01mc@292 221
mas01mc@292 222 Uns32T get_t1(){return t1;}
mas01mc@292 223 Uns32T get_t2(){return t2;}
mas01mc@292 224
mas01mc@292 225 };
mas01mc@292 226
mas01mc@292 227
mas01mc@292 228 typedef void (*ReporterCallbackPtr)(void* objPtr, Uns32T pointID, Uns32T queryIndex, float squaredDistance);
mas01mc@292 229
mas01mc@292 230 // Interface for indexing and retrieval
mas01mc@292 231 class G: public H{
mas01mc@292 232 private:
mas01mc@292 233 float *** A; // m x k x d random projectors from R^d->R^k
mas01mc@292 234 float ** b; // m x k uniform additive constants
mas01mc@292 235 Uns32T ** g; // L x k random hash projections \in Z^k
mas01mc@292 236 float w; // width of hash slots (relative to normalized feature space)
mas01mc@292 237 float radius;// scaling coefficient for data (1./radius)
mas01mc@292 238 vector<vector<Uns32T> > uu; // Storage for m patial hash evaluations
mas01mc@292 239 Uns32T maxp; // highest pointID stored in database
mas01mc@292 240 void* calling_instance; // store calling object instance for member-function callback
mas01mc@292 241 void (*add_point_callback)(void*, Uns32T, Uns32T, float);
mas01mc@292 242
mas01mc@292 243 void initialize_partial_functions();
mas01mc@292 244
mas01mc@292 245 void get_lock(int fd, bool exclusive);
mas01mc@292 246 void release_lock(int fd);
mas01mc@292 247 int serial_create(char* filename, Uns32T FMT);
mas01mc@292 248 int serial_create(char* filename, float binWidth, Uns32T nTables, Uns32T nRows, Uns32T nCols, Uns32T k, Uns32T d, Uns32T FMT);
mas01mc@292 249 char* serial_mmap(int dbfid, Uns32T sz, Uns32T w, off_t offset = 0);
mas01mc@292 250 void serial_munmap(char* db, Uns32T N);
mas01mc@292 251 int serial_open(char* filename,int writeFlag);
mas01mc@292 252 void serial_close(int dbfid);
mas01mc@292 253
mas01mc@292 254 // Function to write hashfunctions to disk
mas01mc@292 255 int serialize_lsh_hashfunctions(int fid);
mas01mc@292 256
mas01mc@292 257 // Functions to write hashtables to disk in format1 (optimized for on-disk retrieval)
mas01mc@292 258 int serialize_lsh_hashtables_format1(int fid, int merge);
mas01mc@292 259 void serial_write_hashtable_row_format1(SerialElementT*& pe, bucket* h, Uns32T& colCount);
mas01mc@292 260 void serial_write_element_format1(SerialElementT*& pe, sbucket* sb, Uns32T t2, Uns32T& colCount);
mas01mc@292 261 void serial_merge_hashtable_row_format1(SerialElementT* pr, bucket* h, Uns32T& colCount);
mas01mc@292 262 void serial_merge_element_format1(SerialElementT* pe, sbucket* sb, Uns32T t2, Uns32T& colCount);
mas01mc@292 263 int serial_can_merge(Uns32T requestedFormat); // Test to see whether core and on-disk structures are compatible
mas01mc@292 264
mas01mc@292 265 // Functions to write hashtables to disk in format2 (optimized for in-core retrieval)
mas01mc@292 266 int serialize_lsh_hashtables_format2(int fid, int merge);
mas01mc@292 267 void serial_write_hashtable_row_format2(int fid, bucket* h, Uns32T& colCount);
mas01mc@292 268 void serial_write_element_format2(int fid, sbucket* sb, Uns32T& colCount);
mas01mc@292 269
mas01mc@292 270 // Functions to read serial header and hash functions (format1 and format2)
mas01mc@292 271 int unserialize_lsh_header(char* filename); // read lsh header from disk into core
mas01mc@292 272 void unserialize_lsh_functions(int fid); // read the lsh hash functions into core
mas01mc@292 273
mas01mc@292 274 // Functions to read hashtables in format1
mas01mc@292 275 void unserialize_lsh_hashtables_format1(int fid); // read FORMAT1 hash tables into core (disk format)
mas01mc@292 276 void unserialize_hashtable_row_format1(SerialElementT* pe, bucket** b); // read lsh hash table row into core
mas01mc@292 277
mas01mc@292 278 // Functions to read hashtables in format2
mas01mc@292 279 void unserialize_lsh_hashtables_format2(int fid); // read FORMAT2 hash tables into core (core format)
mas01mc@292 280 Uns32T unserialize_hashtable_row_format2(int fid, bucket** b); // read lsh hash table row into core
mas01mc@292 281
mas01mc@292 282 // Helper functions
mas01mc@292 283 void serial_print_header(Uns32T requestedFormat);
mas01mc@292 284 float* get_serial_hashfunction_base(char* db);
mas01mc@292 285 SerialElementT* get_serial_hashtable_base(char* db);
mas01mc@292 286 Uns32T get_serial_hashtable_offset(); // Size of SerialHeader + HashFunctions
mas01mc@292 287 SerialHeaderT* serial_get_header(char* db);
mas01mc@292 288 SerialHeaderT* lshHeader;
mas01mc@292 289
mas01mc@292 290 // Core Retrieval/Inspections Functions
mas01mc@292 291 void bucket_chain_point(bucket* p, Uns32T qpos);
mas01mc@292 292 void sbucket_chain_point(sbucket* p, Uns32T qpos);
mas01mc@292 293 void dump_hashtable_row(bucket* p);
mas01mc@292 294
mas01mc@292 295 // Serial (Format 1) Retrieval/Inspection Functions
mas01mc@292 296 void serial_bucket_chain_point(SerialElementT* pe, Uns32T qpos);
mas01mc@292 297 void serial_bucket_dump(SerialElementT* pe);
mas01mc@292 298
mas01mc@292 299 // Hash functions
mas01mc@292 300 void compute_hash_functions(vector<float>& v);
mas01mc@292 301 float randn();
mas01mc@292 302 float ranf();
mas01mc@292 303
mas01mc@292 304 char* db; // pointer to serialized structure
mas01mc@292 305
mas01mc@292 306 public:
mas01mc@292 307 G(char* lshFile, bool lshInCore = false); // unserialize constructor
mas01mc@292 308 G(float w, Uns32T k,Uns32T m, Uns32T d, Uns32T N, Uns32T C, float r); // core constructor
mas01mc@292 309 ~G();
mas01mc@292 310
mas01mc@292 311 Uns32T insert_point(vector<float>&, Uns32T pointID);
mas01mc@292 312 void insert_point_set(vector<vector<float> >& vv, Uns32T basePointID);
mas01mc@292 313
mas01mc@292 314 // point retrieval from core
mas01mc@292 315 void retrieve_point(vector<float>& v, Uns32T qpos, ReporterCallbackPtr, void* me=NULL);
mas01mc@292 316 // point set retrieval from core
mas01mc@292 317 void retrieve_point_set(vector<vector<float> >& vv, ReporterCallbackPtr, void* me=NULL);
mas01mc@292 318 // serial point set retrieval
mas01mc@292 319 void serial_retrieve_point_set(char* filename, vector<vector<float> >& vv, ReporterCallbackPtr, void* me=NULL);
mas01mc@292 320 // serial point retrieval
mas01mc@292 321 void serial_retrieve_point(char* filename, vector<float>& vv, Uns32T qpos, ReporterCallbackPtr, void* me=NULL);
mas01mc@292 322
mas01mc@292 323 void serialize(char* filename, Uns32T serialFormat = O2_SERIAL_FILEFORMAT1); // write hashfunctions and hashtables to disk
mas01mc@292 324
mas01mc@292 325 SerialHeaderT* get_lshHeader(){return lshHeader;}
mas01mc@292 326 float get_radius(){return radius;}
mas01mc@292 327 Uns32T get_maxp(){return maxp;}
mas01mc@292 328 void serial_dump_tables(char* filename);
mas01mc@292 329 float get_mean_collision_rate(){ return (float) pointCount / bucketCount ; }
mas01mc@292 330 };
mas01mc@292 331
mas01mc@292 332 typedef class G LSH;
mas01mc@292 333
mas01mc@292 334
mas01mc@292 335
mas01mc@292 336 #endif