Permalink
Browse files

added prune2 back

  • Loading branch information...
wdong
wdong committed Jun 2, 2016
1 parent 0285623 commit 8fd8d4b9e1eac4750680ca7887c8aeb03ed091a9
Showing with 84 additions and 37 deletions.
  1. +1 −1 Makefile
  2. +59 −36 kgraph.cpp
  3. +24 −0 resave.cpp
@@ -5,7 +5,7 @@ OPENMP=-fopenmp
VERSION=$(shell git describe --always)
BUILD_INFO=-DKGRAPH_VERSION=\"$(VERSION)\" -DKGRAPH_BUILD_ID=\"$(BUILD_ID)\" -DKGRAPH_BUILD_NUMBER=\"$(BUILD_NUMBER)\"
CXXFLAGS+=$(BUILD_INFO) -fPIC -Wall -g -std=c++11 -I. $(OPENMP) $(OPT) $(ARCH)
LDFLAGS+=$(OPENMP)
LDFLAGS+=$(OPENMP) -static
LDLIBS+=-lboost_timer -lboost_chrono -lboost_system -lboost_program_options -lgomp -lm -lrt
FLANN_LIBS+=-lflann_cpp_s -lflann_s
NABO_LIBS+=-lnabo
@@ -524,50 +524,73 @@ namespace kgraph {
}
void prune2 () {
#if 0
vector<vector<unsigned>> new_graph(graph.size());
vector<unsigned> new_M(graph.size());
vector<vector<unsigned>> reverse(graph.size());
vector<unordered_set<unsigned>> todo(graph.size());
vector<vector<unsigned>> reverse(graph.size()); // reverse of new graph
vector<unsigned> new_L(graph.size(), 0);
unsigned L = 0;
{
cerr << "Level 2 Prune, stage 1/2 ..." << endl;
progress_display progress(graph.size(), cerr);
for (unsigned i = 0; i < graph.size(); ++i) {
if (graph[i].size() > L) L = graph[i].size();
todo[i] = unordered_set<unsigned>(graph[i].begin(), graph[i].end());
unsigned total = 0;
for (unsigned i = 0; i < graph.size(); ++i) {
if (M[i] > L) L = M[i];
total += M[i];
for (auto &e: graph[i]) {
e.flag = false; // haven't been visited yet
}
}
progress_display progress(total, cerr);
vector<unsigned> todo(graph.size());
for (unsigned i = 0; i < todo.size(); ++i) todo[i] = i;
vector<unsigned> new_todo(graph.size());
for (unsigned l = 0; todo.size(); ++l) {
BOOST_VERIFY(l <= L);
new_todo.clear();
for (unsigned i: todo) {
if (l >= M[i]) continue;
new_todo.push_back(i);
auto &v = graph[i];
BOOST_VERIFY(l < v.size());
if (v[l].flag) continue; // we have visited this one already
v[l].flag = true; // now we have seen this one
++progress;
}
}
{
cerr << "Level 2 Prune, stage 2/2 ..." << endl;
progress_display progress(L, cerr);
for (unsigned l = 0; l < L; ++l) {
for (unsigned i = 0; i < graph.size(); ++i) {
if (todo[i].empty()) continue;
BOOST_VERIFY(l < graph[i].size());
unsigned T = graph[i][l];
if (todo[i].erase(T)) { // still there, need to be added
new_graph[i].push_back(T);
reverse[T].push_back(i);
// mark newly reachable nodes
for (auto n2: new_graph[T]) {
todo[i].erase(n2);
}
for (auto r: reverse[i]) {
todo[r].erase(T);
unsigned T;
{
auto &nl = new_L[i];
// shift the entry to add
T = v[nl].id = v[l].id;
v[nl].dist = v[l].dist;
++nl;
}
reverse[T].push_back(i);
{
auto const &u = graph[T];
for (unsigned ll = l + 1; ll < M[i]; ++ll) {
if (v[ll].flag) continue;
for (unsigned j = 0; j < new_L[T]; ++j) { // new graph
if (v[ll].id == u[j].id) {
v[ll].flag = true;
++progress;
break;
}
}
}
if (l + 1 == M[i]) {
new_M[i] = new_graph[i].size();
}
{
for (auto r: reverse[i]) {
auto &u = graph[r];
for (unsigned ll = l; ll < M[r]; ++ll) {
// must start from l: as item l might not have been checked
// for reverse
if (u[ll].id == T) {
if (!u[ll].flag) ++progress;
u[ll].flag = true;
}
}
}
}
++progress;
}
todo.swap(new_todo);
}
graph.swap(new_graph);
M.swap(new_M);
#endif
BOOST_VERIFY(progress.count() == total);
M.swap(new_L);
prune1();
}
virtual void prune (IndexOracle const &oracle, unsigned level) {
@@ -21,16 +21,36 @@ using namespace kgraph;
namespace po = boost::program_options;
class DummyIndexOracle: public IndexOracle {
public:
/// Returns the size of the dataset.
virtual unsigned size () const {
throw 0;
return 0;
}
/// Computes similarity
/**
* 0 <= i, j < size() are the index of two objects in the dataset.
* This method return the distance between objects i and j.
*/
virtual float operator () (unsigned i, unsigned j) const {
throw 0;
return 0;
}
};
int main (int argc, char *argv[]) {
string input_path;
string output_path;
int format = 0;
int prune = 0;
po::options_description desc("General options");
desc.add_options()
("help,h", "produce help message.")
("input", po::value(&input_path), "")
("output", po::value(&output_path), "")
("prune", po::value(&prune), "")
("no-dist", "")
;
@@ -51,6 +71,10 @@ int main (int argc, char *argv[]) {
KGraph *kgraph = kgraph::KGraph::create();
kgraph->load(input_path.c_str());
if (vm.count("prune")) {
DummyIndexOracle o;
kgraph->prune(o, prune);
}
if (vm.count("output")) {
kgraph->save(output_path.c_str(), format);

0 comments on commit 8fd8d4b

Please sign in to comment.