gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
threadsafe_rng.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
48 
49 
50 #ifndef __threadsafe_rng_hpp__
51 #define __threadsafe_rng_hpp__
52 
53 #include <random>
54 
57 
58 
59 namespace Gambit
60 {
61 
62  namespace Utils
63  {
64 
70  {
71  public:
73  typedef std::uint64_t result_type;
74 
76  virtual ~threadsafe_rng() = 0;
77 
79  virtual result_type operator()() = 0;
80 
82  static constexpr result_type min() { return 0; }
83  static constexpr result_type max() { return UINT64_MAX; }
84  };
85 
88 
90  template<typename Engine>
92  {
93 
94  public:
97  {
98  const int max_threads = omp_get_max_threads();
99  rngs = new std::independent_bits_engine<Engine,64,result_type>[max_threads];
100  if (seed == -1) seed = std::random_device()();
101  for(int index = 0; index < max_threads; ++index)
102  {
103  rngs[index] = std::independent_bits_engine<Engine,64,result_type>(index + seed);
104  }
105  }
106 
108  virtual ~specialised_threadsafe_rng() { delete [] rngs; }
109 
114  virtual result_type operator()() { return rngs[omp_get_thread_num()](); }
115 
116  private:
117 
119  std::independent_bits_engine<Engine,64,result_type>* rngs;
120 
121  };
122 
123  }
124 
126  {
127 
128  public:
129 
131  static void create_rng_engine(str, int=-1);
132 
134  static double draw();
135 
138  static Utils::threadsafe_rng& rng() { return *local_rng; }
139 
140  private:
141 
143  Random() {};
144 
146  static Utils::threadsafe_rng* local_rng;
147  };
148 
149 }
150 
151 #endif // #defined __threadsafe_rng_hpp__
General small utility macros.
#define EXPORT_SYMBOLS
General small utility classes, typedefs, etc.
virtual ~threadsafe_rng()=0
Pure virtual destructor to force overriding in derived class.
static constexpr result_type max()
Random()
Private constructor makes this a purely managerial class, i.e. unable to be instantiated.
std::independent_bits_engine< Engine, 64, result_type > * rngs
Pointer to array of RNGs, one each for each thread.
Derived thread-safe random number generator class, templated on the RNG engine type.
static constexpr result_type min()
Operators for compliance with RandomNumberEngine interface -> random distribution sampling...
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
virtual result_type operator()()
Generate a random integer using the chosen engine Selected uniformly from range (min,max).
specialised_threadsafe_rng(int &seed)
Create RNG engines, one for each thread.
virtual ~specialised_threadsafe_rng()
Destroy RNG engines.
std::uint64_t result_type
Return type (will convert underlying RNG type to this)
static Utils::threadsafe_rng & rng()
Return a threadsafe wrapper for the chosen RNG engine (to be passed to e.g.
Base class for thread-safe random number generators.
TODO: see if we can use this one:
Definition: Analysis.hpp:33