ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
flann_mpi_client.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <time.h>
3 
4 #include <cstdlib>
5 #include <iostream>
6 #include <FLANN/util/params.h>
7 #include <FLANN/io/hdf5.h>
8 #include <FLANN/mpi/client.h>
9 
10 
11 
12 #define IF_RANK0 if (world.rank()==0)
13 
14 timeval start_time_;
15 void start_timer(const std::string& message = "")
16 {
17  if (!message.empty()) {
18  printf("%s", message.c_str());
19  fflush(stdout);
20  }
21  gettimeofday(&start_time_,NULL);
22 }
23 
24 double stop_timer()
25 {
26  timeval end_time;
27  gettimeofday(&end_time,NULL);
28 
29  return double(end_time.tv_sec-start_time_.tv_sec)+ double(end_time.tv_usec-start_time_.tv_usec)/1000000;
30 }
31 
32 float compute_precision(const flann::Matrix<int>& match, const flann::Matrix<int>& indices)
33 {
34  int count = 0;
35 
36  assert(match.rows == indices.rows);
37  size_t nn = std::min(match.cols, indices.cols);
38 
39  for(size_t i=0; i<match.rows; ++i) {
40  for (size_t j=0;j<nn;++j) {
41  for (size_t k=0;k<nn;++k) {
42  if (match[i][j]==indices[i][k]) {
43  count ++;
44  }
45  }
46  }
47  }
48 
49  return float(count)/(nn*match.rows);
50 }
51 
52 
53 int main(int argc, char* argv[])
54 {
55  try {
56 
58  flann::Matrix<int> match;
59 
60  flann::load_from_file(query, "sift100K.h5","query");
61  flann::load_from_file(match, "sift100K.h5","match");
62  // flann::load_from_file(gt_dists, "sift100K.h5","dists");
63 
64  flann::mpi::Client index("localhost","9999");
65 
66  int nn = 1;
67  flann::Matrix<int> indices(new int[query.rows*nn], query.rows, nn);
68  flann::Matrix<float> dists(new float[query.rows*nn], query.rows, nn);
69 
70  start_timer("Performing search...\n");
71  index.knnSearch(query, indices, dists, nn, flann::SearchParams(64));
72  printf("Search done (%g seconds)\n", stop_timer());
73 
74  printf("Checking results\n");
75  float precision = compute_precision(match, indices);
76  printf("Precision is: %g\n", precision);
77 
78  }
79  catch (std::exception& e) {
80  std::cerr << "Exception: " << e.what() << "\n";
81  }
82 
83  return 0;
84 }
85 
int count
#define NULL
size_t cols
Definition: matrix.h:73
size_t rows
Definition: matrix.h:72
void knnSearch(const flann::Matrix< ElementType > &queries, flann::Matrix< int > &indices, flann::Matrix< DistanceType > &dists, int knn, const SearchParams &params)
Definition: client.h:55
int min(int a, int b)
Definition: cutil_math.h:53
int main(int argc, char *argv[])
double stop_timer()
float compute_precision(const flann::Matrix< int > &match, const flann::Matrix< int > &indices)
timeval start_time_
void start_timer(const std::string &message="")
void load_from_file(flann::Matrix< T > &dataset, const std::string &filename, const std::string &name)
Definition: hdf5.h:130