From: Martin Quinson Date: Sat, 22 Oct 2022 18:00:12 +0000 (+0200) Subject: further refacto smpi_factors to reduce dupplication X-Git-Tag: v3.34~761 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/167d54120fca91495ebe52ca6dec8a830a7ab023?hp=fae5a849bf071f9337fdd4e53c0eb3fd64f6b4af further refacto smpi_factors to reduce dupplication --- diff --git a/src/smpi/include/smpi_host.hpp b/src/smpi/include/smpi_host.hpp index d75aa0ffd8..4223134f71 100644 --- a/src/smpi/include/smpi_host.hpp +++ b/src/smpi/include/smpi_host.hpp @@ -14,11 +14,14 @@ #include namespace simgrid::smpi { - +static auto factor_lambda(s_smpi_factor_t const& fact, double size) +{ + return fact.values[0] + fact.values[1] * static_cast(size); +} class Host { - std::vector orecv_parsed_values; - std::vector osend_parsed_values; - std::vector oisend_parsed_values; + utils::FactorSet orecv_{"smpi/or", 0.0, factor_lambda}; + utils::FactorSet osend_{"smpi/os", 0.0, factor_lambda}; + utils::FactorSet oisend_{"smpi/ois", 0.0, factor_lambda}; s4u::Host* host = nullptr; /** * @brief Generates warning message if user's config is conflicting (callback vs command line/xml) diff --git a/src/smpi/include/smpi_utils.hpp b/src/smpi/include/smpi_utils.hpp index cf441190b0..0e8b6fb0fa 100644 --- a/src/smpi/include/smpi_utils.hpp +++ b/src/smpi/include/smpi_utils.hpp @@ -27,14 +27,14 @@ class FactorSet { const std::string& name_; std::vector factors_; double default_value_; - const std::function lambda_; + const std::function lambda_; bool initialized_ = false; public: // Parse the factor from a string FactorSet( const std::string& name, double default_value = 1, - std::function const& lambda = [](s_smpi_factor_t const& factor) { + std::function const& lambda = [](s_smpi_factor_t const& factor, double) { return factor.values.front(); }); void parse(const std::string& values); diff --git a/src/smpi/internals/smpi_host.cpp b/src/smpi/internals/smpi_host.cpp index a206de910e..de48ade3da 100644 --- a/src/smpi/internals/smpi_host.cpp +++ b/src/smpi/internals/smpi_host.cpp @@ -33,36 +33,13 @@ namespace simgrid::smpi { xbt::Extension Host::EXTENSION_ID; -static double factor_use(std::vector& factors, const char* name, size_t size) -{ - /* fallback to smpi/or config */ - double current = factors.empty() ? 0.0 : factors.front().values[0] + factors.front().values[1] * size; - - // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval - // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size) - // Note: parse_factor() (used before) already sorts the vector we iterate over! - for (auto const& fact : factors) { - if (size <= fact.factor) { // Values already too large, use the previously computed value of current! - XBT_DEBUG("%s: %zu <= %zu return %.10f", name, size, fact.factor, current); - return current; - } else { - // If the next section is too large, the current section must be used. - // Hence, save the cost, as we might have to use it. - current=fact.values[0]+fact.values[1]*size; - } - } - XBT_DEBUG("%s: %zu is larger than largest boundary, return %.10f", name, size, current); - - return current; -} - double Host::orecv(size_t size, s4u::Host* src, s4u::Host* dst) { /* return user's callback if available */ if (auto it = cost_cbs.find(SmpiOperation::RECV); it != cost_cbs.end()) return it->second(size, src, dst); - return factor_use(orecv_parsed_values, "smpi/or", size); + return orecv_(size); } double Host::osend(size_t size, s4u::Host* src, s4u::Host* dst) @@ -71,7 +48,7 @@ double Host::osend(size_t size, s4u::Host* src, s4u::Host* dst) if (auto it = cost_cbs.find(SmpiOperation::SEND); it != cost_cbs.end()) return it->second(size, src, dst); - return factor_use(osend_parsed_values, "smpi/os", size); + return osend_(size); } double Host::oisend(size_t size, s4u::Host* src, s4u::Host* dst) @@ -80,7 +57,7 @@ double Host::oisend(size_t size, s4u::Host* src, s4u::Host* dst) if (auto it = cost_cbs.find(SmpiOperation::ISEND); it != cost_cbs.end()) return it->second(size, src, dst); - return factor_use(oisend_parsed_values, "smpi/ois", size); + return oisend_(size); } void Host::check_factor_configs(const std::string& op) const @@ -100,25 +77,22 @@ Host::Host(s4u::Host* ptr) : host(ptr) smpi::Host::EXTENSION_ID = s4u::Host::extension_create(); check_factor_configs("smpi/or"); - if (const char* orecv_string = host->get_property("smpi/or")) { - orecv_parsed_values = simgrid::smpi::utils::parse_factor(orecv_string); - } else { - orecv_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value("smpi/or")); - } + if (const char* orecv_string = host->get_property("smpi/or")) + orecv_.parse(orecv_string); + else + orecv_.parse(config::get_value("smpi/or")); check_factor_configs("smpi/os"); - if (const char* osend_string = host->get_property("smpi/os")) { - osend_parsed_values = simgrid::smpi::utils::parse_factor(osend_string); - } else { - osend_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value("smpi/os")); - } + if (const char* osend_string = host->get_property("smpi/os")) + osend_.parse(osend_string); + else + osend_.parse(config::get_value("smpi/os")); check_factor_configs("smpi/ois"); - if (const char* oisend_string = host->get_property("smpi/ois")) { - oisend_parsed_values = simgrid::smpi::utils::parse_factor(oisend_string); - } else { - oisend_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value("smpi/ois")); - } + if (const char* oisend_string = host->get_property("smpi/ois")) + oisend_.parse(oisend_string); + else + oisend_.parse(config::get_value("smpi/ois")); } } // namespace simgrid::smpi diff --git a/src/smpi/internals/smpi_utils.cpp b/src/smpi/internals/smpi_utils.cpp index b7f5a80b5c..6660c506fb 100644 --- a/src/smpi/internals/smpi_utils.cpp +++ b/src/smpi/internals/smpi_utils.cpp @@ -56,14 +56,14 @@ void FactorSet::parse(const std::string& values) } FactorSet::FactorSet(const std::string& name, double default_value, - std::function const& lambda) + std::function const& lambda) : name_(name), default_value_(default_value), lambda_(lambda) { } double FactorSet::operator()() { - return lambda_(factors_.front()); + return lambda_(factors_.front(), 0); } double FactorSet::operator()(double size) @@ -80,12 +80,12 @@ double FactorSet::operator()(double size) XBT_DEBUG("%s: %f <= %zu return default %f", name_.c_str(), size, fact.factor, default_value_); return default_value_; } - double val = lambda_(factors_[i - 1]); + double val = lambda_(factors_[i - 1], size); XBT_DEBUG("%s: %f <= %zu return %f", name_.c_str(), size, fact.factor, val); return val; } } - double val = lambda_(factors_.back()); + double val = lambda_(factors_.back(), size); XBT_DEBUG("%s: %f > %zu return %f", name_.c_str(), size, factors_.back().factor, val); return val;