A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rng-stream.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (C) 2001 Pierre L'Ecuyer (lecuyer@iro.umontreal.ca)
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Modified for ns-3 by:
19 // - Rajib Bhattacharjea<raj.b@gatech.edu>
20 // - Mathieu Lacage <mathieu.lacage@gmail.com>
21 //
22 
23 #include <cstdlib>
24 #include <iostream>
25 #include "rng-stream.h"
26 #include "fatal-error.h"
27 #include "log.h"
28 
29 // Note: Logging in this file is largely avoided due to the
30 // number of calls that are made to these functions and the possibility
31 // of causing recursions leading to stack overflow
32 
33 NS_LOG_COMPONENT_DEFINE ("RngStream");
34 
35 namespace
36 {
37 typedef double Matrix[3][3];
38 
39 const double m1 = 4294967087.0;
40 const double m2 = 4294944443.0;
41 const double norm = 1.0 / (m1 + 1.0);
42 const double a12 = 1403580.0;
43 const double a13n = 810728.0;
44 const double a21 = 527612.0;
45 const double a23n = 1370589.0;
46 const double two17 = 131072.0;
47 const double two53 = 9007199254740992.0;
48 
49 const Matrix A1p0 = {
50  { 0.0, 1.0, 0.0 },
51  { 0.0, 0.0, 1.0 },
52  { -810728.0, 1403580.0, 0.0 }
53 };
54 
55 const Matrix A2p0 = {
56  { 0.0, 1.0, 0.0 },
57  { 0.0, 0.0, 1.0 },
58  { -1370589.0, 0.0, 527612.0 }
59 };
60 
61 
62 //-------------------------------------------------------------------------
63 // Return (a*s + c) MOD m; a, s, c and m must be < 2^35
64 //
65 double MultModM (double a, double s, double c, double m)
66 {
67  double v;
68  int32_t a1;
69 
70  v = a * s + c;
71 
72  if (v >= two53 || v <= -two53)
73  {
74  a1 = static_cast<int32_t> (a / two17);
75  a -= a1 * two17;
76  v = a1 * s;
77  a1 = static_cast<int32_t> (v / m);
78  v -= a1 * m;
79  v = v * two17 + a * s + c;
80  }
81 
82  a1 = static_cast<int32_t> (v / m);
83  /* in case v < 0)*/
84  if ((v -= a1 * m) < 0.0)
85  {
86  return v += m;
87  }
88  else
89  {
90  return v;
91  }
92 }
93 
94 
95 //-------------------------------------------------------------------------
96 // Compute the vector v = A*s MOD m. Assume that -m < s[i] < m.
97 // Works also when v = s.
98 //
99 void MatVecModM (const Matrix A, const double s[3], double v[3],
100  double m)
101 {
102  int i;
103  double x[3]; // Necessary if v = s
104 
105  for (i = 0; i < 3; ++i)
106  {
107  x[i] = MultModM (A[i][0], s[0], 0.0, m);
108  x[i] = MultModM (A[i][1], s[1], x[i], m);
109  x[i] = MultModM (A[i][2], s[2], x[i], m);
110  }
111  for (i = 0; i < 3; ++i)
112  {
113  v[i] = x[i];
114  }
115 }
116 
117 
118 //-------------------------------------------------------------------------
119 // Compute the matrix C = A*B MOD m. Assume that -m < s[i] < m.
120 // Note: works also if A = C or B = C or A = B = C.
121 //
122 void MatMatModM (const Matrix A, const Matrix B,
123  Matrix C, double m)
124 {
125  int i, j;
126  double V[3];
127  Matrix W;
128 
129  for (i = 0; i < 3; ++i)
130  {
131  for (j = 0; j < 3; ++j)
132  {
133  V[j] = B[j][i];
134  }
135  MatVecModM (A, V, V, m);
136  for (j = 0; j < 3; ++j)
137  {
138  W[j][i] = V[j];
139  }
140  }
141  for (i = 0; i < 3; ++i)
142  {
143  for (j = 0; j < 3; ++j)
144  {
145  C[i][j] = W[i][j];
146  }
147  }
148 }
149 
150 
151 //-------------------------------------------------------------------------
152 // Compute the matrix B = (A^(2^e) Mod m); works also if A = B.
153 //
154 void MatTwoPowModM (const Matrix src, Matrix dst, double m, int32_t e)
155 {
156  int i, j;
157 
158  /* initialize: dst = src */
159  for (i = 0; i < 3; ++i)
160  {
161  for (j = 0; j < 3; ++j)
162  {
163  dst[i][j] = src[i][j];
164  }
165  }
166  /* Compute dst = src^(2^e) mod m */
167  for (i = 0; i < e; i++)
168  {
169  MatMatModM (dst, dst, dst, m);
170  }
171 }
172 
173 
174 //-------------------------------------------------------------------------
175 // Compute the matrix B = (A^n Mod m); works even if A = B.
176 //
177 /*
178 void MatPowModM (const double A[3][3], double B[3][3], double m, int32_t n)
179 {
180  NS_LOG_FUNCTION (A << B << m << n);
181  int i, j;
182  double W[3][3];
183 
184  // initialize: W = A; B = I
185  for (i = 0; i < 3; ++i)
186  {
187  for (j = 0; j < 3; ++j)
188  {
189  W[i][j] = A[i][j];
190  B[i][j] = 0.0;
191  }
192  }
193  for (j = 0; j < 3; ++j)
194  {
195  B[j][j] = 1.0;
196  }
197 
198  // Compute B = A^n mod m using the binary decomposition of n
199  while (n > 0)
200  {
201  if (n % 2)
202  {
203  MatMatModM (W, B, B, m);
204  }
205  MatMatModM (W, W, W, m);
206  n /= 2;
207  }
208 }
209 */
210 
211 // The following are the transition matrices of the two MRG components
212 // (in matrix form), raised to all powers of 2 from 1 to 191
214 {
215  Matrix a1[190];
216  Matrix a2[190];
217 };
219 {
220  struct Precalculated precalculated;
221  for (int i = 0; i < 190; i++)
222  {
223  int power = i + 1;
224  MatTwoPowModM (A1p0, precalculated.a1[i], m1, power);
225  MatTwoPowModM (A2p0, precalculated.a2[i], m2, power);
226  }
227  return precalculated;
228 }
229 void PowerOfTwoMatrix (int n, Matrix a1p, Matrix a2p)
230 {
231  static struct Precalculated constants = PowerOfTwoConstants ();
232  for (int i = 0; i < 3; i ++)
233  {
234  for (int j = 0; j < 3; j++)
235  {
236  a1p[i][j] = constants.a1[n-1][i][j];
237  a2p[i][j] = constants.a2[n-1][i][j];
238  }
239  }
240 }
241 
242 } // end of anonymous namespace
243 
244 
245 namespace ns3 {
246 //-------------------------------------------------------------------------
247 // Generate the next random number.
248 //
250 {
251  int32_t k;
252  double p1, p2, u;
253 
254  /* Component 1 */
255  p1 = a12 * m_currentState[1] - a13n * m_currentState[0];
256  k = static_cast<int32_t> (p1 / m1);
257  p1 -= k * m1;
258  if (p1 < 0.0)
259  {
260  p1 += m1;
261  }
262  m_currentState[0] = m_currentState[1]; m_currentState[1] = m_currentState[2]; m_currentState[2] = p1;
263 
264  /* Component 2 */
265  p2 = a21 * m_currentState[5] - a23n * m_currentState[3];
266  k = static_cast<int32_t> (p2 / m2);
267  p2 -= k * m2;
268  if (p2 < 0.0)
269  {
270  p2 += m2;
271  }
272  m_currentState[3] = m_currentState[4]; m_currentState[4] = m_currentState[5]; m_currentState[5] = p2;
273 
274  /* Combination */
275  u = ((p1 > p2) ? (p1 - p2) * norm : (p1 - p2 + m1) * norm);
276 
277  return u;
278 }
279 
280 RngStream::RngStream (uint32_t seedNumber, uint64_t stream, uint64_t substream)
281 {
282  if (seedNumber >= m1 || seedNumber >= m2 || seedNumber == 0)
283  {
284  NS_FATAL_ERROR ("invalid Seed " << seedNumber);
285  }
286  for (int i = 0; i < 6; ++i)
287  {
288  m_currentState[i] = seedNumber;
289  }
290  AdvanceNthBy (stream, 127, m_currentState);
291  AdvanceNthBy (substream, 76, m_currentState);
292 }
293 
295 {
296  for (int i = 0; i < 6; ++i)
297  {
298  m_currentState[i] = r.m_currentState[i];
299  }
300 }
301 
302 void
303 RngStream::AdvanceNthBy (uint64_t nth, int by, double state[6])
304 {
305  Matrix matrix1,matrix2;
306  for (int i = 0; i < 64; i++)
307  {
308  int nbit = 63 - i;
309  int bit = (nth >> nbit) & 0x1;
310  if (bit)
311  {
312  PowerOfTwoMatrix(by + nbit, matrix1, matrix2);
313  MatVecModM (matrix1, state, state, m1);
314  MatVecModM (matrix2, &state[3], &state[3], m2);
315  }
316  }
317 }
318 
319 } // namespace ns3
void MatTwoPowModM(const Matrix src, Matrix dst, double m, int32_t e)
Definition: rng-stream.cc:154
void MatVecModM(const Matrix A, const double s[3], double v[3], double m)
Definition: rng-stream.cc:99
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
double MultModM(double a, double s, double c, double m)
Definition: rng-stream.cc:65
Combined Multiple-Recursive Generator MRG32k3a.
Definition: rng-stream.h:38
Ptr< SampleEmitter > s
void AdvanceNthBy(uint64_t nth, int by, double state[6])
Definition: rng-stream.cc:303
double m_currentState[6]
Definition: rng-stream.h:52
RngStream(uint32_t seed, uint64_t stream, uint64_t substream)
Definition: rng-stream.cc:280
struct Precalculated PowerOfTwoConstants(void)
Definition: rng-stream.cc:218
double RandU01(void)
Generate the next random number for this stream.
Definition: rng-stream.cc:249
void PowerOfTwoMatrix(int n, Matrix a1p, Matrix a2p)
Definition: rng-stream.cc:229
void MatMatModM(const Matrix A, const Matrix B, Matrix C, double m)
Definition: rng-stream.cc:122