-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
358 lines (291 loc) · 12 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
/*
*** A proof-of-concept program for lightweight fingerprints for fast approximate keyword matching using bitwise operations.
*** Authors: Aleksander Cisłak, Szymon Grabowski. License: GNU LGPL v3.
*** Set BOOST_DIR in makefile and type "make" for optimized compile.
*/
#include <boost/format.hpp>
#include <boost/program_options.hpp>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
#include "fingerprints.hpp"
#include "fingerprints.cpp"
#include "helpers.hpp"
#include "params.hpp"
using namespace fingerprints;
using namespace std;
namespace po = boost::program_options;
namespace fingerprints
{
namespace
{
Params params;
using FING_T = uint16_t;
/** Indicates that program execution should continue after checking parameters. */
constexpr int paramsResContinue = -1;
}
/** Handles cmd-line parameters, returns paramsResContinue if program execution should continue. */
int handleParams(int argc, const char **argv);
/** Returns true if input files are readable, false otherwise. */
bool checkInputFiles(const char *execName);
/** Runs the main program and returns the program exit code. */
int run();
/** Filters input data (dictionary and patterns) based on cmd-line parameters. */
void filterInput(vector<string> &dict, vector<string> &patterns);
void runFingerprints(const vector<string> &words, const vector<string> &patterns);
void initFingerprintParams(Fingerprints<FING_T>::DistanceType &distanceType,
Fingerprints<FING_T>::FingerprintType &fingerprintType,
Fingerprints<FING_T>::LettersType &lettersType);
void dumpParamInfoToStdout(int fingSizeB);
void dumpRunInfo(float elapsedUs, const vector<string> &words, size_t processedWordsCount);
}
int main(int argc, const char **argv)
{
int paramsRes = handleParams(argc, argv);
if (paramsRes != paramsResContinue)
{
return paramsRes;
}
if (checkInputFiles(argv[0]) == false)
{
return params.errorExitCode;
}
return run();
}
namespace fingerprints
{
int handleParams(int argc, const char **argv)
{
po::options_description options("Parameters");
options.add_options()
("calc-rejection", "calculate percentages of rejected words instead of measuring time")
("dump,d", "dump input files and params info with elapsed time and throughput to output file (useful for testing)")
("dump-construction", "dump fingerprint construction time")
("distance,D", po::value<string>(¶ms.distanceType)->default_value("ham"), "distance metric: ham (Hamming), lev (Levenshtein)")
("fingerprint-type,f", po::value<string>(¶ms.fingerprintType)->default_value("occ"), "fingerprint type: none, occ (occurrence), occhalved (occurrence halved), count, pos (position)")
("help,h", "display help message")
("in-dict-file,i", po::value<string>(¶ms.inDictFile)->required(), "input dictionary file path (positional arg 1)")
("in-pattern-file,I", po::value<string>(¶ms.inPatternFile)->required(), "input pattern file path (positional arg 2)")
("iter", po::value<int>(¶ms.nIter)->default_value(1), "number of iterations per pattern lookup")
("approx,k", po::value<int>(¶ms.kApprox)->required(), "perform approximate search (Hamming or Levenshtein) for k errors")
("letters-type,l", po::value<string>(¶ms.lettersType)->default_value("common"), "letters type: common, mixed, rare")
("out-file,o", po::value<string>(¶ms.outFile)->default_value("res.txt"), "output file path")
("pattern-count,p", po::value<int>(¶ms.nPatterns), "maximum number of patterns read from top of the pattern file (non-positive values are ignored)")
("pattern-size", po::value<int>(¶ms.patternSize), "if set, only patterns of this size (letter count) will be read from the pattern file (non-positive values are ignored)")
// Not using a default value from Boost for separator because it literally prints a newline.
("separator,s", po::value<string>(¶ms.separator), "input data (dictionary and patterns) separator (default = newline)")
("version,v", "display version info")
("word-count,w", po::value<int>(¶ms.nWords), "maximum number of words read from top of the dictionary file (non-positive values are ignored)");
po::positional_options_description positionalOptions;
positionalOptions.add("in-dict-file", 1);
positionalOptions.add("in-pattern-file", 1);
po::variables_map vm;
try
{
po::store(po::command_line_parser(argc, argv).
options(options).
positional(positionalOptions).run(), vm);
if (vm.count("help"))
{
cout << "Usage: " << argv[0] << " " << params.usageInfoString << endl << endl;
cout << options << endl;
return 0;
}
if (vm.count("version"))
{
cout << params.versionInfo << endl;
return 0;
}
po::notify(vm);
}
catch (const po::error &e)
{
cerr << "Usage: " << argv[0] << " " << params.usageInfoString << endl << endl;
cerr << options << endl;
cerr << "Error: " << e.what() << endl;
return params.errorExitCode;
}
if (vm.count("calc-rejection"))
{
params.calcRejection = true;
}
if (vm.count("dump-construction"))
{
params.dumpConstruction = true;
}
if (vm.count("dump"))
{
params.dumpToFile = true;
}
return paramsResContinue;
}
bool checkInputFiles(const char *execName)
{
if (Helpers::isFileReadable(params.inDictFile) == false)
{
cerr << "Cannot access input dictionary file (doesn't exist or insufficient permissions): " << params.inDictFile << endl;
cerr << "Run " << execName << " -h for more information" << endl << endl;
return false;
}
if (Helpers::isFileReadable(params.inPatternFile) == false)
{
cerr << "Cannot access input patterns file (doesn't exist or insufficient permissions): " << params.inPatternFile << endl;
cerr << "Run " << execName << " -h for more information" << endl << endl;
return false;
}
return true;
}
int run()
{
try
{
vector<string> dict = Helpers::readWords(params.inDictFile, params.separator);
vector<string> patterns = Helpers::readWords(params.inPatternFile, params.separator);
filterInput(dict, patterns);
cout << "=====" << endl;
cout << boost::format("Read #words = %1%, #queries = %2%") % dict.size() % patterns.size() << endl;
runFingerprints(dict, patterns);
}
catch (const exception &e)
{
cerr << endl << "Fatal error occurred: " << e.what() << endl;
return params.errorExitCode;
}
return 0;
}
void filterInput(vector<string> &dict, vector<string> &patterns)
{
if (params.nWords > 0 and static_cast<size_t>(params.nWords) < dict.size())
{
dict.resize(params.nWords);
}
if (params.nPatterns > 0 and static_cast<size_t>(params.nPatterns) < patterns.size())
{
patterns.resize(params.nPatterns);
}
if (params.patternSize > 0)
{
Helpers::filterWordsBySize(patterns, params.patternSize);
}
}
void runFingerprints(const vector<string> &words, const vector<string> &patterns)
{
dumpParamInfoToStdout(sizeof(FING_T));
Fingerprints<FING_T>::DistanceType distanceType;
Fingerprints<FING_T>::FingerprintType fingerprintType;
Fingerprints<FING_T>::LettersType lettersType;
initFingerprintParams(distanceType, fingerprintType, lettersType);
Fingerprints<FING_T> fingerprints(distanceType, fingerprintType, lettersType);
fingerprints.preprocess(words);
cout << "Preprocessed #words = " << words.size() << endl;
if (params.dumpConstruction)
{
float elapsedTotalUs = fingerprints.getElapsedUs();
size_t dictSizeB = Helpers::getTotalSize(words);
float dictSizeMB = static_cast<float>(dictSizeB) / 1'000'000.0f;
float throughputMBs = dictSizeMB / (elapsedTotalUs / 1'000'000.0f);
cout << boost::format("Construction: thru = %1% MB/s, elapsed = %2% us")
% throughputMBs % elapsedTotalUs << endl;
}
cout << "Testing #queries = " << patterns.size() << endl;
if (params.calcRejection)
{
float rejectedFrac = fingerprints.testRejection(patterns, params.kApprox);
cout << boost::format("Rejected ratio = %1%%%") % (100.0f * rejectedFrac) << endl;
}
else
{
int nMatches = fingerprints.test(patterns, params.kApprox, params.nIter, false);
cout << "Got #matches = " << nMatches << endl;
float elapsedTotalUs = fingerprints.getElapsedUs();
float elapsedPerIterUs = elapsedTotalUs / static_cast<float>(params.nIter);
size_t processedWordsCount = fingerprints.getProcessedWordsCount();
cout << "Total (all patterns) processed #words = " << processedWordsCount << endl;
dumpRunInfo(elapsedPerIterUs, words, processedWordsCount);
}
}
void initFingerprintParams(Fingerprints<FING_T>::DistanceType &distanceType,
Fingerprints<FING_T>::FingerprintType &fingerprintType,
Fingerprints<FING_T>::LettersType &lettersType)
{
if (params.distanceType == "ham")
{
distanceType = Fingerprints<FING_T>::DistanceType::Ham;
}
else if (params.distanceType == "lev")
{
distanceType = Fingerprints<FING_T>::DistanceType::Lev;
}
else
{
throw invalid_argument("bad distance type: " + params.distanceType);
}
if (params.fingerprintType == "none")
{
fingerprintType = Fingerprints<FING_T>::FingerprintType::None;
}
else if (params.fingerprintType == "occ")
{
fingerprintType = Fingerprints<FING_T>::FingerprintType::Occ;
}
else if (params.fingerprintType == "occhalved")
{
fingerprintType = Fingerprints<FING_T>::FingerprintType::OccHalved;
}
else if (params.fingerprintType == "count")
{
fingerprintType = Fingerprints<FING_T>::FingerprintType::Count;
}
else if (params.fingerprintType == "pos")
{
fingerprintType = Fingerprints<FING_T>::FingerprintType::Pos;
}
else
{
throw invalid_argument("bad fingerprint type: " + params.fingerprintType);
}
if (params.lettersType == "common")
{
lettersType = Fingerprints<FING_T>::LettersType::Common;
}
else if (params.lettersType == "mixed")
{
lettersType = Fingerprints<FING_T>::LettersType::Mixed;
}
else if (params.lettersType == "rare")
{
lettersType = Fingerprints<FING_T>::LettersType::Rare;
}
else
{
throw invalid_argument("bad letters type: " + params.lettersType);
}
}
void dumpParamInfoToStdout(int fingSizeB)
{
cout << "Using distance: " << params.distanceType << endl;
cout << boost::format("Using fingerprint type: %1%, size = %2% bytes") % params.fingerprintType % fingSizeB << endl;
if (params.patternSize != Params::noValue)
{
cout << "Using pattern size = " << params.patternSize << endl;
}
cout << "Using letters type: " << params.lettersType << endl;
cout << "Using k = " << params.kApprox << endl;
cout << "#iterations = " << params.nIter << endl << endl;
}
void dumpRunInfo(float elapsedUs, const vector<string> &words, size_t processedWordsCount)
{
size_t dictSizeB = Helpers::getTotalSize(words);
float dictSizeMB = static_cast<float>(dictSizeB) / 1'000'000.0f;
float elapsedPerWordNs = (1'000.0f * elapsedUs) / static_cast<float>(processedWordsCount);
cout << boost::format("Elapsed = %1% us, per word = %2% ns") % elapsedUs % elapsedPerWordNs << endl;
if (params.dumpToFile)
{
string outStr = (boost::format("%1% %2% %3% %4% %5% %6% %7%") % params.inDictFile % params.inPatternFile % dictSizeMB
% params.distanceType % params.fingerprintType % params.lettersType % elapsedPerWordNs).str();
Helpers::dumpToFile(outStr, params.outFile, true);
cout << "Dumped info to: " << params.outFile << endl << endl;
}
}
} // namespace fingerprints