SST 15.0
Structural Simulation Toolkit
discrete.h
1// Copyright 2009-2025 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-2025, 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 <cstdint>
21#include <cstdlib> // for malloc/free
22
23namespace SST::RNG {
24
25/**
26 \class DiscreteDistribution 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
34public:
35 /**
36 Creates a discrete probability distribution
37 \param probs An array of probabilities for each outcome
38 \param probsCount The number of discrete outcomes
39 */
40 DiscreteDistribution(const double* probs, const uint32_t probsCount) :
42 probCount(probsCount)
43 {
44
45 probabilities = (double*)malloc(sizeof(double) * probsCount);
46 double prob_sum = 0;
47
48 for ( uint32_t i = 0; i < probsCount; i++ ) {
49 probabilities[i] = prob_sum;
50 prob_sum += probs[i];
51 }
52
53 baseDistrib = new MersenneRNG();
54 deleteDistrib = true;
55 }
56
57 /**
58 Creates an exponential distribution with a specific lambda and a base random number generator
59 \param lambda The lambda of the exponential distribution
60 \param baseDist The base random number generator to take the distribution from.
61 */
62 DiscreteDistribution(const double* probs, const uint32_t probsCount, 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 ) {
87 delete baseDistrib;
88 }
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 ) {
104 break;
105 }
106 }
107
108 return (double)index;
109 }
110
111 /**
112 Default constructor. FOR SERIALIZATION ONLY.
113 */
117
118 /**
119 Serialization function for checkpoint
120 */
122 {
123 SST_SER(baseDistrib);
124 SST_SER(deleteDistrib);
125 SST_SER(probCount);
126
127 if ( ser.mode() == SST::Core::Serialization::serializer::UNPACK ) {
128 probabilities = (double*)malloc(sizeof(double) * probCount);
129 }
130
131 for ( uint32_t i = 0; i < probCount; i++ ) {
132 SST_SER(probabilities[i]);
133 }
134 }
135
136 /**
137 Serialization macro
138 */
139 ImplementSerializable(DiscreteDistribution)
140
141protected:
142 /**
143 Sets the base random number generator for the distribution.
144 */
145 Random* baseDistrib;
146
147 /**
148 Controls whether the base distribution should be deleted when this class is destructed.
149 */
151
152 /**
153 The discrete probability list
154 */
156
157 /**
158 Count of discrete probabilities
159 */
160 uint32_t probCount;
161};
162
163} // namespace SST::RNG
164
165using SSTDiscreteDistribution = SST::RNG::DiscreteDistribution;
166
167#endif // SST_CORE_RNG_DISCRETE_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45
Creates a discrete distribution for use within SST.
Definition discrete.h:32
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition discrete.h:121
ImplementSerializable(DiscreteDistribution) protected bool deleteDistrib
Serialization macro.
Definition discrete.h:139
~DiscreteDistribution()
Destroys the exponential distribution.
Definition discrete.h:82
double getNextDouble() override
Gets the next (random) double value in the distribution.
Definition discrete.h:96
DiscreteDistribution(const double *probs, const uint32_t probsCount, Random *baseDist)
Creates an exponential distribution with a specific lambda and a base random number generator.
Definition discrete.h:62
uint32_t probCount
Count of discrete probabilities.
Definition discrete.h:160
DiscreteDistribution()
Default constructor.
Definition discrete.h:114
double * probabilities
The discrete probability list.
Definition discrete.h:155
DiscreteDistribution(const double *probs, const uint32_t probsCount)
Creates a discrete probability distribution.
Definition discrete.h:40
Implements a Mersenne-based RNG for use in the SST core or components.
Definition mersenne.h:34
RandomDistribution()
Creates the base (abstract) class of a distribution.
Definition distrib.h:41
Implements the base class for random number generators for the SST core.
Definition rng.h:29