-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbench_concurrent_hash.cpp
589 lines (477 loc) · 15.1 KB
/
bench_concurrent_hash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/**
* MIT License
*
* Copyright (c) 2022 Victor Moncada <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "seq/concurrent_map.hpp"
#include <libcuckoo/cuckoohash_map.hh>
#include <iostream>
#include <thread>
#include <vector>
#include <functional>
#include <unordered_map>
#include <thread>
#include <seq/testing.hpp>
#include <seq/ordered_map.hpp>
#include <seq/ordered_map.hpp>
#include <seq/radix_hash_map.hpp>
#include <seq/format.hpp>
#include <seq/any.hpp>
#include <seq/tiny_string.hpp>
#ifdef BOOST_CONCURRENT_MAP_FOUND
#include <boost/unordered/concurrent_flat_map.hpp>
#endif
#ifdef SEQ_HAS_CPP_17
#include <gtl/phmap.hpp>
#endif
#ifdef NDEBUG
#ifdef TBB_FOUND
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_unordered_map.h>
#endif
#endif
#ifdef _MSC_VER
#include <concurrent_unordered_map.h>
#endif
#include <seq/radix_hash_map.hpp>
using namespace seq;
inline bool get_res(bool r) { return r; }
template<class A>
inline bool get_res(const std::pair<A, bool>& p) { return p.second; }
#ifdef SEQ_HAS_CPP_17
template<class K, class V, class H, class E, class A, size_t T, class M>
SEQ_ALWAYS_INLINE bool insert(gtl::parallel_flat_hash_map<K, V, H, E, A, T,M>& s, const K& k)
{
return s.emplace(k, 0).second;
}
#endif
#ifdef BOOST_CONCURRENT_MAP_FOUND
template<class K, class V, class H, class P, class A>
SEQ_ALWAYS_INLINE bool insert(boost::concurrent_flat_map<K, V, H,P,A>& s, const K& k)
{
return s.emplace(k, 0);
}
#endif
template<class Key, class V, class H, class E, class A, size_t S, class K>
SEQ_ALWAYS_INLINE bool insert(libcuckoo::cuckoohash_map<Key, V,H,E,A,S>& s, const K& k)
{
return s.insert(k, 0);
}
#ifdef _MSC_VER
template<class K, class V, class H>
SEQ_ALWAYS_INLINE bool insert(Concurrency::concurrent_unordered_map<K, V,H>& s, const K& k)
{
return s.insert(std::pair<K,int>(k, 0)).second;
}
#endif
template<class S, class K>
SEQ_ALWAYS_INLINE bool insert(S& s, const K& k)
{
return get_res(s.emplace(k, 0));
}
template<class K, class Set>
void concurrent_insert(const std::vector<K>& keys, Set& s, size_t start, size_t end, std::atomic<bool> * start_compute)
{
while (!(*start_compute)) {}
for (size_t i = start; i < end; ++i)
{
insert(s, keys[i]);
}
}
template<class Set>
size_t walk_map(const Set& s)
{
size_t count = 0;
for (auto it = s.begin(); it != s.end(); ++it, ++count)
{
}
return count;
}
template<class K, class V, class H, class E, class A, size_t S>
size_t walk_map(const libcuckoo::cuckoohash_map<K, V, H,E,A,S>& s)
{
return s.size();
}
template<class K, class V, class H, class E, class A, unsigned T>
size_t walk_map(const concurrent_map<K, V, H, E, A, T>& s)
{
size_t size = 0;
s.cvisit_all([&size](const auto&) {++size; });
return size;
}
#ifdef SEQ_HAS_CPP_17
template<class K, class V, class H, class E, class A, size_t T, class M>
size_t walk_map(const gtl::parallel_flat_hash_map<K, V, H, E, A, T, M>& s)
{
size_t size = 0;
s.for_each([&size](const auto&) {++size; });
return size;
}
#endif
#ifdef BOOST_CONCURRENT_MAP_FOUND
template<class K, class V, class H, class P, class A>
size_t walk_map(const boost::concurrent_flat_map<K, V, H, P, A>& s)
{
size_t size = 0;
s.cvisit_all([&size](const auto&) {++size; });
return size;
}
#endif
template<class Set>
void concurrent_walk(Set& s, std::atomic<bool>* start_compute, size_t * count)
{
while (!(*start_compute)) {}
while (*start_compute) {
*count = walk_map(s);
}
size_t size = s.size();
*count = walk_map(s);
SEQ_TEST(*count == size);
}
template<class S, class K>
SEQ_ALWAYS_INLINE bool find_map(const S& s, const K& key)
{
return s.contains(key);
}
#ifdef NDEBUG
#ifdef TBB_FOUND
template<class K, class V, class H>
SEQ_ALWAYS_INLINE bool find_map(const tbb::concurrent_hash_map<K, V, H>& s, const K& key)
{
//tbb::concurrent_hash_map<K, V, H>::accessor a;
//return s.find(a,key);
return s.count(key) == 1;
}
template<class K, class V, class H>
SEQ_ALWAYS_INLINE bool find_map(const tbb::concurrent_unordered_map <K, V, H>& s, const K& key)
{
//tbb::concurrent_hash_map<K, V, H>::accessor a;
//return s.find(a,key);
return s.count(key) == 1;
}
#endif
#endif
template<class K, class V, class H, class E, class A, size_t S>
SEQ_ALWAYS_INLINE bool find_map(const libcuckoo::cuckoohash_map<K, V, H,E,A,S>& s, const K& key)
{
return s.contains(key);
}
#ifdef BOOST_CONCURRENT_MAP_FOUND
template<class K, class V, class H, class P, class A>
SEQ_ALWAYS_INLINE bool find_map(const boost::concurrent_flat_map<K, V, H, P, A>& s, const K& key)
{
return s.contains(key);
}
#endif
template<class Map>
void reserve(Map& m, size_t size)
{
m.reserve(size);
}
#ifdef NDEBUG
#ifdef TBB_FOUND
template<class K, class V, class H>
void reserve( tbb::concurrent_hash_map<K, V, H>& s, size_t size)
{
s.rehash(size);
}
template<class K, class V, class H>
void reserve( tbb::concurrent_unordered_map <K, V, H>& s, size_t size)
{
s.reserve(size);
}
#endif
#endif
template<class K, class Set>
void concurrent_find(const std::vector<K>& keys, Set& s, std::atomic<bool>* start_compute, size_t* count)
{
while (!(*start_compute)) {}
while (*start_compute) {
*count = 0;
for (size_t i = 0; i < keys.size(); ++i)
if (find_map(s,keys[i]))
++(*count);
}
*count = 0;
for (size_t i = 0; i < keys.size(); ++i) {
if (find_map(s, keys[i]))
++(*count);
else {
//TEST
//bool stop = true;
//find_map(s, keys[i]);
}
}
}
template<class K, class Set>
void concurrent_find_once(const std::vector<K>& keys, Set& s, std::atomic<bool>* start_compute, size_t* count)
{
while (!(*start_compute)) {}
*count = 0;
for (size_t i = 0; i < keys.size(); ++i)
if (find_map(s, keys[i]))
++(*count);
}
template<class S, class K>
void erase(S& s, const K& k)
{
s.erase(k);
}
#ifdef NDEBUG
#ifdef TBB_FOUND
template<class K, class V>
void erase(tbb::concurrent_hash_map<K, V>& s, const K& k)
{
s.erase(k);
}
template<class K, class V>
void erase(tbb::concurrent_unordered_map <K, V>& s, const K& k)
{
s.unsafe_erase(k);
}
#endif
#endif
#ifdef _MSC_VER
template<class K, class V, class H>
void erase(Concurrency::concurrent_unordered_map<K, V, H>& s, const K& k)
{
s.unsafe_erase(k);
}
#endif
template<class K, class Set>
void concurrent_erase(const std::vector<K>& keys, Set& s, size_t start, size_t end, std::atomic<bool>* start_compute)
{
while (!(*start_compute)) {}
for (size_t i = start; i < end; ++i)
//for (size_t i = 0; i < keys.size(); ++i)
erase( s,keys[i]);
}
template<class K, class Set>
void test_concurrent_map(const std::vector<K>& _keys, Set& s, size_t numthreads , size_t & el_insert, size_t & el_walk, size_t & el_find, size_t & el_find_fail, size_t &el_erase, bool test_walk, bool test_find)
{
el_erase = el_find_fail = el_find = el_insert = el_walk = 0;
std::vector<K> keys(_keys.begin(), _keys.begin() + _keys.size() / 2);
std::vector<K> keys_find = keys;
seq::random_shuffle(keys_find.begin(), keys_find.end(),1);
std::vector<K> keys_not_found(_keys.begin() + _keys.size() / 2, _keys.end());
seq::random_shuffle(keys_not_found.begin(), keys_not_found.end(), 1);
size_t chunk_size = keys.size() / numthreads;
std::atomic<bool> start_compute{ false };
std::vector<std::thread*> threads(numthreads);
for (size_t i = 0; i < numthreads; ++i) {
size_t start = i * chunk_size;
size_t end = i == numthreads-1 ? keys.size() : start + chunk_size;
threads[i] = new std::thread(std::bind(concurrent_insert<K,Set>, std::cref(keys), std::ref(s), start, end, &start_compute));
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
//launch walk
size_t walked = 0;
std::thread* walk;
if(test_walk)
walk = new std::thread(std::bind(concurrent_walk<Set>, std::ref(s), &start_compute, &walked));
//launch find
size_t found = 0;
std::thread* find;
if(test_find)
find = new std::thread(std::bind(concurrent_find<K, Set>, std::cref(keys_find), std::ref(s), &start_compute, &found));
seq::tick();
start_compute = true;
for (size_t i = 0; i < numthreads; ++i) {
threads[i]->join();
delete threads[i];
}
size_t ss = s.size();
if (ss != keys.size()) {
printf("insert error: %i\n",(int)ss);
}
SEQ_TEST(keys.size() == ss);
start_compute = false;
if (test_walk) {
walk->join();
delete walk;
}
if (test_find) {
find->join();
delete find;
}
el_insert = seq::tock_ms();
if (test_walk) SEQ_TEST(walked == s.size());
if (test_find) SEQ_TEST(found == s.size());
seq::tick();
size_t count = walk_map(s);
el_walk = seq::tock_ms();
if (count != s.size()) {
printf("walk error, %i missing\n",(int)(s.size() - count));
}
SEQ_TEST(count == s.size());
// parallel find
start_compute = false;
std::vector<size_t> counts(numthreads, 0);
for (size_t i = 0; i < numthreads; ++i) {
auto k = keys_find;
seq::random_shuffle(k.begin(), k.end(),(unsigned) i);
threads[i] = new std::thread(std::bind(concurrent_find_once<K, Set>, k, std::ref(s), &start_compute,&counts[i]));
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
seq::tick();
start_compute = true;
for (size_t i = 0; i < numthreads; ++i) {
threads[i]->join();
delete threads[i];
SEQ_TEST(s.size() == counts[i]);
}
el_find = seq::tock_ms();
// parallel find failed
start_compute = false;
for (size_t i = 0; i < numthreads; ++i) {
threads[i] = new std::thread(std::bind(concurrent_find_once<K, Set>, keys_not_found, std::ref(s), &start_compute, &counts[i]));
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
seq::tick();
start_compute = true;
for (size_t i = 0; i < numthreads; ++i) {
threads[i]->join();
delete threads[i];
SEQ_TEST(0 == counts[i]);
}
el_find_fail = seq::tock_ms();
//parallel erase
for (size_t i = 0; i < numthreads; ++i) {
size_t start = i * chunk_size;
size_t end = i == numthreads - 1 ? keys.size() : start + chunk_size;
threads[i] = new std::thread(std::bind(concurrent_erase<K, Set>, std::cref(keys), std::ref(s), start, end, &start_compute));
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
//launch walk
walked = 0;
if (test_walk)
walk = new std::thread(std::bind(concurrent_walk<Set>, std::ref(s), &start_compute, &walked));
//launch find
found = 0;
if (test_find)
find = new std::thread(std::bind(concurrent_find<K, Set>, std::cref(keys_find), std::ref(s), &start_compute, &found));
std::this_thread::sleep_for(std::chrono::milliseconds(10));
seq::tick();
start_compute = true;
for (size_t i = 0; i < numthreads; ++i) {
threads[i]->join();
delete threads[i];
}
start_compute = false;
if (test_walk) {
walk->join();
delete walk;
}
if (test_find) {
find->join();
delete find;
}
el_erase = seq::tock_ms();
count = s.size();
SEQ_TEST(0 == count);
}
template<class K, class Map, class Rng>
void test_concurrent_map(size_t count, const char * name, Rng rng)
{
std::vector<K> keys;
for (size_t i = 0; i < count; ++i)
keys.push_back(rng(i));
std::sort(keys.begin(), keys.end());
auto it = std::unique(keys.begin(), keys.end());
keys.erase(it, keys.end());
seq::random_shuffle(keys.begin(), keys.end());
//std::reverse(keys)
//seq::random_shuffle(keys.begin(), keys.end(),1);
count = keys.size();
std::cout << std::endl;
std::cout << "Test concurrent insert type = " << typeid(K).name() << " and count = " << count<< std::endl;
std::cout << std::endl;
auto f = join("|",
_s().l(30), //name
_u().c(10), //threads
_fmt(_u(), " ms").c(20), //insert
_fmt(_u(), " ms").c(20), //walk
_fmt(_u(), " ms").c(20), //find
_fmt(_u(), " ms").c(20), //find fail
_fmt(_u(), " ms").c(20), //erase
"");
auto header = join("|", _s().l(30), _s().c(10), _s().c(20), _s().c(20), _s().c(20), _s().c(20), _s().c(20), "");
std::cout << header("Hash table name","Threads", "Insert", "Walk", "Find","Find fail", "Erase") << std::endl;
std::cout << header(fill('-'), fill('-'), fill('-'), fill('-'), fill('-'), fill('-'), fill('-')) << std::endl;
unsigned max_loop = 1;
for (unsigned seed = 0; seed < max_loop; ++seed) {
//if(seed)
seq::random_shuffle(keys.begin(), keys.end(), seed);
//if (seed < 4)
// continue;
for (size_t threads = 1; threads < 20; ++threads)
{
Map set;
//reserve(set,keys.size());
size_t el_insert, el_walk, el_find, el_find_fail, el_erase;
test_concurrent_map(keys, set, threads, el_insert, el_walk, el_find, el_find_fail, el_erase, false, false);
std::cout << f(name, threads, el_insert, el_walk, el_find, el_find_fail, el_erase) << std::endl;
}
}
}
template <typename Key>
class my_tbb_hash_compare {
public:
std::size_t hash(const Key& a) const { return seq::hasher<Key>{}(a); }
bool equal(const Key& a, const Key& b) const { return a == b; }
};
template<class K, class Gen>
void test_concurrent_hash_maps(size_t count, const Gen & gen)
{
test_concurrent_map<K, concurrent_map < K, size_t, seq::hasher<K>, std::equal_to<K>, std::allocator<std::pair<K, size_t> > > >(count, "seq::concurrent_map", gen);
#ifdef BOOST_CONCURRENT_MAP_FOUND
test_concurrent_map<K, boost::concurrent_flat_map<K, size_t, seq::hasher<K>, std::equal_to<K> > >(count, "boost::concurrent_flat_map", gen);
#endif
test_concurrent_map<K, libcuckoo::cuckoohash_map<K, size_t, seq::hasher<K>, std::equal_to<K>> >(count, "libcuckoo::cuckoohash_map", gen);
#ifdef NDEBUG
#ifdef TBB_FOUND
test_concurrent_map<K, tbb::concurrent_hash_map<K, size_t, my_tbb_hash_compare<K> > >(count, "tbb::concurrent_hash_map", gen); //safe erase
//test_concurrent_map<K, tbb::concurrent_unordered_map <K, size_t> >(count, "tbb::concurrent_unordered_map ", gen); //safe iteration
#endif
#endif
#ifdef SEQ_HAS_CPP_17
test_concurrent_map < K, gtl::parallel_flat_hash_map<K, size_t, seq::hasher<K>, std::equal_to<K>, std::allocator<std::pair<K, size_t>>, 5, std::shared_mutex > >(count, "gtl::parallel_flat_hash_map", gen);
#endif
}
int bench_concurrent_hash(int, char** const)
{
{
size_t count = 20000000;
using K = size_t;
auto gen = [](size_t i) {return i; };
test_concurrent_hash_maps<K>(count, gen);
}
{
size_t count = 10000000;
using K = tstring;
std::vector<tstring> strs;
for (size_t i = 0; i < count; ++i)
strs.push_back(generate_random_string<tstring>(33, false));
auto gen = [&](size_t i) {return strs[i]; };
test_concurrent_hash_maps<K>(count, gen);
}
return 0;
}