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 | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 7 | #define SIZE 1000000 |
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 | |
| 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 | |
| 34 | typedef std::vector<Vector2i> Coordinates; |
| 35 | typedef std::vector<float> Values; |
| 36 | |
| 37 | EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals); |
| 38 | EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals); |
| 39 | EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals); |
| 40 | EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals); |
| 41 | EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals); |
| 42 | 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^] | 43 | 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] | 44 | EIGEN_DONT_INLINE Scalar* setrand_mtl(const Coordinates& coords, const Values& vals); |
| 45 | |
| 46 | int 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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 81 | std::cout << "Eigen std::map\t" << timer.value() << "\n"; |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 82 | } |
Gael Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 83 | #ifndef NOGOOGLE |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 84 | { |
| 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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 100 | #endif |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 101 | |
| 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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 115 | 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 Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 123 | 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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 134 | }*/ |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 135 | #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 | |
| 153 | EIGEN_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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 168 | #ifndef NOGOOGLE |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 169 | EIGEN_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 | |
| 182 | EIGEN_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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 194 | #endif |
| 195 | |
Gael Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 196 | #ifndef NOUBLAS |
| 197 | EIGEN_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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 211 | /*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] | 212 | { |
| 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 | } |
| 224 | EIGEN_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 Guennebaud | 22792c6 | 2009-01-17 16:24:49 +0000 | [diff] [blame^] | 235 | }*/ |
| 236 | EIGEN_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 Guennebaud | cc6c4d8 | 2009-01-17 14:05:01 +0000 | [diff] [blame] | 250 | } |
| 251 | #endif |
| 252 | |
| 253 | #ifndef NOMTL |
| 254 | EIGEN_DONT_INLINE void setrand_mtl(const Coordinates& coords, const Values& vals); |
| 255 | #endif |
| 256 | |