blob: 90112954f5cd4c96b5b3b1b07ffd2c0771d8f4b8 [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
7#define SIZE 300000
8#endif
9
10#ifndef NBPERROW
11#define NBPERROW 12
12#endif
13
14#ifndef REPEAT
15#define REPEAT 1
16#endif
17
18#define EIGEN_GOOGLEHASH_SUPPORT
19#include <google/sparse_hash_map>
20#include "BenchSparseUtil.h"
21
22
23#define BENCH(X) \
24 timer.reset(); \
25 for (int _j=0; _j<NBTRIES; ++_j) { \
26 timer.start(); \
27 for (int _k=0; _k<REPEAT; ++_k) { \
28 X \
29 } timer.stop(); }
30
31typedef std::vector<Vector2i> Coordinates;
32typedef std::vector<float> Values;
33
34EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals);
35EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals);
36EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals);
37EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals);
38EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals);
39EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals);
40EIGEN_DONT_INLINE Scalar* setrand_mtl(const Coordinates& coords, const Values& vals);
41
42int main(int argc, char *argv[])
43{
44 int rows = SIZE;
45 int cols = SIZE;
46 //float density = float(NBPERROW)/float(SIZE);
47
48 BenchTimer timer;
49 Coordinates coords;
50 Values values;
51 for (int i=0; i<cols*NBPERROW; ++i)
52 {
53 coords.push_back(Vector2i(ei_random<int>(0,rows-1),ei_random<int>(0,cols-1)));
54 values.push_back(ei_random<Scalar>());
55 }
56 std::cout << "nnz = " << coords.size() << "\n";
57
58 // dense matrices
59 #ifdef DENSEMATRIX
60 {
61 timer.reset();
62 timer.start();
63 for (int k=0; k<REPEAT; ++k)
64 setrand_eigen_dense(coords,values);
65 timer.stop();
66 std::cout << "Eigen Dense\t" << timer.value() << "\n";
67 }
68 #endif
69
70 // eigen sparse matrices
71 {
72 timer.reset();
73 timer.start();
74 for (int k=0; k<REPEAT; ++k)
75 setrand_eigen_gnu_hash(coords,values);
76 timer.stop();
77 std::cout << "Eigen gnu hashmap\t" << timer.value() << "\n";
78 }
79 {
80 timer.reset();
81 timer.start();
82 for (int k=0; k<REPEAT; ++k)
83 setrand_eigen_google_dense(coords,values);
84 timer.stop();
85 std::cout << "Eigen google dense\t" << timer.value() << "\n";
86 }
87 {
88 timer.reset();
89 timer.start();
90 for (int k=0; k<REPEAT; ++k)
91 setrand_eigen_google_sparse(coords,values);
92 timer.stop();
93 std::cout << "Eigen google sparse\t" << timer.value() << "\n";
94 }
95
96 #ifndef NOUBLAS
97 {
98 timer.reset();
99 timer.start();
100 for (int k=0; k<REPEAT; ++k)
101 setrand_ublas_mapped(coords,values);
102 timer.stop();
103 std::cout << "ublas mapped\t" << timer.value() << "\n";
104 }
105 {
106 timer.reset();
107 timer.start();
108 for (int k=0; k<REPEAT; ++k)
109 setrand_ublas_compressed(coords,values);
110 timer.stop();
111 std::cout << "ublas comp\t" << timer.value() << "\n";
112 }
113 {
114 timer.reset();
115 timer.start();
116 for (int k=0; k<REPEAT; ++k)
117 setrand_ublas_coord(coords,values);
118 timer.stop();
119 std::cout << "ublas coord\t" << timer.value() << "\n";
120 }
121 #endif
122
123
124 // MTL4
125 #ifndef NOMTL
126 {
127 timer.reset();
128 timer.start();
129 for (int k=0; k<REPEAT; ++k)
130 setrand_mtl(coords,values);
131 timer.stop();
132 std::cout << "MTL\t" << timer.value() << "\n";
133 }
134 #endif
135
136 return 0;
137}
138
139EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals)
140{
141 using namespace Eigen;
142 SparseMatrix<Scalar> mat(SIZE,SIZE);
143 {
144 RandomSetter<SparseMatrix<Scalar>, StdMapTraits > setter(mat);
145 for (int i=0; i<coords.size(); ++i)
146 {
147 setter(coords[i].x(), coords[i].y()) = vals[i];
148 }
149// std::cout << "check mem\n"; getchar();
150 }
151 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
152}
153
154EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals)
155{
156 using namespace Eigen;
157 SparseMatrix<Scalar> mat(SIZE,SIZE);
158 {
159 RandomSetter<SparseMatrix<Scalar>, GoogleDenseHashMapTraits> setter(mat);
160 for (int i=0; i<coords.size(); ++i)
161 setter(coords[i].x(), coords[i].y()) = vals[i];
162// std::cout << "check mem\n"; getchar();
163 }
164 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
165}
166
167EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals)
168{
169 using namespace Eigen;
170 SparseMatrix<Scalar> mat(SIZE,SIZE);
171 {
172 RandomSetter<SparseMatrix<Scalar>, GoogleSparseHashMapTraits> setter(mat);
173 for (int i=0; i<coords.size(); ++i)
174 setter(coords[i].x(), coords[i].y()) = vals[i];
175// std::cout << "check mem\n"; getchar();
176 }
177 return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
178}
179#ifndef NOUBLAS
180EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals)
181{
182 using namespace boost;
183 using namespace boost::numeric;
184 using namespace boost::numeric::ublas;
185 mapped_matrix<Scalar> aux(SIZE,SIZE);
186 for (int i=0; i<coords.size(); ++i)
187 {
188 aux(coords[i].x(), coords[i].y()) = vals[i];
189 }
190// std::cout << "check mem\n"; getchar();
191 compressed_matrix<Scalar> mat(aux);
192 return 0;// &mat(coords[0].x(), coords[0].y());
193}
194EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals)
195{
196 using namespace boost;
197 using namespace boost::numeric;
198 using namespace boost::numeric::ublas;
199 coordinate_matrix<Scalar> aux(SIZE,SIZE);
200 for (int i=0; i<coords.size(); ++i)
201 {
202 aux(coords[i].x(), coords[i].y()) = vals[i];
203 }
204 compressed_matrix<Scalar> mat(aux);
205 return 0;//&mat(coords[0].x(), coords[0].y());
206}
207EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals)
208{
209 using namespace boost;
210 using namespace boost::numeric;
211 using namespace boost::numeric::ublas;
212 compressed_matrix<Scalar> mat(SIZE,SIZE);
213 for (int i=0; i<coords.size(); ++i)
214 {
215 mat(coords[i].x(), coords[i].y()) = vals[i];
216 }
217 return 0;//&mat(coords[0].x(), coords[0].y());
218}
219#endif
220
221#ifndef NOMTL
222EIGEN_DONT_INLINE void setrand_mtl(const Coordinates& coords, const Values& vals);
223#endif
224