Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'random_readwritestate' into 'master'
authorYann Duplouy <duplouy@crans.org>
Mon, 27 Apr 2020 17:09:07 +0000 (19:09 +0200)
committerYann Duplouy <duplouy@crans.org>
Mon, 27 Apr 2020 17:09:07 +0000 (19:09 +0200)
[xbt/random] Read/Write the state of the RNG

See merge request simgrid/simgrid!36

include/xbt/random.hpp
src/xbt/random.cpp
src/xbt/random_test.cpp

index 26492e2..81572c7 100644 (file)
@@ -7,7 +7,10 @@
 #define SIMGRID_XBT_RANDOM_HPP
 
 #include "xbt/base.h"
+#include <fstream>
+#include <iostream>
 #include <random>
+#include <string>
 
 namespace simgrid {
 namespace xbt {
@@ -34,6 +37,16 @@ public:
    */
   void set_seed(int seed) { mt19937_gen.seed(seed); }
 
+  /**
+   * @brief Read the state of the Mersenne-Twister RNG from a file
+   */
+  bool read_state(std::string filename);
+
+  /**
+   * @brief Write the state of the Mersenne-Twister RNG to a file
+   */
+  bool write_state(std::string filename);
+
   /**
    * @brief Draws an integer number uniformly in range [min, max] (min and max included)
    *
@@ -111,6 +124,16 @@ void set_implem_std();
  */
 void set_mersenne_seed(int);
 
+/**
+ * @brief Read the state of the Mersenne-Twister RNG from a file.
+ */
+bool read_mersenne_state(std::string filename);
+
+/**
+ * @brief Write the state of the Mersenne-Twister RNG to a file.
+ */
+bool write_mersenne_state(std::string filename);
+
 /**
  * @brief Draws an integer number uniformly in range [min, max] (min and max included)
  *
index dd4b3db..539340b 100644 (file)
@@ -3,15 +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. */
 
-#include "xbt/random.hpp"
 #include "xbt/asserts.h"
+#include <fstream>
+#include <iostream>
 #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 {
 
+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 true;
+  } 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);
@@ -92,6 +123,16 @@ void set_mersenne_seed(int seed)
   default_random->set_seed(seed);
 }
 
+bool read_mersenne_state(std::string filename)
+{
+  return default_random->read_state(filename);
+}
+
+bool write_mersenne_state(std::string filename)
+{
+  return default_random->write_state(filename);
+}
+
 int uniform_int(int min, int max)
 {
   return default_random->uniform_int(min, max);
index a35d580..9b66134 100644 (file)
@@ -40,4 +40,23 @@ TEST_CASE("xbt::random: Random Number Generation")
     REQUIRE_THAT(simgrid::xbt::random::uniform_real(0, 1), EpsilonApprox(distC(gen)));
     REQUIRE_THAT(simgrid::xbt::random::normal(0, 2), EpsilonApprox(distD(gen)));
   }
+
+  SECTION("XBT_RNG_std write to a file")
+  {
+    simgrid::xbt::random::set_implem_std();
+    simgrid::xbt::random::set_mersenne_seed(12345);
+
+    simgrid::xbt::random::exponential(25);
+    bool writtenA = simgrid::xbt::random::write_mersenne_state("rdm_state_tmp.txt");
+    double resB = simgrid::xbt::random::uniform_real(10, 20);
+    double resC = simgrid::xbt::random::normal(0, 2);
+    bool writtenB = simgrid::xbt::random::read_mersenne_state("rdm_state_tmp.txt");
+    REQUIRE(writtenA);
+    REQUIRE(writtenB);
+    REQUIRE_THAT(simgrid::xbt::random::uniform_real(10, 20), EpsilonApprox(resB));
+    REQUIRE_THAT(simgrid::xbt::random::normal(0, 2), EpsilonApprox(resC));
+    if (writtenB) {
+      std::remove("rdm_state_tmp.txt");
+    }
+  }
 }