-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.cpp
388 lines (337 loc) · 14.6 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <chrono>
#include "hnswlib.h"
#include <algorithm>
#include <ctime>
#include <omp.h>
const int defaultEfConstruction = 1024;
const int defaultEfSearch = 128;
const int defaultM = 32;
const int defaultTopK = 1;
void printHelpMessage()
{
std::cerr << "Usage: main [OPTIONS]" << std::endl;
std::cerr << "This tool supports two modes: to construct the graph index from the database and to retrieve top K maximum inner product vectors using the constructed index. Each mode has its own set of parameters." << std::endl;
std::cerr << std::endl;
std::cerr << " --mode " << "\"database\" or \"query\". Use \"database\" for" << std::endl;
std::cerr << " " << "constructing index and \"query\" for top K" << std::endl;
std::cerr << " " << "maximum inner product retrieval" << std::endl;
std::cerr << std::endl;
std::cerr << "Database mode supports the following options:" << std::endl;
std::cerr << " --database " << "Database filename. Database should be stored in binary format." << std::endl;
std::cerr << " " << "Vectors are written consecutive and numbers are" << std::endl;
std::cerr << " " << "represented in 4-bytes floating poing format (float in C/C++)" << std::endl;
std::cerr << " --databaseSize " << "Number of vectors in the database" << std::endl;
std::cerr << " --dimension " << "Dimension of vectors" << std::endl;
std::cerr << " --outputGraph " << "Filename for the output index graph" << std::endl;
std::cerr << " --efConstruction " << "efConstruction parameter. Default: " << defaultEfConstruction << std::endl;
std::cerr << " --M " << "M parameter. Default: " << defaultM << std::endl;
std::cerr << std::endl;
std::cerr << "Query mode supports the following options:" << std::endl;
std::cerr << " --query " << "Query filename. Queries should be stored in binary format." << std::endl;
std::cerr << " " << "Vectors are written consecutive and numbers are" << std::endl;
std::cerr << " " << "represented in 4-bytes floating poing format (float in C/C++)" << std::endl;
std::cerr << " --querySize " << "Number of queries" << std::endl;
std::cerr << " --dimension " << "Dimension of vectors" << std::endl;
std::cerr << " --inputGraph " << "Filename for the input index graph" << std::endl;
std::cerr << " --efSearch " << "efSearch parameter. Default: " << defaultEfSearch << std::endl;
std::cerr << " --topK " << "Top size for retrieval. Default: " << defaultTopK << std::endl;
std::cerr << " --output " << "Filename to print the result. Default: " << "stdout" << std::endl;
}
void printError(std::string err)
{
std::cerr << err << std::endl;
std::cerr << std::endl;
printHelpMessage();
}
int main(int argc, char** argv) {
std::string mode;
std::ifstream input;
std::ifstream inputQ;
int efConstruction = defaultEfConstruction;
int efSearch = defaultEfSearch;
int M = defaultM;
int vecsize = -1;
int qsize = -1;
int vecdim = -1;
std::string graphname;
std::string outputname;
int topK = defaultTopK;
std::string dataname;
std::string queryname;
hnswlib::HierarchicalNSW<float> *appr_alg;
for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "--h" || std::string(argv[i]) == "--help") {
printHelpMessage();
return 0;
}
}
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--m" || std::string(argv[i]) == "--mode") {
if (std::string(argv[i + 1]) == "database") {
mode = "database";
} else if (std::string(argv[i + 1]) == "query") {
mode = "query";
} else {
printError("Unknown running mode \"" + std::string(argv[i + 1]) + "\". Please use \"database\" or \"query\"");
return 0;
}
break;
}
}
if (mode.empty()) {
printError("Running mode was not specified");
return 0;
}
std::cout << "Mode: " << mode << std::endl;
if (mode == "database") {
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--d" || std::string(argv[i]) == "--data" || std::string(argv[i]) == "--database") {
input.open(argv[i + 1], std::ios::binary);
if (!input.is_open()) {
printError("Cannot open file \"" + std::string(argv[i + 1]) + "\"");
return 0;
} else {
dataname = std::string(argv[i + 1]);
}
break;
}
}
if (!input.is_open()) {
printError("Database file was not specified");
return 0;
}
std::cout << "Database file: " << dataname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--dataSize" || std::string(argv[i]) == "--dSize" || std::string(argv[i]) == "--databaseSize") {
if (sscanf(argv[i + 1], "%d", &vecsize) != 1 || vecsize <= 0) {
printError("Inappropriate value for database size: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
if (vecsize == -1) {
printError("Database size was not specified");
return 0;
}
std::cout << "Database size: " << vecsize << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--dataDim" || std::string(argv[i]) == "--dimension" || std::string(argv[i]) == "--databaseDimension") {
if (sscanf(argv[i + 1], "%d", &vecdim) != 1 || vecdim <= 0) {
printError("Inappropriate value for database dimension: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
if (vecdim == -1) {
printError("Database dimension was not specified");
return 0;
}
std::cout << "Database dimension: " << vecdim << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--outGraph" || std::string(argv[i]) == "--outputGraph") {
std::ofstream outGraph(argv[i + 1]);
if (!outGraph.is_open()) {
printError("Cannot create file \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
outGraph.close();
graphname = std::string(argv[i + 1]);
break;
}
}
if (graphname.empty()) {
printError("Filename of the output graph was not specified");
return 0;
}
std::cout << "Output graph: " << graphname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--efConstruction") {
if (sscanf(argv[i + 1], "%d", &efConstruction) != 1 || efConstruction <= 0) {
printError("Inappropriate value for efConstruction: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
std::cout << "efConstruction: " << efConstruction << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--M") {
if (sscanf(argv[i + 1], "%d", &M) != 1 || M <= 0) {
printError("Inappropriate value for M: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
std::cout << "M: " << M << std::endl;
hnswlib::L2Space l2space(vecdim);
float *mass = new float[vecsize * vecdim];
input.read((char *)mass, vecsize * vecdim * sizeof(float));
input.close();
appr_alg = new hnswlib::HierarchicalNSW<float>(&l2space, vecsize, M, efConstruction);
std::cout << "Building index\n";
double t1 = omp_get_wtime();
for (int i = 0; i < 1; i++) {
appr_alg->addPoint((void *)(mass + vecdim*i), (size_t)i);
}
#pragma omp parallel for
for (int i = 1; i < vecsize; i++) {
appr_alg->addPoint((void *)(mass + vecdim*i), (size_t)i);
}
double t2 = omp_get_wtime();
std::cout << "Index built, time=" << t2 - t1 << " s" << "\n";
appr_alg->SaveIndex(graphname.data());
delete appr_alg;
delete mass;
} else {
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--q" || std::string(argv[i]) == "--query") {
inputQ.open(argv[i + 1], std::ios::binary);
if (!inputQ.is_open()) {
printError("Cannot open file \"" + std::string(argv[i + 1]) + "\"");
return 0;
} else {
queryname = std::string(argv[i + 1]);
}
break;
}
}
if (!inputQ.is_open()) {
printError("Query file was not specified");
return 0;
}
std::cout << "Query filename: " << queryname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--querySize" || std::string(argv[i]) == "--qSize") {
if (sscanf(argv[i + 1], "%d", &qsize) != 1 || qsize <= 0) {
printError("Inappropriate value for query size: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
if (qsize == -1) {
printError("Query size was not specified");
return 0;
}
std::cout << "Query size: " << qsize << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--queryDim" || std::string(argv[i]) == "--dimension" || std::string(argv[i]) == "--queryDimension") {
if (sscanf(argv[i + 1], "%d", &vecdim) != 1 || vecdim <= 0) {
printError("Inappropriate value for query dimension: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
if (vecdim == -1) {
printError("Query dimension was not specified");
return 0;
}
std::cout << "Query dimension: " << vecdim << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--inGraph" || std::string(argv[i]) == "--inputGraph") {
std::ifstream inGraph(argv[i + 1]);
if (!inGraph.is_open()) {
printError("Cannot open file \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
inGraph.close();
graphname = std::string(argv[i + 1]);
break;
}
}
if (graphname.empty()) {
printError("Filename of the input graph was not specified");
return 0;
}
std::cout << "Input graph: " << graphname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--efSearch") {
if (sscanf(argv[i + 1], "%d", &efSearch) != 1 || efSearch <= 0) {
printError("Inappropriate value for efSearch: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
std::cout << "efSearch: " << efSearch << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--topK") {
if (sscanf(argv[i + 1], "%d", &topK) != 1 || topK <= 0) {
printError("Inappropriate value for top size: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
std::cout << "Top size: " << topK << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--output" || std::string(argv[i]) == "--out") {
std::ofstream output(argv[i + 1]);
if (!output.is_open()) {
printError("Cannot open file \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
output.close();
outputname = std::string(argv[i + 1]);
break;
}
}
if (outputname.empty()) {
std::cout << "Output file: " << "stdout" << std::endl;
} else {
std::cout << "Output file: " << outputname << std::endl;
}
hnswlib::L2Space l2space(vecdim);
float *massQ = new float[qsize * vecdim];
inputQ.read((char *)massQ, qsize * vecdim * sizeof(float));
inputQ.close();
std::priority_queue< std::pair< float, labeltype >> gt[qsize];
appr_alg = new hnswlib::HierarchicalNSW<float>(&l2space, graphname.data(), false);
appr_alg->setEf(efSearch);
std::ofstream fres;
if (!outputname.empty()) {
fres.open(outputname);
}
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < qsize; i++) {
gt[i] = appr_alg->searchKnn(massQ + vecdim*i, topK);
}
auto end = std::chrono::high_resolution_clock::now();
for (int i = 0; i < qsize; i++) {
std::vector <int> res;
while (!gt[i].empty()) {
res.push_back(gt[i].top().second);
gt[i].pop();
}
std::reverse(res.begin(), res.end());
for (auto it: res) {
if (!outputname.empty()) {
fres << it << ' ';
} else {
std::cout << it << ' ';
}
}
if (!outputname.empty()) {
fres << std::endl;
} else {
std::cout << std::endl;
}
}
if (!outputname.empty()) {
fres.close();
}
std::cout << "Average query time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / (double)qsize << "ms" << std::endl;
delete appr_alg;
delete massQ;
}
return 0;
}