Skip to content

Commit 5d9ac5d

Browse files
authored
[HOT FIX] Ensure deterministic test shuffle order across MPI ranks (#670)
1 parent d040851 commit 5d9ac5d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

modules/runners/src/runners.cpp

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

6+
#include <chrono>
7+
#include <cstdint>
68
#include <cstdlib>
79
#include <format>
810
#include <iostream>
911
#include <memory>
12+
#include <random>
1013
#include <stdexcept>
1114
#include <string>
1215

@@ -88,6 +91,26 @@ int Init(int argc, char **argv) {
8891

8992
::testing::InitGoogleTest(&argc, argv);
9093

94+
// Ensure consistent GoogleTest shuffle order across all MPI ranks.
95+
unsigned int seed = 0;
96+
int rank_for_seed = -1;
97+
MPI_Comm_rank(MPI_COMM_WORLD, &rank_for_seed);
98+
99+
if (rank_for_seed == 0) {
100+
try {
101+
seed = std::random_device{}();
102+
} catch (...) {
103+
seed = 0;
104+
}
105+
if (seed == 0) {
106+
const auto now = static_cast<std::uint64_t>(std::chrono::steady_clock::now().time_since_epoch().count());
107+
seed = static_cast<unsigned int>(((now & 0x7fffffffULL) | 1ULL));
108+
}
109+
}
110+
111+
MPI_Bcast(&seed, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
112+
::testing::GTEST_FLAG(random_seed) = static_cast<int>(seed);
113+
91114
auto &listeners = ::testing::UnitTest::GetInstance()->listeners();
92115
int rank = -1;
93116
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

0 commit comments

Comments
 (0)