Skip to content

Commit 56d9cee

Browse files
committed
add random seed initialization and broadcasting for tests with MPIsimplify OpenMP linking logic
1 parent 87d5b4b commit 56d9cee

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

cmake/openmp.cmake

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,6 @@ endfunction()
2020

2121
function(ppc_link_openmp exec_func_lib)
2222
find_package(OpenMP REQUIRED)
23-
# Link the canonical imported target if available
24-
if(TARGET OpenMP::OpenMP_CXX)
25-
target_link_libraries(${exec_func_lib} PUBLIC OpenMP::OpenMP_CXX)
26-
endif()
27-
28-
if(APPLE)
29-
# Homebrew libomp common paths
30-
find_path(LIBOMP_INCLUDE_DIR omp.h HINTS /opt/homebrew/opt/libomp/include
31-
/usr/local/opt/libomp/include)
32-
find_library(LIBOMP_LIBRARY omp HINTS /opt/homebrew/opt/libomp/lib
33-
/usr/local/opt/libomp/lib)
34-
if(LIBOMP_INCLUDE_DIR)
35-
target_include_directories(${exec_func_lib} PUBLIC ${LIBOMP_INCLUDE_DIR})
36-
endif()
37-
if(LIBOMP_LIBRARY)
38-
target_link_libraries(${exec_func_lib} PUBLIC ${LIBOMP_LIBRARY})
39-
# Ensure Clang generates OpenMP code
40-
target_compile_options(${exec_func_lib} PUBLIC -Xclang -fopenmp)
41-
endif()
42-
endif()
23+
target_link_libraries(${exec_func_lib} PUBLIC ${OpenMP_libomp_LIBRARY}
24+
OpenMP::OpenMP_CXX)
4325
endfunction()

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)