xref: /aosp_15_r20/external/eigen/bench/basicbenchmark.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li 
2*bf2c3715SXin Li #ifndef EIGEN_BENCH_BASICBENCH_H
3*bf2c3715SXin Li #define EIGEN_BENCH_BASICBENCH_H
4*bf2c3715SXin Li 
5*bf2c3715SXin Li enum {LazyEval, EarlyEval, OmpEval};
6*bf2c3715SXin Li 
7*bf2c3715SXin Li template<int Mode, typename MatrixType>
8*bf2c3715SXin Li void benchBasic_loop(const MatrixType& I, MatrixType& m, int iterations) __attribute__((noinline));
9*bf2c3715SXin Li 
10*bf2c3715SXin Li template<int Mode, typename MatrixType>
benchBasic_loop(const MatrixType & I,MatrixType & m,int iterations)11*bf2c3715SXin Li void benchBasic_loop(const MatrixType& I, MatrixType& m, int iterations)
12*bf2c3715SXin Li {
13*bf2c3715SXin Li   for(int a = 0; a < iterations; a++)
14*bf2c3715SXin Li   {
15*bf2c3715SXin Li     if (Mode==LazyEval)
16*bf2c3715SXin Li     {
17*bf2c3715SXin Li       asm("#begin_bench_loop LazyEval");
18*bf2c3715SXin Li       if (MatrixType::SizeAtCompileTime!=Eigen::Dynamic) asm("#fixedsize");
19*bf2c3715SXin Li       m = (I + 0.00005 * (m + m.lazyProduct(m))).eval();
20*bf2c3715SXin Li     }
21*bf2c3715SXin Li     else if (Mode==OmpEval)
22*bf2c3715SXin Li     {
23*bf2c3715SXin Li       asm("#begin_bench_loop OmpEval");
24*bf2c3715SXin Li       if (MatrixType::SizeAtCompileTime!=Eigen::Dynamic) asm("#fixedsize");
25*bf2c3715SXin Li       m = (I + 0.00005 * (m + m.lazyProduct(m))).eval();
26*bf2c3715SXin Li     }
27*bf2c3715SXin Li     else
28*bf2c3715SXin Li     {
29*bf2c3715SXin Li       asm("#begin_bench_loop EarlyEval");
30*bf2c3715SXin Li       if (MatrixType::SizeAtCompileTime!=Eigen::Dynamic) asm("#fixedsize");
31*bf2c3715SXin Li       m = I + 0.00005 * (m + m * m);
32*bf2c3715SXin Li     }
33*bf2c3715SXin Li     asm("#end_bench_loop");
34*bf2c3715SXin Li   }
35*bf2c3715SXin Li }
36*bf2c3715SXin Li 
37*bf2c3715SXin Li template<int Mode, typename MatrixType>
38*bf2c3715SXin Li double benchBasic(const MatrixType& mat, int size, int tries) __attribute__((noinline));
39*bf2c3715SXin Li 
40*bf2c3715SXin Li template<int Mode, typename MatrixType>
benchBasic(const MatrixType & mat,int iterations,int tries)41*bf2c3715SXin Li double benchBasic(const MatrixType& mat, int iterations, int tries)
42*bf2c3715SXin Li {
43*bf2c3715SXin Li   const int rows = mat.rows();
44*bf2c3715SXin Li   const int cols = mat.cols();
45*bf2c3715SXin Li 
46*bf2c3715SXin Li   MatrixType I(rows,cols);
47*bf2c3715SXin Li   MatrixType m(rows,cols);
48*bf2c3715SXin Li 
49*bf2c3715SXin Li   initMatrix_identity(I);
50*bf2c3715SXin Li 
51*bf2c3715SXin Li   Eigen::BenchTimer timer;
52*bf2c3715SXin Li   for(uint t=0; t<tries; ++t)
53*bf2c3715SXin Li   {
54*bf2c3715SXin Li     initMatrix_random(m);
55*bf2c3715SXin Li     timer.start();
56*bf2c3715SXin Li     benchBasic_loop<Mode>(I, m, iterations);
57*bf2c3715SXin Li     timer.stop();
58*bf2c3715SXin Li     cerr << m;
59*bf2c3715SXin Li   }
60*bf2c3715SXin Li   return timer.value();
61*bf2c3715SXin Li };
62*bf2c3715SXin Li 
63*bf2c3715SXin Li #endif // EIGEN_BENCH_BASICBENCH_H
64