Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print seed in unit_thread_pool on failure #3355

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
abigalekim marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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