mas01mc@292: // UNIT_TEST_LSH.cpp mas01mc@292: mas01mc@292: #include mas01mc@292: #include "lshlib.h" mas01mc@292: #include "reporter.h" mas01mc@292: mas01mc@292: #define LSH_IN_CORE mas01mc@292: mas01mc@292: mas01mc@292: #define N_POINT_BITS 14 mas01mc@292: #define POINT_BIT_MASK 0x00003FFF mas01mc@292: mas01mc@292: // Callback method for LSH point retrieval mas01mc@292: void add_point(void* reporter, Uns32T pointID, Uns32T qpos, float dist) mas01mc@292: { mas01mc@292: ReporterBase* pr = (ReporterBase*)reporter; mas01mc@292: pr->add_point(pointID>>N_POINT_BITS, qpos, pointID&POINT_BIT_MASK, dist); mas01mc@292: } mas01mc@292: mas01mc@292: int main(int argc, char* argv[]){ mas01mc@292: mas01mc@292: int nT = 100; // num tracks mas01mc@292: int nP = 1000; // num points-per-track mas01mc@292: float w = 4.0;// LSH bucket width mas01mc@292: int k = 10; mas01mc@292: int m = 2; mas01mc@292: int d = 10; mas01mc@292: int N = 100000; mas01mc@292: int C = 200; mas01mc@292: mas01mc@292: float radius = 0.001; mas01mc@292: char FILENAME[] = "foo.lsh"; mas01mc@292: mas01mc@292: assert(nP>=nT); mas01mc@292: mas01mc@292: int fid = open(FILENAME,O_RDONLY); mas01mc@292: LSH* lsh; mas01mc@292: bool serialized = false; mas01mc@292: Uns32T trackBase = 0; mas01mc@292: mas01mc@292: if(fid< 0){ // Make a new serial LSH file mas01mc@292: lsh = new LSH(w,k,m,d,N,C,radius); mas01mc@292: assert(lsh); mas01mc@292: cout << "NEW LSH:" << endl; mas01mc@292: } mas01mc@292: else{ mas01mc@292: close(fid); // Load LSH structures from disk mas01mc@292: lsh = new LSH(FILENAME); mas01mc@292: assert(lsh); mas01mc@292: cout << "MERGE WITH EXISTING LSH:" << FILENAME << endl; mas01mc@292: serialized=true; mas01mc@292: trackBase = (lsh->get_maxp()>>N_POINT_BITS)+1; // Our encoding of tracks and points mas01mc@292: } mas01mc@294: cout << "k:" << lsh->get_numFuns() << " "; mas01mc@294: cout << "L:" << lsh->get_numTables() << " "; mas01mc@294: cout << "d:" << lsh->get_dataDim() << " "; mas01mc@294: cout << "N:" << lsh->get_numRows() << " "; mas01mc@294: cout << "C:" << lsh->get_numCols() << " "; mas01mc@294: cout << "R:" << lsh->get_radius() << " "; mas01mc@294: cout << "p:" << lsh->get_maxp() << endl; mas01mc@292: cout.flush(); mas01mc@292: mas01mc@292: cout << endl << "Constructing " << nT << " tracks with " << nP << " vectors of dimension " << d << endl; mas01mc@292: cout.flush(); mas01mc@292: // Construct sets of database vectors, use one point from each set for testing mas01mc@292: vector< vector > vv = vector< vector >(nP); // track vectors mas01mc@292: vector< vector > qq = vector< vector >(nP);// query vectors mas01mc@292: for(int i=0; i< nP ; i++){ mas01mc@292: vv[i]=vector(d); // allocate vector mas01mc@292: qq[i]=vector(d); // allocate vector mas01mc@292: } mas01mc@292: mas01mc@292: for(int k = 0 ; k < nT ; k ++){ mas01mc@292: cout << "[" << k << "]"; mas01mc@292: cout.flush(); mas01mc@292: for(int i = 0 ; i< nP ; i++) mas01mc@292: for(int j=0; j< d ; j++) mas01mc@292: vv[i][j] = genrand_real2() / radius; // MT_19937 random numbers mas01mc@292: lsh->insert_point_set(vv, (trackBase+k)<serialize(FILENAME); mas01mc@292: mas01mc@292: // TEST LSH RETRIEVAL IN CORE mas01mc@292: printf("\n********** In-core LSH retrieval from %d track%c **********\n", mas01mc@292: (lsh->get_maxp()>>N_POINT_BITS)+1,(lsh->get_maxp()>>N_POINT_BITS)>0?'s':' '); mas01mc@292: fflush(stdout); mas01mc@292: for(int i = 0; i < nT ; i++ ){ mas01mc@292: trackSequenceQueryRadNNReporter* pr = new trackSequenceQueryRadNNReporter(nP,nT,(lsh->get_maxp()>>N_POINT_BITS)+1); mas01mc@292: lsh->retrieve_point(qq[i], i, &add_point, (void*)pr); // LSH point retrieval from core mas01mc@292: printf("query vector %d] t1:%u t2:%0X\n", i, lsh->get_t1(), lsh->get_t2()); mas01mc@292: fflush(stdout); mas01mc@292: pr->report(0,0); mas01mc@292: delete pr; mas01mc@292: } mas01mc@292: delete lsh; mas01mc@292: mas01mc@292: cout << "Loading Serialized LSH functions from disk ..." << endl; mas01mc@292: cout.flush(); mas01mc@292: lsh = new LSH(FILENAME); mas01mc@292: assert(lsh); mas01mc@292: // lsh->serial_dump_tables(FILENAME); mas01mc@292: printf("\n********** Serialized LSH retrieval from %d track%c **********\n", (lsh->get_maxp()>>N_POINT_BITS)+1,(lsh->get_maxp()>>N_POINT_BITS)>1?'s':' '); mas01mc@292: fflush(stdout); mas01mc@292: for(int i= 0; i < nT ; i++ ){ mas01mc@292: trackSequenceQueryRadNNReporter* pr = new trackSequenceQueryRadNNReporter(nP,nT,(lsh->get_maxp()>>N_POINT_BITS)+1); mas01mc@292: lsh->serial_retrieve_point(FILENAME, qq[i], i, &add_point, (void*) pr); // LSH serialized point retrieval method mas01mc@292: printf("query vector %d] t1:%u t2:%0X\n", i, lsh->get_t1(), lsh->get_t2()); mas01mc@292: fflush(stdout); mas01mc@292: pr->report(0,0); mas01mc@292: delete pr; mas01mc@292: } mas01mc@292: delete lsh; mas01mc@292: mas01mc@292: #ifdef LSH_IN_CORE mas01mc@292: cout << "Loading Serialized LSH functions and tables from disk ..." << endl; mas01mc@292: cout.flush(); mas01mc@292: // Unserialize entire lsh tree to core mas01mc@292: lsh = new LSH(FILENAME,1); mas01mc@292: mas01mc@292: // TEST UNSERIALIZED LSH RETRIEVAL IN CORE mas01mc@292: printf("\n********** Unserialized LSH in-core retrieval from %d track%c **********\n", (lsh->get_maxp()>>N_POINT_BITS)+1,(lsh->get_maxp()>>N_POINT_BITS)>1?'s':' '); mas01mc@292: fflush(stdout); mas01mc@292: for(int i = 0; i < nT ; i++ ){ mas01mc@292: trackSequenceQueryRadNNReporter* pr = new trackSequenceQueryRadNNReporter(nP,nT,(lsh->get_maxp()>>N_POINT_BITS)+1); mas01mc@292: lsh->retrieve_point(qq[i], i, &add_point, (void*) pr); // LSH point retrieval from core mas01mc@292: printf("query vector %d] t1:%u t2:%0X\n", i, lsh->get_t1(), lsh->get_t2()); mas01mc@292: fflush(stdout); mas01mc@292: pr->report(0,0); mas01mc@292: delete pr; mas01mc@292: } mas01mc@292: delete lsh; mas01mc@292: #endif mas01mc@292: mas01mc@292: }