SST  11.1.0 StructuralSimulationToolkit
discrete.h
1 // Copyright 2009-2021 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-2021, NTESS
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 an exponential distribution with a specific lambda
39  \param lambda The lambda of the exponential distribution
40  */
41  DiscreteDistribution(const double* probs, const uint32_t probsCount) :
42  SST::RNG::RandomDistribution(),
43  probCount(probsCount)
44  {
45
46  probabilities = (double*)malloc(sizeof(double) * probsCount);
47  double prob_sum = 0;
48
49  for ( uint32_t i = 0; i < probsCount; i++ ) {
50  probabilities[i] = prob_sum;
51  prob_sum += probs[i];
52  }
53
54  baseDistrib = new MersenneRNG();
55  deleteDistrib = true;
56  }
57
58  /**
59  Creates an exponential distribution with a specific lambda and a base random number generator
60  \param lambda The lambda of the exponential distribution
61  \param baseDist The base random number generator to take the distribution from.
62  */
63  DiscreteDistribution(const double* probs, const uint32_t probsCount, SST::RNG::Random* baseDist) :
64  probCount(probsCount)
65  {
66
67  probabilities = (double*)malloc(sizeof(double) * probsCount);
68  double prob_sum = 0;
69
70  for ( uint32_t i = 0; i < probsCount; i++ ) {
71  probabilities[i] = prob_sum;
72  prob_sum += probs[i];
73  }
74
75  baseDistrib = baseDist;
76  deleteDistrib = false;
77  }
78
79  /**
80  Destroys the exponential distribution
81  */
83  {
84  free(probabilities);
85
86  if ( deleteDistrib ) { delete baseDistrib; }
87  }
88
89  /**
90  Gets the next (random) double value in the distribution
91  \return The next random double from the discrete distribution, this is the double converted of the index where
92  the probability is located
93  */
94  double getNextDouble()
95  {
96  const double nextD = baseDistrib->nextUniform();
97
98  uint32_t index = 0;
99
100  for ( ; index < probCount; index++ ) {
101  if ( probabilities[index] >= nextD ) { break; }
102  }
103
104  return (double)index;
105  }
106
107 protected:
108  /**
109  Sets the base random number generator for the distribution.
110  */
112
113  /**
114  Controls whether the base distribution should be deleted when this class is destructed.
115  */
117
118  /**
119  The discrete probability list
120  */
121  double* probabilities;
122
123  /**
124  Count of discrete probabilities
125  */
126  uint32_t probCount;
127 };
128
130
131 } // namespace RNG
132 } // namespace SST
133
134 #endif // SST_CORE_RNG_DISCRETE_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:27
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:63
bool deleteDistrib
Controls whether the base distribution should be deleted when this class is destructed.
Definition: discrete.h:116
uint32_t probCount
Count of discrete probabilities.
Definition: discrete.h:126
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:36
double getNextDouble()
Gets the next (random) double value in the distribution.
Definition: discrete.h:94
DiscreteDistribution(const double *probs, const uint32_t probsCount)
Creates an exponential distribution with a specific lambda.
Definition: discrete.h:41
double * probabilities
The discrete probability list.
Definition: discrete.h:121
SST::RNG::Random * baseDistrib
Sets the base random number generator for the distribution.
Definition: discrete.h:111
~DiscreteDistribution()
Destroys the exponential distribution.
Definition: discrete.h:82
Base class of statistical distributions in SST.
Definition: distrib.h:22