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.

187 lines
5.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 -DCSPARSE
  4. // -I /home/gael/Coding/LinearAlgebra/CSparse/Include/ /home/gael/Coding/LinearAlgebra/CSparse/Lib/libcsparse.a
  5. #ifndef SIZE
  6. #define SIZE 650000
  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. #ifdef CSPARSE
  29. cs* cs_sorted_multiply(const cs* a, const cs* b)
  30. {
  31. cs* A = cs_transpose (a, 1) ;
  32. cs* B = cs_transpose (b, 1) ;
  33. cs* D = cs_multiply (B,A) ; /* D = B'*A' */
  34. cs_spfree (A) ;
  35. cs_spfree (B) ;
  36. cs_dropzeros (D) ; /* drop zeros from D */
  37. cs* C = cs_transpose (D, 1) ; /* C = D', so that C is sorted */
  38. cs_spfree (D) ;
  39. return C;
  40. }
  41. #endif
  42. int main(int argc, char *argv[])
  43. {
  44. int rows = SIZE;
  45. int cols = SIZE;
  46. float density = DENSITY;
  47. EigenSparseMatrix sm1(rows,cols);
  48. DenseVector v1(cols), v2(cols);
  49. v1.setRandom();
  50. BenchTimer timer;
  51. for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
  52. {
  53. //fillMatrix(density, rows, cols, sm1);
  54. fillMatrix2(7, rows, cols, sm1);
  55. // dense matrices
  56. #ifdef DENSEMATRIX
  57. {
  58. std::cout << "Eigen Dense\t" << density*100 << "%\n";
  59. DenseMatrix m1(rows,cols);
  60. eiToDense(sm1, m1);
  61. timer.reset();
  62. timer.start();
  63. for (int k=0; k<REPEAT; ++k)
  64. v2 = m1 * v1;
  65. timer.stop();
  66. std::cout << " a * v:\t" << timer.best() << " " << double(REPEAT)/timer.best() << " * / sec " << endl;
  67. timer.reset();
  68. timer.start();
  69. for (int k=0; k<REPEAT; ++k)
  70. v2 = m1.transpose() * v1;
  71. timer.stop();
  72. std::cout << " a' * v:\t" << timer.best() << endl;
  73. }
  74. #endif
  75. // eigen sparse matrices
  76. {
  77. std::cout << "Eigen sparse\t" << sm1.nonZeros()/float(sm1.rows()*sm1.cols())*100 << "%\n";
  78. BENCH(asm("#myc"); v2 = sm1 * v1; asm("#myd");)
  79. std::cout << " a * v:\t" << timer.best()/REPEAT << " " << double(REPEAT)/timer.best(REAL_TIMER) << " * / sec " << endl;
  80. BENCH( { asm("#mya"); v2 = sm1.transpose() * v1; asm("#myb"); })
  81. std::cout << " a' * v:\t" << timer.best()/REPEAT << endl;
  82. }
  83. // {
  84. // DynamicSparseMatrix<Scalar> m1(sm1);
  85. // std::cout << "Eigen dyn-sparse\t" << m1.nonZeros()/float(m1.rows()*m1.cols())*100 << "%\n";
  86. //
  87. // BENCH(for (int k=0; k<REPEAT; ++k) v2 = m1 * v1;)
  88. // std::cout << " a * v:\t" << timer.value() << endl;
  89. //
  90. // BENCH(for (int k=0; k<REPEAT; ++k) v2 = m1.transpose() * v1;)
  91. // std::cout << " a' * v:\t" << timer.value() << endl;
  92. // }
  93. // GMM++
  94. #ifndef NOGMM
  95. {
  96. std::cout << "GMM++ sparse\t" << density*100 << "%\n";
  97. //GmmDynSparse gmmT3(rows,cols);
  98. GmmSparse m1(rows,cols);
  99. eiToGmm(sm1, m1);
  100. std::vector<Scalar> gmmV1(cols), gmmV2(cols);
  101. Map<Matrix<Scalar,Dynamic,1> >(&gmmV1[0], cols) = v1;
  102. Map<Matrix<Scalar,Dynamic,1> >(&gmmV2[0], cols) = v2;
  103. BENCH( asm("#myx"); gmm::mult(m1, gmmV1, gmmV2); asm("#myy"); )
  104. std::cout << " a * v:\t" << timer.value() << endl;
  105. BENCH( gmm::mult(gmm::transposed(m1), gmmV1, gmmV2); )
  106. std::cout << " a' * v:\t" << timer.value() << endl;
  107. }
  108. #endif
  109. #ifndef NOUBLAS
  110. {
  111. std::cout << "ublas sparse\t" << density*100 << "%\n";
  112. UBlasSparse m1(rows,cols);
  113. eiToUblas(sm1, m1);
  114. boost::numeric::ublas::vector<Scalar> uv1, uv2;
  115. eiToUblasVec(v1,uv1);
  116. eiToUblasVec(v2,uv2);
  117. // std::vector<Scalar> gmmV1(cols), gmmV2(cols);
  118. // Map<Matrix<Scalar,Dynamic,1> >(&gmmV1[0], cols) = v1;
  119. // Map<Matrix<Scalar,Dynamic,1> >(&gmmV2[0], cols) = v2;
  120. BENCH( uv2 = boost::numeric::ublas::prod(m1, uv1); )
  121. std::cout << " a * v:\t" << timer.value() << endl;
  122. // BENCH( boost::ublas::prod(gmm::transposed(m1), gmmV1, gmmV2); )
  123. // std::cout << " a' * v:\t" << timer.value() << endl;
  124. }
  125. #endif
  126. // MTL4
  127. #ifndef NOMTL
  128. {
  129. std::cout << "MTL4\t" << density*100 << "%\n";
  130. MtlSparse m1(rows,cols);
  131. eiToMtl(sm1, m1);
  132. mtl::dense_vector<Scalar> mtlV1(cols, 1.0);
  133. mtl::dense_vector<Scalar> mtlV2(cols, 1.0);
  134. timer.reset();
  135. timer.start();
  136. for (int k=0; k<REPEAT; ++k)
  137. mtlV2 = m1 * mtlV1;
  138. timer.stop();
  139. std::cout << " a * v:\t" << timer.value() << endl;
  140. timer.reset();
  141. timer.start();
  142. for (int k=0; k<REPEAT; ++k)
  143. mtlV2 = trans(m1) * mtlV1;
  144. timer.stop();
  145. std::cout << " a' * v:\t" << timer.value() << endl;
  146. }
  147. #endif
  148. std::cout << "\n\n";
  149. }
  150. return 0;
  151. }