sparse_lu.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // g++ -I.. sparse_lu.cpp -O3 -g0 -I /usr/include/superlu/ -lsuperlu -lgfortran -DSIZE=1000 -DDENSITY=.05 && ./a.out
  2. #define EIGEN_SUPERLU_SUPPORT
  3. #define EIGEN_UMFPACK_SUPPORT
  4. #include <Eigen/Sparse>
  5. #define NOGMM
  6. #define NOMTL
  7. #ifndef SIZE
  8. #define SIZE 10
  9. #endif
  10. #ifndef DENSITY
  11. #define DENSITY 0.01
  12. #endif
  13. #ifndef REPEAT
  14. #define REPEAT 1
  15. #endif
  16. #include "BenchSparseUtil.h"
  17. #ifndef MINDENSITY
  18. #define MINDENSITY 0.0004
  19. #endif
  20. #ifndef NBTRIES
  21. #define NBTRIES 10
  22. #endif
  23. #define BENCH(X) \
  24. timer.reset(); \
  25. for (int _j=0; _j<NBTRIES; ++_j) { \
  26. timer.start(); \
  27. for (int _k=0; _k<REPEAT; ++_k) { \
  28. X \
  29. } timer.stop(); }
  30. typedef Matrix<Scalar,Dynamic,1> VectorX;
  31. #include <Eigen/LU>
  32. template<int Backend>
  33. void doEigen(const char* name, const EigenSparseMatrix& sm1, const VectorX& b, VectorX& x, int flags = 0)
  34. {
  35. std::cout << name << "..." << std::flush;
  36. BenchTimer timer; timer.start();
  37. SparseLU<EigenSparseMatrix,Backend> lu(sm1, flags);
  38. timer.stop();
  39. if (lu.succeeded())
  40. std::cout << ":\t" << timer.value() << endl;
  41. else
  42. {
  43. std::cout << ":\t FAILED" << endl;
  44. return;
  45. }
  46. bool ok;
  47. timer.reset(); timer.start();
  48. ok = lu.solve(b,&x);
  49. timer.stop();
  50. if (ok)
  51. std::cout << " solve:\t" << timer.value() << endl;
  52. else
  53. std::cout << " solve:\t" << " FAILED" << endl;
  54. //std::cout << x.transpose() << "\n";
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58. int rows = SIZE;
  59. int cols = SIZE;
  60. float density = DENSITY;
  61. BenchTimer timer;
  62. VectorX b = VectorX::Random(cols);
  63. VectorX x = VectorX::Random(cols);
  64. bool densedone = false;
  65. //for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
  66. // float density = 0.5;
  67. {
  68. EigenSparseMatrix sm1(rows, cols);
  69. fillMatrix(density, rows, cols, sm1);
  70. // dense matrices
  71. #ifdef DENSEMATRIX
  72. if (!densedone)
  73. {
  74. densedone = true;
  75. std::cout << "Eigen Dense\t" << density*100 << "%\n";
  76. DenseMatrix m1(rows,cols);
  77. eiToDense(sm1, m1);
  78. BenchTimer timer;
  79. timer.start();
  80. FullPivLU<DenseMatrix> lu(m1);
  81. timer.stop();
  82. std::cout << "Eigen/dense:\t" << timer.value() << endl;
  83. timer.reset();
  84. timer.start();
  85. lu.solve(b,&x);
  86. timer.stop();
  87. std::cout << " solve:\t" << timer.value() << endl;
  88. // std::cout << b.transpose() << "\n";
  89. // std::cout << x.transpose() << "\n";
  90. }
  91. #endif
  92. #ifdef EIGEN_UMFPACK_SUPPORT
  93. x.setZero();
  94. doEigen<Eigen::UmfPack>("Eigen/UmfPack (auto)", sm1, b, x, 0);
  95. #endif
  96. #ifdef EIGEN_SUPERLU_SUPPORT
  97. x.setZero();
  98. doEigen<Eigen::SuperLU>("Eigen/SuperLU (nat)", sm1, b, x, Eigen::NaturalOrdering);
  99. // doEigen<Eigen::SuperLU>("Eigen/SuperLU (MD AT+A)", sm1, b, x, Eigen::MinimumDegree_AT_PLUS_A);
  100. // doEigen<Eigen::SuperLU>("Eigen/SuperLU (MD ATA)", sm1, b, x, Eigen::MinimumDegree_ATA);
  101. doEigen<Eigen::SuperLU>("Eigen/SuperLU (COLAMD)", sm1, b, x, Eigen::ColApproxMinimumDegree);
  102. #endif
  103. }
  104. return 0;
  105. }