Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make HostL07 behave more like the regular Host
[simgrid.git] / src / surf / random_mgr.c
index ebc698d..74210ed 100644 (file)
@@ -1,18 +1,25 @@
-/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2007-2014. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
 * under the terms of the license (GNU LGPL) which comes with this package. */
+ * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "surf/random_mgr.h"
 #include "xbt/sysdep.h"
+#include "src/internal_config.h" /*_XBT_WIN32*/
+#include <math.h>
+#include <stdlib.h>
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(random, surf, "Random part of surf");
 
 #ifdef _XBT_WIN32
 
 static unsigned int _seed = 2147483647;
 
+#ifdef __VISUALC__
 typedef unsigned __int64 uint64_t;
 typedef unsigned int uint32_t;
+#endif
 
 struct drand48_data {
   unsigned short int __x[3];    /* Current state.  */
@@ -49,7 +56,7 @@ union ieee754_double {
   } ieee_nan;
 };
 
-#define IEEE754_DOUBLE_BIAS    0x3ff   /* Added to exponent.  */
+#define IEEE754_DOUBLE_BIAS  0x3ff   /* Added to exponent.  */
 
 double drand48(void);
 
@@ -85,7 +92,8 @@ _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
   return 0;
 }
 
-int _drand48_iterate(unsigned short int xsubi[3], struct drand48_data *buffer)
+int _drand48_iterate(unsigned short int xsubi[3],
+                     struct drand48_data *buffer)
 {
   uint64_t X;
   uint64_t result;
@@ -119,7 +127,8 @@ double _drand48(void)
 {
   double result;
 
-  (void) _erand48_r(__libc_drand48_data.__x, &__libc_drand48_data, &result);
+  (void) _erand48_r(__libc_drand48_data.__x, &__libc_drand48_data,
+                    &result);
 
   return result;
 }
@@ -184,7 +193,7 @@ int _rand_r(unsigned int *pseed)
 
 #endif
 
-static double custom_random(Generator generator, long int *seed)
+static double custom_random(e_random_generator_t generator, long int *seed)
 {
   switch (generator) {
 
@@ -192,6 +201,9 @@ static double custom_random(Generator generator, long int *seed)
     return drand48();
   case RAND:
     return (double) rand_r((unsigned int *) seed) / RAND_MAX;
+  case RNGSTREAM :
+    XBT_INFO("Seen RNGSTREAM");
+    return 0.0;
   default:
     return drand48();
   }
@@ -210,14 +222,12 @@ double random_generate(random_data_t random)
   if (random->std == 0)
     return random->mean * (random->max - random->min) + random->min;
 
-  a =
-    random->mean * (random->mean * (1 - random->mean) /
-                    (random->std * random->std) - 1);
-  b =
-    (1 -
-     random->mean) * (random->mean * (1 -
-                                      random->mean) / (random->std *
-                                                       random->std) - 1);
+  a = random->mean * (random->mean * (1 - random->mean) /
+                      (random->std * random->std) - 1);
+  b = (1 -
+       random->mean) * (random->mean * (1 -
+                                        random->mean) / (random->std *
+                                                         random->std) - 1);
 
   alpha = a + b;
   if (a <= 1. || b <= 1.)
@@ -244,7 +254,7 @@ double random_generate(random_data_t random)
   return X * (random->max - random->min) + random->min;
 }
 
-random_data_t random_new(Generator generator, long int seed,
+random_data_t random_new(e_random_generator_t generator, long int seed,
                          double min, double max, double mean, double std)
 {
   random_data_t random = xbt_new0(s_random_data_t, 1);
@@ -256,18 +266,20 @@ random_data_t random_new(Generator generator, long int seed,
 
   /* Check user stupidities */
   if (max < min)
-    THROW2(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
+    THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
   if (mean < min)
-    THROW2(arg_error, 0, "random->mean < random->min (%f < %f)", mean, min);
+    THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
+           min);
   if (mean > max)
-    THROW2(arg_error, 0, "random->mean > random->max (%f > %f)", mean, max);
+    THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
+           max);
 
   /* normalize the mean and standard deviation before storing */
   random->mean = (mean - min) / (max - min);
   random->std = std / (max - min);
 
   if (random->mean * (1 - random->mean) < random->std * random->std)
-    THROW2(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
+    THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
            random->mean, random->std);
 
   return random;