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.

125 lines
3.9 KiB

  1. // Small bench routine for Eigen available in Eigen
  2. // (C) Desire NUENTSA WAKAM, INRIA
  3. #include <iostream>
  4. #include <fstream>
  5. #include <iomanip>
  6. #include <StormEigen/Jacobi>
  7. #include <StormEigen/Householder>
  8. #include <StormEigen/IterativeLinearSolvers>
  9. #include <StormEigen/LU>
  10. #include <unsupported/StormEigen/SparseExtra>
  11. //#include <StormEigen/SparseLU>
  12. #include <StormEigen/SuperLUSupport>
  13. // #include <unsupported/StormEigen/src/IterativeSolvers/Scaling.h>
  14. #include <bench/BenchTimer.h>
  15. #include <unsupported/StormEigen/IterativeSolvers>
  16. using namespace std;
  17. using namespace StormEigen;
  18. int main(int argc, char **args)
  19. {
  20. SparseMatrix<double, ColMajor> A;
  21. typedef SparseMatrix<double, ColMajor>::Index Index;
  22. typedef Matrix<double, Dynamic, Dynamic> DenseMatrix;
  23. typedef Matrix<double, Dynamic, 1> DenseRhs;
  24. VectorXd b, x, tmp;
  25. BenchTimer timer,totaltime;
  26. //SparseLU<SparseMatrix<double, ColMajor> > solver;
  27. // SuperLU<SparseMatrix<double, ColMajor> > solver;
  28. ConjugateGradient<SparseMatrix<double, ColMajor>, Lower,IncompleteCholesky<double,Lower> > solver;
  29. ifstream matrix_file;
  30. string line;
  31. int n;
  32. // Set parameters
  33. // solver.iparm(IPARM_THREAD_NBR) = 4;
  34. /* Fill the matrix with sparse matrix stored in Matrix-Market coordinate column-oriented format */
  35. if (argc < 2) assert(false && "please, give the matrix market file ");
  36. timer.start();
  37. totaltime.start();
  38. loadMarket(A, args[1]);
  39. cout << "End charging matrix " << endl;
  40. bool iscomplex=false, isvector=false;
  41. int sym;
  42. getMarketHeader(args[1], sym, iscomplex, isvector);
  43. if (iscomplex) { cout<< " Not for complex matrices \n"; return -1; }
  44. if (isvector) { cout << "The provided file is not a matrix file\n"; return -1;}
  45. if (sym != 0) { // symmetric matrices, only the lower part is stored
  46. SparseMatrix<double, ColMajor> temp;
  47. temp = A;
  48. A = temp.selfadjointView<Lower>();
  49. }
  50. timer.stop();
  51. n = A.cols();
  52. // ====== TESTS FOR SPARSE TUTORIAL ======
  53. // cout<< "OuterSize " << A.outerSize() << " inner " << A.innerSize() << endl;
  54. // SparseMatrix<double, RowMajor> mat1(A);
  55. // SparseMatrix<double, RowMajor> mat2;
  56. // cout << " norm of A " << mat1.norm() << endl; ;
  57. // PermutationMatrix<Dynamic, Dynamic, int> perm(n);
  58. // perm.resize(n,1);
  59. // perm.indices().setLinSpaced(n, 0, n-1);
  60. // mat2 = perm * mat1;
  61. // mat.subrows();
  62. // mat2.resize(n,n);
  63. // mat2.reserve(10);
  64. // mat2.setConstant();
  65. // std::cout<< "NORM " << mat1.squaredNorm()<< endl;
  66. cout<< "Time to load the matrix " << timer.value() <<endl;
  67. /* Fill the right hand side */
  68. // solver.set_restart(374);
  69. if (argc > 2)
  70. loadMarketVector(b, args[2]);
  71. else
  72. {
  73. b.resize(n);
  74. tmp.resize(n);
  75. // tmp.setRandom();
  76. for (int i = 0; i < n; i++) tmp(i) = i;
  77. b = A * tmp ;
  78. }
  79. // Scaling<SparseMatrix<double> > scal;
  80. // scal.computeRef(A);
  81. // b = scal.LeftScaling().cwiseProduct(b);
  82. /* Compute the factorization */
  83. cout<< "Starting the factorization "<< endl;
  84. timer.reset();
  85. timer.start();
  86. cout<< "Size of Input Matrix "<< b.size()<<"\n\n";
  87. cout<< "Rows and columns "<< A.rows() <<" " <<A.cols() <<"\n";
  88. solver.compute(A);
  89. // solver.analyzePattern(A);
  90. // solver.factorize(A);
  91. if (solver.info() != Success) {
  92. std::cout<< "The solver failed \n";
  93. return -1;
  94. }
  95. timer.stop();
  96. float time_comp = timer.value();
  97. cout <<" Compute Time " << time_comp<< endl;
  98. timer.reset();
  99. timer.start();
  100. x = solver.solve(b);
  101. // x = scal.RightScaling().cwiseProduct(x);
  102. timer.stop();
  103. float time_solve = timer.value();
  104. cout<< " Time to solve " << time_solve << endl;
  105. /* Check the accuracy */
  106. VectorXd tmp2 = b - A*x;
  107. double tempNorm = tmp2.norm()/b.norm();
  108. cout << "Relative norm of the computed solution : " << tempNorm <<"\n";
  109. // cout << "Iterations : " << solver.iterations() << "\n";
  110. totaltime.stop();
  111. cout << "Total time " << totaltime.value() << "\n";
  112. // std::cout<<x.transpose()<<"\n";
  113. return 0;
  114. }