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