You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.5 KiB

  1. // g++ -DNDEBUG -O3 -I.. benchLLT.cpp -o benchLLT && ./benchLLT
  2. // options:
  3. // -DBENCH_GSL -lgsl /usr/lib/libcblas.so.3
  4. // -DSTORMEIGEN_DONT_VECTORIZE
  5. // -msse2
  6. // -DREPEAT=100
  7. // -DTRIES=10
  8. // -DSCALAR=double
  9. #include <iostream>
  10. #include <StormEigen/Core>
  11. #include <StormEigen/Cholesky>
  12. #include <bench/BenchUtil.h>
  13. using namespace StormEigen;
  14. #ifndef REPEAT
  15. #define REPEAT 10000
  16. #endif
  17. #ifndef TRIES
  18. #define TRIES 10
  19. #endif
  20. typedef float Scalar;
  21. template <typename MatrixType>
  22. __attribute__ ((noinline)) void benchLLT(const MatrixType& m)
  23. {
  24. int rows = m.rows();
  25. int cols = m.cols();
  26. int cost = 0;
  27. for (int j=0; j<rows; ++j)
  28. {
  29. int r = std::max(rows - j -1,0);
  30. cost += 2*(r*j+r+j);
  31. }
  32. int repeats = (REPEAT*1000)/(rows*rows);
  33. typedef typename MatrixType::Scalar Scalar;
  34. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
  35. MatrixType a = MatrixType::Random(rows,cols);
  36. SquareMatrixType covMat = a * a.adjoint();
  37. BenchTimer timerNoSqrt, timerSqrt;
  38. Scalar acc = 0;
  39. int r = internal::random<int>(0,covMat.rows()-1);
  40. int c = internal::random<int>(0,covMat.cols()-1);
  41. for (int t=0; t<TRIES; ++t)
  42. {
  43. timerNoSqrt.start();
  44. for (int k=0; k<repeats; ++k)
  45. {
  46. LDLT<SquareMatrixType> cholnosqrt(covMat);
  47. acc += cholnosqrt.matrixL().coeff(r,c);
  48. }
  49. timerNoSqrt.stop();
  50. }
  51. for (int t=0; t<TRIES; ++t)
  52. {
  53. timerSqrt.start();
  54. for (int k=0; k<repeats; ++k)
  55. {
  56. LLT<SquareMatrixType> chol(covMat);
  57. acc += chol.matrixL().coeff(r,c);
  58. }
  59. timerSqrt.stop();
  60. }
  61. if (MatrixType::RowsAtCompileTime==Dynamic)
  62. std::cout << "dyn ";
  63. else
  64. std::cout << "fixed ";
  65. std::cout << covMat.rows() << " \t"
  66. << (timerNoSqrt.value() * REPEAT) / repeats << "s "
  67. << "(" << 1e-6 * cost*repeats/timerNoSqrt.value() << " MFLOPS)\t"
  68. << (timerSqrt.value() * REPEAT) / repeats << "s "
  69. << "(" << 1e-6 * cost*repeats/timerSqrt.value() << " MFLOPS)\n";
  70. #ifdef BENCH_GSL
  71. if (MatrixType::RowsAtCompileTime==Dynamic)
  72. {
  73. timerSqrt.reset();
  74. gsl_matrix* gslCovMat = gsl_matrix_alloc(covMat.rows(),covMat.cols());
  75. gsl_matrix* gslCopy = gsl_matrix_alloc(covMat.rows(),covMat.cols());
  76. eiToGsl(covMat, &gslCovMat);
  77. for (int t=0; t<TRIES; ++t)
  78. {
  79. timerSqrt.start();
  80. for (int k=0; k<repeats; ++k)
  81. {
  82. gsl_matrix_memcpy(gslCopy,gslCovMat);
  83. gsl_linalg_cholesky_decomp(gslCopy);
  84. acc += gsl_matrix_get(gslCopy,r,c);
  85. }
  86. timerSqrt.stop();
  87. }
  88. std::cout << " | \t"
  89. << timerSqrt.value() * REPEAT / repeats << "s";
  90. gsl_matrix_free(gslCovMat);
  91. }
  92. #endif
  93. std::cout << "\n";
  94. // make sure the compiler does not optimize too much
  95. if (acc==123)
  96. std::cout << acc;
  97. }
  98. int main(int argc, char* argv[])
  99. {
  100. const int dynsizes[] = {4,6,8,16,24,32,49,64,128,256,512,900,0};
  101. std::cout << "size no sqrt standard";
  102. // #ifdef BENCH_GSL
  103. // std::cout << " GSL (standard + double + ATLAS) ";
  104. // #endif
  105. std::cout << "\n";
  106. for (uint i=0; dynsizes[i]>0; ++i)
  107. benchLLT(Matrix<Scalar,Dynamic,Dynamic>(dynsizes[i],dynsizes[i]));
  108. benchLLT(Matrix<Scalar,2,2>());
  109. benchLLT(Matrix<Scalar,3,3>());
  110. benchLLT(Matrix<Scalar,4,4>());
  111. benchLLT(Matrix<Scalar,5,5>());
  112. benchLLT(Matrix<Scalar,6,6>());
  113. benchLLT(Matrix<Scalar,7,7>());
  114. benchLLT(Matrix<Scalar,8,8>());
  115. benchLLT(Matrix<Scalar,12,12>());
  116. benchLLT(Matrix<Scalar,16,16>());
  117. return 0;
  118. }