gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
threadsafe_rng.cpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
40 
41 
45 #include "gambit/Logs/logger.hpp"
46 
47 #include <boost/preprocessor/seq/for_each.hpp>
48 #include <boost/preprocessor/tuple/to_seq.hpp>
49 
50 #define ALL_RNGS (default_random_engine, minstd_rand, minstd_rand0, mt19937, mt19937_64, ranlux24_base, ranlux48_base, ranlux24, ranlux48, knuth_b)
51 #define MAKE_SPECIALISED_RNG(r, data, elem) \
52  else if (engine == STRINGIFY(elem)) \
53  { \
54  static Utils::specialised_threadsafe_rng<elem> ultralocal_rng(seed); \
55  local_rng = &ultralocal_rng; \
56  }
57 #define ENABLE_ALL_RNGS BOOST_PP_SEQ_FOR_EACH(MAKE_SPECIALISED_RNG, , BOOST_PP_TUPLE_TO_SEQ(ALL_RNGS))
58 
59 namespace Gambit
60 {
61 
63  void Random::create_rng_engine(str engine, int seed)
64  {
65  using namespace std;
66  if (engine == "default")
67  {
68  engine = "mt19937_64 (default)";
69  static Utils::specialised_threadsafe_rng<mt19937_64> ultralocal_rng(seed);
70  local_rng = &ultralocal_rng;
71  }
73  else utils_error().raise(LOCAL_INFO, "Unknown random number generation engine: "+engine+". Please check your yaml file.");
74  logger() << LogTags::utils << "Random number engine " << engine << " selected with ";
75  if (seed == -1) logger() << "hardware random ";
76  logger() << "seed " << seed << "." << EOM;
77  }
78 
80  double Random::draw()
81  {
82  if (local_rng == NULL) create_rng_engine("default", -1);
83  return std::generate_canonical<double, 32>(rng());
84  }
85 
86 }
87 
88 
89 
90 
91 
92 
93 
General small utility macros.
EXPORT_SYMBOLS error & utils_error()
Utility errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
STL namespace.
Logging access header for GAMBIT.
Derived thread-safe random number generator class, templated on the RNG engine type.
static double draw()
Draw a single uniform random deviate from the interval (0,1) using the chosen RNG engine...
static Utils::threadsafe_rng * local_rng
Pointer to the actual RNG.
const Logging::endofmessage EOM
Explicit const instance of the end of message struct in Gambit namespace.
Definition: logger.hpp:100
EXPORT_SYMBOLS Logging::LogMaster & logger()
Function to retrieve a reference to the Gambit global log object.
Definition: logger.cpp:95
A threadsafe interface to the STL random number generators.
static void create_rng_engine(str, int=-1)
Choose the engine to use for random number generation, based on the contents of the ini file...
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
Exception objects required for standalone compilation.
#define ENABLE_ALL_RNGS
static Utils::threadsafe_rng & rng()
Return a threadsafe wrapper for the chosen RNG engine (to be passed to e.g.
TODO: see if we can use this one:
Definition: Analysis.hpp:33