sparse_dense_product.cpp File Reference
#include "BenchSparseUtil.h"

Macros

#define SIZE   650000
 
#define DENSITY   0.01
 
#define REPEAT   1
 
#define MINDENSITY   0.0004
 
#define NBTRIES   10
 
#define BENCH(X)
 

Functions

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: sparse_dense_product.cpp:16
#define NBTRIES
Definition: sparse_dense_product.cpp:26

◆ DENSITY

#define DENSITY   0.01

◆ MINDENSITY

#define MINDENSITY   0.0004

◆ NBTRIES

#define NBTRIES   10

◆ REPEAT

#define REPEAT   1

◆ SIZE

#define SIZE   650000

Function Documentation

◆ main()

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  // fillMatrix(density, rows, cols, sm1);
65  fillMatrix2(7, rows, cols, sm1);
66 
67 // dense matrices
68 #ifdef DENSEMATRIX
69  {
70  std::cout << "Eigen Dense\t" << density * 100 << "%\n";
72  eiToDense(sm1, m1);
73 
74  timer.reset();
75  timer.start();
76  for (int k = 0; k < REPEAT; ++k) v2 = m1 * v1;
77  timer.stop();
78  std::cout << " a * v:\t" << timer.best() << " " << double(REPEAT) / timer.best() << " * / sec " << endl;
79 
80  timer.reset();
81  timer.start();
82  for (int k = 0; k < REPEAT; ++k) v2 = m1.transpose() * v1;
83  timer.stop();
84  std::cout << " a' * v:\t" << timer.best() << endl;
85  }
86 #endif
87 
88  // eigen sparse matrices
89  {
90  std::cout << "Eigen sparse\t" << sm1.nonZeros() / float(sm1.rows() * sm1.cols()) * 100 << "%\n";
91 
92  BENCH(asm("#myc"); v2 = sm1 * v1; asm("#myd");)
93  std::cout << " a * v:\t" << timer.best() / REPEAT << " " << double(REPEAT) / timer.best(REAL_TIMER)
94  << " * / sec " << endl;
95 
96  BENCH({
97  asm("#mya");
98  v2 = sm1.transpose() * v1;
99  asm("#myb");
100  })
101 
102  std::cout << " a' * v:\t" << timer.best() / REPEAT << endl;
103  }
104 
105  // {
106  // DynamicSparseMatrix<Scalar> m1(sm1);
107  // std::cout << "Eigen dyn-sparse\t" << m1.nonZeros()/float(m1.rows()*m1.cols())*100 << "%\n";
108  //
109  // BENCH(for (int k=0; k<REPEAT; ++k) v2 = m1 * v1;)
110  // std::cout << " a * v:\t" << timer.value() << endl;
111  //
112  // BENCH(for (int k=0; k<REPEAT; ++k) v2 = m1.transpose() * v1;)
113  // std::cout << " a' * v:\t" << timer.value() << endl;
114  // }
115 
116 // GMM++
117 #ifndef NOGMM
118  {
119  std::cout << "GMM++ sparse\t" << density * 100 << "%\n";
120  // GmmDynSparse gmmT3(rows,cols);
121  GmmSparse m1(rows, cols);
122  eiToGmm(sm1, m1);
123 
124  std::vector<Scalar> gmmV1(cols), gmmV2(cols);
125  Map<Matrix<Scalar, Dynamic, 1> >(&gmmV1[0], cols) = v1;
126  Map<Matrix<Scalar, Dynamic, 1> >(&gmmV2[0], cols) = v2;
127 
128  BENCH(asm("#myx"); gmm::mult(m1, gmmV1, gmmV2); asm("#myy");)
129  std::cout << " a * v:\t" << timer.value() << endl;
130 
131  BENCH(gmm::mult(gmm::transposed(m1), gmmV1, gmmV2);)
132  std::cout << " a' * v:\t" << timer.value() << endl;
133  }
134 #endif
135 
136 #ifndef NOUBLAS
137  {
138  std::cout << "ublas sparse\t" << density * 100 << "%\n";
140  eiToUblas(sm1, m1);
141 
142  boost::numeric::ublas::vector<Scalar> uv1, uv2;
143  eiToUblasVec(v1, uv1);
144  eiToUblasVec(v2, uv2);
145 
146  // std::vector<Scalar> gmmV1(cols), gmmV2(cols);
147  // Map<Matrix<Scalar,Dynamic,1> >(&gmmV1[0], cols) = v1;
148  // Map<Matrix<Scalar,Dynamic,1> >(&gmmV2[0], cols) = v2;
149 
150  BENCH(uv2 = boost::numeric::ublas::prod(m1, uv1);)
151  std::cout << " a * v:\t" << timer.value() << endl;
152 
153  // BENCH( boost::ublas::prod(gmm::transposed(m1), gmmV1, gmmV2); )
154  // std::cout << " a' * v:\t" << timer.value() << endl;
155  }
156 #endif
157 
158 // MTL4
159 #ifndef NOMTL
160  {
161  std::cout << "MTL4\t" << density * 100 << "%\n";
162  MtlSparse m1(rows, cols);
163  eiToMtl(sm1, m1);
164  mtl::dense_vector<Scalar> mtlV1(cols, 1.0);
165  mtl::dense_vector<Scalar> mtlV2(cols, 1.0);
166 
167  timer.reset();
168  timer.start();
169  for (int k = 0; k < REPEAT; ++k) 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) mtlV2 = trans(m1) * mtlV1;
176  timer.stop();
177  std::cout << " a' * v:\t" << timer.value() << endl;
178  }
179 #endif
180 
181  std::cout << "\n\n";
182  }
183 
184  return 0;
185 }
void eiToGmm(const EigenSparseMatrix &src, GmmSparse &dst)
Definition: BenchSparseUtil.h:64
void eiToUblasVec(const EigenType &src, UblasType &dst)
Definition: BenchSparseUtil.h:119
void eiToDense(const EigenSparseMatrix &src, DenseMatrix &dst)
Definition: BenchSparseUtil.h:54
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::col_major > > MtlSparse
Definition: BenchSparseUtil.h:74
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
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
Definition: BenchTimer.h:55
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
char * trans
Definition: level2_impl.h:240
char char char int int * k
Definition: level2_impl.h:374
@ REAL_TIMER
Definition: BenchTimer.h:46
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
#define MINDENSITY
Definition: sparse_dense_product.cpp:22
#define BENCH(X)
Definition: sparse_dense_product.cpp:29
#define SIZE
Definition: sparse_dense_product.cpp:8
#define DENSITY
Definition: sparse_dense_product.cpp:12

References BENCH, cols, Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::cols(), UniformPSDSelfTest::density, DENSITY, eiToDense(), eiToGmm(), eiToMtl(), eiToUblas(), eiToUblasVec(), fillMatrix2(), k, m1, MINDENSITY, Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::nonZeros(), prod(), Eigen::REAL_TIMER, REPEAT, rows, Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::rows(), SIZE, trans, Eigen::SparseMatrixBase< Derived >::transpose(), v1(), and v2().