Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
isolating latency bounded functions with ifdef's
[simgrid.git] / src / surf / random_mgr.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf/random_mgr.h"
8 #include "xbt/sysdep.h"
9 #include "simgrid_config.h" /*_XBT_WIN32*/
10
11 #ifdef _XBT_WIN32
12
13 static unsigned int _seed = 2147483647;
14
15 #ifdef __VISUALC__
16 typedef unsigned __int64 uint64_t;
17 typedef unsigned int uint32_t;
18 #endif
19
20 struct drand48_data {
21   unsigned short int __x[3];    /* Current state.  */
22   unsigned short int __old_x[3];        /* Old state.  */
23   unsigned short int __c;       /* Additive const. in congruential formula.  */
24   unsigned short int __init;    /* Flag for initializing.  */
25   unsigned long long int __a;   /* Factor in congruential formula.  */
26 };
27
28 static struct drand48_data __libc_drand48_data = { 0 };
29
30 union ieee754_double {
31   double d;
32
33   /* This is the IEEE 754 double-precision format.  */
34   struct {
35     /* Together these comprise the mantissa.  */
36     unsigned int mantissa1:32;
37     unsigned int mantissa0:20;
38     unsigned int exponent:11;
39     unsigned int negative:1;
40     /* Little endian.  */
41   } ieee;
42
43   /* This format makes it easier to see if a NaN is a signalling NaN.  */
44   struct {
45     /* Together these comprise the mantissa.  */
46     unsigned int mantissa1:32;
47     unsigned int mantissa0:19;
48     unsigned int quiet_nan:1;
49     unsigned int exponent:11;
50     unsigned int negative:1;
51
52   } ieee_nan;
53 };
54
55 #define IEEE754_DOUBLE_BIAS     0x3ff   /* Added to exponent.  */
56
57 double drand48(void);
58
59 int
60 _drand48_iterate(unsigned short int xsubi[3], struct drand48_data *buffer);
61
62 int
63 _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
64            double *result);
65
66
67 int
68 _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
69            double *result)
70 {
71   union ieee754_double temp;
72
73   /* Compute next state.  */
74   if (_drand48_iterate(xsubi, buffer) < 0)
75     return -1;
76
77   /* Construct a positive double with the 48 random bits distributed over
78      its fractional part so the resulting FP number is [0.0,1.0).  */
79
80   temp.ieee.negative = 0;
81   temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
82   temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
83   temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);
84
85   /* Please note the lower 4 bits of mantissa1 are always 0.  */
86   *result = temp.d - 1.0;
87
88   return 0;
89 }
90
91 int _drand48_iterate(unsigned short int xsubi[3], struct drand48_data *buffer)
92 {
93   uint64_t X;
94   uint64_t result;
95
96   /* Initialize buffer, if not yet done.  */
97
98   if (buffer->__init == 0) {
99     buffer->__a = 0x5deece66dull;
100     buffer->__c = 0xb;
101     buffer->__init = 1;
102   }
103
104   /* Do the real work.  We choose a data type which contains at least
105      48 bits.  Because we compute the modulus it does not care how
106      many bits really are computed.  */
107
108   X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0];
109
110   result = X * buffer->__a + buffer->__c;
111
112
113   xsubi[0] = result & 0xffff;
114   xsubi[1] = (result >> 16) & 0xffff;
115   xsubi[2] = (result >> 32) & 0xffff;
116
117   return 0;
118 }
119
120
121 double _drand48(void)
122 {
123   double result;
124
125   (void) _erand48_r(__libc_drand48_data.__x, &__libc_drand48_data, &result);
126
127   return result;
128 }
129
130 void _srand(unsigned int seed)
131 {
132   _seed = seed;
133 }
134
135 int _rand(void)
136 {
137   const long a = 16807;
138   const long m = 2147483647;
139   const long q = 127773;        /* (m/a) */
140   const long r = 2836;          /* (m%a) */
141
142   long lo, k, s;
143
144   s = (long) _seed;
145
146   k = (long) (s / q);
147
148   lo = (s - q * k);
149
150   s = a * lo - r * k;
151
152   if (s <= 0)
153     s += m;
154
155   _seed = (int) (s & RAND_MAX);
156
157   return _seed;
158 }
159
160 int _rand_r(unsigned int *pseed)
161 {
162   const long a = 16807;
163   const long m = 2147483647;
164   const long q = 127773;        /* (m/a) */
165   const long r = 2836;          /* (m%a) */
166
167   long lo, k, s;
168
169   s = (long) *pseed;
170
171   k = (long) (s / q);
172
173   lo = (s - q * k);
174
175   s = a * lo - r * k;
176
177   if (s <= 0)
178     s += m;
179
180   return (int) (s & RAND_MAX);
181
182 }
183
184
185 #define rand_r _rand_r
186 #define drand48 _drand48
187
188 #endif
189
190 static double custom_random(Generator generator, long int *seed)
191 {
192   switch (generator) {
193
194   case DRAND48:
195     return drand48();
196   case RAND:
197     return (double) rand_r((unsigned int *) seed) / RAND_MAX;
198   default:
199     return drand48();
200   }
201 }
202
203 /* Generate numbers between min and max with a given mean and standard deviation */
204 double random_generate(random_data_t random)
205 {
206   double a, b;
207   double alpha, beta, gamma;
208   double U1, U2, V, W, X;
209
210   if (random == NULL)
211     return 0.0f;
212
213   if (random->std == 0)
214     return random->mean * (random->max - random->min) + random->min;
215
216   a =
217     random->mean * (random->mean * (1 - random->mean) /
218                     (random->std * random->std) - 1);
219   b =
220     (1 -
221      random->mean) * (random->mean * (1 -
222                                       random->mean) / (random->std *
223                                                        random->std) - 1);
224
225   alpha = a + b;
226   if (a <= 1. || b <= 1.)
227     beta = ((1. / a) > (1. / b)) ? (1. / a) : (1. / b);
228   else
229     beta = sqrt((alpha - 2.) / (2. * a * b - alpha));
230   gamma = a + 1. / beta;
231
232   do {
233     /* Random generation for the Beta distribution based on
234      *   R. C. H. Cheng (1978). Generating beta variates with nonintegral shape parameters. _Communications of the ACM_, *21*, 317-322.
235      *   It is good for speed because it does not call math functions many times and respect the 4 given constraints
236      */
237     U1 = custom_random(random->generator, &(random->seed));
238     U2 = custom_random(random->generator, &(random->seed));
239
240     V = beta * log(U1 / (1 - U1));
241     W = a * exp(V);
242   } while (alpha * log(alpha / (b + W)) + gamma * V - log(4) <
243            log(U1 * U1 * U2));
244
245   X = W / (b + W);
246
247   return X * (random->max - random->min) + random->min;
248 }
249
250 random_data_t random_new(Generator generator, long int seed,
251                          double min, double max, double mean, double std)
252 {
253   random_data_t random = xbt_new0(s_random_data_t, 1);
254
255   random->generator = generator;
256   random->seed = seed;
257   random->min = min;
258   random->max = max;
259
260   /* Check user stupidities */
261   if (max < min)
262     THROW2(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
263   if (mean < min)
264     THROW2(arg_error, 0, "random->mean < random->min (%f < %f)", mean, min);
265   if (mean > max)
266     THROW2(arg_error, 0, "random->mean > random->max (%f > %f)", mean, max);
267
268   /* normalize the mean and standard deviation before storing */
269   random->mean = (mean - min) / (max - min);
270   random->std = std / (max - min);
271
272   if (random->mean * (1 - random->mean) < random->std * random->std)
273     THROW2(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
274            random->mean, random->std);
275
276   return random;
277 }