SST  14.0.0
StructuralSimulationToolkit
uniform.h
1 // Copyright 2009-2024 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2024, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_RNG_UNIFORM_H
13 #define SST_CORE_RNG_UNIFORM_H
14 
15 #include "distrib.h"
16 #include "math.h"
17 #include "mersenne.h"
18 #include "rng.h"
19 
20 using namespace SST::RNG;
21 
22 namespace SST {
23 namespace RNG {
24 
25 /**
26  \class UniformDistribution uniform.h "sst/core/rng/uniform.h"
27 
28  Creates a Uniform distribution for use within SST. This distribution is the same across
29  platforms and compilers.
30 */
32 {
33 
34 public:
35  /**
36  Creates an uniform distribution with a specific number of bins
37  \param probsCount Number of probability bins in this distribution
38  */
39  UniformDistribution(const uint32_t probsCount) :
41  deleteDistrib(true),
42  probCount(probsCount),
43  probPerBin(1)
44  {
45 
46  if ( probCount > 0 ) { probPerBin = 1.0 / static_cast<double>(probCount); }
47 
48  baseDistrib = new MersenneRNG();
49  }
50 
51  /**
52  Creates a Uniform distribution with a specific number of bins and user supplied
53  random number generator
54  \param probsCount Number of probability bins in the distribution
55  \param baseDist The base random number generator to take the distribution from.
56  */
57  UniformDistribution(const uint32_t probsCount, SST::RNG::Random* baseDist) :
59  deleteDistrib(false),
60  probCount(probsCount),
61  probPerBin(1)
62  {
63 
64  if ( probCount > 0 ) { probPerBin = 1.0 / static_cast<double>(probCount); }
65 
66  baseDistrib = baseDist;
67  }
68 
69  /**
70  Destroys the distribution and will delete locally allocated RNGs
71  */
73  {
74  if ( deleteDistrib ) { delete baseDistrib; }
75  }
76 
77  /**
78  Gets the next (random) double value in the distribution
79  \return The next random double from the distribution, this is the double converted of the index where the
80  probability is located
81  */
82  double getNextDouble() override
83  {
84  const double nextD = baseDistrib->nextUniform();
85  uint32_t current_bin = 1;
86 
87  while ( nextD > (static_cast<double>(current_bin) * probPerBin) ) {
88  current_bin++;
89  }
90 
91  return static_cast<double>(current_bin - 1);
92  }
93 
94  /**
95  Default constructor. FOR SERIALIZATION ONLY.
96  */
97  UniformDistribution() : RandomDistribution(), deleteDistrib(true), probCount(0) {}
98 
99  /**
100  Serialization function for checkpoint
101  */
103  {
104  ser& baseDistrib;
105  ser& const_cast<bool&>(deleteDistrib);
106  ser& const_cast<uint32_t&>(probCount);
107  ser& probPerBin;
108  }
109 
110  /**
111  Serialization macro
112  */
113  ImplementSerializable(SST::RNG::UniformDistribution)
114 
115 protected:
116  /**
117  Sets the base random number generator for the distribution.
118  */
119  SST::RNG::Random* baseDistrib;
120 
121  /**
122  Controls whether the base distribution should be deleted when this class is destructed.
123  */
124  const bool deleteDistrib;
125 
126  /**
127  Count of discrete probabilities
128  */
129  const uint32_t probCount;
130 
131  /**
132  Range 0..1 split into discrete bins
133  */
134  double probPerBin;
135 };
136 
138 
139 } // namespace RNG
140 } // namespace SST
141 
142 #endif // SST_CORE_RNG_UNIFORM_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:29
Definition: constant.h:21
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition: uniform.h:102
double probPerBin
Range 0..1 split into discrete bins.
Definition: uniform.h:134
Creates a Uniform distribution for use within SST.
Definition: uniform.h:31
Definition: action.cc:18
ImplementSerializable(SST::RNG::UniformDistribution) protected const bool deleteDistrib
Serialization macro.
Definition: uniform.h:113
UniformDistribution(const uint32_t probsCount)
Creates an uniform distribution with a specific number of bins.
Definition: uniform.h:39
Implements a Mersenne-based RNG for use in the SST core or components.
Definition: mersenne.h:34
~UniformDistribution()
Destroys the distribution and will delete locally allocated RNGs.
Definition: uniform.h:72
const uint32_t probCount
Count of discrete probabilities.
Definition: uniform.h:129
double getNextDouble() override
Gets the next (random) double value in the distribution.
Definition: uniform.h:82
UniformDistribution(const uint32_t probsCount, SST::RNG::Random *baseDist)
Creates a Uniform distribution with a specific number of bins and user supplied random number generat...
Definition: uniform.h:57
Base class of statistical distributions in SST.
Definition: distrib.h:24
UniformDistribution()
Default constructor.
Definition: uniform.h:97