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.

220 lines
6.0 KiB

  1. //g++ -O3 -g0 -DNDEBUG sparse_product.cpp -I.. -I/home/gael/Coding/LinearAlgebra/mtl4/ -DDENSITY=0.005 -DSIZE=10000 && ./a.out
  2. //g++ -O3 -g0 -DNDEBUG sparse_product.cpp -I.. -I/home/gael/Coding/LinearAlgebra/mtl4/ -DDENSITY=0.05 -DSIZE=2000 && ./a.out
  3. // -DNOGMM -DNOMTL
  4. // -I /home/gael/Coding/LinearAlgebra/CSparse/Include/ /home/gael/Coding/LinearAlgebra/CSparse/Lib/libcsparse.a
  5. #ifndef SIZE
  6. #define SIZE 10000
  7. #endif
  8. #ifndef DENSITY
  9. #define DENSITY 0.01
  10. #endif
  11. #ifndef REPEAT
  12. #define REPEAT 1
  13. #endif
  14. #include "BenchSparseUtil.h"
  15. #ifndef MINDENSITY
  16. #define MINDENSITY 0.0004
  17. #endif
  18. #ifndef NBTRIES
  19. #define NBTRIES 10
  20. #endif
  21. #define BENCH(X) \
  22. timer.reset(); \
  23. for (int _j=0; _j<NBTRIES; ++_j) { \
  24. timer.start(); \
  25. for (int _k=0; _k<REPEAT; ++_k) { \
  26. X \
  27. } timer.stop(); }
  28. typedef SparseMatrix<Scalar,UpperTriangular> EigenSparseTriMatrix;
  29. typedef SparseMatrix<Scalar,RowMajorBit|UpperTriangular> EigenSparseTriMatrixRow;
  30. void fillMatrix(float density, int rows, int cols, EigenSparseTriMatrix& dst)
  31. {
  32. dst.startFill(rows*cols*density);
  33. for(int j = 0; j < cols; j++)
  34. {
  35. for(int i = 0; i < j; i++)
  36. {
  37. Scalar v = (internal::random<float>(0,1) < density) ? internal::random<Scalar>() : 0;
  38. if (v!=0)
  39. dst.fill(i,j) = v;
  40. }
  41. dst.fill(j,j) = internal::random<Scalar>();
  42. }
  43. dst.endFill();
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. int rows = SIZE;
  48. int cols = SIZE;
  49. float density = DENSITY;
  50. BenchTimer timer;
  51. #if 1
  52. EigenSparseTriMatrix sm1(rows,cols);
  53. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  54. DenseVector b = DenseVector::Random(cols);
  55. DenseVector x = DenseVector::Random(cols);
  56. bool densedone = false;
  57. for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
  58. {
  59. EigenSparseTriMatrix sm1(rows, cols);
  60. fillMatrix(density, rows, cols, sm1);
  61. // dense matrices
  62. #ifdef DENSEMATRIX
  63. if (!densedone)
  64. {
  65. densedone = true;
  66. std::cout << "Eigen Dense\t" << density*100 << "%\n";
  67. DenseMatrix m1(rows,cols);
  68. Matrix<Scalar,Dynamic,Dynamic,Dynamic,Dynamic,RowMajorBit> m2(rows,cols);
  69. eiToDense(sm1, m1);
  70. m2 = m1;
  71. BENCH(x = m1.marked<UpperTriangular>().solveTriangular(b);)
  72. std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
  73. // std::cerr << x.transpose() << "\n";
  74. BENCH(x = m2.marked<UpperTriangular>().solveTriangular(b);)
  75. std::cout << " rowmajor^-1 * b:\t" << timer.value() << endl;
  76. // std::cerr << x.transpose() << "\n";
  77. }
  78. #endif
  79. // eigen sparse matrices
  80. {
  81. std::cout << "Eigen sparse\t" << density*100 << "%\n";
  82. EigenSparseTriMatrixRow sm2 = sm1;
  83. BENCH(x = sm1.solveTriangular(b);)
  84. std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
  85. // std::cerr << x.transpose() << "\n";
  86. BENCH(x = sm2.solveTriangular(b);)
  87. std::cout << " rowmajor^-1 * b:\t" << timer.value() << endl;
  88. // std::cerr << x.transpose() << "\n";
  89. // x = b;
  90. // BENCH(sm1.inverseProductInPlace(x);)
  91. // std::cout << " colmajor^-1 * b:\t" << timer.value() << " (inplace)" << endl;
  92. // std::cerr << x.transpose() << "\n";
  93. //
  94. // x = b;
  95. // BENCH(sm2.inverseProductInPlace(x);)
  96. // std::cout << " rowmajor^-1 * b:\t" << timer.value() << " (inplace)" << endl;
  97. // std::cerr << x.transpose() << "\n";
  98. }
  99. // CSparse
  100. #ifdef CSPARSE
  101. {
  102. std::cout << "CSparse \t" << density*100 << "%\n";
  103. cs *m1;
  104. eiToCSparse(sm1, m1);
  105. BENCH(x = b; if (!cs_lsolve (m1, x.data())){std::cerr << "cs_lsolve failed\n"; break;}; )
  106. std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
  107. }
  108. #endif
  109. // GMM++
  110. #ifndef NOGMM
  111. {
  112. std::cout << "GMM++ sparse\t" << density*100 << "%\n";
  113. GmmSparse m1(rows,cols);
  114. gmm::csr_matrix<Scalar> m2;
  115. eiToGmm(sm1, m1);
  116. gmm::copy(m1,m2);
  117. std::vector<Scalar> gmmX(cols), gmmB(cols);
  118. Map<Matrix<Scalar,Dynamic,1> >(&gmmX[0], cols) = x;
  119. Map<Matrix<Scalar,Dynamic,1> >(&gmmB[0], cols) = b;
  120. gmmX = gmmB;
  121. BENCH(gmm::upper_tri_solve(m1, gmmX, false);)
  122. std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
  123. // std::cerr << Map<Matrix<Scalar,Dynamic,1> >(&gmmX[0], cols).transpose() << "\n";
  124. gmmX = gmmB;
  125. BENCH(gmm::upper_tri_solve(m2, gmmX, false);)
  126. timer.stop();
  127. std::cout << " rowmajor^-1 * b:\t" << timer.value() << endl;
  128. // std::cerr << Map<Matrix<Scalar,Dynamic,1> >(&gmmX[0], cols).transpose() << "\n";
  129. }
  130. #endif
  131. // MTL4
  132. #ifndef NOMTL
  133. {
  134. std::cout << "MTL4\t" << density*100 << "%\n";
  135. MtlSparse m1(rows,cols);
  136. MtlSparseRowMajor m2(rows,cols);
  137. eiToMtl(sm1, m1);
  138. m2 = m1;
  139. mtl::dense_vector<Scalar> x(rows, 1.0);
  140. mtl::dense_vector<Scalar> b(rows, 1.0);
  141. BENCH(x = mtl::upper_trisolve(m1,b);)
  142. std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
  143. // std::cerr << x << "\n";
  144. BENCH(x = mtl::upper_trisolve(m2,b);)
  145. std::cout << " rowmajor^-1 * b:\t" << timer.value() << endl;
  146. // std::cerr << x << "\n";
  147. }
  148. #endif
  149. std::cout << "\n\n";
  150. }
  151. #endif
  152. #if 0
  153. // bench small matrices (in-place versus return bye value)
  154. {
  155. timer.reset();
  156. for (int _j=0; _j<10; ++_j) {
  157. Matrix4f m = Matrix4f::Random();
  158. Vector4f b = Vector4f::Random();
  159. Vector4f x = Vector4f::Random();
  160. timer.start();
  161. for (int _k=0; _k<1000000; ++_k) {
  162. b = m.inverseProduct(b);
  163. }
  164. timer.stop();
  165. }
  166. std::cout << "4x4 :\t" << timer.value() << endl;
  167. }
  168. {
  169. timer.reset();
  170. for (int _j=0; _j<10; ++_j) {
  171. Matrix4f m = Matrix4f::Random();
  172. Vector4f b = Vector4f::Random();
  173. Vector4f x = Vector4f::Random();
  174. timer.start();
  175. for (int _k=0; _k<1000000; ++_k) {
  176. m.inverseProductInPlace(x);
  177. }
  178. timer.stop();
  179. }
  180. std::cout << "4x4 IP :\t" << timer.value() << endl;
  181. }
  182. #endif
  183. return 0;
  184. }