2008-07-09 01:20:17 +08:00
|
|
|
|
2008-10-13 23:53:27 +08:00
|
|
|
// g++ -DNDEBUG -O3 -I.. benchLLT.cpp -o benchLLT && ./benchLLT
|
2008-07-09 01:20:17 +08:00
|
|
|
// options:
|
|
|
|
// -DBENCH_GSL -lgsl /usr/lib/libcblas.so.3
|
|
|
|
// -DEIGEN_DONT_VECTORIZE
|
|
|
|
// -msse2
|
|
|
|
// -DREPEAT=100
|
|
|
|
// -DTRIES=10
|
|
|
|
// -DSCALAR=double
|
|
|
|
|
|
|
|
#include <Eigen/Array>
|
2009-02-04 03:05:10 +08:00
|
|
|
#include <Eigen/Cholesky>
|
2008-07-09 01:20:17 +08:00
|
|
|
#include <bench/BenchUtil.h>
|
|
|
|
using namespace Eigen;
|
|
|
|
|
|
|
|
#ifndef REPEAT
|
|
|
|
#define REPEAT 10000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TRIES
|
2008-08-10 04:06:25 +08:00
|
|
|
#define TRIES 10
|
2008-07-09 01:20:17 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef float Scalar;
|
|
|
|
|
|
|
|
template <typename MatrixType>
|
2008-10-13 23:53:27 +08:00
|
|
|
__attribute__ ((noinline)) void benchLLT(const MatrixType& m)
|
2008-07-09 01:20:17 +08:00
|
|
|
{
|
|
|
|
int rows = m.rows();
|
|
|
|
int cols = m.cols();
|
|
|
|
|
2008-08-10 04:06:25 +08:00
|
|
|
int cost = 0;
|
|
|
|
for (int j=0; j<rows; ++j)
|
|
|
|
{
|
|
|
|
int r = std::max(rows - j -1,0);
|
|
|
|
cost += 2*(r*j+r+j);
|
|
|
|
}
|
|
|
|
|
2008-07-09 01:20:17 +08:00
|
|
|
int repeats = (REPEAT*1000)/(rows*rows);
|
|
|
|
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
|
|
|
|
|
2008-07-24 01:30:00 +08:00
|
|
|
MatrixType a = MatrixType::Random(rows,cols);
|
2008-07-09 01:20:17 +08:00
|
|
|
SquareMatrixType covMat = a * a.adjoint();
|
|
|
|
|
|
|
|
BenchTimer timerNoSqrt, timerSqrt;
|
|
|
|
|
|
|
|
Scalar acc = 0;
|
|
|
|
int r = ei_random<int>(0,covMat.rows()-1);
|
|
|
|
int c = ei_random<int>(0,covMat.cols()-1);
|
|
|
|
for (int t=0; t<TRIES; ++t)
|
|
|
|
{
|
|
|
|
timerNoSqrt.start();
|
|
|
|
for (int k=0; k<repeats; ++k)
|
|
|
|
{
|
2008-10-13 23:53:27 +08:00
|
|
|
LDLT<SquareMatrixType> cholnosqrt(covMat);
|
2008-07-09 01:20:17 +08:00
|
|
|
acc += cholnosqrt.matrixL().coeff(r,c);
|
|
|
|
}
|
|
|
|
timerNoSqrt.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int t=0; t<TRIES; ++t)
|
|
|
|
{
|
|
|
|
timerSqrt.start();
|
|
|
|
for (int k=0; k<repeats; ++k)
|
|
|
|
{
|
2008-10-13 23:53:27 +08:00
|
|
|
LLT<SquareMatrixType> chol(covMat);
|
2008-07-09 01:20:17 +08:00
|
|
|
acc += chol.matrixL().coeff(r,c);
|
|
|
|
}
|
|
|
|
timerSqrt.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MatrixType::RowsAtCompileTime==Dynamic)
|
|
|
|
std::cout << "dyn ";
|
|
|
|
else
|
|
|
|
std::cout << "fixed ";
|
|
|
|
std::cout << covMat.rows() << " \t"
|
2009-02-04 03:05:10 +08:00
|
|
|
<< (timerNoSqrt.value() * REPEAT) / repeats << "s "
|
|
|
|
<< "(" << 1e-6 * cost*repeats/timerNoSqrt.value() << " MFLOPS)\t"
|
2008-08-10 04:06:25 +08:00
|
|
|
<< (timerSqrt.value() * REPEAT) / repeats << "s "
|
|
|
|
<< "(" << 1e-6 * cost*repeats/timerSqrt.value() << " MFLOPS)\n";
|
2008-07-09 01:20:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef BENCH_GSL
|
|
|
|
if (MatrixType::RowsAtCompileTime==Dynamic)
|
|
|
|
{
|
|
|
|
timerSqrt.reset();
|
|
|
|
|
|
|
|
gsl_matrix* gslCovMat = gsl_matrix_alloc(covMat.rows(),covMat.cols());
|
|
|
|
gsl_matrix* gslCopy = gsl_matrix_alloc(covMat.rows(),covMat.cols());
|
2009-02-04 03:05:10 +08:00
|
|
|
|
2008-07-09 01:20:17 +08:00
|
|
|
eiToGsl(covMat, &gslCovMat);
|
|
|
|
for (int t=0; t<TRIES; ++t)
|
|
|
|
{
|
|
|
|
timerSqrt.start();
|
|
|
|
for (int k=0; k<repeats; ++k)
|
|
|
|
{
|
|
|
|
gsl_matrix_memcpy(gslCopy,gslCovMat);
|
|
|
|
gsl_linalg_cholesky_decomp(gslCopy);
|
|
|
|
acc += gsl_matrix_get(gslCopy,r,c);
|
|
|
|
}
|
|
|
|
timerSqrt.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << " | \t"
|
|
|
|
<< timerSqrt.value() * REPEAT / repeats << "s";
|
|
|
|
|
|
|
|
gsl_matrix_free(gslCovMat);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
std::cout << "\n";
|
|
|
|
// make sure the compiler does not optimize too much
|
|
|
|
if (acc==123)
|
|
|
|
std::cout << acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2009-02-06 22:01:01 +08:00
|
|
|
const int dynsizes[] = {4,6,8,16,24,32,49,64,128,256,512,900,0};
|
2009-02-04 03:05:10 +08:00
|
|
|
std::cout << "size no sqrt standard";
|
|
|
|
// #ifdef BENCH_GSL
|
|
|
|
// std::cout << " GSL (standard + double + ATLAS) ";
|
|
|
|
// #endif
|
2008-07-09 01:20:17 +08:00
|
|
|
std::cout << "\n";
|
2009-02-06 22:01:01 +08:00
|
|
|
for (uint i=0; dynsizes[i]>0; ++i)
|
|
|
|
benchLLT(Matrix<Scalar,Dynamic,Dynamic>(dynsizes[i],dynsizes[i]));
|
2009-02-04 03:05:10 +08:00
|
|
|
|
|
|
|
benchLLT(Matrix<Scalar,2,2>());
|
|
|
|
benchLLT(Matrix<Scalar,3,3>());
|
|
|
|
benchLLT(Matrix<Scalar,4,4>());
|
|
|
|
benchLLT(Matrix<Scalar,5,5>());
|
|
|
|
benchLLT(Matrix<Scalar,6,6>());
|
|
|
|
benchLLT(Matrix<Scalar,7,7>());
|
|
|
|
benchLLT(Matrix<Scalar,8,8>());
|
|
|
|
benchLLT(Matrix<Scalar,12,12>());
|
|
|
|
benchLLT(Matrix<Scalar,16,16>());
|
2008-07-09 01:20:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|