gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
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  public:
96  {
97  const int max_threads = omp_get_max_threads();
98  rngs = new std::independent_bits_engine<Engine,64,result_type>[max_threads];
99  if (seed == -1) seed = std::random_device()();
100  for(int index = 0; index < max_threads; ++index)
101  {
102  rngs[index] = std::independent_bits_engine<Engine,64,result_type>(index + seed);
103  }
104  }
105 
107  virtual ~specialised_threadsafe_rng() { delete [] rngs; }
108 
114  {
115  return rngs[omp_get_thread_num()]();
116  }
117 
118  private:
119 
121  std::independent_bits_engine<Engine,64,result_type>* rngs;
122 
123  };
124 
125  }
126 
128  {
129 
130  public:
131 
133  static void create_rng_engine(str, int=-1);
134 
136  static double draw();
137 
140  static Utils::threadsafe_rng& rng() { return *local_rng; }
141 
142  private:
143 
145  Random() {};
146 
148  static Utils::threadsafe_rng* local_rng;
149  };
150 
151 }
152 
153 #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