sparse_dense_product.cpp
Go to the documentation of this file.
1 
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
6 #ifndef SIZE
7 #define SIZE 650000
8 #endif
9 
10 #ifndef DENSITY
11 #define DENSITY 0.01
12 #endif
13 
14 #ifndef REPEAT
15 #define REPEAT 1
16 #endif
17 
18 #include "BenchSparseUtil.h"
19 
20 #ifndef MINDENSITY
21 #define MINDENSITY 0.0004
22 #endif
23 
24 #ifndef NBTRIES
25 #define NBTRIES 10
26 #endif
27 
28 #define BENCH(X) \
29  timer.reset(); \
30  for (int _j=0; _j<NBTRIES; ++_j) { \
31  timer.start(); \
32  for (int _k=0; _k<REPEAT; ++_k) { \
33  X \
34  } timer.stop(); }
35 
36 
37 #ifdef CSPARSE
38 cs* cs_sorted_multiply(const cs* a, const cs* b)
39 {
40  cs* A = cs_transpose (a, 1) ;
41  cs* B = cs_transpose (b, 1) ;
42  cs* D = cs_multiply (B,A) ; /* D = B'*A' */
43  cs_spfree (A) ;
44  cs_spfree (B) ;
45  cs_dropzeros (D) ; /* drop zeros from D */
46  cs* C = cs_transpose (D, 1) ; /* C = D', so that C is sorted */
47  cs_spfree (D) ;
48  return C;
49 }
50 #endif
51 
52 int main(int argc, char *argv[])
53 {
54  int rows = SIZE;
55  int cols = SIZE;
56  float density = DENSITY;
57 
60  v1.setRandom();
61 
63  for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
64  {
65  //fillMatrix(density, rows, cols, sm1);
66  fillMatrix2(7, rows, cols, sm1);
67 
68  // dense matrices
69  #ifdef DENSEMATRIX
70  {
71  std::cout << "Eigen Dense\t" << density*100 << "%\n";
73  eiToDense(sm1, m1);
74 
75  timer.reset();
76  timer.start();
77  for (int k=0; k<REPEAT; ++k)
78  v2 = m1 * v1;
79  timer.stop();
80  std::cout << " a * v:\t" << timer.best() << " " << double(REPEAT)/timer.best() << " * / sec " << endl;
81 
82  timer.reset();
83  timer.start();
84  for (int k=0; k<REPEAT; ++k)
85  v2 = m1.transpose() * v1;
86  timer.stop();
87  std::cout << " a' * v:\t" << timer.best() << endl;
88  }
89  #endif
90 
91  // eigen sparse matrices
92  {
93  std::cout << "Eigen sparse\t" << sm1.nonZeros()/float(sm1.rows()*sm1.cols())*100 << "%\n";
94 
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;
97 
98 
99  BENCH( { asm("#mya"); v2 = sm1.transpose() * v1; asm("#myb"); })
100 
101  std::cout << " a' * v:\t" << timer.best()/REPEAT << endl;
102  }
103 
104 // {
105 // DynamicSparseMatrix<Scalar> m1(sm1);
106 // std::cout << "Eigen dyn-sparse\t" << m1.nonZeros()/float(m1.rows()*m1.cols())*100 << "%\n";
107 //
108 // BENCH(for (int k=0; k<REPEAT; ++k) v2 = m1 * v1;)
109 // std::cout << " a * v:\t" << timer.value() << endl;
110 //
111 // BENCH(for (int k=0; k<REPEAT; ++k) v2 = m1.transpose() * v1;)
112 // std::cout << " a' * v:\t" << timer.value() << endl;
113 // }
114 
115  // GMM++
116  #ifndef NOGMM
117  {
118  std::cout << "GMM++ sparse\t" << density*100 << "%\n";
119  //GmmDynSparse gmmT3(rows,cols);
121  eiToGmm(sm1, m1);
122 
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;
126 
127  BENCH( asm("#myx"); gmm::mult(m1, gmmV1, gmmV2); asm("#myy"); )
128  std::cout << " a * v:\t" << timer.value() << endl;
129 
130  BENCH( gmm::mult(gmm::transposed(m1), gmmV1, gmmV2); )
131  std::cout << " a' * v:\t" << timer.value() << endl;
132  }
133  #endif
134 
135  #ifndef NOUBLAS
136  {
137  std::cout << "ublas sparse\t" << density*100 << "%\n";
139  eiToUblas(sm1, m1);
140 
141  boost::numeric::ublas::vector<Scalar> uv1, uv2;
142  eiToUblasVec(v1,uv1);
143  eiToUblasVec(v2,uv2);
144 
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;
148 
149  BENCH( uv2 = boost::numeric::ublas::prod(m1, uv1); )
150  std::cout << " a * v:\t" << timer.value() << endl;
151 
152 // BENCH( boost::ublas::prod(gmm::transposed(m1), gmmV1, gmmV2); )
153 // std::cout << " a' * v:\t" << timer.value() << endl;
154  }
155  #endif
156 
157  // MTL4
158  #ifndef NOMTL
159  {
160  std::cout << "MTL4\t" << density*100 << "%\n";
162  eiToMtl(sm1, m1);
163  mtl::dense_vector<Scalar> mtlV1(cols, 1.0);
164  mtl::dense_vector<Scalar> mtlV2(cols, 1.0);
165 
166  timer.reset();
167  timer.start();
168  for (int k=0; k<REPEAT; ++k)
169  mtlV2 = m1 * mtlV1;
170  timer.stop();
171  std::cout << " a * v:\t" << timer.value() << endl;
172 
173  timer.reset();
174  timer.start();
175  for (int k=0; k<REPEAT; ++k)
176  mtlV2 = trans(m1) * mtlV1;
177  timer.stop();
178  std::cout << " a' * v:\t" << timer.value() << endl;
179  }
180  #endif
181 
182  std::cout << "\n\n";
183  }
184 
185  return 0;
186 }
187 
Eigen::SparseMatrix::cols
Index cols() const
Definition: SparseMatrix.h:140
Eigen::SparseMatrix< Scalar >
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
MtlSparse
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::col_major > > MtlSparse
Definition: BenchSparseUtil.h:86
D
MatrixXcd D
Definition: EigenSolver_EigenSolver_MatrixType.cpp:14
eiToDense
void eiToDense(const EigenSparseMatrix &src, DenseMatrix &dst)
Definition: BenchSparseUtil.h:62
timer
static BenchTimer timer
Definition: benchmark-blocking-sizes.cpp:31
b
Scalar * b
Definition: benchVecAdd.cpp:17
trans
static char trans
Definition: blas_interface.hh:58
DENSITY
#define DENSITY
Definition: sparse_dense_product.cpp:11
m1
Matrix3d m1
Definition: IOFormat.cpp:2
eiToMtl
void eiToMtl(const EigenSparseMatrix &src, MtlSparse &dst)
Definition: BenchSparseUtil.h:88
Eigen::SparseMatrix::nonZeros
Index nonZeros() const
Definition: SparseCompressedBase.h:56
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
Eigen::BenchTimer::value
double value(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:104
SIZE
#define SIZE
Definition: sparse_dense_product.cpp:7
eiToUblasVec
void eiToUblasVec(const EigenType &src, UblasType &dst)
Definition: BenchSparseUtil.h:137
Eigen::BenchTimer::start
void start()
Definition: BenchTimer.h:81
main
int main(int argc, char *argv[])
Definition: sparse_dense_product.cpp:52
Eigen::BenchTimer
Definition: BenchTimer.h:59
density
Definition: testGaussianConditional.cpp:127
REPEAT
#define REPEAT
Definition: sparse_dense_product.cpp:15
eiToUblas
void eiToUblas(const EigenSparseMatrix &src, UBlasSparse &dst)
Definition: BenchSparseUtil.h:128
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Eigen::BenchTimer::reset
void reset()
Definition: BenchTimer.h:75
DenseVector
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition: BenchSparseUtil.h:24
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
C
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
Eigen::REAL_TIMER
@ REAL_TIMER
Definition: BenchTimer.h:49
v2
Vector v2
Definition: testSerializationBase.cpp:39
GmmSparse
gmm::csc_matrix< Scalar > GmmSparse
Definition: BenchSparseUtil.h:72
DenseMatrix
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: BenchSparseUtil.h:23
Eigen::BenchTimer::best
double best(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:111
Eigen::BenchTimer::stop
void stop()
Definition: BenchTimer.h:86
gtsam.examples.DogLegOptimizerExample.float
float
Definition: DogLegOptimizerExample.py:113
eiToGmm
void eiToGmm(const EigenSparseMatrix &src, GmmSparse &dst)
Definition: BenchSparseUtil.h:74
UBlasSparse
boost::numeric::ublas::compressed_matrix< Scalar, boost::numeric::ublas::column_major > UBlasSparse
Definition: BenchSparseUtil.h:126
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::SparseMatrix::rows
Index rows() const
Definition: SparseMatrix.h:138
prod
EIGEN_DONT_INLINE void prod(const Lhs &a, const Rhs &b, Res &c)
Definition: product_threshold.cpp:39
BENCH
#define BENCH(X)
Definition: sparse_dense_product.cpp:28
MINDENSITY
#define MINDENSITY
Definition: sparse_dense_product.cpp:21
BenchSparseUtil.h
v1
Vector v1
Definition: testSerializationBase.cpp:38
fillMatrix2
void fillMatrix2(int nnzPerCol, int rows, int cols, EigenSparseMatrix &dst)
Definition: BenchSparseUtil.h:42


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:24