--- /dev/null
+/* Copyright (c) 2013-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. */
+
+#include "src/kernel/resource/NetworkModelFactors.hpp"
+#include "simgrid/sg_config.hpp"
+#include "src/kernel/resource/FactorSet.hpp"
+
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
+
+/*********
+ * Model *
+ *********/
+
+namespace simgrid::kernel::resource {
+#if 0
+static FactorSet cfg_latency_factor("network/latency-factor");
+static FactorSet cfg_bandwidth_factor("network/bandwidth-factor");
+
+config::Flag<std::string> cfg_latency_factor_str(
+ "network/latency-factor", std::initializer_list<const char*>{"smpi/lat-factor"},
+ "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", std::initializer_list<const char*>{"smpi/bw-factor"},
+ "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0");
+
+double NetworkModelFactors::get_latency_factor()
+{
+ xbt_assert(not lat_factor_cb_,
+ "Cannot access the global latency factor since a callback is used. Please go for the advanced API.");
+
+ 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(0);
+}
+
+double NetworkModelFactors::get_latency_factor(double size, const s4u::Host* src, const s4u::Host* dst,
+ const std::vector<s4u::Link*>& links,
+ const std::unordered_set<s4u::NetZone*>& netzones)
+{
+ if (lat_factor_cb_)
+ return lat_factor_cb_(size, src, dst, links, netzones);
+
+ 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 NetworkModelFactors::get_bandwidth_factor()
+{
+ xbt_assert(not bw_factor_cb_,
+ "Cannot access the global bandwidth factor since a callback is used. Please go for the advanced API.");
+
+ if (not cfg_bandwidth_factor.is_initialized())
+ cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
+
+ return cfg_bandwidth_factor(0);
+}
+
+double NetworkModelFactors::get_bandwidth_factor(double size, const s4u::Host* src, const s4u::Host* dst,
+ const std::vector<s4u::Link*>& links,
+ const std::unordered_set<s4u::NetZone*>& netzones)
+{
+ if (bw_factor_cb_)
+ return bw_factor_cb_(size, src, dst, links, netzones);
+
+ if (not cfg_bandwidth_factor.is_initialized())
+ cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
+
+ return cfg_bandwidth_factor(size);
+}
+#endif
+
+void NetworkModelIntf::set_lat_factor_cb(const std::function<NetworkFactorCb>& cb)
+{
+ if (not cb)
+ throw std::invalid_argument("NetworkModelFactors: Invalid callback");
+ if (not simgrid::config::is_default("network/latency-factor"))
+ throw std::invalid_argument("You must choose between network/latency-factor and callback configuration.");
+
+ lat_factor_cb_ = cb;
+}
+
+void NetworkModelIntf::set_bw_factor_cb(const std::function<NetworkFactorCb>& cb)
+{
+ if (not cb)
+ throw std::invalid_argument("NetworkModelFactors: Invalid callback");
+ if (not simgrid::config::is_default("network/bandwidth-factor"))
+ throw std::invalid_argument("You must choose between network/bandwidth-factor and callback configuration.");
+
+ bw_factor_cb_ = cb;
+}
+
+} // namespace simgrid::kernel::resource
#ifndef SIMGRID_KERNEL_RESOURCE_NETWORKMODELFACTORS_HPP
#define SIMGRID_KERNEL_RESOURCE_NETWORKMODELFACTORS_HPP
+#include "simgrid/sg_config.hpp"
+#include "xbt/asserts.h"
#include <simgrid/forward.h>
#include <unordered_set>
#include <vector>
-namespace simgrid {
-namespace kernel {
-namespace resource {
+namespace simgrid::kernel::resource {
/** @ingroup SURF_interface
* @brief Network Model interface class
*/
class XBT_PUBLIC NetworkModelIntf {
+ using NetworkFactorCb = double(double size, const s4u::Host* src, const s4u::Host* dst,
+ const std::vector<s4u::Link*>& links,
+ const std::unordered_set<s4u::NetZone*>& netzones);
+
protected:
- ~NetworkModelIntf() = default;
+ std::function<NetworkFactorCb> lat_factor_cb_;
+ std::function<NetworkFactorCb> bw_factor_cb_;
public:
/**
* @param netzones Set with NetZones involved in the comm
* @return Multiply factor
*/
- using NetworkFactorCb = double(double size, const s4u::Host* src, const s4u::Host* dst,
- const std::vector<s4u::Link*>& links,
- const std::unordered_set<s4u::NetZone*>& netzones);
/** @brief Configure the latency factor callback */
- virtual void set_lat_factor_cb(const std::function<NetworkFactorCb>& cb) = 0;
+ void set_lat_factor_cb(const std::function<NetworkFactorCb>& cb);
+
/** @brief Configure the bandwidth factor callback */
- virtual void set_bw_factor_cb(const std::function<NetworkFactorCb>& cb) = 0;
+ void set_bw_factor_cb(const std::function<NetworkFactorCb>& cb);
};
-} // namespace resource
-} // namespace kernel
-} // namespace simgrid
+} // namespace simgrid::kernel::resource
#endif /* SIMGRID_KERNEL_RESOURCE_NETWORKMODELINTF_HPP */