-
Notifications
You must be signed in to change notification settings - Fork 85
/
prune.cpp
113 lines (94 loc) · 3.01 KB
/
prune.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
/*
Copyright (C) 2013,2014 Wei Dong <[email protected]>. All Rights Reserved.
*/
#ifndef KGRAPH_VALUE_TYPE
#define KGRAPH_VALUE_TYPE float
#endif
#include <sys/time.h>
#include <cctype>
#include <random>
#include <iomanip>
#include <iostream>
#include <type_traits>
#include <boost/timer/timer.hpp>
//#include <boost/tr1/random.hpp>
#include <boost/format.hpp>
#include <boost/program_options.hpp>
#include "kgraph.h"
#include "kgraph-data.h"
using namespace std;
using namespace boost;
using namespace boost::timer;
using namespace kgraph;
namespace po = boost::program_options;
typedef KGRAPH_VALUE_TYPE value_type;
int main (int argc, char *argv[]) {
string data_path;
string input_path;
string output_path;
unsigned prune;
unsigned D;
unsigned skip;
unsigned gap;
bool lshkit = true;
po::options_description desc_visible("General options");
desc_visible.add_options()
("help,h", "produce help message.")
("data", po::value(&data_path), "input path")
("input", po::value(&input_path), "input index path")
("output", po::value(&output_path), "output index path")
("prune", po::value(&prune)->default_value(PRUNE_LEVEL_1 & PRUNE_LEVEL_2), "prune level")
;
po::options_description desc_hidden("Expert options");
desc_hidden.add_options()
("dim,D", po::value(&D), "dimension, see format")
("skip", po::value(&skip)->default_value(0), "see format")
("gap", po::value(&gap)->default_value(0), "see format")
("raw", "read raw binary file, need to specify D.")
;
po::options_description desc("Allowed options");
desc.add(desc_visible).add(desc_hidden);
po::positional_options_description p;
p.add("data", 1);
p.add("input", 1);
p.add("output", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("raw") == 1) {
lshkit = false;
}
if (vm.count("help")
|| vm.count("input") == 0) {
cout << "Usage: prune [OTHER OPTIONS]... DATA INPUT [OUTPUT]" << endl;
cout << desc_visible << endl;
cout << desc_hidden << endl;
return 0;
}
if (lshkit) {
static const unsigned LSHKIT_HEADER = 3;
ifstream is(data_path.c_str(), ios::binary);
unsigned header[LSHKIT_HEADER]; /* entry size, row, col */
is.read((char *)header, sizeof header);
BOOST_VERIFY(is);
BOOST_VERIFY(header[0] == sizeof(value_type));
is.close();
D = header[2];
skip = LSHKIT_HEADER * sizeof(unsigned);
gap = 0;
}
Matrix<value_type> data;
data.load(data_path, D, skip, gap);
MatrixOracle<value_type, metric::l2sqr> oracle(data);
KGraph *kgraph = KGraph::create(); //(oracle, params, &info);
kgraph->load(input_path.c_str());
{
auto_cpu_timer timer;
kgraph->prune(oracle, prune);
}
if (output_path.size()) {
kgraph->save(output_path.c_str());
}
delete kgraph;
return 0;
}