Debian Codegraph @

benchmark/bench.cc

raw symbols SCIP
1 #include <inttypes.h>
2
3 #ifdef STL_UNORDERED
4 #include <unordered_map>
5 #define MAPNAME std::unordered_map
6 #define EXTRAARGS
7 #elif defined(SPARSEPP)
8 #define SPP_USE_SPP_ALLOC 1
9 #include <sparsepp/spp.h>
10 #define MAPNAME spp::sparse_hash_map
11 #define EXTRAARGS
12 #elif defined(ABSEIL_FLAT)
13 #include "absl/container/flat_hash_map.h"
14 #define MAPNAME absl::flat_hash_map
15 #define EXTRAARGS
16 #elif defined(PHMAP_FLAT)
17 #include "parallel_hashmap/phmap.h"
18 #define MAPNAME phmap::flat_hash_map
19 #define NMSP phmap
20 #define EXTRAARGS
21 #elif defined(ABSEIL_PARALLEL_FLAT) || defined(PHMAP)
22 #if defined(ABSEIL_PARALLEL_FLAT)
23 #include "absl/container/parallel_flat_hash_map.h"
24 #define MAPNAME absl::parallel_flat_hash_map
25 #define NMSP absl
26 #define MTX absl::Mutex
27 #else
28 #if 1
29 // use Abseil's mutex... faster
30 #include "absl/synchronization/mutex.h"
31 #define MTX absl::Mutex
32 #else
33 #include <mutex>
34 #define MTX std::mutex
35 #endif
36
37 #include "parallel_hashmap/phmap.h"
38 #define MAPNAME phmap::parallel_flat_hash_map
39 #define NMSP phmap
40 #endif
41
42 #define MT_SUPPORT 2
43 #if MT_SUPPORT == 1
44 // create the parallel_flat_hash_map without internal mutexes, for when
45 // we programatically ensure that each thread uses different internal submaps
46 // --------------------------------------------------------------------------
47 #define EXTRAARGS , NMSP::priv::hash_default_hash<K>, \
48 NMSP::priv::hash_default_eq<K>, \
49 std::allocator<std::pair<const K, V>>, 4, NMSP::NullMutex
50 #elif MT_SUPPORT == 2
51 // create the parallel_flat_hash_map with internal mutexes, for when
52 // we read/write the same parallel_flat_hash_map from multiple threads,
53 // without any special precautions.
54 // --------------------------------------------------------------------------
55 #define EXTRAARGS , NMSP::priv::hash_default_hash<K>, \
56 NMSP::priv::hash_default_eq<K>, \
57 std::allocator<std::pair<const K, V>>, 4, MTX
58 #else
59 #define EXTRAARGS
60 #endif
61
62 #endif
63
64 #define xstr(s) str(s)
65 #define str(s) #s
66
67 template <class K, class V>
68 using HashT = MAPNAME<K, V EXTRAARGS>;
69
70 using hash_t = HashT<int64_t, int64_t>;
71 using str_hash_t = HashT<const char *, int64_t>;
72
73 const char *program_slug = xstr(MAPNAME); // "_4";
74
75 #include <cassert>
76 #include <ctime>
77 #include <cstring>
78 #include <cstdlib>
79 #include <cstdio>
80 #include <cmath>
81 #include <thread>
82 #include <chrono>
83 #include <ostream>
84 #include "parallel_hashmap/meminfo.h"
85 #include <vector>
86 using std::vector;
87
88 int64_t _abs(int64_t x) { return (x < 0) ? -x : x; }
89
90
91 // --------------------------------------------------------------------------
92 class Timer
93 {
94 typedef std::chrono::high_resolution_clock high_resolution_clock;
95 typedef std::chrono::milliseconds milliseconds;
96
97 public:
98 explicit Timer(bool run = false) { if (run) reset(); }
99 void reset() { _start = high_resolution_clock::now(); }
100
101 milliseconds elapsed() const
102 {
103 return std::chrono::duration_cast<milliseconds>(high_resolution_clock::now() - _start);
104 }
105
106 private:
107 high_resolution_clock::time_point _start;
108 };
109
110
111 // --------------------------------------------------------------------------
112 // from: https://github.com/preshing/RandomSequence
113 // --------------------------------------------------------------------------
114 class RSU
115 {
116 private:
117 unsigned int m_index;
118 unsigned int m_intermediateOffset;
119
120 static unsigned int permuteQPR(unsigned int x)
121 {
122 static const unsigned int prime = 4294967291u;
123 if (x >= prime)
124 return x; // The 5 integers out of range are mapped to themselves.
125 unsigned int residue = ((unsigned long long) x * x) % prime;
126 return (x <= prime / 2) ? residue : prime - residue;
127 }
128
129 public:
130 RSU(unsigned int seedBase, unsigned int seedOffset)
131 {
132 m_index = permuteQPR(permuteQPR(seedBase) + 0x682f0161);
133 m_intermediateOffset = permuteQPR(permuteQPR(seedOffset) + 0x46790905);
134 }
135
136 unsigned int next()
137 {
138 return permuteQPR((permuteQPR(m_index++) + m_intermediateOffset) ^ 0x5bf03635);
139 }
140 };
141
142 // --------------------------------------------------------------------------
143 char * new_string_from_integer(int num)
144 {
145 int ndigits = num == 0 ? 1 : (int)log10(num) + 1;
146 char * str = (char *)malloc(ndigits + 1);
147 sprintf(str, "%d", num);
148 return str;
149 }
150
151 // --------------------------------------------------------------------------
152 template <class T>
153 void _fill(vector<T> &v)
154 {
155 srand(1); // for a fair/deterministic comparison
156 for (size_t i = 0, sz = v.size(); i < sz; ++i)
157 v[i] = (T)(i * 10 + rand() % 10);
158 }
159
160 // --------------------------------------------------------------------------
161 template <class T>
162 void _shuffle(vector<T> &v)
163 {
164 for (size_t n = v.size(); n >= 2; --n)
165 std::swap(v[n - 1], v[static_cast<unsigned>(rand()) % n]);
166 }
167
168 // --------------------------------------------------------------------------
169 template <class T, class HT>
170 Timer _fill_random(vector<T> &v, HT &hash)
171 {
172 _fill<T>(v);
173 _shuffle<T>(v);
174
175 Timer timer(true);
176
177 for (size_t i = 0, sz = v.size(); i < sz; ++i)
178 hash.insert(typename HT::value_type(v[i], 0));
179 return timer;
180 }
181
182 // --------------------------------------------------------------------------
183 void out(const char* test, int64_t cnt, const Timer &t)
184 {
185 printf("%s,time,%lld,%s,%f\n", test, cnt, program_slug, (float)((double)t.elapsed().count() / 1000));
186 }
187
188 // --------------------------------------------------------------------------
189 void outmem(const char* test, int64_t cnt, uint64_t mem)
190 {
191 printf("%s,memory,%lld,%s,%lld\n", test, cnt, program_slug, mem);
192 }
193
194 static bool all_done = false;
195 static int64_t num_keys[16] = { 0 };
196 static int64_t loop_idx = 0;
197 static int64_t inner_cnt = 0;
198 static const char *test = "random";
199
200 // --------------------------------------------------------------------------
201 template <class HT>
202 void _fill_random_inner(int64_t cnt, HT &hash, RSU &rsu)
203 {
204 for (int64_t i=0; i<cnt; ++i)
205 {
206 hash.insert(typename HT::value_type(rsu.next(), 0));
207 ++num_keys[0];
208 }
209 }
210
211 // --------------------------------------------------------------------------
212 template <class HT>
213 void _fill_random_inner_mt(int64_t cnt, HT &hash, RSU &rsu)
214 {
215 constexpr int64_t num_threads = 8; // has to be a power of two
216 std::unique_ptr<std::thread> threads[num_threads];
217
218 auto thread_fn = [&hash, cnt, num_threads](int64_t thread_idx, RSU rsu) {
219 #if MT_SUPPORT
220 size_t modulo = hash.subcnt() / num_threads; // subcnt() returns the number of submaps
221
222 for (int64_t i=0; i<cnt; ++i) // iterate over all values
223 {
224 unsigned int key = rsu.next(); // get next key to insert
225 #if MT_SUPPORT == 1
226 size_t hashval = hash.hash(key); // compute its hash
227 size_t idx = hash.subidx(hashval); // compute the submap index for this hash
228 if (idx / modulo == thread_idx) // if the submap is suitable for this thread
229 #elif MT_SUPPORT == 2
230 if (i % num_threads == thread_idx)
231 #endif
232 {
233 hash.insert(typename HT::value_type(key, 0)); // insert the value
234 ++(num_keys[thread_idx]); // increment count of inserted values
235 }
236 }
237 #endif
238 };
239
240 // create and start 8 threads - each will insert in their own submaps
241 // thread 0 will insert the keys whose hash direct them to submap0 or submap1
242 // thread 1 will insert the keys whose hash direct them to submap2 or submap3
243 // --------------------------------------------------------------------------
244 for (int64_t i=0; i<num_threads; ++i)
245 threads[i].reset(new std::thread(thread_fn, i, rsu));
246
247 // rsu passed by value to threads... we need to increment the reference object
248 for (int64_t i=0; i<cnt; ++i)
249 rsu.next();
250
251 // wait for the threads to finish their work and exit
252 for (int64_t i=0; i<num_threads; ++i)
253 threads[i]->join();
254 }
255
256 // --------------------------------------------------------------------------
257 size_t total_num_keys()
258 {
259 size_t n = 0;
260 for (int i=0; i<16; ++i)
261 n += num_keys[i];
262 return n;
263 }
264
265 // --------------------------------------------------------------------------
266 template <class HT>
267 Timer _fill_random2(int64_t cnt, HT &hash)
268 {
269 test = "random";
270 unsigned int seed = 76687;
271 RSU rsu(seed, seed + 1);
272
273 Timer timer(true);
274 const int64_t num_loops = 10;
275 inner_cnt = cnt / num_loops;
276
277 for (int i=0; i<16; ++i)
278 num_keys[i] = 0;
279
280 for (loop_idx=0; loop_idx<num_loops; ++loop_idx)
281 {
282 #if 1 && MT_SUPPORT
283 // multithreaded insert
284 _fill_random_inner_mt(inner_cnt, hash, rsu);
285 #else
286 _fill_random_inner(inner_cnt, hash, rsu);
287 #endif
288 out(test, total_num_keys(), timer);
289 }
290 fprintf(stderr, "inserted %.2lfM\n", (double)hash.size() / 1000000);
291 return timer;
292 }
293
294 // --------------------------------------------------------------------------
295 template <class T, class HT>
296 Timer _lookup(vector<T> &v, HT &hash, size_t &num_present)
297 {
298 _fill_random(v, hash);
299
300 num_present = 0;
301 size_t max_val = v.size() * 10;
302 Timer timer(true);
303
304 for (size_t i = 0, sz = v.size(); i < sz; ++i)
305 {
306 num_present += (size_t)(hash.find(v[i]) != hash.end());
307 num_present += (size_t)(hash.find((T)(rand() % max_val)) != hash.end());
308 }
309 return timer;
310 }
311
312 // --------------------------------------------------------------------------
313 template <class T, class HT>
314 Timer _delete(vector<T> &v, HT &hash)
315 {
316 _fill_random(v, hash);
317 _shuffle(v); // don't delete in insertion order
318
319 Timer timer(true);
320
321 for(size_t i = 0, sz = v.size(); i < sz; ++i)
322 hash.erase(v[i]);
323 return timer;
324 }
325
326 // --------------------------------------------------------------------------
327 void memlog()
328 {
329 std::this_thread::sleep_for(std::chrono::milliseconds(10));
330 uint64_t nbytes_old_out = spp::GetProcessMemoryUsed();
331 uint64_t nbytes_old = spp::GetProcessMemoryUsed(); // last non outputted mem measurement
332 outmem(test, 0, nbytes_old);
333 int64_t last_loop = 0;
334
335 while (!all_done)
336 {
337 uint64_t nbytes = spp::GetProcessMemoryUsed();
338
339 if ((double)_abs(nbytes - nbytes_old_out) / nbytes_old_out > 0.03 ||
340 (double)_abs(nbytes - nbytes_old) / nbytes_old > 0.01)
341 {
342 if ((double)(nbytes - nbytes_old) / nbytes_old > 0.03)
343 outmem(test, total_num_keys() - 1, nbytes_old);
344 outmem(test, total_num_keys(), nbytes);
345 nbytes_old_out = nbytes;
346 last_loop = loop_idx;
347 }
348 else if (loop_idx > last_loop)
349 {
350 outmem(test, total_num_keys(), nbytes);
351 nbytes_old_out = nbytes;
352 last_loop = loop_idx;
353 }
354 nbytes_old = nbytes;
355
356 std::this_thread::sleep_for(std::chrono::milliseconds(1));
357 }
358 }
359
360
361 // --------------------------------------------------------------------------
362 int main(int argc, char ** argv)
363 {
364 int64_t i, value = 0;
365
366 if(argc <= 2)
367 return 1;
368
369 int64_t num_keys = atoi(argv[1]);
370
371 hash_t hash;
372 str_hash_t str_hash;
373
374 srand(1); // for a fair/deterministic comparison
375 Timer timer(true);
376
377 #if MT_SUPPORT
378 if (!strcmp(program_slug,"absl::parallel_flat_hash_map") ||
379 !strcmp(program_slug,"phmap::parallel_flat_hash_map"))
380 program_slug = xstr(MAPNAME) "_mt";
381 #endif
382
383 std::thread t1(memlog);
384
385 try
386 {
387 if(!strcmp(argv[2], "sequential"))
388 {
389 for(i = 0; i < num_keys; i++)
390 hash.insert(hash_t::value_type(i, value));
391 }
392 #if 0
393 else if(!strcmp(argv[2], "random"))
394 {
395 vector<int64_t> v(num_keys);
396 timer = _fill_random(v, hash);
397 out("random", num_keys, timer);
398 }
399 #endif
400 else if(!strcmp(argv[2], "random"))
401 {
402 fprintf(stderr, "size = %d\n", sizeof(hash));
403 timer = _fill_random2(num_keys, hash);
404 //out("random", num_keys, timer);
405 //fprintf(stderr, "inserted %llu\n", hash.size());
406 }
407 else if(!strcmp(argv[2], "lookup"))
408 {
409 vector<int64_t> v(num_keys);
410 size_t num_present;
411
412 timer = _lookup(v, hash, num_present);
413 //fprintf(stderr, "found %llu\n", num_present);
414 }
415 else if(!strcmp(argv[2], "delete"))
416 {
417 vector<int64_t> v(num_keys);
418 timer = _delete(v, hash);
419 }
420 else if(!strcmp(argv[2], "sequentialstring"))
421 {
422 for(i = 0; i < num_keys; i++)
423 str_hash.insert(str_hash_t::value_type(new_string_from_integer(i), value));
424 }
425 else if(!strcmp(argv[2], "randomstring"))
426 {
427 for(i = 0; i < num_keys; i++)
428 str_hash.insert(str_hash_t::value_type(new_string_from_integer((int)rand()), value));
429 }
430 else if(!strcmp(argv[2], "deletestring"))
431 {
432 for(i = 0; i < num_keys; i++)
433 str_hash.insert(str_hash_t::value_type(new_string_from_integer(i), value));
434 timer.reset();
435 for(i = 0; i < num_keys; i++)
436 str_hash.erase(new_string_from_integer(i));
437 }
438
439
440 //printf("%f\n", (float)((double)timer.elapsed().count() / 1000));
441 fflush(stdout);
442 //std::this_thread::sleep_for(std::chrono::seconds(1000));
443 }
444 catch (...)
445 {
446 }
447
448 all_done = true;
449 t1.join();
450 return 0;
451 }