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.

76 lines
2.8 KiB

  1. #include <iostream>
  2. #include "BenchTimer.h"
  3. #include <Eigen/Dense>
  4. #include <map>
  5. #include <string>
  6. using namespace Eigen;
  7. std::map<std::string,Array<float,1,4> > results;
  8. template<typename Scalar,int Size>
  9. void bench(int id, int size = Size)
  10. {
  11. typedef Matrix<Scalar,Size,Size> Mat;
  12. Mat A(size,size);
  13. A.setRandom();
  14. A = A*A.adjoint();
  15. BenchTimer t_llt, t_ldlt, t_lu, t_fplu, t_qr, t_cpqr, t_fpqr, t_jsvd;
  16. int tries = 3;
  17. int rep = 1000/size;
  18. if(rep==0) rep = 1;
  19. rep = rep*rep;
  20. LLT<Mat> llt(A);
  21. LDLT<Mat> ldlt(A);
  22. PartialPivLU<Mat> lu(A);
  23. FullPivLU<Mat> fplu(A);
  24. HouseholderQR<Mat> qr(A);
  25. ColPivHouseholderQR<Mat> cpqr(A);
  26. FullPivHouseholderQR<Mat> fpqr(A);
  27. JacobiSVD<Mat> jsvd(A.rows(),A.cols());
  28. BENCH(t_llt, tries, rep, llt.compute(A));
  29. BENCH(t_ldlt, tries, rep, ldlt.compute(A));
  30. BENCH(t_lu, tries, rep, lu.compute(A));
  31. BENCH(t_fplu, tries, rep, fplu.compute(A));
  32. BENCH(t_qr, tries, rep, qr.compute(A));
  33. BENCH(t_cpqr, tries, rep, cpqr.compute(A));
  34. BENCH(t_fpqr, tries, rep, fpqr.compute(A));
  35. if(size<500) // JacobiSVD is really too slow for too large matrices
  36. BENCH(t_jsvd, tries, rep, jsvd.compute(A,ComputeFullU|ComputeFullV));
  37. results["LLT"][id] = t_llt.best();
  38. results["LDLT"][id] = t_ldlt.best();
  39. results["PartialPivLU"][id] = t_lu.best();
  40. results["FullPivLU"][id] = t_fplu.best();
  41. results["HouseholderQR"][id] = t_qr.best();
  42. results["ColPivHouseholderQR"][id] = t_cpqr.best();
  43. results["FullPivHouseholderQR"][id] = t_fpqr.best();
  44. results["JacobiSVD"][id] = size<500 ? t_jsvd.best() : 0;
  45. }
  46. int main()
  47. {
  48. const int small = 8;
  49. const int medium = 100;
  50. const int large = 1000;
  51. const int xl = 4000;
  52. bench<float,small>(0);
  53. bench<float,Dynamic>(1,medium);
  54. bench<float,Dynamic>(2,large);
  55. bench<float,Dynamic>(3,xl);
  56. IOFormat fmt(3, 0, " \t", "\n", "", "");
  57. std::cout << "solver/size " << small << "\t" << medium << "\t" << large << "\t" << xl << "\n";
  58. std::cout << "LLT (ms) " << (results["LLT"]/1000.).format(fmt) << "\n";
  59. std::cout << "LDLT (%) " << (results["LDLT"]/results["LLT"]).format(fmt) << "\n";
  60. std::cout << "PartialPivLU (%) " << (results["PartialPivLU"]/results["LLT"]).format(fmt) << "\n";
  61. std::cout << "FullPivLU (%) " << (results["FullPivLU"]/results["LLT"]).format(fmt) << "\n";
  62. std::cout << "HouseholderQR (%) " << (results["HouseholderQR"]/results["LLT"]).format(fmt) << "\n";
  63. std::cout << "ColPivHouseholderQR (%) " << (results["ColPivHouseholderQR"]/results["LLT"]).format(fmt) << "\n";
  64. std::cout << "FullPivHouseholderQR (%) " << (results["FullPivHouseholderQR"]/results["LLT"]).format(fmt) << "\n";
  65. std::cout << "JacobiSVD (%) " << (results["JacobiSVD"]/results["LLT"]).format(fmt) << "\n";
  66. }