Skip to content

Commit 5b37891

Browse files
committed
add random seed initialization and broadcasting for tests with MPI
1 parent 87d5b4b commit 5b37891

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

modules/runners/src/runners.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
#include <gtest/gtest.h>
44
#include <mpi.h>
55

6+
#include <chrono>
7+
#include <cstdint>
68
#include <cstdlib>
9+
#include <ctime>
710
#include <format>
811
#include <iostream>
912
#include <memory>
13+
#include <random>
1014
#include <stdexcept>
1115
#include <string>
1216

@@ -87,6 +91,24 @@ int Init(int argc, char **argv) {
8791
tbb::global_control control(tbb::global_control::max_allowed_parallelism, ppc::util::GetNumThreads());
8892

8993
::testing::InitGoogleTest(&argc, argv);
94+
unsigned int seed = 0;
95+
int rank_for_seed = -1;
96+
MPI_Comm_rank(MPI_COMM_WORLD, &rank_for_seed);
97+
98+
if (rank_for_seed == 0) {
99+
try {
100+
seed = std::random_device{}();
101+
} catch (...) {
102+
seed = 0;
103+
}
104+
if (seed == 0) {
105+
const auto now = static_cast<std::uint64_t>(std::chrono::steady_clock::now().time_since_epoch().count());
106+
seed = static_cast<unsigned int>(((now & 0x7fffffffULL) | 1ULL));
107+
}
108+
}
109+
110+
MPI_Bcast(&seed, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
111+
::testing::GTEST_FLAG(random_seed) = static_cast<int>(seed);
90112

91113
auto &listeners = ::testing::UnitTest::GetInstance()->listeners();
92114
int rank = -1;

0 commit comments

Comments
 (0)