SST  6.0.0 StructuralSimulationToolkit
discrete.h
1 // Copyright 2009-2016 Sandia Corporation. Under the terms
2 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2016, Sandia Corporation
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 "distrib.h"
18 #include "mersenne.h"
19
20 using namespace SST::RNG;
21
22 namespace SST {
23 namespace RNG {
24
25 /**
26  \class SSTDiscreteDistribution discrete.h "sst/core/rng/discrete.h"
27
28  Creates a discrete distribution for use within SST. This distribution is the same across
29  platforms and compilers.
30 */
32
33  public:
34  /**
35  Creates an exponential distribution with a specific lambda
36  \param lambda The lambda of the exponential distribution
37  */
38  SSTDiscreteDistribution(const double* probs, const uint32_t probsCount) :
40  probCount(probsCount) {
41
42  probabilities = (double*) malloc(sizeof(double) * probsCount);
43  double prob_sum = 0;
44
45  for(uint32_t i = 0; i < probsCount; i++) {
46  probabilities[i] = prob_sum;
47  prob_sum += probs[i];
48  }
49
50  baseDistrib = new MersenneRNG();
51  deleteDistrib = true;
52  }
53
54  /**
55  Creates an exponential distribution with a specific lambda and a base random number generator
56  \param lambda The lambda of the exponential distribution
57  \param baseDist The base random number generator to take the distribution from.
58  */
59  SSTDiscreteDistribution(const double* probs, const uint32_t probsCount, SSTRandom* baseDist) :
60  probCount(probsCount) {
61
62  probabilities = (double*) malloc(sizeof(double) * probsCount);
63  double prob_sum = 0;
64
65  for(uint32_t i = 0; i < probsCount; i++) {
66  probabilities[i] = prob_sum;
67  prob_sum += probs[i];
68  }
69
70  baseDistrib = baseDist;
71  deleteDistrib = false;
72  }
73
74  /**
75  Destroys the exponential distribution
76  */
78  free(probabilities);
79
80  if(deleteDistrib) {
81  delete baseDistrib;
82  }
83  }
84
85  /**
86  Gets the next (random) double value in the distribution
87  \return The next random double from the discrete distribution, this is the double converted of the index where the probability is located
88  */
89  double getNextDouble() {
90  const double nextD = baseDistrib->nextUniform();
91
92  uint32_t index = 0;
93
94  for(; index < probCount; index++) {
95  if(probabilities[index] >= nextD) {
96  break;
97  }
98  }
99
100  return (double) index;
101  }
102
103  protected:
104  /**
105  Sets the base random number generator for the distribution.
106  */
108
109  /**
110  Controls whether the base distribution should be deleted when this class is destructed.
111  */
113
114  /**
115  The discrete probability list
116  */
117  double* probabilities;
118
119  /**
120  Count of discrete probabilities
121  */
122  uint32_t probCount;
123
124 };
125
126 }
127 }
128
129 #endif
Definition: constant.h:22
Base class of statistical distributions in SST.
Definition: distrib.h:24
Definition: action.cc:17
~SSTDiscreteDistribution()
Destroys the exponential distribution.
Definition: discrete.h:77
bool deleteDistrib
Controls whether the base distribution should be deleted when this class is destructed.
Definition: discrete.h:112
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:89
double * probabilities
The discrete probability list.
Definition: discrete.h:117
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:122
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:59
Creates a discrete distribution for use within SST.
Definition: discrete.h:31
SSTDiscreteDistribution(const double *probs, const uint32_t probsCount)
Creates an exponential distribution with a specific lambda.
Definition: discrete.h:38
SSTRandom * baseDistrib
Sets the base random number generator for the distribution.
Definition: discrete.h:107