From 43593083f5e507b563bddc7d8d9c6022a9d7fab4 Mon Sep 17 00:00:00 2001 From: Yann Duplouy Date: Mon, 27 Apr 2020 09:53:58 +0200 Subject: [PATCH 1/1] Tries to handle IO errors --- include/xbt/random.hpp | 20 ++++++-------------- src/xbt/random.cpp | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/include/xbt/random.hpp b/include/xbt/random.hpp index 83c8cbf240..81572c78ff 100644 --- a/include/xbt/random.hpp +++ b/include/xbt/random.hpp @@ -40,20 +40,12 @@ public: /** * @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 */ - 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) @@ -133,14 +125,14 @@ void set_implem_std(); 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) diff --git a/src/xbt/random.cpp b/src/xbt/random.cpp index 3b2962fb85..004418eadd 100644 --- a/src/xbt/random.cpp +++ b/src/xbt/random.cpp @@ -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. */ -#include "xbt/random.hpp" #include "xbt/asserts.h" +#include +#include #include #include #include +#include +#include + +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 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); @@ -93,14 +123,14 @@ void set_mersenne_seed(int 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) -- 2.20.1