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.

93 lines
2.8 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 <unsupported/StormEigen/SparseExtra>
  7. #include <StormEigen/SparseLU>
  8. #include <bench/BenchTimer.h>
  9. #ifdef STORMEIGEN_METIS_SUPPORT
  10. #include <StormEigen/MetisSupport>
  11. #endif
  12. using namespace std;
  13. using namespace StormEigen;
  14. int main(int argc, char **args)
  15. {
  16. // typedef complex<double> scalar;
  17. typedef double scalar;
  18. SparseMatrix<scalar, ColMajor> A;
  19. typedef SparseMatrix<scalar, ColMajor>::Index Index;
  20. typedef Matrix<scalar, Dynamic, Dynamic> DenseMatrix;
  21. typedef Matrix<scalar, Dynamic, 1> DenseRhs;
  22. Matrix<scalar, Dynamic, 1> b, x, tmp;
  23. // SparseLU<SparseMatrix<scalar, ColMajor>, AMDOrdering<int> > solver;
  24. // #ifdef STORMEIGEN_METIS_SUPPORT
  25. // SparseLU<SparseMatrix<scalar, ColMajor>, MetisOrdering<int> > solver;
  26. // std::cout<< "ORDERING : METIS\n";
  27. // #else
  28. SparseLU<SparseMatrix<scalar, ColMajor>, COLAMDOrdering<int> > solver;
  29. std::cout<< "ORDERING : COLAMD\n";
  30. // #endif
  31. ifstream matrix_file;
  32. string line;
  33. int n;
  34. BenchTimer timer;
  35. // Set parameters
  36. /* Fill the matrix with sparse matrix stored in Matrix-Market coordinate column-oriented format */
  37. if (argc < 2) assert(false && "please, give the matrix market file ");
  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<scalar, ColMajor> temp;
  47. temp = A;
  48. A = temp.selfadjointView<Lower>();
  49. }
  50. n = A.cols();
  51. /* Fill the right hand side */
  52. if (argc > 2)
  53. loadMarketVector(b, args[2]);
  54. else
  55. {
  56. b.resize(n);
  57. tmp.resize(n);
  58. // tmp.setRandom();
  59. for (int i = 0; i < n; i++) tmp(i) = i;
  60. b = A * tmp ;
  61. }
  62. /* Compute the factorization */
  63. // solver.isSymmetric(true);
  64. timer.start();
  65. // solver.compute(A);
  66. solver.analyzePattern(A);
  67. timer.stop();
  68. cout << "Time to analyze " << timer.value() << std::endl;
  69. timer.reset();
  70. timer.start();
  71. solver.factorize(A);
  72. timer.stop();
  73. cout << "Factorize Time " << timer.value() << std::endl;
  74. timer.reset();
  75. timer.start();
  76. x = solver.solve(b);
  77. timer.stop();
  78. cout << "solve time " << timer.value() << std::endl;
  79. /* Check the accuracy */
  80. Matrix<scalar, Dynamic, 1> tmp2 = b - A*x;
  81. scalar tempNorm = tmp2.norm()/b.norm();
  82. cout << "Relative norm of the computed solution : " << tempNorm <<"\n";
  83. cout << "Number of nonzeros in the factor : " << solver.nnzL() + solver.nnzU() << std::endl;
  84. return 0;
  85. }