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