aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/test/nesting_profiling.cpp
blob: c19081e5a167534770776736804729d2e0b3d313 (plain)
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
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#include "main.h"

#define EIGEN_OLD_NESTED

#include "Eigen/Core"
#include "Eigen/Array"
#include "Eigen/Geometry"

#include "Bench/BenchTimer.h"

using namespace Eigen;

struct Transform2D
{
  static void run(int num_runs)
  {
    const Matrix2d T = Matrix2d::Random();
    const Vector2d t = Vector2d::Random();
    const Matrix2Xd pts = Matrix2Xd::Random(2,100);

    Matrix2Xd res;
    for (int i=0; i<num_runs; ++i)
    {
      run(res, T, pts, t);
    }
  }

  EIGEN_DONT_INLINE static void run(Matrix2Xd& res, const Matrix2d& T, const Matrix2Xd& pts, const Vector2d& t)
  {
    res = T * pts + Replicate<Vector2d,1,100>(t);
  }
};

struct ColwiseTransform2D
{
  static void run(int num_runs)
  {
    const Matrix2d T = Matrix2d::Random();
    const Vector2d t = Vector2d::Random();
    const Matrix2Xd pts = Matrix2Xd::Random(2,100);

    Matrix2Xd res;
    for (int i=0; i<num_runs; ++i)
    {
      run(res, T, pts, t);
    }
  }

  EIGEN_DONT_INLINE static void run(Matrix2Xd& res, const Matrix2d& T, const Matrix2Xd& pts, const Vector2d& t)
  {
    res = T * pts + Replicate<Vector2d,1,100>(t);
  }
};

struct LinearCombination
{
  typedef Eigen::Matrix<double,2,4> Matrix2x4d;

  static void run(int num_runs)
  {
    const Matrix2Xd pts = Matrix2Xd::Random(2,100);
    const Matrix2x4d coefs = Matrix2x4d::Random();

    Matrix2x4d linear_combined = Matrix2x4d::Zero();
    for (int i=0; i<num_runs; ++i)
    {
      for (int r=0; r<coefs.rows(); ++r)
      {
        for (int c=0; c<pts.cols()-coefs.cols()+1; ++c)
        {
          run(linear_combined, pts, coefs, r, c);
        }
      }
    }
  }

  EIGEN_DONT_INLINE static void run(Matrix2x4d& res, const Matrix2Xd& pts, const Matrix2x4d& coefs, int r, int c)
  {
    res += pts.block(0,c,2,coefs.cols()).cwise() * Replicate<Matrix2x4d::RowXpr,2,1>(coefs.row(r));
  }
};

template <typename VectorType>
struct VectorAddition
{
  typedef VectorType ReturnType;
  EIGEN_DONT_INLINE static VectorType run(int)
  {
    VectorType a,b,c,d;
    return a+b+c+d;
  }
};

template <typename MatrixType>
struct MatrixProduct
{
  typedef MatrixType ReturnType;
  EIGEN_DONT_INLINE static MatrixType run(int num_runs)
  {
    MatrixType a,b;
    return a*b;
  }
};

template <typename MatrixType>
struct MatrixScaling
{
  typedef MatrixType ReturnType;
  EIGEN_DONT_INLINE static MatrixType run(int num_runs)
  {
      ei_traits<MatrixType>::Scalar s;
      MatrixType a,b;
      return s*a;
  }
};

template<typename TestFunction>
EIGEN_DONT_INLINE void run(int num_runs)
{
  for (int outer_runs=0; outer_runs<30; ++outer_runs)
  {
    //BenchTimer timer;
    //const double start = timer.getTime();
    {
      TestFunction::run(num_runs);
    }
    //const double stop = timer.getTime();
    //std::cout << (stop-start)*1000.0 << " ms" << std::endl;
  }
}

template<typename TestFunction>
EIGEN_DONT_INLINE void run_direct(int num_runs = 1)
{
  for (int outer_runs=0; outer_runs<30; ++outer_runs)
  {
    // required to prevent that the compiler replaces the run-call by nop
    typename TestFunction::ReturnType return_type;
    for (int i=0; i<num_runs; ++i)
    {
      return_type += TestFunction::run(num_runs);
    }
  }
}

void test_nesting_profiling()
{
  const int num_runs = 100000;

  BenchTimer timer;
  const double start = timer.getTime();
  {
    // leads to better run-time
    run<Transform2D>(num_runs);
    run<ColwiseTransform2D>(num_runs);
    run<LinearCombination>(num_runs);
  }
  const double stop = timer.getTime();
  std::cout << (stop-start)*1000.0 << " ms" << std::endl;

  // leads to identical assembly
  run_direct< MatrixProduct<Matrix2d> >();
  run_direct< MatrixProduct<Matrix3d> >();
  run_direct< MatrixProduct<Matrix4d> >();

  // leads to identical assembly
  run_direct< MatrixScaling<Matrix2d> >();
  run_direct< MatrixScaling<Matrix3d> >();
  run_direct< MatrixScaling<Matrix4d> >();

  // leads to better assembly
  run_direct< VectorAddition<Vector4f> >();
  run_direct< VectorAddition<Vector4d> >();
  run_direct< VectorAddition<Vector4i> >();
}