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.

123 lines
3.8 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2013 Gauthier Brun <brun.gauthier@gmail.com>
  5. // Copyright (C) 2013 Nicolas Carre <nicolas.carre@ensimag.fr>
  6. // Copyright (C) 2013 Jean Ceccato <jean.ceccato@ensimag.fr>
  7. // Copyright (C) 2013 Pierre Zoppitelli <pierre.zoppitelli@ensimag.fr>
  8. //
  9. // This Source Code Form is subject to the terms of the Mozilla
  10. // Public License v. 2.0. If a copy of the MPL was not distributed
  11. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/
  12. // Bench to compare the efficiency of SVD algorithms
  13. #include <iostream>
  14. #include <bench/BenchTimer.h>
  15. #include <unsupported/Eigen/SVD>
  16. using namespace Eigen;
  17. using namespace std;
  18. // number of computations of each algorithm before the print of the time
  19. #ifndef REPEAT
  20. #define REPEAT 10
  21. #endif
  22. // number of tests of the same type
  23. #ifndef NUMBER_SAMPLE
  24. #define NUMBER_SAMPLE 2
  25. #endif
  26. template<typename MatrixType>
  27. void bench_svd(const MatrixType& a = MatrixType())
  28. {
  29. MatrixType m = MatrixType::Random(a.rows(), a.cols());
  30. BenchTimer timerJacobi;
  31. BenchTimer timerBDC;
  32. timerJacobi.reset();
  33. timerBDC.reset();
  34. cout << " Only compute Singular Values" <<endl;
  35. for (int k=1; k<=NUMBER_SAMPLE; ++k)
  36. {
  37. timerBDC.start();
  38. for (int i=0; i<REPEAT; ++i)
  39. {
  40. BDCSVD<MatrixType> bdc_matrix(m);
  41. }
  42. timerBDC.stop();
  43. timerJacobi.start();
  44. for (int i=0; i<REPEAT; ++i)
  45. {
  46. JacobiSVD<MatrixType> jacobi_matrix(m);
  47. }
  48. timerJacobi.stop();
  49. cout << "Sample " << k << " : " << REPEAT << " computations : Jacobi : " << fixed << timerJacobi.value() << "s ";
  50. cout << " || " << " BDC : " << timerBDC.value() << "s " <<endl <<endl;
  51. if (timerBDC.value() >= timerJacobi.value())
  52. cout << "KO : BDC is " << timerJacobi.value() / timerBDC.value() << " times faster than Jacobi" <<endl;
  53. else
  54. cout << "OK : BDC is " << timerJacobi.value() / timerBDC.value() << " times faster than Jacobi" <<endl;
  55. }
  56. cout << " =================" <<endl;
  57. std::cout<< std::endl;
  58. timerJacobi.reset();
  59. timerBDC.reset();
  60. cout << " Computes rotaion matrix" <<endl;
  61. for (int k=1; k<=NUMBER_SAMPLE; ++k)
  62. {
  63. timerBDC.start();
  64. for (int i=0; i<REPEAT; ++i)
  65. {
  66. BDCSVD<MatrixType> bdc_matrix(m, ComputeFullU|ComputeFullV);
  67. }
  68. timerBDC.stop();
  69. timerJacobi.start();
  70. for (int i=0; i<REPEAT; ++i)
  71. {
  72. JacobiSVD<MatrixType> jacobi_matrix(m, ComputeFullU|ComputeFullV);
  73. }
  74. timerJacobi.stop();
  75. cout << "Sample " << k << " : " << REPEAT << " computations : Jacobi : " << fixed << timerJacobi.value() << "s ";
  76. cout << " || " << " BDC : " << timerBDC.value() << "s " <<endl <<endl;
  77. if (timerBDC.value() >= timerJacobi.value())
  78. cout << "KO : BDC is " << timerJacobi.value() / timerBDC.value() << " times faster than Jacobi" <<endl;
  79. else
  80. cout << "OK : BDC is " << timerJacobi.value() / timerBDC.value() << " times faster than Jacobi" <<endl;
  81. }
  82. std::cout<< std::endl;
  83. }
  84. int main(int argc, char* argv[])
  85. {
  86. std::cout<< std::endl;
  87. std::cout<<"On a (Dynamic, Dynamic) (6, 6) Matrix" <<std::endl;
  88. bench_svd<Matrix<double,Dynamic,Dynamic> >(Matrix<double,Dynamic,Dynamic>(6, 6));
  89. std::cout<<"On a (Dynamic, Dynamic) (32, 32) Matrix" <<std::endl;
  90. bench_svd<Matrix<double,Dynamic,Dynamic> >(Matrix<double,Dynamic,Dynamic>(32, 32));
  91. //std::cout<<"On a (Dynamic, Dynamic) (128, 128) Matrix" <<std::endl;
  92. //bench_svd<Matrix<double,Dynamic,Dynamic> >(Matrix<double,Dynamic,Dynamic>(128, 128));
  93. std::cout<<"On a (Dynamic, Dynamic) (160, 160) Matrix" <<std::endl;
  94. bench_svd<Matrix<double,Dynamic,Dynamic> >(Matrix<double,Dynamic,Dynamic>(160, 160));
  95. std::cout<< "--------------------------------------------------------------------"<< std::endl;
  96. }