Skip to content

Commit

Permalink
Improve unit_thread_pool seeding, and print seeds (#3355)
Browse files Browse the repository at this point in the history
We now have an array of 5 seeds and print them when we reset them.
  • Loading branch information
abigalekim committed Jul 13, 2022
1 parent 15fd781 commit 378786e
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions tiledb/common/thread_pool/test/unit_thread_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,60 @@
#include <stdint.h>
#include <atomic>
#include <catch.hpp>
#include <cstdio>
#include <iostream>
#include <vector>

#include "tiledb/common/thread_pool.h"
#include "tiledb/sm/misc/cancelable_tasks.h"

// Fixed seed for determinism.
static std::vector<uint64_t> generator_seed_arr = {
0xBE08D299, 0x4E996D11, 0x402A1E10, 0x95379958, 0x22101AA9};

static std::atomic<uint64_t> generator_seed = 0;

std::once_flag once_flag;
thread_local static uint64_t local_seed{0};
thread_local static std::mt19937_64 generator;

/**
* Get one of the pre-set seeds.
*/
void set_generator_seed() {
// Set the global seed only once
std::call_once(once_flag, []() {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(
0, generator_seed_arr.size() - 1);
generator_seed = generator_seed_arr[dis(gen)];
std::string gen_seed_str =
"Generator seed: " + std::to_string(generator_seed);
puts(gen_seed_str.c_str());
});

// Different threads need different seeds.
// Safely increment the generator seed and assign to local seed.
if (local_seed == 0) {
do {
local_seed = generator_seed;
} while (
!generator_seed.compare_exchange_strong(local_seed, local_seed + 1));
generator.seed(local_seed);
}
}

/**
* Generate a random number from a uniform distribution, between 0 and specified
* max.
*/
size_t random_ms(size_t max = 3) {
thread_local static uint64_t generator_seed =
std::hash<std::thread::id>()(std::this_thread::get_id());
thread_local static std::mt19937_64 generator(generator_seed);
std::uniform_int_distribution<size_t> distribution(0, max);
// Pick generator seed at random.
set_generator_seed();

thread_local static std::uniform_int_distribution<size_t> distribution(
0, max);
return distribution(generator);
}

Expand Down

0 comments on commit 378786e

Please sign in to comment.