xref: /aosp_15_r20/external/eigen/bench/BenchUtil.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li 
2*bf2c3715SXin Li #ifndef EIGEN_BENCH_UTIL_H
3*bf2c3715SXin Li #define EIGEN_BENCH_UTIL_H
4*bf2c3715SXin Li 
5*bf2c3715SXin Li #include <Eigen/Core>
6*bf2c3715SXin Li #include "BenchTimer.h"
7*bf2c3715SXin Li 
8*bf2c3715SXin Li using namespace std;
9*bf2c3715SXin Li using namespace Eigen;
10*bf2c3715SXin Li 
11*bf2c3715SXin Li #include <boost/preprocessor/repetition/enum_params.hpp>
12*bf2c3715SXin Li #include <boost/preprocessor/repetition.hpp>
13*bf2c3715SXin Li #include <boost/preprocessor/seq.hpp>
14*bf2c3715SXin Li #include <boost/preprocessor/array.hpp>
15*bf2c3715SXin Li #include <boost/preprocessor/arithmetic.hpp>
16*bf2c3715SXin Li #include <boost/preprocessor/comparison.hpp>
17*bf2c3715SXin Li #include <boost/preprocessor/punctuation.hpp>
18*bf2c3715SXin Li #include <boost/preprocessor/punctuation/comma.hpp>
19*bf2c3715SXin Li #include <boost/preprocessor/stringize.hpp>
20*bf2c3715SXin Li 
21*bf2c3715SXin Li template<typename MatrixType> void initMatrix_random(MatrixType& mat) __attribute__((noinline));
initMatrix_random(MatrixType & mat)22*bf2c3715SXin Li template<typename MatrixType> void initMatrix_random(MatrixType& mat)
23*bf2c3715SXin Li {
24*bf2c3715SXin Li   mat.setRandom();// = MatrixType::random(mat.rows(), mat.cols());
25*bf2c3715SXin Li }
26*bf2c3715SXin Li 
27*bf2c3715SXin Li template<typename MatrixType> void initMatrix_identity(MatrixType& mat) __attribute__((noinline));
initMatrix_identity(MatrixType & mat)28*bf2c3715SXin Li template<typename MatrixType> void initMatrix_identity(MatrixType& mat)
29*bf2c3715SXin Li {
30*bf2c3715SXin Li   mat.setIdentity();
31*bf2c3715SXin Li }
32*bf2c3715SXin Li 
33*bf2c3715SXin Li #ifndef __INTEL_COMPILER
34*bf2c3715SXin Li #define DISABLE_SSE_EXCEPTIONS()  { \
35*bf2c3715SXin Li   int aux; \
36*bf2c3715SXin Li   asm( \
37*bf2c3715SXin Li   "stmxcsr   %[aux]           \n\t" \
38*bf2c3715SXin Li   "orl       $32832, %[aux]   \n\t" \
39*bf2c3715SXin Li   "ldmxcsr   %[aux]           \n\t" \
40*bf2c3715SXin Li   : : [aux] "m" (aux)); \
41*bf2c3715SXin Li }
42*bf2c3715SXin Li #else
43*bf2c3715SXin Li #define DISABLE_SSE_EXCEPTIONS()
44*bf2c3715SXin Li #endif
45*bf2c3715SXin Li 
46*bf2c3715SXin Li #ifdef BENCH_GMM
47*bf2c3715SXin Li #include <gmm/gmm.h>
48*bf2c3715SXin Li template <typename EigenMatrixType, typename GmmMatrixType>
eiToGmm(const EigenMatrixType & src,GmmMatrixType & dst)49*bf2c3715SXin Li void eiToGmm(const EigenMatrixType& src, GmmMatrixType& dst)
50*bf2c3715SXin Li {
51*bf2c3715SXin Li   dst.resize(src.rows(),src.cols());
52*bf2c3715SXin Li   for (int j=0; j<src.cols(); ++j)
53*bf2c3715SXin Li     for (int i=0; i<src.rows(); ++i)
54*bf2c3715SXin Li       dst(i,j) = src.coeff(i,j);
55*bf2c3715SXin Li }
56*bf2c3715SXin Li #endif
57*bf2c3715SXin Li 
58*bf2c3715SXin Li 
59*bf2c3715SXin Li #ifdef BENCH_GSL
60*bf2c3715SXin Li #include <gsl/gsl_matrix.h>
61*bf2c3715SXin Li #include <gsl/gsl_linalg.h>
62*bf2c3715SXin Li #include <gsl/gsl_eigen.h>
63*bf2c3715SXin Li template <typename EigenMatrixType>
eiToGsl(const EigenMatrixType & src,gsl_matrix ** dst)64*bf2c3715SXin Li void eiToGsl(const EigenMatrixType& src, gsl_matrix** dst)
65*bf2c3715SXin Li {
66*bf2c3715SXin Li   for (int j=0; j<src.cols(); ++j)
67*bf2c3715SXin Li     for (int i=0; i<src.rows(); ++i)
68*bf2c3715SXin Li       gsl_matrix_set(*dst, i, j, src.coeff(i,j));
69*bf2c3715SXin Li }
70*bf2c3715SXin Li #endif
71*bf2c3715SXin Li 
72*bf2c3715SXin Li #ifdef BENCH_UBLAS
73*bf2c3715SXin Li #include <boost/numeric/ublas/matrix.hpp>
74*bf2c3715SXin Li #include <boost/numeric/ublas/vector.hpp>
75*bf2c3715SXin Li template <typename EigenMatrixType, typename UblasMatrixType>
eiToUblas(const EigenMatrixType & src,UblasMatrixType & dst)76*bf2c3715SXin Li void eiToUblas(const EigenMatrixType& src, UblasMatrixType& dst)
77*bf2c3715SXin Li {
78*bf2c3715SXin Li   dst.resize(src.rows(),src.cols());
79*bf2c3715SXin Li   for (int j=0; j<src.cols(); ++j)
80*bf2c3715SXin Li     for (int i=0; i<src.rows(); ++i)
81*bf2c3715SXin Li       dst(i,j) = src.coeff(i,j);
82*bf2c3715SXin Li }
83*bf2c3715SXin Li template <typename EigenType, typename UblasType>
eiToUblasVec(const EigenType & src,UblasType & dst)84*bf2c3715SXin Li void eiToUblasVec(const EigenType& src, UblasType& dst)
85*bf2c3715SXin Li {
86*bf2c3715SXin Li   dst.resize(src.size());
87*bf2c3715SXin Li   for (int j=0; j<src.size(); ++j)
88*bf2c3715SXin Li       dst[j] = src.coeff(j);
89*bf2c3715SXin Li }
90*bf2c3715SXin Li #endif
91*bf2c3715SXin Li 
92*bf2c3715SXin Li #endif // EIGEN_BENCH_UTIL_H
93