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
|
#include <inttypes.h>
#ifdef STL_UNORDERED
#include <unordered_map>
#define MAPNAME std::unordered_map
#define EXTRAARGS
#elif defined(PHMAP_FLAT)
#include "parallel_hashmap/phmap.h"
#define MAPNAME phmap::flat_hash_map
#define NMSP phmap
#define EXTRAARGS
#else
#include "parallel_hashmap/phmap.h"
#if 1
#include <mutex>
#define MTX std::mutex
#elif 0
// Abseil's mutexes are very efficient (at least on windows)
#include "absl/synchronization/mutex.h"
#define MTX phmap::AbslMutex
#elif 1
#include <boost/thread/locks.hpp>
#if 1
#include <boost/thread/mutex.hpp>
#define MTX boost::mutex // faster if all we do is exclusive locks like this bench
#else
#include <boost/thread/shared_mutex.hpp>
#define MTX boost::upgrade_mutex
#endif
#elif 1
#include <windows.h>
class srwlock {
SRWLOCK _lock;
public:
srwlock() { InitializeSRWLock(&_lock); }
void lock() { AcquireSRWLockExclusive(&_lock); }
void unlock() { ReleaseSRWLockExclusive(&_lock); }
};
#define MTX srwlock
#else
// spinlocks - slow!
#include <atomic>
class spinlock {
std::atomic_flag flag = ATOMIC_FLAG_INIT;
public:
void lock() { while(flag.test_and_set(std::memory_order_acquire)); }
void unlock() { flag.clear(std::memory_order_release); }
};
#define MTX spinlock
#endif
#define MAPNAME phmap::parallel_flat_hash_map
#define NMSP phmap
#define MT_SUPPORT 1
#if MT_SUPPORT == 1
// create the parallel_flat_hash_map without internal mutexes, for when
// we programatically ensure that each thread uses different internal submaps
// --------------------------------------------------------------------------
#define EXTRAARGS , NMSP::priv::hash_default_hash<K>, \
NMSP::priv::hash_default_eq<K>, \
std::allocator<std::pair<const K, V>>, 4, NMSP::NullMutex
#elif MT_SUPPORT == 2
// create the parallel_flat_hash_map with internal mutexes, for when
// we read/write the same parallel_flat_hash_map from multiple threads,
// without any special precautions.
// --------------------------------------------------------------------------
#define EXTRAARGS , NMSP::priv::hash_default_hash<K>, \
NMSP::priv::hash_default_eq<K>, \
std::allocator<std::pair<const K, V>>, 4, MTX
#else
#define EXTRAARGS
#endif
#endif
#define phmap_xstr(s) phmap_str(s)
#define phmap_str(s) #s
template <class K, class V>
using HashT = MAPNAME<K, V EXTRAARGS>;
using hash_t = HashT<int64_t, int64_t>;
using str_hash_t = HashT<const char *, int64_t>;
const char *program_slug = phmap_xstr(MAPNAME); // "_4";
#include <cassert>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <thread>
#include <chrono>
#include <ostream>
#include "parallel_hashmap/meminfo.h"
#include <vector>
using std::vector;
int64_t _abs(int64_t x) { return (x < 0) ? -x : x; }
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif // _MSC_VER
// --------------------------------------------------------------------------
class Timer
{
typedef std::chrono::high_resolution_clock high_resolution_clock;
typedef std::chrono::milliseconds milliseconds;
public:
explicit Timer(bool run = false) { if (run) reset(); }
void reset() { _start = high_resolution_clock::now(); }
milliseconds elapsed() const
{
return std::chrono::duration_cast<milliseconds>(high_resolution_clock::now() - _start);
}
private:
high_resolution_clock::time_point _start;
};
// --------------------------------------------------------------------------
// from: https://github.com/preshing/RandomSequence
// --------------------------------------------------------------------------
class RSU
{
private:
unsigned int m_index;
unsigned int m_intermediateOffset;
static unsigned int permuteQPR(unsigned int x)
{
static const unsigned int prime = 4294967291u;
if (x >= prime)
return x; // The 5 integers out of range are mapped to themselves.
unsigned int residue = ((unsigned long long) x * x) % prime;
return (x <= prime / 2) ? residue : prime - residue;
}
public:
RSU(unsigned int seedBase, unsigned int seedOffset)
{
m_index = permuteQPR(permuteQPR(seedBase) + 0x682f0161);
m_intermediateOffset = permuteQPR(permuteQPR(seedOffset) + 0x46790905);
}
unsigned int next()
{
return permuteQPR((permuteQPR(m_index++) + m_intermediateOffset) ^ 0x5bf03635);
}
};
// --------------------------------------------------------------------------
char * new_string_from_integer(uint64_t num)
{
int ndigits = num == 0 ? 1 : (int)log10(num) + 1;
char * str = (char *)malloc(ndigits + 1);
sprintf(str, "%u", (unsigned int)num);
return str;
}
// --------------------------------------------------------------------------
template <class T>
void _fill(vector<T> &v)
{
srand(1); // for a fair/deterministic comparison
for (size_t i = 0, sz = v.size(); i < sz; ++i)
v[i] = (T)(i * 10 + rand() % 10);
}
// --------------------------------------------------------------------------
template <class T>
void _shuffle(vector<T> &v)
{
for (size_t n = v.size(); n >= 2; --n)
std::swap(v[n - 1], v[static_cast<unsigned>(rand()) % n]);
}
// --------------------------------------------------------------------------
template <class T, class HT>
Timer _fill_random(vector<T> &v, HT &hash)
{
_fill<T>(v);
_shuffle<T>(v);
Timer timer(true);
for (size_t i = 0, sz = v.size(); i < sz; ++i)
hash.insert(typename HT::value_type(v[i], 0));
return timer;
}
// --------------------------------------------------------------------------
void out(const char* test, int64_t cnt, const Timer &t, bool = false)
{
printf("%s,time,%u,%s,%f\n", test, (unsigned int)cnt, program_slug,
(float)((double)t.elapsed().count() / 1000));
}
// --------------------------------------------------------------------------
void outmem(const char*, int64_t cnt, uint64_t mem, bool final = false)
{
static uint64_t max_mem = 0;
static uint64_t max_keys = 0;
if (final)
printf("peak memory usage for %u values: %.2f GB\n", (unsigned int)max_keys,
max_mem / ((double)1000 * 1000 * 1000));
else {
if (mem > max_mem)
max_mem = mem;
if ((uint64_t)cnt > max_keys)
max_keys = cnt;
}
}
static bool all_done = false;
static int64_t s_num_keys[16] = { 0 };
static int64_t loop_idx = 0;
static int64_t inner_cnt = 0;
static const char *test = "random";
// --------------------------------------------------------------------------
template <class HT>
void _fill_random_inner(int64_t cnt, HT &hash, RSU &rsu)
{
for (int64_t i=0; i<cnt; ++i)
{
hash.insert(typename HT::value_type(rsu.next(), 0));
++s_num_keys[0];
}
}
// --------------------------------------------------------------------------
template <class HT>
void _fill_random_inner_mt(int64_t cnt, HT &hash, RSU &rsu)
{
constexpr int64_t num_threads = 8; // has to be a power of two
std::unique_ptr<std::thread> threads[num_threads];
auto thread_fn = [&hash, cnt, num_threads](size_t thread_idx, RSU rsu_) {
#if MT_SUPPORT
size_t modulo = hash.subcnt() / num_threads; // subcnt() returns the number of submaps
for (int64_t i=0; i<cnt; ++i) // iterate over all values
{
unsigned int key = rsu_.next(); // get next key to insert
#if MT_SUPPORT == 1
size_t hashval = hash.hash(key); // compute its hash
size_t idx = hash.subidx(hashval); // compute the submap index for this hash
if (idx / modulo == thread_idx) // if the submap is suitable for this thread
#elif MT_SUPPORT == 2
if (i % num_threads == thread_idx)
#endif
{
hash.insert(typename HT::value_type(key, 0)); // insert the value
++(s_num_keys[thread_idx]); // increment count of inserted values
}
}
#endif
};
// create and start 8 threads - each will insert in their own submaps
// thread 0 will insert the keys whose hash direct them to submap0 or submap1
// thread 1 will insert the keys whose hash direct them to submap2 or submap3
// --------------------------------------------------------------------------
for (size_t i=0; i<num_threads; ++i)
threads[i].reset(new std::thread(thread_fn, i, rsu));
// rsu passed by value to threads... we need to increment the reference object
for (int64_t i=0; i<cnt; ++i)
rsu.next();
// wait for the threads to finish their work and exit
for (size_t i=0; i<num_threads; ++i)
threads[i]->join();
}
// --------------------------------------------------------------------------
size_t total_num_keys()
{
size_t n = 0;
for (int i=0; i<16; ++i)
n += s_num_keys[i];
return n;
}
// --------------------------------------------------------------------------
template <class HT>
Timer _fill_random2(int64_t cnt, HT &hash)
{
test = "random";
unsigned int seed = 76687;
RSU rsu(seed, seed + 1);
Timer timer(true);
const int64_t num_loops = 10;
inner_cnt = cnt / num_loops;
for (int i=0; i<16; ++i)
s_num_keys[i] = 0;
for (loop_idx=0; loop_idx<num_loops; ++loop_idx)
{
#if 1 && MT_SUPPORT
// multithreaded insert
_fill_random_inner_mt(inner_cnt, hash, rsu);
#else
_fill_random_inner(inner_cnt, hash, rsu);
#endif
out(test, total_num_keys(), timer);
}
fprintf(stderr, "inserted %.2lfM\n", (double)hash.size() / 1000000);
outmem(test, total_num_keys(), spp::GetProcessMemoryUsed());
return timer;
}
// --------------------------------------------------------------------------
template <class T, class HT>
Timer _lookup(vector<T> &v, HT &hash, size_t &num_present)
{
_fill_random(v, hash);
num_present = 0;
size_t max_val = v.size() * 10;
Timer timer(true);
for (size_t i = 0, sz = v.size(); i < sz; ++i)
{
num_present += (size_t)(hash.find(v[i]) != hash.end());
num_present += (size_t)(hash.find((T)(rand() % max_val)) != hash.end());
}
return timer;
}
// --------------------------------------------------------------------------
template <class T, class HT>
Timer _delete(vector<T> &v, HT &hash)
{
_fill_random(v, hash);
_shuffle(v); // don't delete in insertion order
Timer timer(true);
for(size_t i = 0, sz = v.size(); i < sz; ++i)
hash.erase(v[i]);
return timer;
}
// --------------------------------------------------------------------------
void memlog()
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
uint64_t nbytes_old_out = spp::GetProcessMemoryUsed();
uint64_t nbytes_old = spp::GetProcessMemoryUsed(); // last non outputted mem measurement
outmem(test, 0, nbytes_old);
int64_t last_loop = 0;
while (!all_done)
{
uint64_t nbytes = spp::GetProcessMemoryUsed();
if ((double)_abs(nbytes - nbytes_old_out) / nbytes_old_out > 0.03 ||
(double)_abs(nbytes - nbytes_old) / nbytes_old > 0.01)
{
if ((double)(nbytes - nbytes_old) / nbytes_old > 0.03)
outmem(test, total_num_keys() - 1, nbytes_old);
outmem(test, total_num_keys(), nbytes);
nbytes_old_out = nbytes;
last_loop = loop_idx;
}
else if (loop_idx > last_loop)
{
outmem(test, total_num_keys(), nbytes);
nbytes_old_out = nbytes;
last_loop = loop_idx;
}
nbytes_old = nbytes;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
// --------------------------------------------------------------------------
int main(int argc, char ** argv)
{
int64_t num_keys = 100000000;
const char *bench_name = "random";
int64_t i, value = 0;
if(argc > 2)
{
num_keys = atoi(argv[1]);
bench_name = argv[2];
}
hash_t hash;
str_hash_t str_hash;
srand(1); // for a fair/deterministic comparison
Timer timer(true);
#if MT_SUPPORT
if (!strcmp(program_slug,"absl::parallel_flat_hash_map") ||
!strcmp(program_slug,"phmap::parallel_flat_hash_map"))
program_slug = phmap_xstr(MAPNAME) "_mt";
#endif
std::thread t1(memlog);
try
{
if(!strcmp(bench_name, "sequential"))
{
for(i = 0; i < num_keys; i++)
hash.insert(hash_t::value_type(i, value));
}
#if 0
else if(!strcmp(bench_name, "random"))
{
vector<int64_t> v(num_keys);
timer = _fill_random(v, hash);
out("random", num_keys, timer);
}
#endif
else if(!strcmp(bench_name, "random"))
{
fprintf(stderr, "size = %zu\n", sizeof(hash));
timer = _fill_random2(num_keys, hash);
}
else if(!strcmp(bench_name, "lookup"))
{
vector<int64_t> v(num_keys);
size_t num_present;
timer = _lookup(v, hash, num_present);
//fprintf(stderr, "found %zu\n", num_present);
}
else if(!strcmp(bench_name, "delete"))
{
vector<int64_t> v(num_keys);
timer = _delete(v, hash);
}
else if(!strcmp(bench_name, "sequentialstring"))
{
for(i = 0; i < num_keys; i++)
str_hash.insert(str_hash_t::value_type(new_string_from_integer(i), value));
}
else if(!strcmp(bench_name, "randomstring"))
{
for(i = 0; i < num_keys; i++)
str_hash.insert(str_hash_t::value_type(new_string_from_integer((int)rand()), value));
}
else if(!strcmp(bench_name, "deletestring"))
{
for(i = 0; i < num_keys; i++)
str_hash.insert(str_hash_t::value_type(new_string_from_integer(i), value));
timer.reset();
for(i = 0; i < num_keys; i++)
str_hash.erase(new_string_from_integer(i));
}
//printf("%f\n", (float)((double)timer.elapsed().count() / 1000));
fflush(stdout);
//std::this_thread::sleep_for(std::chrono::seconds(1000));
}
catch (...)
{
}
all_done = true;
outmem(test, 0, 0, true);
t1.join();
return 0;
}
|