-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_create_tree.cpp
212 lines (182 loc) · 4.92 KB
/
test_create_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdio.h>
#include "root_node.h"
//#include "node.h"
#include "io.h"
#include <time.h>
#include "nodeops.h"
#include <stdlib.h>
#include "bitops.h"
#include "SearchNode.h"
int Node::B_over_8;
UINT8* Node::dbcode;
int main(int argc, char**argv)
{
if (argc < 3) {
printf("Usage:\n\nmih <infile> <outfile> [options]\n\n");
printf("Options:\n");
printf(" -N <number> Set the number of binary codes from the beginning of the dataset file to be used\n");
printf(" -Q <number> Set the number of query points to use from <infile>, default all\n");
printf(" -B <number> Set the number of bits per code, default autodetect\n");
printf(" -m <number> Set the number of chunks to use, default 1\n");
printf(" -K <number> Set number of nearest neighbors to be retrieved\n");
printf(" -R <number> Set the number of codes (in Millions) to use in computing the optimal bit reordering, default OFF (0)\n");
printf("\n");
return 0;
}
char *infile = argv[1];
char *outfile = argv[2];
UINT32 N = 0;
UINT32 NQ = 0, Q0 = 0, Q1 = 0;
int B = 0;
int m = 1;
UINT32 K = -1;
size_t R = 0;
for (int argnum = 3; argnum < argc; argnum++) {
if (argv[argnum][0] == '-') {
switch (argv[argnum][1]) {
case 'B':
B = atoi(argv[++argnum]);
break;
case 'K':
K = atoi(argv[++argnum]);
break;
case 'N':
N = atoi(argv[++argnum]);
break;
case 'Q':
Q0 = atoi(argv[++argnum]);
if (++argnum < argc) {
if (argv[argnum][0] != '-') {
Q1 = atof(argv[argnum]);
} else {
argnum--;
Q1 = Q0;
Q0 = 0;
}
}
NQ = Q1-Q0;
break;
case 'm':
m = atoi(argv[++argnum]);
break;
case 'R':
R = atoi(argv[++argnum])*1000000;
break;
default:
printf("Unrecognized Option or Missing Parameter when parsing: %s\n", argv[argnum]);
return EXIT_FAILURE;
}
} else {
printf("Invalid Argument: %s\n", argv[argnum]);
return EXIT_FAILURE;
}
}
if (!NQ) {
printf("-Q is required.\n");
return EXIT_FAILURE;
}
if (B % 8 != 0) { // in case of B == 0 this should be fine
printf("Non-multiple of 8 code lengths are not currently supported.\n");
return EXIT_FAILURE;
}
if (R > N) {
printf("R was greater than N, R will now be equal to N.\n");
R = N;
}
if (K < 1 || K > N) {
printf("A valid K is not provided.\n");
return EXIT_FAILURE;
}
int B_over_8 = B/8;
UINT8 *codes_db;
int dim1codes;
UINT8 *codes_query;
int dim1queries;
clock_t start0, end0;
time_t start1, end1;
printf("Loading codes... ");
fflush(stdout);
codes_db = (UINT8*)malloc((size_t)N * (B/8) * sizeof(UINT8));
load_bin_codes(infile, "B", codes_db, &N, &B_over_8);
//RootNode root = RootNode(B);
start1 = time(NULL);
start0 = clock();
// for(UINT32 i=0;i<N;i++)
// {
// if(i%500000==0)
// printf("%d items are load\n",i);
// root.insert(codes_db+i*B_over_8,i);
// }
int capacity = 1000;
Node* curr_node;
UINT8* ccode;
int tree_lvl = 0;
int max_tree_lvl = 0;
RootNode *root = new RootNode(B,capacity);
Node::B_over_8 = B_over_8;
Node::dbcode = codes_db;
int max_lvl = log2(B);
int expansions=0;
//UINT8* subnroms = (UINT8*)malloc((B*2-1)*sizeof(UINT8)); //all possible norms of a binary string with B
printf("Started creating tree\n");
for(UINT32 i=0;i<N;i++)
{
if(i==37805)
printf("i = %d\n",i);
tree_lvl = 0;
//printf("%d items are load\n",i);
ccode = codes_db + i*B_over_8;
root->insert(ccode,curr_node);
tree_lvl++;
while(true){
if(curr_node->isleaf) {
curr_node->push_to_node(ccode,i);
if(curr_node->size == capacity && tree_lvl<max_lvl){
curr_node->expand();
expansions++;
if(tree_lvl==6)
printf("depth not supported\n");
}
break;
}
else{
UINT8 subnorms[(int)pow(2,tree_lvl)];
norm_chunks(subnorms,tree_lvl,ccode,B_over_8);
curr_node = curr_node->find_the_child(ccode,subnorms);
// if(tree_lvl>=5)
// printf("i = %d\n",i);
}
// curr_node == root;
tree_lvl++;
}
if(tree_lvl>max_tree_lvl)
max_tree_lvl = tree_lvl;
if(i%500000==0)
{
printf("%d items are loaded\n",i);
fflush(stdout);
}
}
end0 = clock();
end1 = time(NULL);
double cput = (double)(end0-start0) / (CLOCKS_PER_SEC) ;
double wt = (double)(end1-start1) ;
printf("cput = %f, wt = %f\n",cput,wt);
printf("Done Loading\n");
delete root;
printf("max tree level = %d\n",max_tree_lvl);
printf("expansions = %d\n",expansions);
printf("Done\n");
codes_query = (UINT8*)malloc((size_t)NQ * (B/8) * sizeof(UINT8));
UINT8* codesq, ccodeq;
load_bin_codes(infile, "Q", codesq, &NQ, &B_over_8, Q0);
for (int i=0; i < NQ; i++) {
ccodeq += B_over_8;
UINT8* set_norm_chunks = new UINT8[(int)pow(2,max_tree_lvl+1)];
for(int j=0; j<max_tree_lvl; j++) {
norm_chunks(set_norm_chunks,j,ccodeq,B_over_8);
}
hammin_radius = 0
visited_parents_list* = NULL;
SearchNode sn = new SearchNode(codes_db, ccodeq, B_over_8max_tree_lvl);
}