File: timeMatrixOps.cpp

package info (click to toggle)
gtsam 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,108 kB
  • sloc: cpp: 127,191; python: 14,312; xml: 8,442; makefile: 252; sh: 119; ansic: 101
file content (322 lines) | stat: -rw-r--r-- 12,256 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* ----------------------------------------------------------------------------

 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
 * Atlanta, Georgia 30332-0415
 * All Rights Reserved
 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)

 * See LICENSE for the license information

 * -------------------------------------------------------------------------- */

/**
 * @file    timeublas.cpp
 * @brief   Tests to help determine which way of accomplishing something with Eigen is faster
 * @author  Richard Roberts
 * @date    Sep 18, 2010
 */

#include <boost/format.hpp>
#include <boost/lambda/lambda.hpp>

#include <gtsam/base/timing.h>
#include <gtsam/base/Matrix.h>

#include <iostream>
#include <random>
#include <vector>
#include <utility>

using namespace std;
//namespace ublas = boost::numeric::ublas;
//using namespace Eigen;
using boost::format;
using namespace boost::lambda;

static std::mt19937 rng;
static std::uniform_real_distribution<> uniform(-1.0, 0.0);
//typedef ublas::matrix<double> matrix;
//typedef ublas::matrix_range<matrix> matrix_range;
//typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix;
//typedef Eigen::Block<matrix> matrix_block;

//using ublas::range;
//using ublas::triangular_matrix;

int main(int argc, char* argv[]) {

  if(true) {
    cout << "\nTiming matrix_block:" << endl;

    // We use volatile here to make these appear to the optimizing compiler as
    // if their values are only known at run-time.
    volatile size_t m=500;
    volatile size_t n=300;
    volatile size_t nReps = 1000;
    assert(m > n);
    std::uniform_int_distribution<size_t> uniform_i(0,m-1);
    std::uniform_int_distribution<size_t> uniform_j(0,n-1);
    gtsam::Matrix mat((int)m,(int)n);
    gtsam::SubMatrix full = mat.block(0, 0, m, n);
    gtsam::SubMatrix top = mat.block(0, 0, n, n);
    gtsam::SubMatrix block = mat.block(m/4, n/4, m-m/2, n-n/2);

    cout << format("  Basic: %1%x%2%\n") % (int)m % (int)n;
    cout << format("  Full:  mat(%1%:%2%, %3%:%4%)\n") % 0 % (int)m % 0 % (int)n;
    cout << format("  Top:   mat(%1%:%2%, %3%:%4%)\n") % 0 % (int)n % 0 % (int)n;
    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);
    cout << endl;

    {
      double basicTime, fullTime, topTime, blockTime;

      cout << "Row-major matrix, row-major assignment:" << endl;

      // Do a few initial assignments to let any cache effects stabilize
      for(size_t rep=0; rep<1000; ++rep)
        for(size_t i=0; i<(size_t)mat.rows(); ++i)
          for(size_t j=0; j<(size_t)mat.cols(); ++j)
            mat(i,j) = uniform(rng);

      gttic_(basicTime);
      for(size_t rep=0; rep<nReps; ++rep)
        for(size_t i=0; i<(size_t)mat.rows(); ++i)
          for(size_t j=0; j<(size_t)mat.cols(); ++j)
            mat(i,j) = uniform(rng);
      gttoc_(basicTime);
      tictoc_getNode(basicTimeNode, basicTime);
      basicTime = basicTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Basic: %1% mus/element") % double(1000000 * basicTime / double(mat.rows()*mat.cols()*nReps)) << endl;

      gttic_(fullTime);
      for(size_t rep=0; rep<nReps; ++rep)
        for(size_t i=0; i<(size_t)full.rows(); ++i)
          for(size_t j=0; j<(size_t)full.cols(); ++j)
            full(i,j) = uniform(rng);
      gttoc_(fullTime);
      tictoc_getNode(fullTimeNode, fullTime);
      fullTime = fullTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Full:  %1% mus/element") % double(1000000 * fullTime / double(full.rows()*full.cols()*nReps)) << endl;

      gttic_(topTime);
      for(size_t rep=0; rep<nReps; ++rep)
        for(size_t i=0; i<(size_t)top.rows(); ++i)
          for(size_t j=0; j<(size_t)top.cols(); ++j)
            top(i,j) = uniform(rng);
      gttoc_(topTime);
      tictoc_getNode(topTimeNode, topTime);
      topTime = topTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Top:   %1% mus/element") % double(1000000 * topTime / double(top.rows()*top.cols()*nReps)) << endl;

      gttic_(blockTime);
      for(size_t rep=0; rep<nReps; ++rep)
        for(size_t i=0; i<(size_t)block.rows(); ++i)
          for(size_t j=0; j<(size_t)block.cols(); ++j)
            block(i,j) = uniform(rng);
      gttoc_(blockTime);
      tictoc_getNode(blockTimeNode, blockTime);
      blockTime = blockTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Block: %1% mus/element") % double(1000000 * blockTime / double(block.rows()*block.cols()*nReps)) << endl;

      cout << endl;
    }

    {
      double basicTime, fullTime, topTime, blockTime;

      cout << "Row-major matrix, column-major assignment:" << endl;

      // Do a few initial assignments to let any cache effects stabilize
      for(size_t rep=0; rep<1000; ++rep)
        for(size_t j=0; j<(size_t)mat.cols(); ++j)
          for(size_t i=0; i<(size_t)mat.rows(); ++i)
            mat(i,j) = uniform(rng);

      gttic_(basicTime);
      for(size_t rep=0; rep<nReps; ++rep)
        for(size_t j=0; j<(size_t)mat.cols(); ++j)
          for(size_t i=0; i<(size_t)mat.rows(); ++i)
            mat(i,j) = uniform(rng);
      gttoc_(basicTime);
      tictoc_getNode(basicTimeNode, basicTime);
      basicTime = basicTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Basic: %1% mus/element") % double(1000000 * basicTime / double(mat.rows()*mat.cols()*nReps)) << endl;

      gttic_(fullTime);
      for(size_t rep=0; rep<nReps; ++rep)
        for(size_t j=0; j<(size_t)full.cols(); ++j)
          for(size_t i=0; i<(size_t)full.rows(); ++i)
            full(i,j) = uniform(rng);
      gttoc_(fullTime);
      tictoc_getNode(fullTimeNode, fullTime);
      fullTime = fullTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Full:  %1% mus/element") % double(1000000 * fullTime / double(full.rows()*full.cols()*nReps)) << endl;

      gttic_(topTime);
      for(size_t rep=0; rep<nReps; ++rep)
        for(size_t j=0; j<(size_t)top.cols(); ++j)
          for(size_t i=0; i<(size_t)top.rows(); ++i)
            top(i,j) = uniform(rng);
      gttoc_(topTime);
      tictoc_getNode(topTimeNode, topTime);
      topTime = topTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Top:   %1% mus/element") % double(1000000 * topTime / double(top.rows()*top.cols()*nReps)) << endl;

      gttic_(blockTime);
      for(size_t rep=0; rep<nReps; ++rep)
        for(size_t j=0; j<(size_t)block.cols(); ++j)
          for(size_t i=0; i<(size_t)block.rows(); ++i)
            block(i,j) = uniform(rng);
      gttoc_(blockTime);
      tictoc_getNode(blockTimeNode, blockTime);
      blockTime = blockTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Block: %1% mus/element") % double(1000000 * blockTime / double(block.rows()*block.cols()*nReps)) << endl;

      cout << endl;
    }

    {
      double basicTime, fullTime, topTime, blockTime;
      typedef pair<size_t,size_t> ij_t;
      vector<ij_t> ijs(100000);

      cout << "Row-major matrix, random assignment:" << endl;

      // Do a few initial assignments to let any cache effects stabilize
      for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng),uniform_j(rng)));
      for(size_t rep=0; rep<1000; ++rep)
        for(const ij_t& ij: ijs) { mat(ij.first, ij.second) = uniform(rng); }

      gttic_(basicTime);
      for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng),uniform_j(rng)));
      for(size_t rep=0; rep<1000; ++rep)
        for(const ij_t& ij: ijs) { mat(ij.first, ij.second) = uniform(rng); }
      gttoc_(basicTime);
      tictoc_getNode(basicTimeNode, basicTime);
      basicTime = basicTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Basic: %1% mus/element") % double(1000000 * basicTime / double(ijs.size()*nReps)) << endl;

      gttic_(fullTime);
      for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng),uniform_j(rng)));
      for(size_t rep=0; rep<1000; ++rep)
        for(const ij_t& ij: ijs) { full(ij.first, ij.second) = uniform(rng); }
      gttoc_(fullTime);
      tictoc_getNode(fullTimeNode, fullTime);
      fullTime = fullTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Full:  %1% mus/element") % double(1000000 * fullTime / double(ijs.size()*nReps)) << endl;

      gttic_(topTime);
      for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng)%top.rows(),uniform_j(rng)));
      for(size_t rep=0; rep<1000; ++rep)
        for(const ij_t& ij: ijs) { top(ij.first, ij.second) = uniform(rng); }
      gttoc_(topTime);
      tictoc_getNode(topTimeNode, topTime);
      topTime = topTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Top:   %1% mus/element") % double(1000000 * topTime / double(ijs.size()*nReps)) << endl;

      gttic_(blockTime);
      for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(rng)%block.rows(),uniform_j(rng)%block.cols()));
      for(size_t rep=0; rep<1000; ++rep)
        for(const ij_t& ij: ijs) { block(ij.first, ij.second) = uniform(rng); }
      gttoc_(blockTime);
      tictoc_getNode(blockTimeNode, blockTime);
      blockTime = blockTimeNode->secs();
      gtsam::tictoc_reset_();
      cout << format("  Block: %1% mus/element") % double(1000000 * blockTime / double(ijs.size()*nReps)) << endl;

      cout << endl;
    }
  }

//  if(true) {
//    cout << "\nTesting square triangular matrices:" << endl;
//
////    typedef triangular_matrix<double, ublas::upper, ublas::column_major> triangular;
////    typedef ublas::matrix<double, ublas::column_major> matrix;
//    typedef MatrixXd matrix; // default col major
//
////    triangular tri(5,5);
//
//    matrix mat(5,5);
//    for(size_t j=0; j<(size_t)mat.cols(); ++j)
//      for(size_t i=0; i<(size_t)mat.rows(); ++i)
//        mat(i,j) = uniform(rng);
//
//    tri = ublas::triangular_adaptor<matrix, ublas::upper>(mat);
//    cout << "  Assigned from triangular adapter: " << tri << endl;
//
//    cout << "  Triangular adapter of mat: " << ublas::triangular_adaptor<matrix, ublas::upper>(mat) << endl;
//
//    for(size_t j=0; j<(size_t)mat.cols(); ++j)
//      for(size_t i=0; i<(size_t)mat.rows(); ++i)
//        mat(i,j) = uniform(rng);
//    mat = tri;
//    cout << "  Assign matrix from triangular: " << mat << endl;
//
//    for(size_t j=0; j<(size_t)mat.cols(); ++j)
//      for(size_t i=0; i<(size_t)mat.rows(); ++i)
//        mat(i,j) = uniform(rng);
//    (ublas::triangular_adaptor<matrix, ublas::upper>(mat)) = tri;
//    cout << "  Assign triangular adaptor from triangular: " << mat << endl;
//  }

//  {
//    cout << "\nTesting wide triangular matrices:" << endl;
//
//    typedef triangular_matrix<double, ublas::upper, ublas::column_major> triangular;
//    typedef ublas::matrix<double, ublas::column_major> matrix;
//
//    triangular tri(5,7);
//
//    matrix mat(5,7);
//    for(size_t j=0; j<(size_t)mat.cols(); ++j)
//      for(size_t i=0; i<(size_t)mat.rows(); ++i)
//        mat(i,j) = uniform(rng);
//
//    tri = ublas::triangular_adaptor<matrix, ublas::upper>(mat);
//    cout << "  Assigned from triangular adapter: " << tri << endl;
//
//    cout << "  Triangular adapter of mat: " << ublas::triangular_adaptor<matrix, ublas::upper>(mat) << endl;
//
//    for(size_t j=0; j<(size_t)mat.cols(); ++j)
//      for(size_t i=0; i<(size_t)mat.rows(); ++i)
//        mat(i,j) = uniform(rng);
//    mat = tri;
//    cout << "  Assign matrix from triangular: " << mat << endl;
//
//    for(size_t j=0; j<(size_t)mat.cols(); ++j)
//      for(size_t i=0; i<(size_t)mat.rows(); ++i)
//        mat(i,j) = uniform(rng);
//    mat = ublas::triangular_adaptor<matrix, ublas::upper>(mat);
//    cout << "  Assign matrix from triangular adaptor of self: " << mat << endl;
//  }

//  {
//    cout << "\nTesting subvectors:" << endl;
//
//    typedef MatrixXd matrix;
//    matrix mat(4,4);
//
//    for(size_t j=0; j<(size_t)mat.cols(); ++j)
//      for(size_t i=0; i<(size_t)mat.rows(); ++i)
//        mat(i,j) = i*mat.rows() + j;
//    cout << "  mat = " << mat;
//
//    cout << "  vec(1:4, 2:2) = " << mat.block(1,2, ), ublas::range(1,4), ublas::range(2,2));
//
//  }

  return 0;

}