testILSmethods.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @file testILSmethods.cpp
  3. * @author Paul Bodesheim
  4. * @date 23/01/2012
  5. * @brief test routine for Iterative Linear Solvers
  6. */
  7. #include "core/vector/MatrixT.h"
  8. #include "core/vector/VectorT.h"
  9. #include <stdio.h>
  10. #include <ctime>
  11. #include "core/basics/Exception.h"
  12. #include "core/vector/Algorithms.h"
  13. #include "core/algebra/GMStandard.h"
  14. #include "core/algebra/ILSConjugateGradients.h"
  15. #include "core/algebra/ILSConjugateGradientsLanczos.h"
  16. #include "core/algebra/ILSSymmLqLanczos.h"
  17. #include "core/algebra/ILSMinResLanczos.h"
  18. using namespace std;
  19. using namespace NICE;
  20. int main(int argc, char* argv[])
  21. {
  22. int mySize = 10000; // number of equations
  23. srand ( time(NULL) );
  24. // generate matrix A
  25. Matrix A(mySize,mySize,0.0);
  26. for (uint i = 0; i < A.rows(); i++)
  27. {
  28. for (uint j = i; j < A.cols(); j++)
  29. {
  30. if ( j == i ) A(i,j) = (i+1)+(j+1);
  31. else {
  32. double r = ( (double) rand() ) / (RAND_MAX);
  33. A(i,j) = r;
  34. A(j,i) = r;
  35. }
  36. }
  37. }
  38. // generate vector b (RHS of LS)
  39. Vector b(mySize,0.0);
  40. for (uint i = 0; i < b.size(); i++)
  41. {
  42. b(i) = ( (double) rand() ) / (RAND_MAX);
  43. }
  44. // solve Ax = b with cgm method
  45. ILSConjugateGradients cgm(false,mySize);
  46. ILSConjugateGradientsLanczos cgm_L(false,mySize);
  47. ILSSymmLqLanczos symmlq(false,mySize);
  48. ILSMinResLanczos minres(false,mySize);
  49. Vector x(mySize,0.0);
  50. //tic
  51. time_t start = clock();
  52. cgm.solveLin(GMStandard(A),b,x);
  53. //toc
  54. float duration = (float) (clock() - start);
  55. std::cerr << "Time for solveLin (ILSConjugateGradients): " << duration/CLOCKS_PER_SEC << std::endl;
  56. x.set(0.0);
  57. //tic
  58. start = clock();
  59. cgm_L.solveLin(GMStandard(A),b,x);
  60. //toc
  61. duration = (float) (clock() - start);
  62. std::cerr << "Time for solveLin (ILSConjugateGradientsLanczos): " << duration/CLOCKS_PER_SEC << std::endl;
  63. x.set(0.0);
  64. //tic
  65. start = clock();
  66. symmlq.solveLin(GMStandard(A),b,x);
  67. //toc
  68. duration = (float) (clock() - start);
  69. std::cerr << "Time for solveLin (ILSSymmLqLanczos): " << duration/CLOCKS_PER_SEC << std::endl;
  70. x.set(0.0);
  71. //tic
  72. start = clock();
  73. minres.solveLin(GMStandard(A),b,x);
  74. //toc
  75. duration = (float) (clock() - start);
  76. std::cerr << "Time for solveLin (ILSMinResLanczos): " << duration/CLOCKS_PER_SEC << std::endl;
  77. return 0;
  78. }