timings-eigen.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2015-2025 CNRS
3 //
4 
5 #include "pinocchio/macros.hpp"
6 
7 #include <Eigen/Dense>
8 #include <Eigen/Geometry>
9 #include <Eigen/StdVector>
10 
11 #include <benchmark/benchmark.h>
12 
13 using namespace Eigen;
14 
15 static void CustomArguments(benchmark::internal::Benchmark * b)
16 {
17  b->MinWarmUpTime(3.);
18 }
19 
20 // quaternionToMatrix
21 
22 PINOCCHIO_DONT_INLINE void quaternionToMatrixCall(const Quaterniond & q, Matrix3d & m)
23 {
24  m = q.toRotationMatrix();
25 }
26 static void quaternionToMatrix(benchmark::State & st)
27 {
28  Quaterniond q(Quaterniond(Vector4d::Random()).normalized());
29  Matrix3d m(Matrix3d::Random());
30  for (auto _ : st)
31  {
33  benchmark::DoNotOptimize(m);
34  }
35 }
37 
38 // quaternionMultVector
39 
41 quaternionMultVectorCall(const Quaterniond & q, const Vector3d & rhs, Vector3d & lhs)
42 {
43  lhs.noalias() = q * rhs;
44 }
45 static void quaternionMultVector(benchmark::State & st)
46 {
47  Quaterniond q(Quaterniond(Vector4d::Random()).normalized());
48  Vector3d rhs(Vector3d::Random());
49  Vector3d lhs(Vector3d::Random());
50  for (auto _ : st)
51  {
53  benchmark::DoNotOptimize(lhs);
54  }
55 }
57 
58 // quaternionMultQuaternion
59 
61 quaternionMultQuaternionCall(const Quaterniond & q, const Quaterniond & rhs, Quaterniond & lhs)
62 {
63  lhs = q * rhs;
64 }
65 static void quaternionMultQuaternion(benchmark::State & st)
66 {
67  Quaterniond q(Quaterniond(Vector4d::Random()).normalized());
68  Quaterniond rhs(Quaterniond(Vector4d::Random()).normalized());
69  Quaterniond lhs(Quaterniond(Vector4d::Random()).normalized());
70  for (auto _ : st)
71  {
73  benchmark::DoNotOptimize(lhs);
74  }
75 }
77 
78 // quaternionMultVectorX
79 
81 quaternionMultVectorXCall(const Quaterniond & q, const VectorXd & rhs, VectorXd & lhs)
82 {
83  lhs.noalias() = q * rhs;
84 }
85 static void quaternionMultVectorX(benchmark::State & st)
86 {
87  Quaterniond q(Quaterniond(Vector4d::Random()).normalized());
88  VectorXd rhs(VectorXd::Random(3));
89  VectorXd lhs(VectorXd::Random(3));
90  for (auto _ : st)
91  {
93  }
94 }
96 
97 // matrixMultMatrix
98 
99 template<int MSIZE, int OptionM1, int OptionM2, int OptionM3>
101  const Matrix<double, MSIZE, MSIZE, OptionM1> & m,
102  const Matrix<double, MSIZE, MSIZE, OptionM2> & rhs,
103  Matrix<double, MSIZE, MSIZE, OptionM3> & lhs)
104 {
105  lhs.noalias() = m * rhs;
106 }
107 template<int MSIZE, int OptionM1, int OptionM2, int OptionM3>
108 static void matrixMultMatrix(benchmark::State & st)
109 {
110  Matrix<double, MSIZE, MSIZE, OptionM1> m(Matrix<double, MSIZE, MSIZE, OptionM1>::Random());
111  Matrix<double, MSIZE, MSIZE, OptionM2> rhs(Matrix<double, MSIZE, MSIZE, OptionM2>::Random());
112  Matrix<double, MSIZE, MSIZE, OptionM3> lhs(Matrix<double, MSIZE, MSIZE, OptionM3>::Random());
113  for (auto _ : st)
114  {
115  matrixMultMatrixCall(m, rhs, lhs);
116  }
117 }
118 
119 BENCHMARK(matrixMultMatrix<3, ColMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
120 BENCHMARK(matrixMultMatrix<3, RowMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
121 BENCHMARK(matrixMultMatrix<3, ColMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
122 BENCHMARK(matrixMultMatrix<3, RowMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
123 BENCHMARK(matrixMultMatrix<3, ColMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
124 BENCHMARK(matrixMultMatrix<3, RowMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
125 BENCHMARK(matrixMultMatrix<3, ColMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
126 BENCHMARK(matrixMultMatrix<3, RowMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
127 BENCHMARK(matrixMultMatrix<4, ColMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
128 BENCHMARK(matrixMultMatrix<4, RowMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
129 BENCHMARK(matrixMultMatrix<4, ColMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
130 BENCHMARK(matrixMultMatrix<4, RowMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
131 BENCHMARK(matrixMultMatrix<4, ColMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
132 BENCHMARK(matrixMultMatrix<4, RowMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
133 BENCHMARK(matrixMultMatrix<4, ColMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
134 BENCHMARK(matrixMultMatrix<4, RowMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
135 BENCHMARK(matrixMultMatrix<50, ColMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
136 BENCHMARK(matrixMultMatrix<50, RowMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
137 BENCHMARK(matrixMultMatrix<50, ColMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
138 BENCHMARK(matrixMultMatrix<50, RowMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
139 BENCHMARK(matrixMultMatrix<50, ColMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
140 BENCHMARK(matrixMultMatrix<50, RowMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
141 BENCHMARK(matrixMultMatrix<50, ColMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
142 BENCHMARK(matrixMultMatrix<50, RowMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
143 
144 // matrixTransposeMultMatrix
145 
146 template<int MSIZE, int OptionM1, int OptionM2, int OptionM3>
148  const Matrix<double, MSIZE, MSIZE, OptionM1> & m,
149  const Matrix<double, MSIZE, MSIZE, OptionM2> & rhs,
150  Matrix<double, MSIZE, MSIZE, OptionM3> & lhs)
151 {
152  lhs.noalias() = m.transpose() * rhs;
153 }
154 template<int MSIZE, int OptionM1, int OptionM2, int OptionM3>
155 static void matrixTransposeMultMatrix(benchmark::State & st)
156 {
157  Matrix<double, MSIZE, MSIZE, OptionM1> m(Matrix<double, MSIZE, MSIZE, OptionM1>::Random());
158  Matrix<double, MSIZE, MSIZE, OptionM2> rhs(Matrix<double, MSIZE, MSIZE, OptionM2>::Random());
159  Matrix<double, MSIZE, MSIZE, OptionM3> lhs(Matrix<double, MSIZE, MSIZE, OptionM3>::Random());
160  for (auto _ : st)
161  {
163  }
164 }
165 
166 BENCHMARK(matrixTransposeMultMatrix<4, ColMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
167 BENCHMARK(matrixTransposeMultMatrix<4, RowMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
168 BENCHMARK(matrixTransposeMultMatrix<4, ColMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
169 BENCHMARK(matrixTransposeMultMatrix<4, RowMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
170 BENCHMARK(matrixTransposeMultMatrix<4, ColMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
171 BENCHMARK(matrixTransposeMultMatrix<4, RowMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
172 BENCHMARK(matrixTransposeMultMatrix<4, ColMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
173 BENCHMARK(matrixTransposeMultMatrix<4, RowMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
174 BENCHMARK(matrixTransposeMultMatrix<50, ColMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
175 BENCHMARK(matrixTransposeMultMatrix<50, RowMajor, ColMajor, ColMajor>)->Apply(CustomArguments);
176 BENCHMARK(matrixTransposeMultMatrix<50, ColMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
177 BENCHMARK(matrixTransposeMultMatrix<50, RowMajor, RowMajor, ColMajor>)->Apply(CustomArguments);
178 BENCHMARK(matrixTransposeMultMatrix<50, ColMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
179 BENCHMARK(matrixTransposeMultMatrix<50, RowMajor, ColMajor, RowMajor>)->Apply(CustomArguments);
180 BENCHMARK(matrixTransposeMultMatrix<50, ColMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
181 BENCHMARK(matrixTransposeMultMatrix<50, RowMajor, RowMajor, RowMajor>)->Apply(CustomArguments);
182 
183 // matrixMultVector
184 
185 template<int MSIZE>
187  const Matrix<double, MSIZE, MSIZE> & m,
188  const Matrix<double, MSIZE, 1> & rhs,
189  Matrix<double, MSIZE, 1> & lhs)
190 {
191  lhs.noalias() = m * rhs;
192 }
193 template<int MSIZE>
194 static void matrixMultVector(benchmark::State & st)
195 {
196  Matrix<double, MSIZE, MSIZE> m(Matrix<double, MSIZE, MSIZE>::Random());
197  Matrix<double, MSIZE, 1> rhs(Matrix<double, MSIZE, 1>::Random());
198  Matrix<double, MSIZE, 1> lhs(Matrix<double, MSIZE, 1>::Random());
199  for (auto _ : st)
200  {
201  matrixMultVectorCall(m, rhs, lhs);
202  }
203 }
204 
205 BENCHMARK(matrixMultVector<3>)->Apply(CustomArguments);
206 BENCHMARK(matrixMultVector<4>)->Apply(CustomArguments);
207 
208 // matrixDynamicMultMatrix
209 
210 static void CustomArgumentsDynamicMatrix(benchmark::internal::Benchmark * b)
211 {
212  b->MinWarmUpTime(3.)->Arg(3)->Arg(4)->Arg(50);
213 }
214 
215 template<int OptionM1, int OptionM2, int OptionM3>
217  const Matrix<double, Dynamic, Dynamic, OptionM1> & m,
218  const Matrix<double, Dynamic, Dynamic, OptionM2> & rhs,
219  Matrix<double, Dynamic, Dynamic, OptionM3> & lhs)
220 {
221  lhs.noalias() = m * rhs;
222 }
223 template<int OptionM1, int OptionM2, int OptionM3>
224 static void matrixDynamicMultMatrix(benchmark::State & st)
225 {
226  const auto MSIZE = st.range(0);
227  Matrix<double, Dynamic, Dynamic, OptionM1> m(
228  Matrix<double, Dynamic, Dynamic, OptionM1>::Random(MSIZE, MSIZE));
229  Matrix<double, Dynamic, Dynamic, OptionM2> rhs(
230  Matrix<double, Dynamic, Dynamic, OptionM2>::Random(MSIZE, MSIZE));
231  Matrix<double, Dynamic, Dynamic, OptionM3> lhs(
232  Matrix<double, Dynamic, Dynamic, OptionM3>::Random(MSIZE, MSIZE));
233  for (auto _ : st)
234  {
236  }
237 }
238 
239 BENCHMARK(matrixDynamicMultMatrix<ColMajor, ColMajor, ColMajor>)
241 BENCHMARK(matrixDynamicMultMatrix<RowMajor, ColMajor, ColMajor>)
243 BENCHMARK(matrixDynamicMultMatrix<ColMajor, RowMajor, ColMajor>)
245 BENCHMARK(matrixDynamicMultMatrix<RowMajor, RowMajor, ColMajor>)
247 BENCHMARK(matrixDynamicMultMatrix<ColMajor, ColMajor, RowMajor>)
249 BENCHMARK(matrixDynamicMultMatrix<RowMajor, ColMajor, RowMajor>)
251 BENCHMARK(matrixDynamicMultMatrix<ColMajor, RowMajor, RowMajor>)
253 BENCHMARK(matrixDynamicMultMatrix<RowMajor, RowMajor, RowMajor>)
255 
256 // matrixDynamicTransposeMultMatrix
257 
258 static void CustomArgumentsDynamicMatrixTranspose(benchmark::internal::Benchmark * b)
259 {
260  b->MinWarmUpTime(3.)->Arg(4)->Arg(50);
261 }
262 
263 template<int OptionM1, int OptionM2, int OptionM3>
265  const Matrix<double, Dynamic, Dynamic, OptionM1> & m,
266  const Matrix<double, Dynamic, Dynamic, OptionM2> & rhs,
267  Matrix<double, Dynamic, Dynamic, OptionM3> & lhs)
268 {
269  lhs.noalias() = m.transpose() * rhs;
270 }
271 template<int OptionM1, int OptionM2, int OptionM3>
272 static void matrixDynamicTransposeMultMatrix(benchmark::State & st)
273 {
274  const auto MSIZE = st.range(0);
275  Matrix<double, Dynamic, Dynamic, OptionM1> m(
276  Matrix<double, Dynamic, Dynamic, OptionM1>::Random(MSIZE, MSIZE));
277  Matrix<double, Dynamic, Dynamic, OptionM2> rhs(
278  Matrix<double, Dynamic, Dynamic, OptionM2>::Random(MSIZE, MSIZE));
279  Matrix<double, Dynamic, Dynamic, OptionM3> lhs(
280  Matrix<double, Dynamic, Dynamic, OptionM3>::Random(MSIZE, MSIZE));
281  for (auto _ : st)
282  {
284  }
285 }
286 
287 BENCHMARK(matrixDynamicTransposeMultMatrix<ColMajor, ColMajor, ColMajor>)
289 BENCHMARK(matrixDynamicTransposeMultMatrix<RowMajor, ColMajor, ColMajor>)
291 BENCHMARK(matrixDynamicTransposeMultMatrix<ColMajor, RowMajor, ColMajor>)
293 BENCHMARK(matrixDynamicTransposeMultMatrix<RowMajor, RowMajor, ColMajor>)
295 BENCHMARK(matrixDynamicTransposeMultMatrix<ColMajor, ColMajor, RowMajor>)
297 BENCHMARK(matrixDynamicTransposeMultMatrix<RowMajor, ColMajor, RowMajor>)
299 BENCHMARK(matrixDynamicTransposeMultMatrix<ColMajor, RowMajor, RowMajor>)
301 BENCHMARK(matrixDynamicTransposeMultMatrix<RowMajor, RowMajor, RowMajor>)
303 
304 // matrixDynamicMultVector
305 
307 matrixDynamicMultVectorCall(const MatrixXd & m, const MatrixXd & rhs, MatrixXd & lhs)
308 {
309  lhs.noalias() = m * rhs;
310 }
311 static void matrixDynamicMultVector(benchmark::State & st)
312 {
313  const auto MSIZE = st.range(0);
314  MatrixXd m(MatrixXd::Random(MSIZE, MSIZE));
315  MatrixXd rhs(MatrixXd::Random(MSIZE, 1));
316  MatrixXd lhs(MatrixXd::Random(MSIZE, 1));
317  for (auto _ : st)
318  {
320  }
321 }
322 
323 BENCHMARK(matrixDynamicMultVector)->Apply(CustomArguments)->Arg(3)->Arg(4);
324 
Eigen
test-cpp2pybind11.m
m
Definition: test-cpp2pybind11.py:25
quaternionMultVectorCall
PINOCCHIO_DONT_INLINE void quaternionMultVectorCall(const Quaterniond &q, const Vector3d &rhs, Vector3d &lhs)
Definition: timings-eigen.cpp:41
PINOCCHIO_DONT_INLINE
#define PINOCCHIO_DONT_INLINE
Function attribute to forbid inlining. This is a compiler hint that can be not respected.
Definition: include/pinocchio/macros.hpp:53
matrixDynamicTransposeMultMatrix
static void matrixDynamicTransposeMultMatrix(benchmark::State &st)
Definition: timings-eigen.cpp:272
macros.hpp
CustomArgumentsDynamicMatrix
static void CustomArgumentsDynamicMatrix(benchmark::internal::Benchmark *b)
Definition: timings-eigen.cpp:210
CustomArguments
static void CustomArguments(benchmark::internal::Benchmark *b)
Definition: timings-eigen.cpp:15
matrixDynamicTransposeMultMatrixCall
PINOCCHIO_DONT_INLINE void matrixDynamicTransposeMultMatrixCall(const Matrix< double, Dynamic, Dynamic, OptionM1 > &m, const Matrix< double, Dynamic, Dynamic, OptionM2 > &rhs, Matrix< double, Dynamic, Dynamic, OptionM3 > &lhs)
Definition: timings-eigen.cpp:264
BENCHMARK
BENCHMARK(quaternionToMatrix) -> Apply(CustomArguments)
quaternionMultQuaternion
static void quaternionMultQuaternion(benchmark::State &st)
Definition: timings-eigen.cpp:65
b
Vec3f b
matrixMultMatrix
static void matrixMultMatrix(benchmark::State &st)
Definition: timings-eigen.cpp:108
quaternionMultVector
static void quaternionMultVector(benchmark::State &st)
Definition: timings-eigen.cpp:45
matrixDynamicMultVectorCall
PINOCCHIO_DONT_INLINE void matrixDynamicMultVectorCall(const MatrixXd &m, const MatrixXd &rhs, MatrixXd &lhs)
Definition: timings-eigen.cpp:307
matrixMultVector
static void matrixMultVector(benchmark::State &st)
Definition: timings-eigen.cpp:194
quaternionMultVectorXCall
PINOCCHIO_DONT_INLINE void quaternionMultVectorXCall(const Quaterniond &q, const VectorXd &rhs, VectorXd &lhs)
Definition: timings-eigen.cpp:81
matrixTransposeMultMatrix
static void matrixTransposeMultMatrix(benchmark::State &st)
Definition: timings-eigen.cpp:155
BENCHMARK_MAIN
BENCHMARK_MAIN()
inverse-dynamics._
_
Definition: inverse-dynamics.py:22
quaternionMultQuaternionCall
PINOCCHIO_DONT_INLINE void quaternionMultQuaternionCall(const Quaterniond &q, const Quaterniond &rhs, Quaterniond &lhs)
Definition: timings-eigen.cpp:61
q
q
matrixTransposeMultMatrixCall
PINOCCHIO_DONT_INLINE void matrixTransposeMultMatrixCall(const Matrix< double, MSIZE, MSIZE, OptionM1 > &m, const Matrix< double, MSIZE, MSIZE, OptionM2 > &rhs, Matrix< double, MSIZE, MSIZE, OptionM3 > &lhs)
Definition: timings-eigen.cpp:147
matrixDynamicMultMatrix
static void matrixDynamicMultMatrix(benchmark::State &st)
Definition: timings-eigen.cpp:224
quaternionMultVectorX
static void quaternionMultVectorX(benchmark::State &st)
Definition: timings-eigen.cpp:85
matrixDynamicMultMatrixCall
PINOCCHIO_DONT_INLINE void matrixDynamicMultMatrixCall(const Matrix< double, Dynamic, Dynamic, OptionM1 > &m, const Matrix< double, Dynamic, Dynamic, OptionM2 > &rhs, Matrix< double, Dynamic, Dynamic, OptionM3 > &lhs)
Definition: timings-eigen.cpp:216
quaternionToMatrix
static void quaternionToMatrix(benchmark::State &st)
Definition: timings-eigen.cpp:26
CustomArgumentsDynamicMatrixTranspose
static void CustomArgumentsDynamicMatrixTranspose(benchmark::internal::Benchmark *b)
Definition: timings-eigen.cpp:258
quaternionToMatrixCall
PINOCCHIO_DONT_INLINE void quaternionToMatrixCall(const Quaterniond &q, Matrix3d &m)
Definition: timings-eigen.cpp:22
matrixMultVectorCall
PINOCCHIO_DONT_INLINE void matrixMultVectorCall(const Matrix< double, MSIZE, MSIZE > &m, const Matrix< double, MSIZE, 1 > &rhs, Matrix< double, MSIZE, 1 > &lhs)
Definition: timings-eigen.cpp:186
matrixMultMatrixCall
PINOCCHIO_DONT_INLINE void matrixMultMatrixCall(const Matrix< double, MSIZE, MSIZE, OptionM1 > &m, const Matrix< double, MSIZE, MSIZE, OptionM2 > &rhs, Matrix< double, MSIZE, MSIZE, OptionM3 > &lhs)
Definition: timings-eigen.cpp:100
matrixDynamicMultVector
static void matrixDynamicMultVector(benchmark::State &st)
Definition: timings-eigen.cpp:311
simulation-closed-kinematic-chains.rhs
rhs
Definition: simulation-closed-kinematic-chains.py:134


pinocchio
Author(s):
autogenerated on Thu Apr 10 2025 02:42:22