blob: 6f7a19ddfb92debacf1621091ef7b672654f1115 [file] [log] [blame]
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +00001
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
Gael Guennebaud0c7974d2009-01-18 09:53:06 +00007#define SIZE 100000
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +00008#endif
9
10#ifndef NBPERROW
Gael Guennebaud22792c62009-01-17 16:24:49 +000011#define NBPERROW 24
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000012#endif
13
14#ifndef REPEAT
15#define REPEAT 1
16#endif
17
Gael Guennebaud22792c62009-01-17 16:24:49 +000018#ifndef NOGOOGLE
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000019#define EIGEN_GOOGLEHASH_SUPPORT
20#include <google/sparse_hash_map>
Gael Guennebaud22792c62009-01-17 16:24:49 +000021#endif
22
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000023#include "BenchSparseUtil.h"
24
Gael Guennebaud0c7974d2009-01-18 09:53:06 +000025#define CHECK_MEM
26// #define CHECK_MEM std/**/::cout << "check mem\n"; getchar();
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000027
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
36typedef std::vector<Vector2i> Coordinates;
37typedef std::vector<float> Values;
38
Gael Guennebaud0c7974d2009-01-18 09:53:06 +000039EIGEN_DONT_INLINE Scalar* setinnerrand_eigen(const Coordinates& coords, const Values& vals);
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000040EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals);
41EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals);
42EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals);
43EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals);
44EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals);
45EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals);
Gael Guennebaud22792c62009-01-17 16:24:49 +000046EIGEN_DONT_INLINE Scalar* setrand_ublas_genvec(const Coordinates& coords, const Values& vals);
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000047EIGEN_DONT_INLINE Scalar* setrand_mtl(const Coordinates& coords, const Values& vals);
48
49int main(int argc, char *argv[])
50{
51 int rows = SIZE;
52 int cols = SIZE;
Gael Guennebaud0c7974d2009-01-18 09:53:06 +000053 bool fullyrand = false;
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000054 //float density = float(NBPERROW)/float(SIZE);
55
56 BenchTimer timer;
57 Coordinates coords;
58 Values values;
Gael Guennebaud0c7974d2009-01-18 09:53:06 +000059 if(fullyrand)
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000060 {
Gael Guennebaud0c7974d2009-01-18 09:53:06 +000061 for (int i=0; i<cols*NBPERROW; ++i)
62 {
63 coords.push_back(Vector2i(ei_random<int>(0,rows-1),ei_random<int>(0,cols-1)));
64 values.push_back(ei_random<Scalar>());
65 }
66 }
67 else
68 {
69 for (int j=0; j<cols; ++j)
70 for (int i=0; i<NBPERROW; ++i)
71 {
72 coords.push_back(Vector2i(ei_random<int>(0,rows-1),j));
73 values.push_back(ei_random<Scalar>());
74 }
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000075 }
76 std::cout << "nnz = " << coords.size() << "\n";
Gael Guennebaud0c7974d2009-01-18 09:53:06 +000077 CHECK_MEM
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000078
79 // dense matrices
80 #ifdef DENSEMATRIX
81 {
82 timer.reset();
83 timer.start();
84 for (int k=0; k<REPEAT; ++k)
85 setrand_eigen_dense(coords,values);
86 timer.stop();
87 std::cout << "Eigen Dense\t" << timer.value() << "\n";
88 }
89 #endif
90
91 // eigen sparse matrices
Gael Guennebaud0c7974d2009-01-18 09:53:06 +000092 if (!fullyrand)
93 {
94 timer.reset();
95 timer.start();
96 for (int k=0; k<REPEAT; ++k)
97 setinnerrand_eigen(coords,values);
98 timer.stop();
99 std::cout << "Eigen fillrand\t" << timer.value() << "\n";
100 }
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000101 {
102 timer.reset();
103 timer.start();
104 for (int k=0; k<REPEAT; ++k)
105 setrand_eigen_gnu_hash(coords,values);
106 timer.stop();
Gael Guennebaud22792c62009-01-17 16:24:49 +0000107 std::cout << "Eigen std::map\t" << timer.value() << "\n";
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000108 }
Gael Guennebaud22792c62009-01-17 16:24:49 +0000109 #ifndef NOGOOGLE
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000110 {
111 timer.reset();
112 timer.start();
113 for (int k=0; k<REPEAT; ++k)
114 setrand_eigen_google_dense(coords,values);
115 timer.stop();
116 std::cout << "Eigen google dense\t" << timer.value() << "\n";
117 }
118 {
119 timer.reset();
120 timer.start();
121 for (int k=0; k<REPEAT; ++k)
122 setrand_eigen_google_sparse(coords,values);
123 timer.stop();
124 std::cout << "Eigen google sparse\t" << timer.value() << "\n";
125 }
Gael Guennebaud22792c62009-01-17 16:24:49 +0000126 #endif
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000127
128 #ifndef NOUBLAS
129 {
130 timer.reset();
131 timer.start();
132 for (int k=0; k<REPEAT; ++k)
133 setrand_ublas_mapped(coords,values);
134 timer.stop();
135 std::cout << "ublas mapped\t" << timer.value() << "\n";
136 }
137 {
138 timer.reset();
139 timer.start();
140 for (int k=0; k<REPEAT; ++k)
Gael Guennebaud22792c62009-01-17 16:24:49 +0000141 setrand_ublas_genvec(coords,values);
142 timer.stop();
143 std::cout << "ublas vecofvec\t" << timer.value() << "\n";
144 }
145 /*{
146 timer.reset();
147 timer.start();
148 for (int k=0; k<REPEAT; ++k)
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000149 setrand_ublas_compressed(coords,values);
150 timer.stop();
151 std::cout << "ublas comp\t" << timer.value() << "\n";
152 }
153 {
154 timer.reset();
155 timer.start();
156 for (int k=0; k<REPEAT; ++k)
157 setrand_ublas_coord(coords,values);
158 timer.stop();
159 std::cout << "ublas coord\t" << timer.value() << "\n";
Gael Guennebaud22792c62009-01-17 16:24:49 +0000160 }*/
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000161 #endif
162
163
164 // MTL4
165 #ifndef NOMTL
166 {
167 timer.reset();
168 timer.start();
169 for (int k=0; k<REPEAT; ++k)
170 setrand_mtl(coords,values);
171 timer.stop();
172 std::cout << "MTL\t" << timer.value() << "\n";
173 }
174 #endif
175
176 return 0;
177}
178
Gael Guennebaud0c7974d2009-01-18 09:53:06 +0000179EIGEN_DONT_INLINE Scalar* setinnerrand_eigen(const Coordinates& coords, const Values& vals)
180{
181 using namespace Eigen;
182 SparseMatrix<Scalar> mat(SIZE,SIZE);
183 mat.startFill(2000000/*coords.size()*/);
184 for (int i=0; i<coords.size(); ++i)
185 {
186 mat.fillrand(coords[i].x(), coords[i].y()) = vals[i];
187 }
188 mat.endFill();
189 CHECK_MEM;
190 return 0;
191}
192
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000193EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals)
194{
195 using namespace Eigen;
196 SparseMatrix<Scalar> mat(SIZE,SIZE);
197 {
198 RandomSetter<SparseMatrix<Scalar>, StdMapTraits > setter(mat);
199 for (int i=0; i<coords.size(); ++i)
200 {
201 setter(coords[i].x(), coords[i].y()) = vals[i];
202 }
Gael Guennebaud0c7974d2009-01-18 09:53:06 +0000203 CHECK_MEM;
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000204 }
205 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
206}
207
Gael Guennebaud22792c62009-01-17 16:24:49 +0000208#ifndef NOGOOGLE
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000209EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals)
210{
211 using namespace Eigen;
212 SparseMatrix<Scalar> mat(SIZE,SIZE);
213 {
214 RandomSetter<SparseMatrix<Scalar>, GoogleDenseHashMapTraits> setter(mat);
215 for (int i=0; i<coords.size(); ++i)
216 setter(coords[i].x(), coords[i].y()) = vals[i];
Gael Guennebaud0c7974d2009-01-18 09:53:06 +0000217 CHECK_MEM;
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000218 }
219 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
220}
221
222EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals)
223{
224 using namespace Eigen;
225 SparseMatrix<Scalar> mat(SIZE,SIZE);
226 {
227 RandomSetter<SparseMatrix<Scalar>, GoogleSparseHashMapTraits> setter(mat);
228 for (int i=0; i<coords.size(); ++i)
229 setter(coords[i].x(), coords[i].y()) = vals[i];
Gael Guennebaud0c7974d2009-01-18 09:53:06 +0000230 CHECK_MEM;
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000231 }
232 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
233}
Gael Guennebaud22792c62009-01-17 16:24:49 +0000234#endif
235
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000236#ifndef NOUBLAS
237EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals)
238{
239 using namespace boost;
240 using namespace boost::numeric;
241 using namespace boost::numeric::ublas;
242 mapped_matrix<Scalar> aux(SIZE,SIZE);
243 for (int i=0; i<coords.size(); ++i)
244 {
245 aux(coords[i].x(), coords[i].y()) = vals[i];
246 }
Gael Guennebaud0c7974d2009-01-18 09:53:06 +0000247 CHECK_MEM;
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000248 compressed_matrix<Scalar> mat(aux);
249 return 0;// &mat(coords[0].x(), coords[0].y());
250}
Gael Guennebaud22792c62009-01-17 16:24:49 +0000251/*EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals)
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000252{
253 using namespace boost;
254 using namespace boost::numeric;
255 using namespace boost::numeric::ublas;
256 coordinate_matrix<Scalar> aux(SIZE,SIZE);
257 for (int i=0; i<coords.size(); ++i)
258 {
259 aux(coords[i].x(), coords[i].y()) = vals[i];
260 }
261 compressed_matrix<Scalar> mat(aux);
262 return 0;//&mat(coords[0].x(), coords[0].y());
263}
264EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals)
265{
266 using namespace boost;
267 using namespace boost::numeric;
268 using namespace boost::numeric::ublas;
269 compressed_matrix<Scalar> mat(SIZE,SIZE);
270 for (int i=0; i<coords.size(); ++i)
271 {
272 mat(coords[i].x(), coords[i].y()) = vals[i];
273 }
274 return 0;//&mat(coords[0].x(), coords[0].y());
Gael Guennebaud22792c62009-01-17 16:24:49 +0000275}*/
276EIGEN_DONT_INLINE Scalar* setrand_ublas_genvec(const Coordinates& coords, const Values& vals)
277{
278 using namespace boost;
279 using namespace boost::numeric;
280 using namespace boost::numeric::ublas;
281
282// ublas::vector<coordinate_vector<Scalar> > foo;
283 generalized_vector_of_vector<Scalar, row_major, ublas::vector<coordinate_vector<Scalar> > > aux(SIZE,SIZE);
284 for (int i=0; i<coords.size(); ++i)
285 {
286 aux(coords[i].x(), coords[i].y()) = vals[i];
287 }
Gael Guennebaud0c7974d2009-01-18 09:53:06 +0000288 CHECK_MEM;
Gael Guennebaud22792c62009-01-17 16:24:49 +0000289 compressed_matrix<Scalar,row_major> mat(aux);
290 return 0;//&mat(coords[0].x(), coords[0].y());
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000291}
292#endif
293
294#ifndef NOMTL
295EIGEN_DONT_INLINE void setrand_mtl(const Coordinates& coords, const Values& vals);
296#endif
297