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.

216 lines
6.2 KiB

  1. // #define STORMEIGEN_TAUCS_SUPPORT
  2. // #define STORMEIGEN_CHOLMOD_SUPPORT
  3. #include <iostream>
  4. #include <StormEigen/Sparse>
  5. // g++ -DSIZE=10000 -DDENSITY=0.001 sparse_cholesky.cpp -I.. -DDENSEMATRI -O3 -g0 -DNDEBUG -DNBTRIES=1 -I /home/gael/Coding/LinearAlgebra/taucs_full/src/ -I/home/gael/Coding/LinearAlgebra/taucs_full/build/linux/ -L/home/gael/Coding/LinearAlgebra/taucs_full/lib/linux/ -ltaucs /home/gael/Coding/LinearAlgebra/GotoBLAS/libgoto.a -lpthread -I /home/gael/Coding/LinearAlgebra/SuiteSparse/CHOLMOD/Include/ $CHOLLIB -I /home/gael/Coding/LinearAlgebra/SuiteSparse/UFconfig/ /home/gael/Coding/LinearAlgebra/SuiteSparse/CCOLAMD/Lib/libccolamd.a /home/gael/Coding/LinearAlgebra/SuiteSparse/CHOLMOD/Lib/libcholmod.a -lmetis /home/gael/Coding/LinearAlgebra/SuiteSparse/AMD/Lib/libamd.a /home/gael/Coding/LinearAlgebra/SuiteSparse/CAMD/Lib/libcamd.a /home/gael/Coding/LinearAlgebra/SuiteSparse/CCOLAMD/Lib/libccolamd.a /home/gael/Coding/LinearAlgebra/SuiteSparse/COLAMD/Lib/libcolamd.a -llapack && ./a.out
  6. #define NOGMM
  7. #define NOMTL
  8. #ifndef SIZE
  9. #define SIZE 10
  10. #endif
  11. #ifndef DENSITY
  12. #define DENSITY 0.01
  13. #endif
  14. #ifndef REPEAT
  15. #define REPEAT 1
  16. #endif
  17. #include "BenchSparseUtil.h"
  18. #ifndef MINDENSITY
  19. #define MINDENSITY 0.0004
  20. #endif
  21. #ifndef NBTRIES
  22. #define NBTRIES 10
  23. #endif
  24. #define BENCH(X) \
  25. timer.reset(); \
  26. for (int _j=0; _j<NBTRIES; ++_j) { \
  27. timer.start(); \
  28. for (int _k=0; _k<REPEAT; ++_k) { \
  29. X \
  30. } timer.stop(); }
  31. // typedef SparseMatrix<Scalar,UpperTriangular> EigenSparseTriMatrix;
  32. typedef SparseMatrix<Scalar,SelfAdjoint|LowerTriangular> EigenSparseSelfAdjointMatrix;
  33. void fillSpdMatrix(float density, int rows, int cols, EigenSparseSelfAdjointMatrix& dst)
  34. {
  35. dst.startFill(rows*cols*density);
  36. for(int j = 0; j < cols; j++)
  37. {
  38. dst.fill(j,j) = internal::random<Scalar>(10,20);
  39. for(int i = j+1; i < rows; i++)
  40. {
  41. Scalar v = (internal::random<float>(0,1) < density) ? internal::random<Scalar>() : 0;
  42. if (v!=0)
  43. dst.fill(i,j) = v;
  44. }
  45. }
  46. dst.endFill();
  47. }
  48. #include <StormEigen/Cholesky>
  49. template<int Backend>
  50. void doEigen(const char* name, const EigenSparseSelfAdjointMatrix& sm1, int flags = 0)
  51. {
  52. std::cout << name << "..." << std::flush;
  53. BenchTimer timer;
  54. timer.start();
  55. SparseLLT<EigenSparseSelfAdjointMatrix,Backend> chol(sm1, flags);
  56. timer.stop();
  57. std::cout << ":\t" << timer.value() << endl;
  58. std::cout << " nnz: " << sm1.nonZeros() << " => " << chol.matrixL().nonZeros() << "\n";
  59. // std::cout << "sparse\n" << chol.matrixL() << "%\n";
  60. }
  61. int main(int argc, char *argv[])
  62. {
  63. int rows = SIZE;
  64. int cols = SIZE;
  65. float density = DENSITY;
  66. BenchTimer timer;
  67. VectorXf b = VectorXf::Random(cols);
  68. VectorXf x = VectorXf::Random(cols);
  69. bool densedone = false;
  70. //for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
  71. // float density = 0.5;
  72. {
  73. EigenSparseSelfAdjointMatrix sm1(rows, cols);
  74. std::cout << "Generate sparse matrix (might take a while)...\n";
  75. fillSpdMatrix(density, rows, cols, sm1);
  76. std::cout << "DONE\n\n";
  77. // dense matrices
  78. #ifdef DENSEMATRIX
  79. if (!densedone)
  80. {
  81. densedone = true;
  82. std::cout << "Eigen Dense\t" << density*100 << "%\n";
  83. DenseMatrix m1(rows,cols);
  84. eiToDense(sm1, m1);
  85. m1 = (m1 + m1.transpose()).eval();
  86. m1.diagonal() *= 0.5;
  87. // BENCH(LLT<DenseMatrix> chol(m1);)
  88. // std::cout << "dense:\t" << timer.value() << endl;
  89. BenchTimer timer;
  90. timer.start();
  91. LLT<DenseMatrix> chol(m1);
  92. timer.stop();
  93. std::cout << "dense:\t" << timer.value() << endl;
  94. int count = 0;
  95. for (int j=0; j<cols; ++j)
  96. for (int i=j; i<rows; ++i)
  97. if (!internal::isMuchSmallerThan(internal::abs(chol.matrixL()(i,j)), 0.1))
  98. count++;
  99. std::cout << "dense: " << "nnz = " << count << "\n";
  100. // std::cout << "dense:\n" << m1 << "\n\n" << chol.matrixL() << endl;
  101. }
  102. #endif
  103. // eigen sparse matrices
  104. doEigen<StormEigen::DefaultBackend>("StormEigen/Sparse", sm1, StormEigen::IncompleteFactorization);
  105. #ifdef STORMEIGEN_CHOLMOD_SUPPORT
  106. doEigen<StormEigen::Cholmod>("StormEigen/Cholmod", sm1, StormEigen::IncompleteFactorization);
  107. #endif
  108. #ifdef STORMEIGEN_TAUCS_SUPPORT
  109. doEigen<StormEigen::Taucs>("StormEigen/Taucs", sm1, StormEigen::IncompleteFactorization);
  110. #endif
  111. #if 0
  112. // TAUCS
  113. {
  114. taucs_ccs_matrix A = sm1.asTaucsMatrix();
  115. //BENCH(taucs_ccs_matrix* chol = taucs_ccs_factor_llt(&A, 0, 0);)
  116. // BENCH(taucs_supernodal_factor_to_ccs(taucs_ccs_factor_llt_ll(&A));)
  117. // std::cout << "taucs:\t" << timer.value() << endl;
  118. taucs_ccs_matrix* chol = taucs_ccs_factor_llt(&A, 0, 0);
  119. for (int j=0; j<cols; ++j)
  120. {
  121. for (int i=chol->colptr[j]; i<chol->colptr[j+1]; ++i)
  122. std::cout << chol->values.d[i] << " ";
  123. }
  124. }
  125. // CHOLMOD
  126. #ifdef STORMEIGEN_CHOLMOD_SUPPORT
  127. {
  128. cholmod_common c;
  129. cholmod_start (&c);
  130. cholmod_sparse A;
  131. cholmod_factor *L;
  132. A = sm1.asCholmodMatrix();
  133. BenchTimer timer;
  134. // timer.reset();
  135. timer.start();
  136. std::vector<int> perm(cols);
  137. // std::vector<int> set(ncols);
  138. for (int i=0; i<cols; ++i)
  139. perm[i] = i;
  140. // c.nmethods = 1;
  141. // c.method[0] = 1;
  142. c.nmethods = 1;
  143. c.method [0].ordering = CHOLMOD_NATURAL;
  144. c.postorder = 0;
  145. c.final_ll = 1;
  146. L = cholmod_analyze_p(&A, &perm[0], &perm[0], cols, &c);
  147. timer.stop();
  148. std::cout << "cholmod/analyze:\t" << timer.value() << endl;
  149. timer.reset();
  150. timer.start();
  151. cholmod_factorize(&A, L, &c);
  152. timer.stop();
  153. std::cout << "cholmod/factorize:\t" << timer.value() << endl;
  154. cholmod_sparse* cholmat = cholmod_factor_to_sparse(L, &c);
  155. cholmod_print_factor(L, "Factors", &c);
  156. cholmod_print_sparse(cholmat, "Chol", &c);
  157. cholmod_write_sparse(stdout, cholmat, 0, 0, &c);
  158. //
  159. // cholmod_print_sparse(&A, "A", &c);
  160. // cholmod_write_sparse(stdout, &A, 0, 0, &c);
  161. // for (int j=0; j<cols; ++j)
  162. // {
  163. // for (int i=chol->colptr[j]; i<chol->colptr[j+1]; ++i)
  164. // std::cout << chol->values.s[i] << " ";
  165. // }
  166. }
  167. #endif
  168. #endif
  169. }
  170. return 0;
  171. }