Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Hide cfg_latency_factor and bw, and change them into smpi::utils::FactorSet
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 22 Oct 2022 21:36:36 +0000 (23:36 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 22 Oct 2022 22:30:02 +0000 (00:30 +0200)
src/instr/instr_platform.cpp
src/kernel/resource/NetworkModel.cpp
src/kernel/resource/NetworkModel.hpp
src/smpi/include/smpi_utils.hpp
src/smpi/internals/smpi_utils.cpp
src/surf/network_constant.cpp
src/xbt/xbt_str.cpp

index fc0117d..b37ed60 100644 (file)
@@ -450,7 +450,10 @@ void define_callbacks()
     s4u::Link::on_bandwidth_change_cb([](s4u::Link const& link) {
       Container::by_name(link.get_name())
           ->get_variable("bandwidth")
-          ->set_event(simgrid_get_clock(), kernel::resource::NetworkModel::cfg_bandwidth_factor * link.get_bandwidth());
+          ->set_event(
+              simgrid_get_clock(),
+              static_cast<kernel::resource::NetworkModel*>(link.get_impl()->get_model())->get_bandwidth_factor() *
+                  link.get_bandwidth());
     });
     s4u::NetZone::on_seal_cb([](s4u::NetZone const& /*netzone*/) { currentContainer.pop_back(); });
     kernel::routing::NetPoint::on_creation.connect([](kernel::routing::NetPoint const& netpoint) {
index ca437ce..4573367 100644 (file)
@@ -8,6 +8,7 @@
 #include "simgrid/sg_config.hpp"
 #include "src/kernel/resource/NetworkModel.hpp"
 #include "src/kernel/resource/profile/Profile.hpp"
+#include "src/smpi/include/smpi_utils.hpp"
 #include "src/surf/surf_interface.hpp"
 
 #include <numeric>
@@ -22,23 +23,30 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_network, ker_resource, "Network resources, t
  *********/
 
 namespace simgrid::kernel::resource {
-double NetworkModel::cfg_latency_factor   = 1.0; // default value
-double NetworkModel::cfg_bandwidth_factor = 1.0; // default value
+static smpi::utils::FactorSet cfg_latency_factor("network/latency-factor");
+static smpi::utils::FactorSet cfg_bandwidth_factor("network/bandwidth-factor");
 
-static config::Flag<std::string> cfg_latency_factor_str(
+config::Flag<std::string> cfg_latency_factor_str(
     "network/latency-factor",
-    "Correction factor to apply to the provided latency (default value overridden by network model)", "1.0",
-    [](std::string str) {
-      NetworkModel::cfg_latency_factor =
-          xbt_str_parse_double(str.c_str(), "The value of 'network/latency-factor' is not a double");
-    });
+    "Correction factor to apply to the provided latency (default value overridden by network model)", "1.0");
 static config::Flag<std::string> cfg_bandwidth_factor_str(
     "network/bandwidth-factor",
-    "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0",
-    [](std::string str) {
-      NetworkModel::cfg_bandwidth_factor =
-          xbt_str_parse_double(str.c_str(), "The value of 'network/bandwidth-factor' is not a double");
-    });
+    "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0");
+
+double NetworkModel::get_latency_factor(double size)
+{
+  if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
+    cfg_latency_factor.parse(cfg_latency_factor_str.get());
+
+  return cfg_latency_factor(size);
+}
+double NetworkModel::get_bandwidth_factor(double size)
+{
+  if (not cfg_bandwidth_factor.is_initialized())
+    cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
+
+  return cfg_bandwidth_factor(size);
+}
 
 /** @brief Command-line option 'network/TCP-gamma' -- see @ref options_model_network_gamma */
 config::Flag<double> NetworkModel::cfg_tcp_gamma(
index 7740382..515c0ef 100644 (file)
@@ -26,8 +26,6 @@ class NetworkModel : public Model, public NetworkModelIntf {
 public:
   static config::Flag<double> cfg_tcp_gamma;
   static config::Flag<bool> cfg_crosstraffic;
-  static double cfg_latency_factor;
-  static double cfg_bandwidth_factor;
   static config::Flag<double> cfg_weight_S_parameter;
 
   using Model::Model;
@@ -66,7 +64,7 @@ public:
    * @param size The size of the message.
    * @return The latency factor.
    */
-  virtual double get_latency_factor(double /* size */) { return cfg_latency_factor; }
+  virtual double get_latency_factor(double size = 0);
 
   /**
    * @brief Get the right multiplicative factor for the bandwidth.
@@ -77,7 +75,7 @@ public:
    * @param size The size of the message.
    * @return The bandwidth factor.
    */
-  virtual double get_bandwidth_factor(double /* size*/) { return cfg_bandwidth_factor; }
+  virtual double get_bandwidth_factor(double size = 0);
 
   double next_occurring_event_full(double now) override;
 
index 1b30f58..b5dc550 100644 (file)
@@ -36,7 +36,7 @@ public:
       const std::string& name, double default_value = 1,
       std::function<double(std::vector<double> const&, double)> const& lambda = [](std::vector<double> const& values,
                                                                                    double) { return values.front(); });
-  void parse(const std::string& values);
+  void parse(const std::string& string_values);
   bool is_initialized() const { return initialized_; }
   // Get the default value
   double operator()();
index d8c1a34..d023d9b 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (c) 2016-2022. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2016-2022. 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. */
@@ -9,10 +8,11 @@
 #include "private.hpp"
 #include "smpi_config.hpp"
 #include "src/surf/xml/platf.hpp"
+#include "xbt/ex.h"
 #include "xbt/file.hpp"
 #include "xbt/log.h"
-#include "xbt/ex.h"
 #include "xbt/parse_units.hpp"
+#include "xbt/str.h"
 #include "xbt/sysdep.h"
 #include <algorithm>
 #include <boost/tokenizer.hpp>
@@ -51,6 +51,14 @@ std::unordered_map<int, std::vector<std::string>> collective_calls;
 
 void FactorSet::parse(const std::string& values)
 {
+  const char* str = values.c_str();
+  initialized_    = true;
+
+  if (strchr(str, ':') == nullptr && strchr(str, ';') == nullptr) { // Single value
+    default_value_ = xbt_str_parse_double(str, name_.c_str());
+    return;
+  }
+
   /** Setup the tokenizer that parses the string **/
   using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
   boost::char_separator<char> sep(";");
@@ -68,7 +76,8 @@ void FactorSet::parse(const std::string& values)
     XBT_DEBUG("token: %s", token_iter->c_str());
     Tokenizer factor_values(*token_iter, factor_separator);
     s_smpi_factor_t fact;
-    xbt_assert(factor_values.begin() != factor_values.end(), "Malformed radical for smpi factor: '%s'", values.c_str());
+    xbt_assert(factor_values.begin() != factor_values.end(), "Malformed radical for %s: '%s'", name_.c_str(),
+               values.c_str());
     unsigned int iteration = 0;
     for (auto factor_iter = factor_values.begin(); factor_iter != factor_values.end(); ++factor_iter) {
       iteration++;
@@ -78,14 +87,14 @@ void FactorSet::parse(const std::string& values)
           fact.factor = std::stoi(*factor_iter);
         } catch (const std::invalid_argument&) {
           throw std::invalid_argument(std::string("Invalid factor in chunk ") + std::to_string(factors_.size() + 1) +
-                                      ": " + *factor_iter);
+                                      ": " + *factor_iter + " for " + name_);
         }
       } else {
         try {
           fact.values.push_back(xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, *factor_iter, ""));
         } catch (const std::invalid_argument&) {
           throw std::invalid_argument(std::string("Invalid factor value ") + std::to_string(iteration) + " in chunk " +
-                                      std::to_string(factors_.size() + 1) + ": " + *factor_iter);
+                                      std::to_string(factors_.size() + 1) + ": " + *factor_iter + " for " + name_);
         }
       }
     }
@@ -99,8 +108,6 @@ void FactorSet::parse(const std::string& values)
     XBT_DEBUG("smpi_factor:\t%zu: %zu values, first: %f", fact.factor, factors_.size(), fact.values[0]);
   }
   factors_.shrink_to_fit();
-
-  initialized_ = true;
 }
 
 FactorSet::FactorSet(const std::string& name, double default_value,
index 4e2371c..957d8c7 100644 (file)
@@ -61,7 +61,7 @@ void NetworkConstantModel::update_actions_state(double /*now*/, double delta)
         action.latency_ = 0.0;
       }
     }
-    action.update_remains(action.get_cost() * delta / cfg_latency_factor);
+    action.update_remains(action.get_cost() * delta / get_latency_factor());
     action.update_max_duration(delta);
 
     if ((action.get_remains_no_update() <= 0) ||
@@ -82,7 +82,7 @@ Action* NetworkConstantModel::communicate(s4u::Host* src, s4u::Host* dst, double
 NetworkConstantAction::NetworkConstantAction(NetworkConstantModel* model_, s4u::Host& src, s4u::Host& dst, double size)
     : NetworkAction(model_, src, dst, size, false)
 {
-  latency_ = NetworkModel::cfg_latency_factor;
+  latency_ = model_->get_latency_factor();
   if (latency_ <= 0.0)
     set_state(Action::State::FINISHED);
 }
index 4f95133..2819da3 100644 (file)
@@ -41,7 +41,7 @@ double xbt_str_parse_double(const char* str, const char* error_msg)
 
   double res = strtod(str, &endptr);
   if (endptr[0] != '\0')
-    throw std::invalid_argument(simgrid::xbt::string_printf("%s: %s", error_msg, str));
+    throw std::invalid_argument(simgrid::xbt::string_printf("%s: %s (parse error at '%s')", error_msg, str, endptr));
 
   return res;
 }