RockyML  0.0.1
A High-Performance Scientific Computing Framework
utils.h
1 /*
2  Copyright (C) 2022 Amirabbas Asadi , All Rights Reserved
3  distributed under Apache-2.0 license
4 */
5 #ifndef ROCKY_UTILS
6 #define ROCKY_UTILS
7 #include<cmath>
8 #include<utility>
9 #include<memory>
10 #include<limits>
11 #include<random>
12 #include<chrono>
13 #include<thread>
14 #include<functional>
15 
16 #ifdef ROCKY_USE_MPI
17 #include<mpi.h>
18 #endif
19 
20 #include<tbb/tbb.h>
21 #include<Eigen/Core>
22 
23 namespace rocky{
24 namespace utils{
25 
27 public:
28  std::mt19937 prng_;
30  prng_.seed(static_cast<unsigned>(time(0)) + std::hash<std::thread::id>{}(std::this_thread::get_id()));
31  }
32  std::mt19937& prng(){
33  return prng_;
34  }
35 };
36 
37 class random{
38 public:
39  static std::mt19937& prng(){
40  static tbb::enumerable_thread_specific<thread_safe_prng> thread_prng;
41  return thread_prng.local().prng();
42  }
43  // generate a uniform random variable
44  template<typename T_e>
45  static T_e uniform(float a=0.0, float b=1.0){
46  static std::uniform_real_distribution<T_e> dist(a, b);
47  return dist(prng());
48  }
49 };
50 
51 };
52 };
53 #endif
rocky::utils::thread_safe_prng
Definition: utils.h:26
rocky::utils::random
Definition: utils.h:37