SST  14.1.0
StructuralSimulationToolkit
discrete.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_DISCRETE_H
13 #define SST_CORE_RNG_DISCRETE_H
14 
15 #include "distrib.h"
16 #include "math.h"
17 #include "mersenne.h"
18 #include "rng.h"
19 
20 #include <cstdlib> // for malloc/free
21 
22 using namespace SST::RNG;
23 
24 namespace SST {
25 namespace RNG {
26 
27 /**
28  \class DiscreteDistribution discrete.h "sst/core/rng/discrete.h"
29 
30  Creates a discrete distribution for use within SST. This distribution is the same across
31  platforms and compilers.
32 */
34 {
35 
36 public:
37  /**
38  Creates a discrete probability distribution
39  \param probs An array of probabilities for each outcome
40  \param probsCount The number of discrete outcomes
41  */
42  DiscreteDistribution(const double* probs, const uint32_t probsCount) :
43  SST::RNG::RandomDistribution(),
44  probCount(probsCount)
45  {
46 
47  probabilities = (double*)malloc(sizeof(double) * probsCount);
48  double prob_sum = 0;
49 
50  for ( uint32_t i = 0; i < probsCount; i++ ) {
51  probabilities[i] = prob_sum;
52  prob_sum += probs[i];
53  }
54 
55  baseDistrib = new MersenneRNG();
56  deleteDistrib = true;
57  }
58 
59  /**
60  Creates an exponential distribution with a specific lambda and a base random number generator
61  \param lambda The lambda of the exponential distribution
62  \param baseDist The base random number generator to take the distribution from.
63  */
64  DiscreteDistribution(const double* probs, const uint32_t probsCount, SST::RNG::Random* baseDist) :
66  probCount(probsCount)
67  {
68 
69  probabilities = (double*)malloc(sizeof(double) * probsCount);
70  double prob_sum = 0;
71 
72  for ( uint32_t i = 0; i < probsCount; i++ ) {
73  probabilities[i] = prob_sum;
74  prob_sum += probs[i];
75  }
76 
77  baseDistrib = baseDist;
78  deleteDistrib = false;
79  }
80 
81  /**
82  Destroys the exponential distribution
83  */
85  {
86  free(probabilities);
87 
88  if ( deleteDistrib ) { delete baseDistrib; }
89  }
90 
91  /**
92  Gets the next (random) double value in the distribution
93  \return The next random double from the discrete distribution, this is the double converted of the index where
94  the probability is located
95  */
96  double getNextDouble() override
97  {
98  const double nextD = baseDistrib->nextUniform();
99 
100  uint32_t index = 0;
101 
102  for ( ; index < probCount; index++ ) {
103  if ( probabilities[index] >= nextD ) { break; }
104  }
105 
106  return (double)index;
107  }
108 
109  /**
110  Default constructor. FOR SERIALIZATION ONLY.
111  */
113 
114  /**
115  Serialization function for checkpoint
116  */
118  {
119  ser& baseDistrib;
120  ser& deleteDistrib;
121  ser& probCount;
122 
123  if ( ser.mode() == SST::Core::Serialization::serializer::UNPACK ) {
124  probabilities = (double*)malloc(sizeof(double) * probCount);
125  }
126 
127  for ( uint32_t i = 0; i < probCount; i++ ) {
128  ser& probabilities[i];
129  }
130  }
131 
132  /**
133  Serialization macro
134  */
135  ImplementSerializable(SST::RNG::DiscreteDistribution)
136 
137 protected:
138  /**
139  Sets the base random number generator for the distribution.
140  */
141  SST::RNG::Random* baseDistrib;
142 
143  /**
144  Controls whether the base distribution should be deleted when this class is destructed.
145  */
146  bool deleteDistrib;
147 
148  /**
149  The discrete probability list
150  */
151  double* probabilities;
152 
153  /**
154  Count of discrete probabilities
155  */
156  uint32_t probCount;
157 };
158 
160 
161 } // namespace RNG
162 } // namespace SST
163 
164 #endif // SST_CORE_RNG_DISCRETE_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:43
DiscreteDistribution(const double *probs, const uint32_t probsCount, SST::RNG::Random *baseDist)
Creates an exponential distribution with a specific lambda and a base random number generator...
Definition: discrete.h:64
Definition: action.cc:18
double getNextDouble() override
Gets the next (random) double value in the distribution.
Definition: discrete.h:96
uint32_t probCount
Count of discrete probabilities.
Definition: discrete.h:156
Creates a discrete distribution for use within SST.
Definition: discrete.h:33
Implements a Mersenne-based RNG for use in the SST core or components.
Definition: mersenne.h:34
DiscreteDistribution(const double *probs, const uint32_t probsCount)
Creates a discrete probability distribution.
Definition: discrete.h:42
double * probabilities
The discrete probability list.
Definition: discrete.h:151
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition: discrete.h:117
ImplementSerializable(SST::RNG::DiscreteDistribution) protected bool deleteDistrib
Serialization macro.
Definition: discrete.h:135
~DiscreteDistribution()
Destroys the exponential distribution.
Definition: discrete.h:84
DiscreteDistribution()
Default constructor.
Definition: discrete.h:112
Base class of statistical distributions in SST.
Definition: distrib.h:24