Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Tries to handle IO errors
authorYann Duplouy <yann.duplouy@inria.fr>
Mon, 27 Apr 2020 07:53:58 +0000 (09:53 +0200)
committerYann Duplouy <yann.duplouy@inria.fr>
Mon, 27 Apr 2020 07:53:58 +0000 (09:53 +0200)
include/xbt/random.hpp
src/xbt/random.cpp

index 83c8cbf..81572c7 100644 (file)
@@ -40,20 +40,12 @@ public:
   /**
    * @brief Read the state of the Mersenne-Twister RNG from a file
    */
   /**
    * @brief Read the state of the Mersenne-Twister RNG from a file
    */
-  void read_state(std::string filename)
-  {
-    std::ifstream file(filename);
-    file >> mt19937_gen;
-  }
+  bool read_state(std::string filename);
 
   /**
    * @brief Write the state of the Mersenne-Twister RNG to a file
    */
 
   /**
    * @brief Write the state of the Mersenne-Twister RNG to a file
    */
-  void write_state(std::string filename)
-  {
-    std::ofstream file(filename);
-    file << mt19937_gen;
-  }
+  bool write_state(std::string filename);
 
   /**
    * @brief Draws an integer number uniformly in range [min, max] (min and max included)
 
   /**
    * @brief Draws an integer number uniformly in range [min, max] (min and max included)
@@ -133,14 +125,14 @@ void set_implem_std();
 void set_mersenne_seed(int);
 
 /**
 void set_mersenne_seed(int);
 
 /**
- * @brief Read the state of the Mersenne-Twister RNG from a file
+ * @brief Read the state of the Mersenne-Twister RNG from a file.
  */
  */
-void read_mersenne_state(std::string filename);
+bool read_mersenne_state(std::string filename);
 
 /**
 
 /**
- * @brief Write the state of the Mersenne-Twister RNG to a file
+ * @brief Write the state of the Mersenne-Twister RNG to a file.
  */
  */
-void write_mersenne_state(std::string filename);
+bool write_mersenne_state(std::string filename);
 
 /**
  * @brief Draws an integer number uniformly in range [min, max] (min and max included)
 
 /**
  * @brief Draws an integer number uniformly in range [min, max] (min and max included)
index 3b2962f..004418e 100644 (file)
@@ -3,16 +3,46 @@
 /* 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. */
 
 /* 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. */
 
-#include "xbt/random.hpp"
 #include "xbt/asserts.h"
 #include "xbt/asserts.h"
+#include <fstream>
+#include <iostream>
 #include <limits>
 #include <memory>
 #include <string>
 #include <limits>
 #include <memory>
 #include <string>
+#include <xbt/log.hpp>
+#include <xbt/random.hpp>
+
+XBT_LOG_EXTERNAL_CATEGORY(xbt);
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_random, xbt, "Random");
 
 namespace simgrid {
 namespace xbt {
 namespace random {
 
 
 namespace simgrid {
 namespace xbt {
 namespace random {
 
+bool Random::read_state(std::string filename)
+{
+  std::ifstream file(filename);
+  if (file) {
+    file >> mt19937_gen;
+    return true;
+  } else {
+    XBT_WARN("Could not open %s and thus not save the RNG state.", filename.c_str());
+    return false;
+  }
+}
+
+bool Random::write_state(std::string filename)
+{
+  std::ofstream file(filename);
+  if (file) {
+    file << mt19937_gen;
+    return false;
+  } else {
+    XBT_WARN("Could not open %s and thus not read the RNG state.", filename.c_str());
+    return false;
+  }
+}
+
 int StdRandom::uniform_int(int min, int max)
 {
   std::uniform_int_distribution<> dist(min, max);
 int StdRandom::uniform_int(int min, int max)
 {
   std::uniform_int_distribution<> dist(min, max);
@@ -93,14 +123,14 @@ void set_mersenne_seed(int seed)
   default_random->set_seed(seed);
 }
 
   default_random->set_seed(seed);
 }
 
-void read_mersenne_state(std::string filename)
+bool read_mersenne_state(std::string filename)
 {
 {
-  default_random->read_state(filename);
+  return default_random->read_state(filename);
 }
 
 }
 
-void write_mersenne_state(std::string filename)
+bool write_mersenne_state(std::string filename)
 {
 {
-  default_random->write_state(filename);
+  return default_random->write_state(filename);
 }
 
 int uniform_int(int min, int max)
 }
 
 int uniform_int(int min, int max)