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.

233 lines
6.0 KiB

  1. //g++-4.4 -DNOMTL -Wl,-rpath /usr/local/lib/oski -L /usr/local/lib/oski/ -l oski -l oski_util -l oski_util_Tid -DOSKI -I ~/Coding/LinearAlgebra/mtl4/ spmv.cpp -I .. -O2 -DNDEBUG -lrt -lm -l oski_mat_CSC_Tid -loskilt && ./a.out r200000 c200000 n100 t1 p1
  2. #define SCALAR double
  3. #include <iostream>
  4. #include <algorithm>
  5. #include "BenchTimer.h"
  6. #include "BenchSparseUtil.h"
  7. #define SPMV_BENCH(CODE) BENCH(t,tries,repeats,CODE);
  8. // #ifdef MKL
  9. //
  10. // #include "mkl_types.h"
  11. // #include "mkl_spblas.h"
  12. //
  13. // template<typename Lhs,typename Rhs,typename Res>
  14. // void mkl_multiply(const Lhs& lhs, const Rhs& rhs, Res& res)
  15. // {
  16. // char n = 'N';
  17. // float alpha = 1;
  18. // char matdescra[6];
  19. // matdescra[0] = 'G';
  20. // matdescra[1] = 0;
  21. // matdescra[2] = 0;
  22. // matdescra[3] = 'C';
  23. // mkl_scscmm(&n, lhs.rows(), rhs.cols(), lhs.cols(), &alpha, matdescra,
  24. // lhs._valuePtr(), lhs._innerIndexPtr(), lhs.outerIndexPtr(),
  25. // pntre, b, &ldb, &beta, c, &ldc);
  26. // // mkl_somatcopy('C', 'T', lhs.rows(), lhs.cols(), 1,
  27. // // lhs._valuePtr(), lhs.rows(), DST, dst_stride);
  28. // }
  29. //
  30. // #endif
  31. int main(int argc, char *argv[])
  32. {
  33. int size = 10000;
  34. int rows = size;
  35. int cols = size;
  36. int nnzPerCol = 40;
  37. int tries = 2;
  38. int repeats = 2;
  39. bool need_help = false;
  40. for(int i = 1; i < argc; i++)
  41. {
  42. if(argv[i][0] == 'r')
  43. {
  44. rows = atoi(argv[i]+1);
  45. }
  46. else if(argv[i][0] == 'c')
  47. {
  48. cols = atoi(argv[i]+1);
  49. }
  50. else if(argv[i][0] == 'n')
  51. {
  52. nnzPerCol = atoi(argv[i]+1);
  53. }
  54. else if(argv[i][0] == 't')
  55. {
  56. tries = atoi(argv[i]+1);
  57. }
  58. else if(argv[i][0] == 'p')
  59. {
  60. repeats = atoi(argv[i]+1);
  61. }
  62. else
  63. {
  64. need_help = true;
  65. }
  66. }
  67. if(need_help)
  68. {
  69. std::cout << argv[0] << " r<nb rows> c<nb columns> n<non zeros per column> t<nb tries> p<nb repeats>\n";
  70. return 1;
  71. }
  72. std::cout << "SpMV " << rows << " x " << cols << " with " << nnzPerCol << " non zeros per column. (" << repeats << " repeats, and " << tries << " tries)\n\n";
  73. EigenSparseMatrix sm(rows,cols);
  74. DenseVector dv(cols), res(rows);
  75. dv.setRandom();
  76. BenchTimer t;
  77. while (nnzPerCol>=4)
  78. {
  79. std::cout << "nnz: " << nnzPerCol << "\n";
  80. sm.setZero();
  81. fillMatrix2(nnzPerCol, rows, cols, sm);
  82. // dense matrices
  83. #ifdef DENSEMATRIX
  84. {
  85. DenseMatrix dm(rows,cols), (rows,cols);
  86. eiToDense(sm, dm);
  87. SPMV_BENCH(res = dm * sm);
  88. std::cout << "Dense " << t.value()/repeats << "\t";
  89. SPMV_BENCH(res = dm.transpose() * sm);
  90. std::cout << t.value()/repeats << endl;
  91. }
  92. #endif
  93. // eigen sparse matrices
  94. {
  95. SPMV_BENCH(res.noalias() += sm * dv; )
  96. std::cout << "Eigen " << t.value()/repeats << "\t";
  97. SPMV_BENCH(res.noalias() += sm.transpose() * dv; )
  98. std::cout << t.value()/repeats << endl;
  99. }
  100. // CSparse
  101. #ifdef CSPARSE
  102. {
  103. std::cout << "CSparse \n";
  104. cs *csm;
  105. eiToCSparse(sm, csm);
  106. // BENCH();
  107. // timer.stop();
  108. // std::cout << " a * b:\t" << timer.value() << endl;
  109. // BENCH( { m3 = cs_sorted_multiply2(m1, m2); cs_spfree(m3); } );
  110. // std::cout << " a * b:\t" << timer.value() << endl;
  111. }
  112. #endif
  113. #ifdef OSKI
  114. {
  115. oski_matrix_t om;
  116. oski_vecview_t ov, ores;
  117. oski_Init();
  118. om = oski_CreateMatCSC(sm._outerIndexPtr(), sm._innerIndexPtr(), sm._valuePtr(), rows, cols,
  119. SHARE_INPUTMAT, 1, INDEX_ZERO_BASED);
  120. ov = oski_CreateVecView(dv.data(), cols, STRIDE_UNIT);
  121. ores = oski_CreateVecView(res.data(), rows, STRIDE_UNIT);
  122. SPMV_BENCH( oski_MatMult(om, OP_NORMAL, 1, ov, 0, ores) );
  123. std::cout << "OSKI " << t.value()/repeats << "\t";
  124. SPMV_BENCH( oski_MatMult(om, OP_TRANS, 1, ov, 0, ores) );
  125. std::cout << t.value()/repeats << "\n";
  126. // tune
  127. t.reset();
  128. t.start();
  129. oski_SetHintMatMult(om, OP_NORMAL, 1.0, SYMBOLIC_VEC, 0.0, SYMBOLIC_VEC, ALWAYS_TUNE_AGGRESSIVELY);
  130. oski_TuneMat(om);
  131. t.stop();
  132. double tuning = t.value();
  133. SPMV_BENCH( oski_MatMult(om, OP_NORMAL, 1, ov, 0, ores) );
  134. std::cout << "OSKI tuned " << t.value()/repeats << "\t";
  135. SPMV_BENCH( oski_MatMult(om, OP_TRANS, 1, ov, 0, ores) );
  136. std::cout << t.value()/repeats << "\t(" << tuning << ")\n";
  137. oski_DestroyMat(om);
  138. oski_DestroyVecView(ov);
  139. oski_DestroyVecView(ores);
  140. oski_Close();
  141. }
  142. #endif
  143. #ifndef NOUBLAS
  144. {
  145. using namespace boost::numeric;
  146. UblasMatrix um(rows,cols);
  147. eiToUblas(sm, um);
  148. boost::numeric::ublas::vector<Scalar> uv(cols), ures(rows);
  149. Map<Matrix<Scalar,Dynamic,1> >(&uv[0], cols) = dv;
  150. Map<Matrix<Scalar,Dynamic,1> >(&ures[0], rows) = res;
  151. SPMV_BENCH(ublas::axpy_prod(um, uv, ures, true));
  152. std::cout << "ublas " << t.value()/repeats << "\t";
  153. SPMV_BENCH(ublas::axpy_prod(boost::numeric::ublas::trans(um), uv, ures, true));
  154. std::cout << t.value()/repeats << endl;
  155. }
  156. #endif
  157. // GMM++
  158. #ifndef NOGMM
  159. {
  160. GmmSparse gm(rows,cols);
  161. eiToGmm(sm, gm);
  162. std::vector<Scalar> gv(cols), gres(rows);
  163. Map<Matrix<Scalar,Dynamic,1> >(&gv[0], cols) = dv;
  164. Map<Matrix<Scalar,Dynamic,1> >(&gres[0], rows) = res;
  165. SPMV_BENCH(gmm::mult(gm, gv, gres));
  166. std::cout << "GMM++ " << t.value()/repeats << "\t";
  167. SPMV_BENCH(gmm::mult(gmm::transposed(gm), gv, gres));
  168. std::cout << t.value()/repeats << endl;
  169. }
  170. #endif
  171. // MTL4
  172. #ifndef NOMTL
  173. {
  174. MtlSparse mm(rows,cols);
  175. eiToMtl(sm, mm);
  176. mtl::dense_vector<Scalar> mv(cols, 1.0);
  177. mtl::dense_vector<Scalar> mres(rows, 1.0);
  178. SPMV_BENCH(mres = mm * mv);
  179. std::cout << "MTL4 " << t.value()/repeats << "\t";
  180. SPMV_BENCH(mres = trans(mm) * mv);
  181. std::cout << t.value()/repeats << endl;
  182. }
  183. #endif
  184. std::cout << "\n";
  185. if(nnzPerCol==1)
  186. break;
  187. nnzPerCol -= nnzPerCol/2;
  188. }
  189. return 0;
  190. }