Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 1 | |
| 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 Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 7 | #define SIZE 100000 |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 8 | #endif |
| 9 | |
| 10 | #ifndef NBPERROW |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 11 | #define NBPERROW 24 |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
| 14 | #ifndef REPEAT |
| 15 | #define REPEAT 1 |
| 16 | #endif |
| 17 | |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 18 | #ifndef NOGOOGLE |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 19 | #define EIGEN_GOOGLEHASH_SUPPORT |
| 20 | #include <google/sparse_hash_map> |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 23 | #include "BenchSparseUtil.h" |
| 24 | |
Gael Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 25 | #define CHECK_MEM |
| 26 | // #define CHECK_MEM std/**/::cout << "check mem\n"; getchar(); |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 27 | |
| 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 | |
| 36 | typedef std::vector<Vector2i> Coordinates; |
| 37 | typedef std::vector<float> Values; |
| 38 | |
Gael Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 39 | EIGEN_DONT_INLINE Scalar* setinnerrand_eigen(const Coordinates& coords, const Values& vals); |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 40 | EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals); |
| 41 | EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals); |
| 42 | EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals); |
| 43 | EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals); |
| 44 | EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals); |
| 45 | EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals); |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 46 | EIGEN_DONT_INLINE Scalar* setrand_ublas_genvec(const Coordinates& coords, const Values& vals); |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 47 | EIGEN_DONT_INLINE Scalar* setrand_mtl(const Coordinates& coords, const Values& vals); |
| 48 | |
| 49 | int main(int argc, char *argv[]) |
| 50 | { |
| 51 | int rows = SIZE; |
| 52 | int cols = SIZE; |
Gael Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 53 | bool fullyrand = false; |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 54 | //float density = float(NBPERROW)/float(SIZE); |
| 55 | |
| 56 | BenchTimer timer; |
| 57 | Coordinates coords; |
| 58 | Values values; |
Gael Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 59 | if(fullyrand) |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 60 | { |
Gael Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 61 | 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 Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 75 | } |
| 76 | std::cout << "nnz = " << coords.size() << "\n"; |
Gael Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 77 | CHECK_MEM |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 78 | |
| 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 Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 92 | 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 Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 101 | { |
| 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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 107 | std::cout << "Eigen std::map\t" << timer.value() << "\n"; |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 108 | } |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 109 | #ifndef NOGOOGLE |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 110 | { |
| 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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 126 | #endif |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 127 | |
| 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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 141 | 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 Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 149 | 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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 160 | }*/ |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 161 | #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 Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 179 | EIGEN_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 Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 193 | EIGEN_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 Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 203 | CHECK_MEM; |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 204 | } |
| 205 | return 0;//&mat.coeffRef(coords[0].x(), coords[0].y()); |
| 206 | } |
| 207 | |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 208 | #ifndef NOGOOGLE |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 209 | EIGEN_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 Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 217 | CHECK_MEM; |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 218 | } |
| 219 | return 0;//&mat.coeffRef(coords[0].x(), coords[0].y()); |
| 220 | } |
| 221 | |
| 222 | EIGEN_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 Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 230 | CHECK_MEM; |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 231 | } |
| 232 | return 0;//&mat.coeffRef(coords[0].x(), coords[0].y()); |
| 233 | } |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 234 | #endif |
| 235 | |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 236 | #ifndef NOUBLAS |
| 237 | EIGEN_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 Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 247 | CHECK_MEM; |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 248 | compressed_matrix<Scalar> mat(aux); |
| 249 | return 0;// &mat(coords[0].x(), coords[0].y()); |
| 250 | } |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 251 | /*EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals) |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 252 | { |
| 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 | } |
| 264 | EIGEN_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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 275 | }*/ |
| 276 | EIGEN_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 Guennebaud | 0c7974d | 2009-01-18 09:53:06 +0000 | [diff] [blame] | 288 | CHECK_MEM; |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame] | 289 | compressed_matrix<Scalar,row_major> mat(aux); |
| 290 | return 0;//&mat(coords[0].x(), coords[0].y()); |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 291 | } |
| 292 | #endif |
| 293 | |
| 294 | #ifndef NOMTL |
| 295 | EIGEN_DONT_INLINE void setrand_mtl(const Coordinates& coords, const Values& vals); |
| 296 | #endif |
| 297 | |