2 //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
8 #include "BenchTimer.h"
9 #include "BenchSparseUtil.h"
11 #define SPMV_BENCH(CODE) BENCH(t,tries,repeats,CODE);
15 // #include "mkl_types.h"
16 // #include "mkl_spblas.h"
18 // template<typename Lhs,typename Rhs,typename Res>
19 // void mkl_multiply(const Lhs& lhs, const Rhs& rhs, Res& res)
24 // matdescra[0] = 'G';
27 // matdescra[3] = 'C';
28 // mkl_scscmm(&n, lhs.rows(), rhs.cols(), lhs.cols(), &alpha, matdescra,
29 // lhs._valuePtr(), lhs._innerIndexPtr(), lhs.outerIndexPtr(),
30 // pntre, b, &ldb, &beta, c, &ldc);
31 // // mkl_somatcopy('C', 'T', lhs.rows(), lhs.cols(), 1,
32 // // lhs._valuePtr(), lhs.rows(), DST, dst_stride);
37 int main(int argc
, char *argv
[])
46 bool need_help
= false;
47 for(int i
= 1; i
< argc
; i
++)
51 rows
= atoi(argv
[i
]+1);
53 else if(argv
[i
][0] == 'c')
55 cols
= atoi(argv
[i
]+1);
57 else if(argv
[i
][0] == 'n')
59 nnzPerCol
= atoi(argv
[i
]+1);
61 else if(argv
[i
][0] == 't')
63 tries
= atoi(argv
[i
]+1);
65 else if(argv
[i
][0] == 'p')
67 repeats
= atoi(argv
[i
]+1);
76 std::cout
<< argv
[0] << " r<nb rows> c<nb columns> n<non zeros per column> t<nb tries> p<nb repeats>\n";
80 std::cout
<< "SpMV " << rows
<< " x " << cols
<< " with " << nnzPerCol
<< " non zeros per column. (" << repeats
<< " repeats, and " << tries
<< " tries)\n\n";
82 EigenSparseMatrix
sm(rows
,cols
);
83 DenseVector
dv(cols
), res(rows
);
89 std::cout
<< "nnz: " << nnzPerCol
<< "\n";
91 fillMatrix2(nnzPerCol
, rows
, cols
, sm
);
96 DenseMatrix
dm(rows
,cols
), (rows
,cols
);
99 SPMV_BENCH(res
= dm
* sm
);
100 std::cout
<< "Dense " << t
.value()/repeats
<< "\t";
102 SPMV_BENCH(res
= dm
.transpose() * sm
);
103 std::cout
<< t
.value()/repeats
<< endl
;
107 // eigen sparse matrices
109 SPMV_BENCH(res
.noalias() += sm
* dv
; )
110 std::cout
<< "Eigen " << t
.value()/repeats
<< "\t";
112 SPMV_BENCH(res
.noalias() += sm
.transpose() * dv
; )
113 std::cout
<< t
.value()/repeats
<< endl
;
119 std::cout
<< "CSparse \n";
121 eiToCSparse(sm
, csm
);
125 // std::cout << " a * b:\t" << timer.value() << endl;
127 // BENCH( { m3 = cs_sorted_multiply2(m1, m2); cs_spfree(m3); } );
128 // std::cout << " a * b:\t" << timer.value() << endl;
135 oski_vecview_t ov
, ores
;
137 om
= oski_CreateMatCSC(sm
._outerIndexPtr(), sm
._innerIndexPtr(), sm
._valuePtr(), rows
, cols
,
138 SHARE_INPUTMAT
, 1, INDEX_ZERO_BASED
);
139 ov
= oski_CreateVecView(dv
.data(), cols
, STRIDE_UNIT
);
140 ores
= oski_CreateVecView(res
.data(), rows
, STRIDE_UNIT
);
142 SPMV_BENCH( oski_MatMult(om
, OP_NORMAL
, 1, ov
, 0, ores
) );
143 std::cout
<< "OSKI " << t
.value()/repeats
<< "\t";
145 SPMV_BENCH( oski_MatMult(om
, OP_TRANS
, 1, ov
, 0, ores
) );
146 std::cout
<< t
.value()/repeats
<< "\n";
151 oski_SetHintMatMult(om
, OP_NORMAL
, 1.0, SYMBOLIC_VEC
, 0.0, SYMBOLIC_VEC
, ALWAYS_TUNE_AGGRESSIVELY
);
154 double tuning
= t
.value();
156 SPMV_BENCH( oski_MatMult(om
, OP_NORMAL
, 1, ov
, 0, ores
) );
157 std::cout
<< "OSKI tuned " << t
.value()/repeats
<< "\t";
159 SPMV_BENCH( oski_MatMult(om
, OP_TRANS
, 1, ov
, 0, ores
) );
160 std::cout
<< t
.value()/repeats
<< "\t(" << tuning
<< ")\n";
164 oski_DestroyVecView(ov
);
165 oski_DestroyVecView(ores
);
172 using namespace boost::numeric
;
173 UblasMatrix
um(rows
,cols
);
176 boost::numeric::ublas::vector
<Scalar
> uv(cols
), ures(rows
);
177 Map
<Matrix
<Scalar
,Dynamic
,1> >(&uv
[0], cols
) = dv
;
178 Map
<Matrix
<Scalar
,Dynamic
,1> >(&ures
[0], rows
) = res
;
180 SPMV_BENCH(ublas::axpy_prod(um
, uv
, ures
, true));
181 std::cout
<< "ublas " << t
.value()/repeats
<< "\t";
183 SPMV_BENCH(ublas::axpy_prod(boost::numeric::ublas::trans(um
), uv
, ures
, true));
184 std::cout
<< t
.value()/repeats
<< endl
;
191 GmmSparse
gm(rows
,cols
);
194 std::vector
<Scalar
> gv(cols
), gres(rows
);
195 Map
<Matrix
<Scalar
,Dynamic
,1> >(&gv
[0], cols
) = dv
;
196 Map
<Matrix
<Scalar
,Dynamic
,1> >(&gres
[0], rows
) = res
;
198 SPMV_BENCH(gmm::mult(gm
, gv
, gres
));
199 std::cout
<< "GMM++ " << t
.value()/repeats
<< "\t";
201 SPMV_BENCH(gmm::mult(gmm::transposed(gm
), gv
, gres
));
202 std::cout
<< t
.value()/repeats
<< endl
;
209 MtlSparse
mm(rows
,cols
);
211 mtl::dense_vector
<Scalar
> mv(cols
, 1.0);
212 mtl::dense_vector
<Scalar
> mres(rows
, 1.0);
214 SPMV_BENCH(mres
= mm
* mv
);
215 std::cout
<< "MTL4 " << t
.value()/repeats
<< "\t";
217 SPMV_BENCH(mres
= trans(mm
) * mv
);
218 std::cout
<< t
.value()/repeats
<< endl
;
226 nnzPerCol
-= nnzPerCol
/2;