// g++ -DNDEBUG -O3 -I.. benchEigenSolver.cpp -o benchEigenSolver && ./benchEigenSolver // options: // -DBENCH_GMM // -DBENCH_GSL -lgsl /usr/lib/libcblas.so.3 // -DEIGEN_DONT_VECTORIZE // -msse2 // -DREPEAT=100 // -DTRIES=10 // -DSCALAR=double #include #include #include #include using namespace Eigen; #ifndef REPEAT #define REPEAT 1000 #endif #ifndef TRIES #define TRIES 4 #endif #ifndef SCALAR #define SCALAR float #endif typedef SCALAR Scalar; template __attribute__((noinline)) void benchEigenSolver(const MatrixType& m) { int rows = m.rows(); int cols = m.cols(); int stdRepeats = std::max(1, int((REPEAT * 1000) / (rows * rows * sqrt(rows)))); int saRepeats = stdRepeats * 4; typedef typename MatrixType::Scalar Scalar; typedef Matrix SquareMatrixType; MatrixType a = MatrixType::Random(rows, cols); SquareMatrixType covMat = a * a.adjoint(); BenchTimer timerSa, timerStd; Scalar acc = 0; int r = internal::random(0, covMat.rows() - 1); int c = internal::random(0, covMat.cols() - 1); { SelfAdjointEigenSolver ei(covMat); for (int t = 0; t < TRIES; ++t) { timerSa.start(); for (int k = 0; k < saRepeats; ++k) { ei.compute(covMat); acc += ei.eigenvectors().coeff(r, c); } timerSa.stop(); } } { EigenSolver ei(covMat); for (int t = 0; t < TRIES; ++t) { timerStd.start(); for (int k = 0; k < stdRepeats; ++k) { ei.compute(covMat); acc += ei.eigenvectors().coeff(r, c); } timerStd.stop(); } } if (MatrixType::RowsAtCompileTime == Dynamic) std::cout << "dyn "; else std::cout << "fixed "; std::cout << covMat.rows() << " \t" << timerSa.value() * REPEAT / saRepeats << "s \t" << timerStd.value() * REPEAT / stdRepeats << "s"; #ifdef BENCH_GMM if (MatrixType::RowsAtCompileTime == Dynamic) { timerSa.reset(); timerStd.reset(); gmm::dense_matrix gmmCovMat(covMat.rows(), covMat.cols()); gmm::dense_matrix eigvect(covMat.rows(), covMat.cols()); std::vector eigval(covMat.rows()); eiToGmm(covMat, gmmCovMat); for (int t = 0; t < TRIES; ++t) { timerSa.start(); for (int k = 0; k < saRepeats; ++k) { gmm::symmetric_qr_algorithm(gmmCovMat, eigval, eigvect); acc += eigvect(r, c); } timerSa.stop(); } // the non-selfadjoint solver does not compute the eigen vectors // for (int t=0; t 0; ++i) benchEigenSolver(Matrix(dynsizes[i], dynsizes[i])); benchEigenSolver(Matrix()); benchEigenSolver(Matrix()); benchEigenSolver(Matrix()); benchEigenSolver(Matrix()); benchEigenSolver(Matrix()); benchEigenSolver(Matrix()); benchEigenSolver(Matrix()); return 0; }