SST  13.0.0
StructuralSimulationToolkit
discrete.h
1 // Copyright 2009-2023 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-2023, 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) :
65  probCount(probsCount)
66  {
67 
68  probabilities = (double*)malloc(sizeof(double) * probsCount);
69  double prob_sum = 0;
70 
71  for ( uint32_t i = 0; i < probsCount; i++ ) {
72  probabilities[i] = prob_sum;
73  prob_sum += probs[i];
74  }
75 
76  baseDistrib = baseDist;
77  deleteDistrib = false;
78  }
79 
80  /**
81  Destroys the exponential distribution
82  */
84  {
85  free(probabilities);
86 
87  if ( deleteDistrib ) { delete baseDistrib; }
88  }
89 
90  /**
91  Gets the next (random) double value in the distribution
92  \return The next random double from the discrete distribution, this is the double converted of the index where
93  the probability is located
94  */
95  double getNextDouble()
96  {
97  const double nextD = baseDistrib->nextUniform();
98 
99  uint32_t index = 0;
100 
101  for ( ; index < probCount; index++ ) {
102  if ( probabilities[index] >= nextD ) { break; }
103  }
104 
105  return (double)index;
106  }
107 
108 protected:
109  /**
110  Sets the base random number generator for the distribution.
111  */
113 
114  /**
115  Controls whether the base distribution should be deleted when this class is destructed.
116  */
118 
119  /**
120  The discrete probability list
121  */
122  double* probabilities;
123 
124  /**
125  Count of discrete probabilities
126  */
127  uint32_t probCount;
128 };
129 
131 
132 } // namespace RNG
133 } // namespace SST
134 
135 #endif // SST_CORE_RNG_DISCRETE_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:27
Definition: constant.h:21
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
bool deleteDistrib
Controls whether the base distribution should be deleted when this class is destructed.
Definition: discrete.h:117
Definition: action.cc:18
uint32_t probCount
Count of discrete probabilities.
Definition: discrete.h:127
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
double getNextDouble()
Gets the next (random) double value in the distribution.
Definition: discrete.h:95
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:122
SST::RNG::Random * baseDistrib
Sets the base random number generator for the distribution.
Definition: discrete.h:112
~DiscreteDistribution()
Destroys the exponential distribution.
Definition: discrete.h:83
Base class of statistical distributions in SST.
Definition: distrib.h:22