benchEigenSolver.cpp File Reference
#include <iostream>
#include <Eigen/Core>
#include <Eigen/QR>
#include <bench/BenchUtil.h>

Macros

#define REPEAT   1000
 
#define TRIES   4
 
#define SCALAR   float
 

Typedefs

typedef SCALAR Scalar
 

Functions

template<typename MatrixType >
 __attribute__ ((noinline)) void benchEigenSolver(const MatrixType &m)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ REPEAT

#define REPEAT   1000

◆ SCALAR

#define SCALAR   float

◆ TRIES

#define TRIES   4

Typedef Documentation

◆ Scalar

typedef SCALAR Scalar

Function Documentation

◆ __attribute__()

template<typename MatrixType >
__attribute__ ( (noinline)  ) const &
34  {
35  int rows = m.rows();
36  int cols = m.cols();
37 
38  int stdRepeats = std::max(1, int((REPEAT * 1000) / (rows * rows * sqrt(rows))));
39  int saRepeats = stdRepeats * 4;
40 
41  typedef typename MatrixType::Scalar Scalar;
43 
44  MatrixType a = MatrixType::Random(rows, cols);
45  SquareMatrixType covMat = a * a.adjoint();
46 
47  BenchTimer timerSa, timerStd;
48 
49  Scalar acc = 0;
50  int r = internal::random<int>(0, covMat.rows() - 1);
51  int c = internal::random<int>(0, covMat.cols() - 1);
52  {
54  for (int t = 0; t < TRIES; ++t) {
55  timerSa.start();
56  for (int k = 0; k < saRepeats; ++k) {
57  ei.compute(covMat);
58  acc += ei.eigenvectors().coeff(r, c);
59  }
60  timerSa.stop();
61  }
62  }
63 
64  {
66  for (int t = 0; t < TRIES; ++t) {
67  timerStd.start();
68  for (int k = 0; k < stdRepeats; ++k) {
69  ei.compute(covMat);
70  acc += ei.eigenvectors().coeff(r, c);
71  }
72  timerStd.stop();
73  }
74  }
75 
76  if (MatrixType::RowsAtCompileTime == Dynamic)
77  std::cout << "dyn ";
78  else
79  std::cout << "fixed ";
80  std::cout << covMat.rows() << " \t" << timerSa.value() * REPEAT / saRepeats << "s \t"
81  << timerStd.value() * REPEAT / stdRepeats << "s";
82 
83 #ifdef BENCH_GMM
84  if (MatrixType::RowsAtCompileTime == Dynamic) {
85  timerSa.reset();
86  timerStd.reset();
87 
88  gmm::dense_matrix<Scalar> gmmCovMat(covMat.rows(), covMat.cols());
89  gmm::dense_matrix<Scalar> eigvect(covMat.rows(), covMat.cols());
90  std::vector<Scalar> eigval(covMat.rows());
91  eiToGmm(covMat, gmmCovMat);
92  for (int t = 0; t < TRIES; ++t) {
93  timerSa.start();
94  for (int k = 0; k < saRepeats; ++k) {
95  gmm::symmetric_qr_algorithm(gmmCovMat, eigval, eigvect);
96  acc += eigvect(r, c);
97  }
98  timerSa.stop();
99  }
100  // the non-selfadjoint solver does not compute the eigen vectors
101  // for (int t=0; t<TRIES; ++t)
102  // {
103  // timerStd.start();
104  // for (int k=0; k<stdRepeats; ++k)
105  // {
106  // gmm::implicit_qr_algorithm(gmmCovMat, eigval, eigvect);
107  // acc += eigvect(r,c);
108  // }
109  // timerStd.stop();
110  // }
111 
112  std::cout << " | \t" << timerSa.value() * REPEAT / saRepeats << "s"
113  << /*timerStd.value() * REPEAT / stdRepeats << "s"*/ " na ";
114  }
115 #endif
116 
117 #ifdef BENCH_GSL
118  if (MatrixType::RowsAtCompileTime == Dynamic) {
119  timerSa.reset();
120  timerStd.reset();
121 
122  gsl_matrix* gslCovMat = gsl_matrix_alloc(covMat.rows(), covMat.cols());
123  gsl_matrix* gslCopy = gsl_matrix_alloc(covMat.rows(), covMat.cols());
124  gsl_matrix* eigvect = gsl_matrix_alloc(covMat.rows(), covMat.cols());
125  gsl_vector* eigval = gsl_vector_alloc(covMat.rows());
126  gsl_eigen_symmv_workspace* eisymm = gsl_eigen_symmv_alloc(covMat.rows());
127 
128  gsl_matrix_complex* eigvectz = gsl_matrix_complex_alloc(covMat.rows(), covMat.cols());
129  gsl_vector_complex* eigvalz = gsl_vector_complex_alloc(covMat.rows());
130  gsl_eigen_nonsymmv_workspace* einonsymm = gsl_eigen_nonsymmv_alloc(covMat.rows());
131 
132  eiToGsl(covMat, &gslCovMat);
133  for (int t = 0; t < TRIES; ++t) {
134  timerSa.start();
135  for (int k = 0; k < saRepeats; ++k) {
136  gsl_matrix_memcpy(gslCopy, gslCovMat);
137  gsl_eigen_symmv(gslCopy, eigval, eigvect, eisymm);
138  acc += gsl_matrix_get(eigvect, r, c);
139  }
140  timerSa.stop();
141  }
142  for (int t = 0; t < TRIES; ++t) {
143  timerStd.start();
144  for (int k = 0; k < stdRepeats; ++k) {
145  gsl_matrix_memcpy(gslCopy, gslCovMat);
146  gsl_eigen_nonsymmv(gslCopy, eigvalz, eigvectz, einonsymm);
147  acc += GSL_REAL(gsl_matrix_complex_get(eigvectz, r, c));
148  }
149  timerStd.stop();
150  }
151 
152  std::cout << " | \t" << timerSa.value() * REPEAT / saRepeats << "s \t" << timerStd.value() * REPEAT / stdRepeats
153  << "s";
154 
155  gsl_matrix_free(gslCovMat);
156  gsl_vector_free(gslCopy);
157  gsl_matrix_free(eigvect);
158  gsl_vector_free(eigval);
159  gsl_matrix_complex_free(eigvectz);
160  gsl_vector_complex_free(eigvalz);
161  gsl_eigen_symmv_free(eisymm);
162  gsl_eigen_nonsymmv_free(einonsymm);
163  }
164 #endif
165 
166  std::cout << "\n";
167 
168  // make sure the compiler does not optimize too much
169  if (acc == 123) std::cout << acc;
170 }
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
void eiToGmm(const EigenSparseMatrix &src, GmmSparse &dst)
Definition: BenchSparseUtil.h:64
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: benchEigenSolver.cpp:31
#define REPEAT
Definition: benchEigenSolver.cpp:20
#define TRIES
Definition: benchEigenSolver.cpp:24
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Definition: BenchTimer.h:55
double value(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:94
void reset()
Definition: BenchTimer.h:68
void stop()
Definition: BenchTimer.h:77
void start()
Definition: BenchTimer.h:73
Computes eigenvalues and eigenvectors of general matrices.
Definition: EigenSolver.h:68
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Computes eigenvalues and eigenvectors of selfadjoint matrices.
Definition: SelfAdjointEigenSolver.h:82
#define max(a, b)
Definition: datatypes.h:23
const Scalar * a
Definition: level2_cplx_impl.h:32
int * m
Definition: level2_cplx_impl.h:294
char char char int int * k
Definition: level2_impl.h:374
const int Dynamic
Definition: Constants.h:25
r
Definition: UniformPSDSelfTest.py:20
int c
Definition: calibrate.py:100
t
Definition: plotPSD.py:36

References a, calibrate::c, Eigen::PlainObjectBase< Derived >::coeff(), cols, Eigen::EigenSolver< MatrixType_ >::compute(), Eigen::SelfAdjointEigenSolver< MatrixType_ >::compute(), Eigen::Dynamic, Eigen::EigenSolver< MatrixType_ >::eigenvectors(), Eigen::SelfAdjointEigenSolver< MatrixType_ >::eigenvectors(), eiToGmm(), k, m, max, UniformPSDSelfTest::r, REPEAT, Eigen::BenchTimer::reset(), rows, sqrt(), Eigen::BenchTimer::start(), Eigen::BenchTimer::stop(), plotPSD::t, TRIES, and Eigen::BenchTimer::value().

◆ main()

int main ( int argc  ,
char argv[] 
)
172  {
173  const int dynsizes[] = {4, 6, 8, 12, 16, 24, 32, 64, 128, 256, 512, 0};
174  std::cout << "size selfadjoint generic";
175 #ifdef BENCH_GMM
176  std::cout << " GMM++ ";
177 #endif
178 #ifdef BENCH_GSL
179  std::cout << " GSL (double + ATLAS) ";
180 #endif
181  std::cout << "\n";
182  for (uint i = 0; dynsizes[i] > 0; ++i) benchEigenSolver(Matrix<Scalar, Dynamic, Dynamic>(dynsizes[i], dynsizes[i]));
183 
184  benchEigenSolver(Matrix<Scalar, 2, 2>());
185  benchEigenSolver(Matrix<Scalar, 3, 3>());
186  benchEigenSolver(Matrix<Scalar, 4, 4>());
187  benchEigenSolver(Matrix<Scalar, 6, 6>());
188  benchEigenSolver(Matrix<Scalar, 8, 8>());
189  benchEigenSolver(Matrix<Scalar, 12, 12>());
190  benchEigenSolver(Matrix<Scalar, 16, 16>());
191  return 0;
192 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9

References i.