bench/sparse_product.cpp File Reference
#include <typeinfo>
#include <algorithm>
#include "BenchTimer.h"
#include "BenchUtil.h"
#include "BenchSparseUtil.h"

Macros

#define SIZE   1000000
 
#define NNZPERCOL   6
 
#define REPEAT   1
 
#define NBTRIES   1
 
#define BENCH(X)
 

Functions

void bench_sort ()
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ BENCH

#define BENCH (   X)
Value:
timer.reset(); \
for (int _j = 0; _j < NBTRIES; ++_j) { \
timer.start(); \
for (int _k = 0; _k < REPEAT; ++_k) { \
X \
} \
timer.stop(); \
}
#define REPEAT
Definition: bench/sparse_product.cpp:19
#define NBTRIES
Definition: bench/sparse_product.cpp:28

◆ NBTRIES

#define NBTRIES   1

◆ NNZPERCOL

#define NNZPERCOL   6

◆ REPEAT

#define REPEAT   1

◆ SIZE

#define SIZE   1000000

Function Documentation

◆ bench_sort()

void bench_sort ( )

◆ main()

int main ( int argc  ,
char argv[] 
)
96  {
97  // bench_sort();
98 
99  int rows = SIZE;
100  int cols = SIZE;
101  float density = DENSITY;
102 
103  EigenSparseMatrix sm1(rows, cols), sm2(rows, cols), sm3(rows, cols), sm4(rows, cols);
104 
106  for (int nnzPerCol = NNZPERCOL; nnzPerCol > 1; nnzPerCol /= 1.1) {
107  sm1.setZero();
108  sm2.setZero();
109  fillMatrix2(nnzPerCol, rows, cols, sm1);
110  fillMatrix2(nnzPerCol, rows, cols, sm2);
111  // std::cerr << "filling OK\n";
112 
113 // dense matrices
114 #ifdef DENSEMATRIX
115  {
116  std::cout << "Eigen Dense\t" << nnzPerCol << "%\n";
117  DenseMatrix m1(rows, cols), m2(rows, cols), m3(rows, cols);
118  eiToDense(sm1, m1);
119  eiToDense(sm2, m2);
120 
121  timer.reset();
122  timer.start();
123  for (int k = 0; k < REPEAT; ++k) m3 = m1 * m2;
124  timer.stop();
125  std::cout << " a * b:\t" << timer.value() << endl;
126 
127  timer.reset();
128  timer.start();
129  for (int k = 0; k < REPEAT; ++k) m3 = m1.transpose() * m2;
130  timer.stop();
131  std::cout << " a' * b:\t" << timer.value() << endl;
132 
133  timer.reset();
134  timer.start();
135  for (int k = 0; k < REPEAT; ++k) m3 = m1.transpose() * m2.transpose();
136  timer.stop();
137  std::cout << " a' * b':\t" << timer.value() << endl;
138 
139  timer.reset();
140  timer.start();
141  for (int k = 0; k < REPEAT; ++k) m3 = m1 * m2.transpose();
142  timer.stop();
143  std::cout << " a * b':\t" << timer.value() << endl;
144  }
145 #endif
146 
147  // eigen sparse matrices
148  {
149  std::cout << "Eigen sparse\t" << sm1.nonZeros() / (float(sm1.rows()) * float(sm1.cols())) * 100 << "% * "
150  << sm2.nonZeros() / (float(sm2.rows()) * float(sm2.cols())) * 100 << "%\n";
151 
152  BENCH(sm3 = sm1 * sm2;)
153  std::cout << " a * b:\t" << timer.value() << endl;
154 
155  // BENCH(sm3 = sm1.transpose() * sm2; )
156  // std::cout << " a' * b:\t" << timer.value() << endl;
157  // //
158  // BENCH(sm3 = sm1.transpose() * sm2.transpose(); )
159  // std::cout << " a' * b':\t" << timer.value() << endl;
160  // //
161  // BENCH(sm3 = sm1 * sm2.transpose(); )
162  // std::cout << " a * b' :\t" << timer.value() << endl;
163 
164  // std::cout << "\n";
165  //
166  // BENCH( sm3._experimentalNewProduct(sm1, sm2); )
167  // std::cout << " a * b:\t" << timer.value() << endl;
168  //
169  // BENCH(sm3._experimentalNewProduct(sm1.transpose(),sm2); )
170  // std::cout << " a' * b:\t" << timer.value() << endl;
171  // //
172  // BENCH(sm3._experimentalNewProduct(sm1.transpose(),sm2.transpose()); )
173  // std::cout << " a' * b':\t" << timer.value() << endl;
174  // //
175  // BENCH(sm3._experimentalNewProduct(sm1, sm2.transpose());)
176  // std::cout << " a * b' :\t" << timer.value() << endl;
177  }
178 
179 // eigen dyn-sparse matrices
180 /*{
181  DynamicSparseMatrix<Scalar> m1(sm1), m2(sm2), m3(sm3);
182  std::cout << "Eigen dyn-sparse\t" << m1.nonZeros()/(float(m1.rows())*float(m1.cols()))*100 << "% * "
183  << m2.nonZeros()/(float(m2.rows())*float(m2.cols()))*100 << "%\n";
184 
185 // timer.reset();
186 // timer.start();
187  BENCH(for (int k=0; k<REPEAT; ++k) m3 = m1 * m2;)
188 // timer.stop();
189  std::cout << " a * b:\t" << timer.value() << endl;
190 // std::cout << sm3 << "\n";
191 
192  timer.reset();
193  timer.start();
194 // std::cerr << "transpose...\n";
195 // EigenSparseMatrix sm4 = sm1.transpose();
196 // std::cout << sm4.nonZeros() << " == " << sm1.nonZeros() << "\n";
197 // exit(1);
198 // std::cerr << "transpose OK\n";
199 // std::cout << sm1 << "\n\n" << sm1.transpose() << "\n\n" << sm4.transpose() << "\n\n";
200  BENCH(for (int k=0; k<REPEAT; ++k) m3 = m1.transpose() * m2;)
201 // timer.stop();
202  std::cout << " a' * b:\t" << timer.value() << endl;
203 
204 // timer.reset();
205 // timer.start();
206  BENCH( for (int k=0; k<REPEAT; ++k) m3 = m1.transpose() * m2.transpose(); )
207 // timer.stop();
208  std::cout << " a' * b':\t" << timer.value() << endl;
209 
210 // timer.reset();
211 // timer.start();
212  BENCH( for (int k=0; k<REPEAT; ++k) m3 = m1 * m2.transpose(); )
213 // timer.stop();
214  std::cout << " a * b' :\t" << timer.value() << endl;
215 }*/
216 
217 // CSparse
218 #ifdef CSPARSE
219  {
220  std::cout << "CSparse \t" << nnzPerCol << "%\n";
221  cs *m1, *m2, *m3;
222  eiToCSparse(sm1, m1);
223  eiToCSparse(sm2, m2);
224 
225  BENCH({
226  m3 = cs_sorted_multiply(m1, m2);
227  if (!m3) {
228  std::cerr << "cs_multiply failed\n";
229  }
230  // cs_print(m3, 0);
231  cs_spfree(m3);
232  });
233  // timer.stop();
234  std::cout << " a * b:\t" << timer.value() << endl;
235 
236  // BENCH( { m3 = cs_sorted_multiply2(m1, m2); cs_spfree(m3); } );
237  // std::cout << " a * b:\t" << timer.value() << endl;
238  }
239 #endif
240 
241 #ifndef NOUBLAS
242  {
243  std::cout << "ublas\t" << nnzPerCol << "%\n";
244  UBlasSparse m1(rows, cols), m2(rows, cols), m3(rows, cols);
245  eiToUblas(sm1, m1);
246  eiToUblas(sm2, m2);
247 
249  std::cout << " a * b:\t" << timer.value() << endl;
250  }
251 #endif
252 
253 // GMM++
254 #ifndef NOGMM
255  {
256  std::cout << "GMM++ sparse\t" << nnzPerCol << "%\n";
257  GmmDynSparse gmmT3(rows, cols);
258  GmmSparse m1(rows, cols), m2(rows, cols), m3(rows, cols);
259  eiToGmm(sm1, m1);
260  eiToGmm(sm2, m2);
261 
262  BENCH(gmm::mult(m1, m2, gmmT3););
263  std::cout << " a * b:\t" << timer.value() << endl;
264 
265  // BENCH(gmm::mult(gmm::transposed(m1), m2, gmmT3););
266  // std::cout << " a' * b:\t" << timer.value() << endl;
267  //
268  // if (rows<500)
269  // {
270  // BENCH(gmm::mult(gmm::transposed(m1), gmm::transposed(m2), gmmT3););
271  // std::cout << " a' * b':\t" << timer.value() << endl;
272  //
273  // BENCH(gmm::mult(m1, gmm::transposed(m2), gmmT3););
274  // std::cout << " a * b':\t" << timer.value() << endl;
275  // }
276  // else
277  // {
278  // std::cout << " a' * b':\t" << "forever" << endl;
279  // std::cout << " a * b':\t" << "forever" << endl;
280  // }
281  }
282 #endif
283 
284 // MTL4
285 #ifndef NOMTL
286  {
287  std::cout << "MTL4\t" << nnzPerCol << "%\n";
288  MtlSparse m1(rows, cols), m2(rows, cols), m3(rows, cols);
289  eiToMtl(sm1, m1);
290  eiToMtl(sm2, m2);
291 
292  BENCH(m3 = m1 * m2;);
293  std::cout << " a * b:\t" << timer.value() << endl;
294 
295  // BENCH(m3 = trans(m1) * m2;);
296  // std::cout << " a' * b:\t" << timer.value() << endl;
297  //
298  // BENCH(m3 = trans(m1) * trans(m2););
299  // std::cout << " a' * b':\t" << timer.value() << endl;
300  //
301  // BENCH(m3 = m1 * trans(m2););
302  // std::cout << " a * b' :\t" << timer.value() << endl;
303  }
304 #endif
305 
306  std::cout << "\n\n";
307  }
308 
309  return 0;
310 }
void eiToGmm(const EigenSparseMatrix &src, GmmSparse &dst)
Definition: BenchSparseUtil.h:64
void eiToDense(const EigenSparseMatrix &src, DenseMatrix &dst)
Definition: BenchSparseUtil.h:54
gmm::col_matrix< gmm::wsvector< Scalar > > GmmDynSparse
Definition: BenchSparseUtil.h:63
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::col_major > > MtlSparse
Definition: BenchSparseUtil.h:74
#define DENSITY
Definition: BenchSparseUtil.h:15
void fillMatrix2(int nnzPerCol, int rows, int cols, EigenSparseMatrix &dst)
Definition: BenchSparseUtil.h:38
gmm::csc_matrix< Scalar > GmmSparse
Definition: BenchSparseUtil.h:62
boost::numeric::ublas::compressed_matrix< Scalar, boost::numeric::ublas::column_major > UBlasSparse
Definition: BenchSparseUtil.h:110
void eiToMtl(const EigenSparseMatrix &src, MtlSparse &dst)
Definition: BenchSparseUtil.h:76
void eiToUblas(const EigenSparseMatrix &src, UBlasSparse &dst)
Definition: BenchSparseUtil.h:112
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
#define NNZPERCOL
Definition: bench/sparse_product.cpp:15
#define BENCH(X)
Definition: bench/sparse_product.cpp:31
#define SIZE
Definition: bench/sparse_product.cpp:11
Definition: BenchTimer.h:55
char char char int int * k
Definition: level2_impl.h:374
density
Definition: UniformPSDSelfTest.py:19
double timer
Definition: oomph_metis_from_parmetis_3.1.1/struct.h:210
EIGEN_DONT_INLINE void prod(const Lhs &a, const Rhs &b, Res &c)
Definition: product_threshold.cpp:53

References BENCH, cols, UniformPSDSelfTest::density, DENSITY, eiToDense(), eiToGmm(), eiToMtl(), eiToUblas(), fillMatrix2(), k, m1, m2(), NNZPERCOL, prod(), REPEAT, rows, and SIZE.