2 //g++ -O3 -g0 -DNDEBUG sparse_product.cpp -I.. -I/home/gael/Coding/LinearAlgebra/mtl4/ -DDENSITY=0.005 -DSIZE=10000 && ./a.out
3 //g++ -O3 -g0 -DNDEBUG sparse_product.cpp -I.. -I/home/gael/Coding/LinearAlgebra/mtl4/ -DDENSITY=0.05 -DSIZE=2000 && ./a.out
4 // -DNOGMM -DNOMTL -DCSPARSE
5 // -I /home/gael/Coding/LinearAlgebra/CSparse/Include/ /home/gael/Coding/LinearAlgebra/CSparse/Lib/libcsparse.a
18 #include "BenchSparseUtil.h"
21 #define MINDENSITY 0.0004
30 for (int _j=0; _j<NBTRIES; ++_j) { \
32 for (int _k=0; _k<REPEAT; ++_k) { \
38 cs
* cs_sorted_multiply(const cs
* a
, const cs
* b
)
40 cs
* A
= cs_transpose (a
, 1) ;
41 cs
* B
= cs_transpose (b
, 1) ;
42 cs
* D
= cs_multiply (B
,A
) ; /* D = B'*A' */
45 cs_dropzeros (D
) ; /* drop zeros from D */
46 cs
* C
= cs_transpose (D
, 1) ; /* C = D', so that C is sorted */
52 int main(int argc
, char *argv
[])
56 float density
= DENSITY
;
58 EigenSparseMatrix
sm1(rows
,cols
);
59 DenseVector
v1(cols
), v2(cols
);
63 for (float density
= DENSITY
; density
>=MINDENSITY
; density
*=0.5)
65 //fillMatrix(density, rows, cols, sm1);
66 fillMatrix2(7, rows
, cols
, sm1
);
71 std::cout
<< "Eigen Dense\t" << density
*100 << "%\n";
72 DenseMatrix
m1(rows
,cols
);
77 for (int k
=0; k
<REPEAT
; ++k
)
80 std::cout
<< " a * v:\t" << timer
.best() << " " << double(REPEAT
)/timer
.best() << " * / sec " << endl
;
84 for (int k
=0; k
<REPEAT
; ++k
)
85 v2
= m1
.transpose() * v1
;
87 std::cout
<< " a' * v:\t" << timer
.best() << endl
;
91 // eigen sparse matrices
93 std::cout
<< "Eigen sparse\t" << sm1
.nonZeros()/float(sm1
.rows()*sm1
.cols())*100 << "%\n";
95 BENCH(asm("#myc"); v2
= sm1
* v1
; asm("#myd");)
96 std::cout
<< " a * v:\t" << timer
.best()/REPEAT
<< " " << double(REPEAT
)/timer
.best(REAL_TIMER
) << " * / sec " << endl
;
99 BENCH( { asm("#mya"); v2
= sm1
.transpose() * v1
; asm("#myb"); })
101 std::cout
<< " a' * v:\t" << timer
.best()/REPEAT
<< endl
;
105 // DynamicSparseMatrix<Scalar> m1(sm1);
106 // std::cout << "Eigen dyn-sparse\t" << m1.nonZeros()/float(m1.rows()*m1.cols())*100 << "%\n";
108 // BENCH(for (int k=0; k<REPEAT; ++k) v2 = m1 * v1;)
109 // std::cout << " a * v:\t" << timer.value() << endl;
111 // BENCH(for (int k=0; k<REPEAT; ++k) v2 = m1.transpose() * v1;)
112 // std::cout << " a' * v:\t" << timer.value() << endl;
118 std::cout
<< "GMM++ sparse\t" << density
*100 << "%\n";
119 //GmmDynSparse gmmT3(rows,cols);
120 GmmSparse
m1(rows
,cols
);
123 std::vector
<Scalar
> gmmV1(cols
), gmmV2(cols
);
124 Map
<Matrix
<Scalar
,Dynamic
,1> >(&gmmV1
[0], cols
) = v1
;
125 Map
<Matrix
<Scalar
,Dynamic
,1> >(&gmmV2
[0], cols
) = v2
;
127 BENCH( asm("#myx"); gmm::mult(m1
, gmmV1
, gmmV2
); asm("#myy"); )
128 std::cout
<< " a * v:\t" << timer
.value() << endl
;
130 BENCH( gmm::mult(gmm::transposed(m1
), gmmV1
, gmmV2
); )
131 std::cout
<< " a' * v:\t" << timer
.value() << endl
;
137 std::cout
<< "ublas sparse\t" << density
*100 << "%\n";
138 UBlasSparse
m1(rows
,cols
);
141 boost::numeric::ublas::vector
<Scalar
> uv1
, uv2
;
142 eiToUblasVec(v1
,uv1
);
143 eiToUblasVec(v2
,uv2
);
145 // std::vector<Scalar> gmmV1(cols), gmmV2(cols);
146 // Map<Matrix<Scalar,Dynamic,1> >(&gmmV1[0], cols) = v1;
147 // Map<Matrix<Scalar,Dynamic,1> >(&gmmV2[0], cols) = v2;
149 BENCH( uv2
= boost::numeric::ublas::prod(m1
, uv1
); )
150 std::cout
<< " a * v:\t" << timer
.value() << endl
;
152 // BENCH( boost::ublas::prod(gmm::transposed(m1), gmmV1, gmmV2); )
153 // std::cout << " a' * v:\t" << timer.value() << endl;
160 std::cout
<< "MTL4\t" << density
*100 << "%\n";
161 MtlSparse
m1(rows
,cols
);
163 mtl::dense_vector
<Scalar
> mtlV1(cols
, 1.0);
164 mtl::dense_vector
<Scalar
> mtlV2(cols
, 1.0);
168 for (int k
=0; k
<REPEAT
; ++k
)
171 std::cout
<< " a * v:\t" << timer
.value() << endl
;
175 for (int k
=0; k
<REPEAT
; ++k
)
176 mtlV2
= trans(m1
) * mtlV1
;
178 std::cout
<< " a' * v:\t" << timer
.value() << endl
;