forked from cisco-system-traffic-generator/trex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_uniform_prob.cpp
267 lines (218 loc) · 7.43 KB
/
n_uniform_prob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
taken from here https://oroboro.com/non-uniform-random-numbers/
Copyright (c) 2015-2017 oroboro
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <common/n_uniform_prob.h>
#include <assert.h>
u32 KxuNuRand::getRandom() {
const Distribution& dist = mDist[mRand->getRandomInRange( mDist.size() )];
return ( mRand->getRandom() <= dist.mProb ) ? dist.mA : dist.mB;
}
static void prob_conv_fix_size(std::vector<double> prob,
std::vector<u32> & result ){
double sum=0.0;
for (auto& n : prob)
sum += n;
result.clear();
int i;
for (i=0; i<prob.size(); i++) {
result.push_back(floatToFixed_0_32(prob[i]/sum));
}
}
static void normProbs( std::vector<u32>& probs );
static void computeBiDist( std::vector<u32>& p, u32 n,
Distribution& dist, u32 aIdx, u32 bIdx );
// Since we are using fixed point math, we first implement
// a fixed 0.32 point inversion: 1/(n-1)
#define DIST_INV( n ) ( 0xFFFFFFFF/( (n) - 1 ))
KxuNuRand::KxuNuRand( const std::vector<double> prob,
KxuRandUniform* rand ){
std::vector<u32> fix_p;
prob_conv_fix_size(prob,fix_p);
init(fix_p, rand);
}
void KxuNuRand::init(const std::vector<u32>& dist,
KxuRandUniform* rand ){
mRand = rand;
if ( dist.size() == 1 ){
// handle a the special case of just one symbol.
mDist.push_back( Distribution( 0, 0, 0 ));
} else {
// The non-uniform distribution is passed in as an argument to the
// constructor. This is a series of integers in the desired proportions.
// Normalize these into a series of probabilities that sum to 1, expressed
// in 0.32 fixed point.
std::vector<u32> p; p = dist;
normProbs( p );
// Then we count up the number of non-zero probabilities so that we can
// figure out the number of distributions we need.
u32 numDistros = 0;
for ( u32 i = 0; i < p.size(); i++ )
if ( p[i] ) numDistros++;
if ( numDistros < 2 )
numDistros = 2;
u32 thresh = DIST_INV( numDistros );
// reserve space for the distributions.
mDist.resize( numDistros - 1 );
u32 aIdx = 0;
u32 bIdx = 0;
for ( u32 i = 0; i < mDist.size(); i++ ) {
// find a small prob, non-zero preferred
while ( aIdx < p.size()-1 ){
if (( p[aIdx] <= thresh ) && p[aIdx] ) break;
aIdx++;
}
assert (aIdx < p.size());
if ( p[aIdx] > thresh ){
aIdx = 0;
while ( aIdx < p.size()-1 ){
if ( p[aIdx] <= thresh ) break;
aIdx++;
}
}
assert (aIdx < p.size());
// find a prob that is not aIdx, and the sum is more than thresh.
while ( bIdx < p.size()-1 ){
if ( bIdx == aIdx ) { bIdx++; continue; } // can't be aIdx
assert (bIdx < p.size());
if ((( p[aIdx] >> 1 ) + ( p[bIdx] >> 1 )) >= ( thresh >> 1 )) break;
bIdx++; // find a sum big enough or equal
}
// We've selected 2 symbols, at indexes aIdx, and bIdx.
// This function will initialize a new binary distribution, and make
// the appropriate adjustments to the input non-uniform distribution.
computeBiDist( p, numDistros, mDist[i], aIdx, bIdx );
assert (bIdx < p.size());
if (( bIdx < aIdx ) && ( p[bIdx] < thresh ))
aIdx = bIdx;
else
aIdx++;
}
}
}
KxuNuRand::KxuNuRand( const std::vector<u32>& dist, KxuRandUniform* rand ){
init(dist, rand);
}
static void computeBiDist( std::vector<u32>& p, u32 n,
Distribution& dist, u32 aIdx, u32 bIdx ) {
dist.mA = aIdx;
dist.mB = bIdx;
if ( aIdx == bIdx ){
dist.mProb = 0;
} else {
if ((( p[aIdx] >> 1 ) * ( n - 1 )) >= 0x80000000 ){
dist.mProb = 0xFFFFFFFF;
}else{
dist.mProb = p[aIdx] * ( n - 1 );
}
p[bIdx] -= ( DIST_INV( n ) - p[aIdx] );
}
p[aIdx] = 0;
}
static bool tryDistributingErrorToBiggest(std::vector<u32>& probs){
/* Returns true if the error is smaller than the max prob and can be distributed
to it. False otherwise */
u32 max = 0;
u32 maxIdx = 0;
u32 sum = 0;
for (u32 i = 0; i < probs.size(); i++ ){
sum += probs[i];
if (probs[i] > max) {
max = probs[i];
maxIdx = i;
}
}
u32 error = -sum; //4G - sum
u32 AllowedError = max / 1000 ; // We permit an error of 0.1% of the maximum.
if (error < AllowedError) {
// Distribute the error to the max, it is indistinguishable (0.1%)
probs[maxIdx] += error;
return true;
}
return false;
}
static void distributeErrorRelatively(std::vector<u32>& probs){
u32 sum = 0;
u32 error = 0;
u32 errorPerEntry = 0;
for (u32 i = 0; i < probs.size(); i++) {
sum += probs[i];
}
error = - sum; // 4G - sum
for (u32 i = 0; i < probs.size(); i++) {
errorPerEntry = error * fixedToFloat_0_32(probs[i]);
probs[i] += errorPerEntry;
}
}
static void distributeOnesToNoError(std::vector<u32>& probs){
u32 sum = 0;
u32 error = 0;
for (u32 i = 0; i < probs.size(); i++) {
sum += probs[i];
}
error = - sum;
assert (error < probs.size());
while(error){
probs[--error]++;
}
}
static void normProbs( std::vector<u32>& probs ){
// how many non-zero probabilities?
u32 numNonZero = 0;
for ( u32 i = 0; i < probs.size(); i++ ){
if ( probs[i] ) numNonZero++;
}
if ( numNonZero == 0 ){
// degenerate all zero probability array.
// Can't do anything with it... result is undefined
assert(0);
return;
}
if ( numNonZero == 1 ){
// trivial case with only one real prob - handle special because
// computation would overflow below anyway.
for ( u32 i = 0; i < probs.size(); i++ ){
probs[i] = probs[i] ? U32_MAX : 0;
}
return;
}
if(!tryDistributingErrorToBiggest(probs)) {
distributeErrorRelatively(probs);
if (!tryDistributingErrorToBiggest(probs)) {
distributeOnesToNoError(probs);
}
}
// The sum of the probabilities in a probability distribution is always 1, in our case 4G = 0.
uint64_t sum = 0;
uint64_t fourGiga = 0x100000000; // 2^32
for ( u32 i = 0; i < probs.size(); i++ ) {
sum += probs[i];
}
assert (sum == fourGiga);
}
void Kx_norm_prob(std::vector<double> prob,
std::vector<double> & result ){
double sum=0.0;
for (auto& n : prob)
sum += n;
result.clear();
int i;
for (i=0; i<prob.size(); i++) {
result.push_back(prob[i]/sum);
}
}
void Kx_dump_prob(std::vector<double> prob){
int i;
for (i=0; i<prob.size(); i++) {
printf(" %f \n", prob[i]*100.0);
}
}