blob: 3781bb8fc72ec6b32097555b74dc5b03aa4f7a96 [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 Guennebaud22792c62009-01-17 16:24:49 +00007#define SIZE 1000000
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
25
26#define BENCH(X) \
27 timer.reset(); \
28 for (int _j=0; _j<NBTRIES; ++_j) { \
29 timer.start(); \
30 for (int _k=0; _k<REPEAT; ++_k) { \
31 X \
32 } timer.stop(); }
33
34typedef std::vector<Vector2i> Coordinates;
35typedef std::vector<float> Values;
36
37EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals);
38EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals);
39EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals);
40EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals);
41EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals);
42EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals);
Gael Guennebaud22792c62009-01-17 16:24:49 +000043EIGEN_DONT_INLINE Scalar* setrand_ublas_genvec(const Coordinates& coords, const Values& vals);
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000044EIGEN_DONT_INLINE Scalar* setrand_mtl(const Coordinates& coords, const Values& vals);
45
46int main(int argc, char *argv[])
47{
48 int rows = SIZE;
49 int cols = SIZE;
50 //float density = float(NBPERROW)/float(SIZE);
51
52 BenchTimer timer;
53 Coordinates coords;
54 Values values;
55 for (int i=0; i<cols*NBPERROW; ++i)
56 {
57 coords.push_back(Vector2i(ei_random<int>(0,rows-1),ei_random<int>(0,cols-1)));
58 values.push_back(ei_random<Scalar>());
59 }
60 std::cout << "nnz = " << coords.size() << "\n";
61
62 // dense matrices
63 #ifdef DENSEMATRIX
64 {
65 timer.reset();
66 timer.start();
67 for (int k=0; k<REPEAT; ++k)
68 setrand_eigen_dense(coords,values);
69 timer.stop();
70 std::cout << "Eigen Dense\t" << timer.value() << "\n";
71 }
72 #endif
73
74 // eigen sparse matrices
75 {
76 timer.reset();
77 timer.start();
78 for (int k=0; k<REPEAT; ++k)
79 setrand_eigen_gnu_hash(coords,values);
80 timer.stop();
Gael Guennebaud22792c62009-01-17 16:24:49 +000081 std::cout << "Eigen std::map\t" << timer.value() << "\n";
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000082 }
Gael Guennebaud22792c62009-01-17 16:24:49 +000083 #ifndef NOGOOGLE
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +000084 {
85 timer.reset();
86 timer.start();
87 for (int k=0; k<REPEAT; ++k)
88 setrand_eigen_google_dense(coords,values);
89 timer.stop();
90 std::cout << "Eigen google dense\t" << timer.value() << "\n";
91 }
92 {
93 timer.reset();
94 timer.start();
95 for (int k=0; k<REPEAT; ++k)
96 setrand_eigen_google_sparse(coords,values);
97 timer.stop();
98 std::cout << "Eigen google sparse\t" << timer.value() << "\n";
99 }
Gael Guennebaud22792c62009-01-17 16:24:49 +0000100 #endif
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000101
102 #ifndef NOUBLAS
103 {
104 timer.reset();
105 timer.start();
106 for (int k=0; k<REPEAT; ++k)
107 setrand_ublas_mapped(coords,values);
108 timer.stop();
109 std::cout << "ublas mapped\t" << timer.value() << "\n";
110 }
111 {
112 timer.reset();
113 timer.start();
114 for (int k=0; k<REPEAT; ++k)
Gael Guennebaud22792c62009-01-17 16:24:49 +0000115 setrand_ublas_genvec(coords,values);
116 timer.stop();
117 std::cout << "ublas vecofvec\t" << timer.value() << "\n";
118 }
119 /*{
120 timer.reset();
121 timer.start();
122 for (int k=0; k<REPEAT; ++k)
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000123 setrand_ublas_compressed(coords,values);
124 timer.stop();
125 std::cout << "ublas comp\t" << timer.value() << "\n";
126 }
127 {
128 timer.reset();
129 timer.start();
130 for (int k=0; k<REPEAT; ++k)
131 setrand_ublas_coord(coords,values);
132 timer.stop();
133 std::cout << "ublas coord\t" << timer.value() << "\n";
Gael Guennebaud22792c62009-01-17 16:24:49 +0000134 }*/
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000135 #endif
136
137
138 // MTL4
139 #ifndef NOMTL
140 {
141 timer.reset();
142 timer.start();
143 for (int k=0; k<REPEAT; ++k)
144 setrand_mtl(coords,values);
145 timer.stop();
146 std::cout << "MTL\t" << timer.value() << "\n";
147 }
148 #endif
149
150 return 0;
151}
152
153EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals)
154{
155 using namespace Eigen;
156 SparseMatrix<Scalar> mat(SIZE,SIZE);
157 {
158 RandomSetter<SparseMatrix<Scalar>, StdMapTraits > setter(mat);
159 for (int i=0; i<coords.size(); ++i)
160 {
161 setter(coords[i].x(), coords[i].y()) = vals[i];
162 }
163// std::cout << "check mem\n"; getchar();
164 }
165 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
166}
167
Gael Guennebaud22792c62009-01-17 16:24:49 +0000168#ifndef NOGOOGLE
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000169EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals)
170{
171 using namespace Eigen;
172 SparseMatrix<Scalar> mat(SIZE,SIZE);
173 {
174 RandomSetter<SparseMatrix<Scalar>, GoogleDenseHashMapTraits> setter(mat);
175 for (int i=0; i<coords.size(); ++i)
176 setter(coords[i].x(), coords[i].y()) = vals[i];
177// std::cout << "check mem\n"; getchar();
178 }
179 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
180}
181
182EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals)
183{
184 using namespace Eigen;
185 SparseMatrix<Scalar> mat(SIZE,SIZE);
186 {
187 RandomSetter<SparseMatrix<Scalar>, GoogleSparseHashMapTraits> setter(mat);
188 for (int i=0; i<coords.size(); ++i)
189 setter(coords[i].x(), coords[i].y()) = vals[i];
190// std::cout << "check mem\n"; getchar();
191 }
192 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
193}
Gael Guennebaud22792c62009-01-17 16:24:49 +0000194#endif
195
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000196#ifndef NOUBLAS
197EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals)
198{
199 using namespace boost;
200 using namespace boost::numeric;
201 using namespace boost::numeric::ublas;
202 mapped_matrix<Scalar> aux(SIZE,SIZE);
203 for (int i=0; i<coords.size(); ++i)
204 {
205 aux(coords[i].x(), coords[i].y()) = vals[i];
206 }
207// std::cout << "check mem\n"; getchar();
208 compressed_matrix<Scalar> mat(aux);
209 return 0;// &mat(coords[0].x(), coords[0].y());
210}
Gael Guennebaud22792c62009-01-17 16:24:49 +0000211/*EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals)
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000212{
213 using namespace boost;
214 using namespace boost::numeric;
215 using namespace boost::numeric::ublas;
216 coordinate_matrix<Scalar> aux(SIZE,SIZE);
217 for (int i=0; i<coords.size(); ++i)
218 {
219 aux(coords[i].x(), coords[i].y()) = vals[i];
220 }
221 compressed_matrix<Scalar> mat(aux);
222 return 0;//&mat(coords[0].x(), coords[0].y());
223}
224EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals)
225{
226 using namespace boost;
227 using namespace boost::numeric;
228 using namespace boost::numeric::ublas;
229 compressed_matrix<Scalar> mat(SIZE,SIZE);
230 for (int i=0; i<coords.size(); ++i)
231 {
232 mat(coords[i].x(), coords[i].y()) = vals[i];
233 }
234 return 0;//&mat(coords[0].x(), coords[0].y());
Gael Guennebaud22792c62009-01-17 16:24:49 +0000235}*/
236EIGEN_DONT_INLINE Scalar* setrand_ublas_genvec(const Coordinates& coords, const Values& vals)
237{
238 using namespace boost;
239 using namespace boost::numeric;
240 using namespace boost::numeric::ublas;
241
242// ublas::vector<coordinate_vector<Scalar> > foo;
243 generalized_vector_of_vector<Scalar, row_major, ublas::vector<coordinate_vector<Scalar> > > aux(SIZE,SIZE);
244 for (int i=0; i<coords.size(); ++i)
245 {
246 aux(coords[i].x(), coords[i].y()) = vals[i];
247 }
248 compressed_matrix<Scalar,row_major> mat(aux);
249 return 0;//&mat(coords[0].x(), coords[0].y());
Gael Guennebaudcc6c4d82009-01-17 14:05:01 +0000250}
251#endif
252
253#ifndef NOMTL
254EIGEN_DONT_INLINE void setrand_mtl(const Coordinates& coords, const Values& vals);
255#endif
256