10 #include "Tpetra_Details_Random.hpp"
11 #include "Teuchos_TestForException.hpp"
18 unsigned int getSeedFromRank(
int mpi_rank) {
24 uint64_t myRank =
static_cast<uint64_t
>(mpi_rank);
25 uint64_t seed64 =
static_cast<uint64_t
> (std::rand ()) + myRank + 17311uLL;
26 unsigned int seed =
static_cast<unsigned int> (seed64&0xffffffff);
30 #ifdef KOKKOS_ENABLE_CUDA
31 Kokkos::Random_XorShift64_Pool<typename Kokkos::CudaSpace::execution_space> * cuda_pool_=
nullptr;
33 void finalize_cuda_pool() {
34 if(cuda_pool_ !=
nullptr) {
39 #endif // KOKKOS_ENABLE_CUDA
42 #ifdef KOKKOS_ENABLE_HIP
43 Kokkos::Random_XorShift64_Pool<typename Kokkos::HIPSpace::execution_space> * hip_pool_=
nullptr;
45 void finalize_hip_pool() {
46 if(hip_pool_ !=
nullptr) {
51 #endif // KOKKOS_ENABLE_HIP
53 #ifdef KOKKOS_ENABLE_SYCL
54 Kokkos::Random_XorShift64_Pool<typename Kokkos::Experimental::SYCLDeviceUSMSpace::execution_space> * sycl_pool_=
nullptr;
56 void finalize_sycl_pool() {
57 if(sycl_pool_ !=
nullptr) {
62 #endif // KOKKOS_ENABLE_SYCL
65 #ifdef KOKKOS_ENABLE_OPENMP
66 Kokkos::Random_XorShift64_Pool<Kokkos::OpenMP> * openmp_pool_=
nullptr;
68 void finalize_openmp_pool() {
69 if(openmp_pool_ !=
nullptr) {
71 openmp_pool_ =
nullptr;
74 #endif // KOKKOS_ENABLE_OPENMP
77 #ifdef KOKKOS_ENABLE_SERIAL
78 Kokkos::Random_XorShift64_Pool<Kokkos::Serial> * serial_pool_=
nullptr;
80 void finalize_serial_pool() {
81 if(serial_pool_ !=
nullptr) {
83 serial_pool_ =
nullptr;
86 #endif // KOKKOS_ENABLE_SERIAL
92 #ifdef KOKKOS_ENABLE_CUDA
94 Static_Random_XorShift64_Pool<typename Kokkos::CudaSpace::execution_space>::
95 resetPool(
int mpi_rank) {
96 using pool_type = Kokkos::Random_XorShift64_Pool<typename Kokkos::CudaSpace::execution_space>;
101 Kokkos::push_finalize_hook(finalize_cuda_pool);
103 cuda_pool_ =
new pool_type(getSeedFromRank(mpi_rank));
107 Static_Random_XorShift64_Pool<typename Kokkos::CudaSpace::execution_space>::
109 return cuda_pool_!=
nullptr;
112 Kokkos::Random_XorShift64_Pool<typename Kokkos::CudaSpace::execution_space> &
113 Static_Random_XorShift64_Pool<typename Kokkos::CudaSpace::execution_space>::
115 TEUCHOS_TEST_FOR_EXCEPTION(!isSet(),std::runtime_error,
"Tpetra::Details::Static_Random_XorShift64_Pool: resetPool() must be called before getPool");
118 #endif // KOKKOS_ENABLE_CUDA
122 #ifdef KOKKOS_ENABLE_HIP
124 Static_Random_XorShift64_Pool<typename Kokkos::HIPSpace::execution_space>::
125 resetPool(
int mpi_rank) {
126 using pool_type = Kokkos::Random_XorShift64_Pool<typename Kokkos::HIPSpace::execution_space>;
131 Kokkos::push_finalize_hook(finalize_hip_pool);
133 hip_pool_ =
new pool_type(getSeedFromRank(mpi_rank));
137 Static_Random_XorShift64_Pool<typename Kokkos::HIPSpace::execution_space>::
139 return hip_pool_!=
nullptr;
142 Kokkos::Random_XorShift64_Pool<typename Kokkos::HIPSpace::execution_space> &
143 Static_Random_XorShift64_Pool<typename Kokkos::HIPSpace::execution_space>::
145 TEUCHOS_TEST_FOR_EXCEPTION(!isSet(),std::runtime_error,
"Tpetra::Details::Static_Random_XorShift64_Pool: resetPool() must be called before getPool");
148 #endif // KOKKOS_ENABLE_HIP
152 #ifdef KOKKOS_ENABLE_SYCL
154 Static_Random_XorShift64_Pool<typename Kokkos::Experimental::SYCLDeviceUSMSpace::execution_space>::
155 resetPool(
int mpi_rank) {
156 using pool_type = Kokkos::Random_XorShift64_Pool<typename Kokkos::Experimental::SYCLDeviceUSMSpace::execution_space>;
161 Kokkos::push_finalize_hook(finalize_sycl_pool);
163 sycl_pool_ =
new pool_type(getSeedFromRank(mpi_rank));
167 Static_Random_XorShift64_Pool<typename Kokkos::Experimental::SYCLDeviceUSMSpace::execution_space>::
169 return sycl_pool_!=
nullptr;
172 Kokkos::Random_XorShift64_Pool<typename Kokkos::Experimental::SYCLDeviceUSMSpace::execution_space> &
173 Static_Random_XorShift64_Pool<typename Kokkos::Experimental::SYCLDeviceUSMSpace::execution_space>::
175 TEUCHOS_TEST_FOR_EXCEPTION(!isSet(),std::runtime_error,
"Tpetra::Details::Static_Random_XorShift64_Pool: resetPool() must be called before getPool");
178 #endif // KOKKOS_ENABLE_SYCL
182 #ifdef KOKKOS_ENABLE_OPENMP
184 Static_Random_XorShift64_Pool<Kokkos::OpenMP>::
185 resetPool(
int mpi_rank) {
186 using pool_type = Kokkos::Random_XorShift64_Pool<Kokkos::OpenMP>;
191 Kokkos::push_finalize_hook(finalize_openmp_pool);
193 openmp_pool_ =
new pool_type(getSeedFromRank(mpi_rank));
197 Static_Random_XorShift64_Pool<Kokkos::OpenMP>::
199 return openmp_pool_!=
nullptr;
202 Kokkos::Random_XorShift64_Pool<Kokkos::OpenMP> &
203 Static_Random_XorShift64_Pool<Kokkos::OpenMP>::
205 TEUCHOS_TEST_FOR_EXCEPTION(!isSet(),std::runtime_error,
"Tpetra::Details::Static_Random_XorShift64_Pool: resetPool() must be called before getPool");
206 return *openmp_pool_;
208 #endif // KOKKOS_ENABLE_OPENMP
212 #ifdef KOKKOS_ENABLE_SERIAL
214 Static_Random_XorShift64_Pool<Kokkos::Serial>::
215 resetPool(
int mpi_rank) {
216 using pool_type = Kokkos::Random_XorShift64_Pool<Kokkos::Serial>;
221 Kokkos::push_finalize_hook(finalize_serial_pool);
223 serial_pool_ =
new pool_type(getSeedFromRank(mpi_rank));
227 Static_Random_XorShift64_Pool<Kokkos::Serial>::
229 return serial_pool_!=
nullptr;
232 Kokkos::Random_XorShift64_Pool<Kokkos::Serial> &
233 Static_Random_XorShift64_Pool<Kokkos::Serial>::
235 TEUCHOS_TEST_FOR_EXCEPTION(!isSet(),std::runtime_error,
"Tpetra::Details::Static_Random_XorShift64_Pool: resetPool() must be called before getPool");
236 return *serial_pool_;
238 #endif // KOKKOS_ENABLE_SERIAL