timeDSFvariants.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #include <gtsam/base/timing.h>
20 #include <gtsam/base/DSFVector.h>
22 #include <gtsam/base/DSFMap.h>
23 
24 #include <boost/format.hpp>
25 #include <boost/assign/std/vector.hpp>
26 
27 #include <fstream>
28 #include <iostream>
29 #include <random>
30 #include <vector>
31 #include <utility>
32 
33 using namespace std;
34 using namespace gtsam;
35 using namespace boost::assign;
36 using boost::format;
37 
38 int main(int argc, char* argv[]) {
39 
40  // Create CSV file for results
41  ofstream os("dsf-timing.csv");
42  os << "images,points,matches,Base,Map,BTree" << endl;
43 
44  // loop over number of images
45  vector<size_t> ms;
46  ms += 10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 1000;
47  for(size_t m: ms) {
48  // We use volatile here to make these appear to the optimizing compiler as
49  // if their values are only known at run-time.
50  volatile size_t n = 500; // number of points per image
51  volatile size_t N = m * n; // number of points per image
52 
53  volatile double fm = 0.1; // fraction of image pairs matched
54  volatile size_t np = fm * m * m / 2; // number of image pairs
55  volatile double fpm = 0.5; // fraction of points matched
56  volatile size_t nm = fpm * n * np; // number of matches
57 
58  cout << format("\nTesting with %1% images, %2% points, %3% matches\n")
59  % (int)m % (int)N % (int)nm;
60  cout << "Generating " << nm << " matches" << endl;
61  std::mt19937 rng;
62  std::uniform_int_distribution<> rn(0, N - 1);
63 
64  typedef pair<size_t, size_t> Match;
65  vector<Match> matches;
66  matches.reserve(nm);
67  for (size_t k = 0; k < nm; k++)
68  matches.push_back(Match(rn(rng), rn(rng)));
69 
70  os << format("%1%,%2%,%3%,") % (int)m % (int)N % (int)nm;
71 
72  {
73  // DSFBase version
74  double dsftime = 0;
75  gttic_(dsftime);
76  DSFBase dsf(N); // Allow for N keys
77  for(const Match& m: matches)
78  dsf.merge(m.first, m.second);
79  gttoc_(dsftime);
80  tictoc_getNode(dsftimeNode, dsftime);
81  dsftime = dsftimeNode->secs();
82  os << dsftime << ",";
83  cout << format("DSFBase: %1% s") % dsftime << endl;
84  tictoc_reset_();
85  }
86 
87  {
88  // DSFMap version
89  double dsftime = 0;
90  gttic_(dsftime);
91  DSFMap<size_t> dsf;
92  for(const Match& m: matches)
93  dsf.merge(m.first, m.second);
94  gttoc_(dsftime);
95  tictoc_getNode(dsftimeNode, dsftime);
96  dsftime = dsftimeNode->secs();
97  os << dsftime << endl;
98  cout << format("DSFMap: %1% s") % dsftime << endl;
99  tictoc_reset_();
100  }
101 
102  if (false) {
103  // DSF version, functional
104  double dsftime = 0;
105  gttic_(dsftime);
106  DSF<size_t> dsf;
107  for (size_t j = 0; j < N; j++)
108  dsf = dsf.makeSet(j);
109  for(const Match& m: matches)
110  dsf = dsf.makeUnion(m.first, m.second);
111  gttoc_(dsftime);
112  tictoc_getNode(dsftimeNode, dsftime);
113  dsftime = dsftimeNode->secs();
114  os << dsftime << endl;
115  cout << format("DSF functional: %1% s") % dsftime << endl;
116  tictoc_reset_();
117  }
118 
119  if (false) {
120  // DSF version, in place - always slower - use functional !
121  double dsftime = 0;
122  gttic_(dsftime);
123  DSF<size_t> dsf;
124  for (size_t j = 0; j < N; j++)
125  dsf.makeSetInPlace(j);
126  for(const Match& m: matches)
127  dsf.makeUnionInPlace(m.first, m.second);
128  gttoc_(dsftime);
129  tictoc_getNode(dsftimeNode, dsftime);
130  dsftime = dsftimeNode->secs();
131  os << dsftime << ",";
132  cout << format("DSF in-place: %1% s") % dsftime << endl;
133  tictoc_reset_();
134  }
135 
136  }
137 
138  return 0;
139 
140 }
Matrix3f m
#define tictoc_getNode(variable, label)
Definition: timing.h:261
#define gttic_(label)
Definition: timing.h:230
return int(ret)+1
void tictoc_reset_()
Definition: timing.h:267
void merge(const KEY &x, const KEY &y)
Merge two sets.
Definition: DSFMap.h:87
static std::mt19937 rng
int n
void makeSetInPlace(const KEY &key)
Definition: DSF.h:80
Definition: Half.h:150
static const double ms
#define N
Definition: gksort.c:12
Self makeSet(const KEY &key) const
Definition: DSF.h:72
A faster implementation for DSF, which uses vector rather than btree.
void makeUnionInPlace(const KEY &key1, const KEY &key2)
Definition: DSF.h:99
An implementation of Disjoint set forests (see CLR page 446 and up)
int main(int argc, char *argv[])
traits
Definition: chartTesting.h:28
ofstream os("timeSchurFactors.csv")
void merge(const size_t &i1, const size_t &i2)
Merge the sets containing i1 and i2. Does nothing if i1 and i2 are already in the same set...
Definition: DSFVector.cpp:54
Allow for arbitrary type in DSF.
#define gttoc_(label)
Definition: timing.h:235
std::ptrdiff_t j
Timing utilities.
Self makeUnion(const KEY &key1, const KEY &key2) const
Definition: DSF.h:92
Definition: DSF.h:39


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:50:33