1
2 #define NOGMM
3 #define NOMTL
4 #define EIGEN_GOOGLEHASH_SUPPORT 1
5
6 #include <map>
7 #include <ext/hash_map>
8 #include <google/dense_hash_map>
9 #include <google/sparse_hash_map>
10
11 #ifndef SIZE
12 #define SIZE 10000
13 #endif
14
15 #ifndef DENSITY
16 #define DENSITY 0.01
17 #endif
18
19 #ifndef REPEAT
20 #define REPEAT 1
21 #endif
22
23 #include "BenchSparseUtil.h"
24
25 #ifndef MINDENSITY
26 #define MINDENSITY 0.0004
27 #endif
28
29 #ifndef NBTRIES
30 #define NBTRIES 10
31 #endif
32
33 #define BENCH(X) \
34 timer.reset(); \
35 for (int _j=0; _j<NBTRIES; ++_j) { \
36 timer.start(); \
37 for (int _k=0; _k<REPEAT; ++_k) { \
38 X \
39 } timer.stop(); }
40
41
42 static double rtime;
43 static double nentries;
44
45 template<typename SetterType>
dostuff(const char * name,EigenSparseMatrix & sm1)46 void dostuff(const char* name, EigenSparseMatrix& sm1)
47 {
48 int rows = sm1.rows();
49 int cols = sm1.cols();
50 sm1.setZero();
51 BenchTimer t;
52 SetterType* set1 = new SetterType(sm1);
53 t.reset(); t.start();
54 for (int k=0; k<nentries; ++k)
55 (*set1)(internal::random<int>(0,rows-1),internal::random<int>(0,cols-1)) += 1;
56 t.stop();
57 std::cout << "std::map => \t" << t.value()-rtime
58 << " nnz=" << set1->nonZeros() << std::flush;
59
60 // getchar();
61
62 t.reset(); t.start(); delete set1; t.stop();
63 std::cout << " back: \t" << t.value() << "\n";
64 }
65
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 int rows = SIZE;
69 int cols = SIZE;
70 float density = DENSITY;
71
72 EigenSparseMatrix sm1(rows,cols), sm2(rows,cols);
73
74
75 nentries = rows*cols*density;
76 std::cout << "n = " << nentries << "\n";
77 int dummy;
78 BenchTimer t;
79
80 t.reset(); t.start();
81 for (int k=0; k<nentries; ++k)
82 dummy = internal::random<int>(0,rows-1) + internal::random<int>(0,cols-1);
83 t.stop();
84 rtime = t.value();
85 std::cout << "rtime = " << rtime << " (" << dummy << ")\n\n";
86 const int Bits = 6;
87 for (;;)
88 {
89 dostuff<RandomSetter<EigenSparseMatrix,StdMapTraits,Bits> >("std::map ", sm1);
90 dostuff<RandomSetter<EigenSparseMatrix,GnuHashMapTraits,Bits> >("gnu::hash_map", sm1);
91 dostuff<RandomSetter<EigenSparseMatrix,GoogleDenseHashMapTraits,Bits> >("google::dense", sm1);
92 dostuff<RandomSetter<EigenSparseMatrix,GoogleSparseHashMapTraits,Bits> >("google::sparse", sm1);
93
94 // {
95 // RandomSetter<EigenSparseMatrix,GnuHashMapTraits,Bits> set1(sm1);
96 // t.reset(); t.start();
97 // for (int k=0; k<n; ++k)
98 // set1(internal::random<int>(0,rows-1),internal::random<int>(0,cols-1)) += 1;
99 // t.stop();
100 // std::cout << "gnu::hash_map => \t" << t.value()-rtime
101 // << " nnz=" << set1.nonZeros() << "\n";getchar();
102 // }
103 // {
104 // RandomSetter<EigenSparseMatrix,GoogleDenseHashMapTraits,Bits> set1(sm1);
105 // t.reset(); t.start();
106 // for (int k=0; k<n; ++k)
107 // set1(internal::random<int>(0,rows-1),internal::random<int>(0,cols-1)) += 1;
108 // t.stop();
109 // std::cout << "google::dense => \t" << t.value()-rtime
110 // << " nnz=" << set1.nonZeros() << "\n";getchar();
111 // }
112 // {
113 // RandomSetter<EigenSparseMatrix,GoogleSparseHashMapTraits,Bits> set1(sm1);
114 // t.reset(); t.start();
115 // for (int k=0; k<n; ++k)
116 // set1(internal::random<int>(0,rows-1),internal::random<int>(0,cols-1)) += 1;
117 // t.stop();
118 // std::cout << "google::sparse => \t" << t.value()-rtime
119 // << " nnz=" << set1.nonZeros() << "\n";getchar();
120 // }
121 std::cout << "\n\n";
122 }
123
124 return 0;
125 }
126
127