timeMatrixOps.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 <boost/format.hpp>
20 #include <boost/lambda/lambda.hpp>
21 
22 #include <gtsam/base/timing.h>
23 #include <gtsam/base/Matrix.h>
24 
25 #include <iostream>
26 #include <random>
27 #include <vector>
28 #include <utility>
29 
30 using namespace std;
31 //namespace ublas = boost::numeric::ublas;
32 //using namespace Eigen;
33 using boost::format;
34 using namespace boost::lambda;
35 
36 static std::mt19937 rng;
37 static std::uniform_real_distribution<> uniform(-1.0, 0.0);
38 //typedef ublas::matrix<double> matrix;
39 //typedef ublas::matrix_range<matrix> matrix_range;
40 //typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix;
41 //typedef Eigen::Block<matrix> matrix_block;
42 
43 //using ublas::range;
44 //using ublas::triangular_matrix;
45 
46 int main(int argc, char* argv[]) {
47 
48  if(true) {
49  cout << "\nTiming matrix_block:" << endl;
50 
51  // We use volatile here to make these appear to the optimizing compiler as
52  // if their values are only known at run-time.
53  volatile size_t m=500;
54  volatile size_t n=300;
55  volatile size_t nReps = 1000;
56  assert(m > n);
57  std::uniform_int_distribution<size_t> uniform_i(0,m-1);
58  std::uniform_int_distribution<size_t> uniform_j(0,n-1);
59  gtsam::Matrix mat((int)m,(int)n);
60  gtsam::SubMatrix full = mat.block(0, 0, m, n);
61  gtsam::SubMatrix top = mat.block(0, 0, n, n);
62  gtsam::SubMatrix block = mat.block(m/4, n/4, m-m/2, n-n/2);
63 
64  cout << format(" Basic: %1%x%2%\n") % (int)m % (int)n;
65  cout << format(" Full: mat(%1%:%2%, %3%:%4%)\n") % 0 % (int)m % 0 % (int)n;
66  cout << format(" Top: mat(%1%:%2%, %3%:%4%)\n") % 0 % (int)n % 0 % (int)n;
67  cout << format(" Block: mat(%1%:%2%, %3%:%4%)\n") % size_t(m/4) % size_t(m-m/4) % size_t(n/4) % size_t(n-n/4);
68  cout << endl;
69 
70  {
71  double basicTime, fullTime, topTime, blockTime;
72 
73  cout << "Row-major matrix, row-major assignment:" << endl;
74 
75  // Do a few initial assignments to let any cache effects stabilize
76  for(size_t rep=0; rep<1000; ++rep)
77  for(size_t i=0; i<(size_t)mat.rows(); ++i)
78  for(size_t j=0; j<(size_t)mat.cols(); ++j)
79  mat(i,j) = uniform(rng);
80 
81  gttic_(basicTime);
82  for(size_t rep=0; rep<nReps; ++rep)
83  for(size_t i=0; i<(size_t)mat.rows(); ++i)
84  for(size_t j=0; j<(size_t)mat.cols(); ++j)
85  mat(i,j) = uniform(rng);
86  gttoc_(basicTime);
87  tictoc_getNode(basicTimeNode, basicTime);
88  basicTime = basicTimeNode->secs();
90  cout << format(" Basic: %1% mus/element") % double(1000000 * basicTime / double(mat.rows()*mat.cols()*nReps)) << endl;
91 
92  gttic_(fullTime);
93  for(size_t rep=0; rep<nReps; ++rep)
94  for(size_t i=0; i<(size_t)full.rows(); ++i)
95  for(size_t j=0; j<(size_t)full.cols(); ++j)
96  full(i,j) = uniform(rng);
97  gttoc_(fullTime);
98  tictoc_getNode(fullTimeNode, fullTime);
99  fullTime = fullTimeNode->secs();
101  cout << format(" Full: %1% mus/element") % double(1000000 * fullTime / double(full.rows()*full.cols()*nReps)) << endl;
102 
103  gttic_(topTime);
104  for(size_t rep=0; rep<nReps; ++rep)
105  for(size_t i=0; i<(size_t)top.rows(); ++i)
106  for(size_t j=0; j<(size_t)top.cols(); ++j)
107  top(i,j) = uniform(rng);
108  gttoc_(topTime);
109  tictoc_getNode(topTimeNode, topTime);
110  topTime = topTimeNode->secs();
112  cout << format(" Top: %1% mus/element") % double(1000000 * topTime / double(top.rows()*top.cols()*nReps)) << endl;
113 
114  gttic_(blockTime);
115  for(size_t rep=0; rep<nReps; ++rep)
116  for(size_t i=0; i<(size_t)block.rows(); ++i)
117  for(size_t j=0; j<(size_t)block.cols(); ++j)
118  block(i,j) = uniform(rng);
119  gttoc_(blockTime);
120  tictoc_getNode(blockTimeNode, blockTime);
121  blockTime = blockTimeNode->secs();
123  cout << format(" Block: %1% mus/element") % double(1000000 * blockTime / double(block.rows()*block.cols()*nReps)) << endl;
124 
125  cout << endl;
126  }
127 
128  {
129  double basicTime, fullTime, topTime, blockTime;
130 
131  cout << "Row-major matrix, column-major assignment:" << endl;
132 
133  // Do a few initial assignments to let any cache effects stabilize
134  for(size_t rep=0; rep<1000; ++rep)
135  for(size_t j=0; j<(size_t)mat.cols(); ++j)
136  for(size_t i=0; i<(size_t)mat.rows(); ++i)
137  mat(i,j) = uniform(rng);
138 
139  gttic_(basicTime);
140  for(size_t rep=0; rep<nReps; ++rep)
141  for(size_t j=0; j<(size_t)mat.cols(); ++j)
142  for(size_t i=0; i<(size_t)mat.rows(); ++i)
143  mat(i,j) = uniform(rng);
144  gttoc_(basicTime);
145  tictoc_getNode(basicTimeNode, basicTime);
146  basicTime = basicTimeNode->secs();
148  cout << format(" Basic: %1% mus/element") % double(1000000 * basicTime / double(mat.rows()*mat.cols()*nReps)) << endl;
149 
150  gttic_(fullTime);
151  for(size_t rep=0; rep<nReps; ++rep)
152  for(size_t j=0; j<(size_t)full.cols(); ++j)
153  for(size_t i=0; i<(size_t)full.rows(); ++i)
154  full(i,j) = uniform(rng);
155  gttoc_(fullTime);
156  tictoc_getNode(fullTimeNode, fullTime);
157  fullTime = fullTimeNode->secs();
159  cout << format(" Full: %1% mus/element") % double(1000000 * fullTime / double(full.rows()*full.cols()*nReps)) << endl;
160 
161  gttic_(topTime);
162  for(size_t rep=0; rep<nReps; ++rep)
163  for(size_t j=0; j<(size_t)top.cols(); ++j)
164  for(size_t i=0; i<(size_t)top.rows(); ++i)
165  top(i,j) = uniform(rng);
166  gttoc_(topTime);
167  tictoc_getNode(topTimeNode, topTime);
168  topTime = topTimeNode->secs();
170  cout << format(" Top: %1% mus/element") % double(1000000 * topTime / double(top.rows()*top.cols()*nReps)) << endl;
171 
172  gttic_(blockTime);
173  for(size_t rep=0; rep<nReps; ++rep)
174  for(size_t j=0; j<(size_t)block.cols(); ++j)
175  for(size_t i=0; i<(size_t)block.rows(); ++i)
176  block(i,j) = uniform(rng);
177  gttoc_(blockTime);
178  tictoc_getNode(blockTimeNode, blockTime);
179  blockTime = blockTimeNode->secs();
181  cout << format(" Block: %1% mus/element") % double(1000000 * blockTime / double(block.rows()*block.cols()*nReps)) << endl;
182 
183  cout << endl;
184  }
185 
186  {
187  double basicTime, fullTime, topTime, blockTime;
188  typedef pair<size_t,size_t> ij_t;
189  vector<ij_t> ijs(100000);
190 
191  cout << "Row-major matrix, random assignment:" << endl;
192 
193  // Do a few initial assignments to let any cache effects stabilize
194  for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng),uniform_j(rng)));
195  for(size_t rep=0; rep<1000; ++rep)
196  for(const ij_t& ij: ijs) { mat(ij.first, ij.second) = uniform(rng); }
197 
198  gttic_(basicTime);
199  for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng),uniform_j(rng)));
200  for(size_t rep=0; rep<1000; ++rep)
201  for(const ij_t& ij: ijs) { mat(ij.first, ij.second) = uniform(rng); }
202  gttoc_(basicTime);
203  tictoc_getNode(basicTimeNode, basicTime);
204  basicTime = basicTimeNode->secs();
206  cout << format(" Basic: %1% mus/element") % double(1000000 * basicTime / double(ijs.size()*nReps)) << endl;
207 
208  gttic_(fullTime);
209  for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng),uniform_j(rng)));
210  for(size_t rep=0; rep<1000; ++rep)
211  for(const ij_t& ij: ijs) { full(ij.first, ij.second) = uniform(rng); }
212  gttoc_(fullTime);
213  tictoc_getNode(fullTimeNode, fullTime);
214  fullTime = fullTimeNode->secs();
216  cout << format(" Full: %1% mus/element") % double(1000000 * fullTime / double(ijs.size()*nReps)) << endl;
217 
218  gttic_(topTime);
219  for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng)%top.rows(),uniform_j(rng)));
220  for(size_t rep=0; rep<1000; ++rep)
221  for(const ij_t& ij: ijs) { top(ij.first, ij.second) = uniform(rng); }
222  gttoc_(topTime);
223  tictoc_getNode(topTimeNode, topTime);
224  topTime = topTimeNode->secs();
226  cout << format(" Top: %1% mus/element") % double(1000000 * topTime / double(ijs.size()*nReps)) << endl;
227 
228  gttic_(blockTime);
229  for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng)%block.rows(),uniform_j(rng)%block.cols()));
230  for(size_t rep=0; rep<1000; ++rep)
231  for(const ij_t& ij: ijs) { block(ij.first, ij.second) = uniform(rng); }
232  gttoc_(blockTime);
233  tictoc_getNode(blockTimeNode, blockTime);
234  blockTime = blockTimeNode->secs();
236  cout << format(" Block: %1% mus/element") % double(1000000 * blockTime / double(ijs.size()*nReps)) << endl;
237 
238  cout << endl;
239  }
240  }
241 
242 // if(true) {
243 // cout << "\nTesting square triangular matrices:" << endl;
244 //
247 // typedef MatrixXd matrix; // default col major
248 //
250 //
251 // matrix mat(5,5);
252 // for(size_t j=0; j<(size_t)mat.cols(); ++j)
253 // for(size_t i=0; i<(size_t)mat.rows(); ++i)
254 // mat(i,j) = uniform(rng);
255 //
256 // tri = ublas::triangular_adaptor<matrix, ublas::upper>(mat);
257 // cout << " Assigned from triangular adapter: " << tri << endl;
258 //
259 // cout << " Triangular adapter of mat: " << ublas::triangular_adaptor<matrix, ublas::upper>(mat) << endl;
260 //
261 // for(size_t j=0; j<(size_t)mat.cols(); ++j)
262 // for(size_t i=0; i<(size_t)mat.rows(); ++i)
263 // mat(i,j) = uniform(rng);
264 // mat = tri;
265 // cout << " Assign matrix from triangular: " << mat << endl;
266 //
267 // for(size_t j=0; j<(size_t)mat.cols(); ++j)
268 // for(size_t i=0; i<(size_t)mat.rows(); ++i)
269 // mat(i,j) = uniform(rng);
270 // (ublas::triangular_adaptor<matrix, ublas::upper>(mat)) = tri;
271 // cout << " Assign triangular adaptor from triangular: " << mat << endl;
272 // }
273 
274 // {
275 // cout << "\nTesting wide triangular matrices:" << endl;
276 //
277 // typedef triangular_matrix<double, ublas::upper, ublas::column_major> triangular;
278 // typedef ublas::matrix<double, ublas::column_major> matrix;
279 //
280 // triangular tri(5,7);
281 //
282 // matrix mat(5,7);
283 // for(size_t j=0; j<(size_t)mat.cols(); ++j)
284 // for(size_t i=0; i<(size_t)mat.rows(); ++i)
285 // mat(i,j) = uniform(rng);
286 //
287 // tri = ublas::triangular_adaptor<matrix, ublas::upper>(mat);
288 // cout << " Assigned from triangular adapter: " << tri << endl;
289 //
290 // cout << " Triangular adapter of mat: " << ublas::triangular_adaptor<matrix, ublas::upper>(mat) << endl;
291 //
292 // for(size_t j=0; j<(size_t)mat.cols(); ++j)
293 // for(size_t i=0; i<(size_t)mat.rows(); ++i)
294 // mat(i,j) = uniform(rng);
295 // mat = tri;
296 // cout << " Assign matrix from triangular: " << mat << endl;
297 //
298 // for(size_t j=0; j<(size_t)mat.cols(); ++j)
299 // for(size_t i=0; i<(size_t)mat.rows(); ++i)
300 // mat(i,j) = uniform(rng);
301 // mat = ublas::triangular_adaptor<matrix, ublas::upper>(mat);
302 // cout << " Assign matrix from triangular adaptor of self: " << mat << endl;
303 // }
304 
305 // {
306 // cout << "\nTesting subvectors:" << endl;
307 //
308 // typedef MatrixXd matrix;
309 // matrix mat(4,4);
310 //
311 // for(size_t j=0; j<(size_t)mat.cols(); ++j)
312 // for(size_t i=0; i<(size_t)mat.rows(); ++i)
313 // mat(i,j) = i*mat.rows() + j;
314 // cout << " mat = " << mat;
315 //
316 // cout << " vec(1:4, 2:2) = " << mat.block(1,2, ), ublas::range(1,4), ublas::range(2,2));
317 //
318 // }
319 
320  return 0;
321 
322 }
Matrix3f m
#define tictoc_getNode(variable, label)
Definition: timing.h:261
m m block(1, 0, 2, 2)<< 4
#define gttic_(label)
Definition: timing.h:230
return int(ret)+1
void tictoc_reset_()
Definition: timing.h:267
static std::mt19937 rng
int n
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
Definition: Half.h:150
static std::uniform_real_distribution uniform(-1.0, 0.0)
int main(int argc, char *argv[])
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
#define gttoc_(label)
Definition: timing.h:235
std::ptrdiff_t j
Timing utilities.


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