aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/sparse_dense_product.cpp
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2009-01-14 18:27:17 +0000
committerGravatar Gael Guennebaud <g.gael@free.fr>2009-01-14 18:27:17 +0000
commitf5741d4277e6921bfc0b3a820d340eb15b6c66e7 (patch)
tree31203ee8671c0ae47333d389df83cb25e34817c9 /bench/sparse_dense_product.cpp
parent0b606dcccd58fef640f5037088005dcdd1d3487e (diff)
add a sparse * dense_vector bench
Diffstat (limited to 'bench/sparse_dense_product.cpp')
-rw-r--r--bench/sparse_dense_product.cpp198
1 files changed, 198 insertions, 0 deletions
diff --git a/bench/sparse_dense_product.cpp b/bench/sparse_dense_product.cpp
new file mode 100644
index 000000000..3caf8598a
--- /dev/null
+++ b/bench/sparse_dense_product.cpp
@@ -0,0 +1,198 @@
+
+//g++ -O3 -g0 -DNDEBUG sparse_product.cpp -I.. -I/home/gael/Coding/LinearAlgebra/mtl4/ -DDENSITY=0.005 -DSIZE=10000 && ./a.out
+//g++ -O3 -g0 -DNDEBUG sparse_product.cpp -I.. -I/home/gael/Coding/LinearAlgebra/mtl4/ -DDENSITY=0.05 -DSIZE=2000 && ./a.out
+// -DNOGMM -DNOMTL -DCSPARSE
+// -I /home/gael/Coding/LinearAlgebra/CSparse/Include/ /home/gael/Coding/LinearAlgebra/CSparse/Lib/libcsparse.a
+#ifndef SIZE
+#define SIZE 10000
+#endif
+
+#ifndef DENSITY
+#define DENSITY 0.01
+#endif
+
+#ifndef REPEAT
+#define REPEAT 1
+#endif
+
+#include "BenchSparseUtil.h"
+
+#ifndef MINDENSITY
+#define MINDENSITY 0.0004
+#endif
+
+#ifndef NBTRIES
+#define NBTRIES 10
+#endif
+
+#define BENCH(X) \
+ timer.reset(); \
+ for (int _j=0; _j<NBTRIES; ++_j) { \
+ timer.start(); \
+ for (int _k=0; _k<REPEAT; ++_k) { \
+ X \
+ } timer.stop(); }
+
+
+#ifdef CSPARSE
+cs* cs_sorted_multiply(const cs* a, const cs* b)
+{
+ cs* A = cs_transpose (a, 1) ;
+ cs* B = cs_transpose (b, 1) ;
+ cs* D = cs_multiply (B,A) ; /* D = B'*A' */
+ cs_spfree (A) ;
+ cs_spfree (B) ;
+ cs_dropzeros (D) ; /* drop zeros from D */
+ cs* C = cs_transpose (D, 1) ; /* C = D', so that C is sorted */
+ cs_spfree (D) ;
+ return C;
+}
+#endif
+
+int main(int argc, char *argv[])
+{
+ int rows = SIZE;
+ int cols = SIZE;
+ float density = DENSITY;
+
+ EigenSparseMatrix sm1(rows,cols);
+ DenseVector v1(cols), v2(cols);
+ v1.setRandom();
+
+ BenchTimer timer;
+ for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
+ {
+ fillMatrix(density, rows, cols, sm1);
+
+ // dense matrices
+ #ifdef DENSEMATRIX
+ {
+ std::cout << "Eigen Dense\t" << density*100 << "%\n";
+ DenseMatrix m1(rows,cols);
+ eiToDense(sm1, m1);
+
+ timer.reset();
+ timer.start();
+ for (int k=0; k<REPEAT; ++k)
+ v2 = m1 * v1;
+ timer.stop();
+ std::cout << " a * v:\t" << timer.value() << endl;
+
+ timer.reset();
+ timer.start();
+ for (int k=0; k<REPEAT; ++k)
+ v2 = m1.transpose() * v1;
+ timer.stop();
+ std::cout << " a' * v:\t" << timer.value() << endl;
+ }
+ #endif
+
+ // eigen sparse matrices
+ {
+ std::cout << "Eigen sparse\t" << sm1.nonZeros()/float(sm1.rows()*sm1.cols())*100 << "%\n";
+
+// timer.reset();
+// timer.start();
+ BENCH(for (int k=0; k<REPEAT; ++k) v2 = sm1 * v1;)
+// timer.stop();
+ std::cout << " a * v:\t" << timer.value() << endl;
+// std::cout << sm3 << "\n";
+
+ timer.reset();
+ timer.start();
+// std::cerr << "transpose...\n";
+// EigenSparseMatrix sm4 = sm1.transpose();
+// std::cout << sm4.nonZeros() << " == " << sm1.nonZeros() << "\n";
+// exit(1);
+// std::cerr << "transpose OK\n";
+// std::cout << sm1 << "\n\n" << sm1.transpose() << "\n\n" << sm4.transpose() << "\n\n";
+ BENCH(for (int k=0; k<REPEAT; ++k) v2 = sm1.transpose() * v1;)
+// timer.stop();
+ std::cout << " a' * v:\t" << timer.value() << endl;
+ }
+
+ // CSparse
+ #ifdef CSPARSE
+ {
+ std::cout << "CSparse \t" << density*100 << "%\n";
+ cs *m1, *m2, *m3;
+ eiToCSparse(sm1, m1);
+ eiToCSparse(sm2, m2);
+
+ timer.reset();
+ timer.start();
+ for (int k=0; k<REPEAT; ++k)
+ {
+ m3 = cs_sorted_multiply(m1, m2);
+ if (!m3)
+ {
+ std::cerr << "cs_multiply failed\n";
+// break;
+ }
+// cs_print(m3, 0);
+ cs_spfree(m3);
+ }
+ timer.stop();
+ std::cout << " a * b:\t" << timer.value() << endl;
+ }
+ #endif
+
+ // GMM++
+ #ifndef NOGMM
+ {
+ std::cout << "GMM++ sparse\t" << density*100 << "%\n";
+ //GmmDynSparse gmmT3(rows,cols);
+ GmmSparse m1(rows,cols);
+ eiToGmm(sm1, m1);
+
+ std::vector<Scalar> gmmV1(cols), gmmV2(cols);
+ Map<Matrix<Scalar,Dynamic,1> >(&gmmV1[0], cols) = v1;
+ Map<Matrix<Scalar,Dynamic,1> >(&gmmV2[0], cols) = v2;
+
+ timer.reset();
+ timer.start();
+ for (int k=0; k<REPEAT; ++k)
+ gmm::mult(m1, gmmV1, gmmV2);
+ timer.stop();
+ std::cout << " a * v:\t" << timer.value() << endl;
+
+ timer.reset();
+ timer.start();
+ for (int k=0; k<REPEAT; ++k)
+ gmm::mult(gmm::transposed(m1), gmmV1, gmmV2);
+ timer.stop();
+ std::cout << " a' * v:\t" << timer.value() << endl;
+ }
+ #endif
+
+ // MTL4
+ #ifndef NOMTL
+ {
+ std::cout << "MTL4\t" << density*100 << "%\n";
+ MtlSparse m1(rows,cols);
+ eiToMtl(sm1, m1);
+ mtl::dense_vector<Scalar> mtlV1(cols, 1.0);
+ mtl::dense_vector<Scalar> mtlV2(cols, 1.0);
+
+ timer.reset();
+ timer.start();
+ for (int k=0; k<REPEAT; ++k)
+ mtlV2 = m1 * mtlV1;
+ timer.stop();
+ std::cout << " a * v:\t" << timer.value() << endl;
+
+ timer.reset();
+ timer.start();
+ for (int k=0; k<REPEAT; ++k)
+ mtlV2 = trans(m1) * mtlV1;
+ timer.stop();
+ std::cout << " a' * v:\t" << timer.value() << endl;
+ }
+ #endif
+
+ std::cout << "\n\n";
+ }
+
+ return 0;
+}
+