Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / resource / NetworkModelFactors.cpp
1 /* Copyright (c) 2013-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/resource/NetworkModelFactors.hpp"
7 #include "src/simgrid/sg_config.hpp"
8
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
10
11 /*********
12  * Model *
13  *********/
14
15 namespace simgrid::kernel::resource {
16 config::Flag<std::string> cfg_latency_factor_str(
17     "network/latency-factor", std::initializer_list<const char*>{"smpi/lat-factor"},
18     "Correction factor to apply to the provided latency (default value overridden by network model)", "1.0");
19 static config::Flag<std::string> cfg_bandwidth_factor_str(
20     "network/bandwidth-factor", std::initializer_list<const char*>{"smpi/bw-factor"},
21     "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0");
22
23 FactorSet NetworkModelFactors::cfg_latency_factor("network/latency-factor");
24 FactorSet NetworkModelFactors::cfg_bandwidth_factor("network/bandwidth-factor");
25
26 double NetworkModelFactors::get_bandwidth_factor() const
27 {
28   xbt_assert(not bw_factor_cb_,
29              "Cannot access the global bandwidth factor since a callback is used. Please go for the advanced API.");
30
31   if (not cfg_bandwidth_factor.is_initialized())
32     cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
33
34   return cfg_bandwidth_factor(0);
35 }
36
37 double NetworkModelFactors::get_latency_factor() const
38 {
39   xbt_assert(not lat_factor_cb_,
40              "Cannot access the global latency factor since a callback is used. Please go for the advanced API.");
41
42   if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
43     cfg_latency_factor.parse(cfg_latency_factor_str.get());
44
45   return cfg_latency_factor(0);
46 }
47
48 double NetworkModelFactors::get_latency_factor(double size, const s4u::Host* src, const s4u::Host* dst,
49                                                const std::vector<s4u::Link*>& links,
50                                                const std::unordered_set<s4u::NetZone*>& netzones) const
51 {
52   if (lat_factor_cb_)
53     return lat_factor_cb_(size, src, dst, links, netzones);
54
55   if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
56     cfg_latency_factor.parse(cfg_latency_factor_str.get());
57
58   return cfg_latency_factor(size);
59 }
60
61 double NetworkModelFactors::get_bandwidth_factor(double size, const s4u::Host* src, const s4u::Host* dst,
62                                                  const std::vector<s4u::Link*>& links,
63                                                  const std::unordered_set<s4u::NetZone*>& netzones) const
64 {
65   if (bw_factor_cb_)
66     return bw_factor_cb_(size, src, dst, links, netzones);
67
68   if (not cfg_bandwidth_factor.is_initialized())
69     cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
70
71   return cfg_bandwidth_factor(size);
72 }
73
74 void NetworkModelFactors::set_lat_factor_cb(const std::function<NetworkFactorCb>& cb)
75 {
76   if (not cb)
77     throw std::invalid_argument("NetworkModelFactors: Invalid callback");
78   if (not simgrid::config::is_default("network/latency-factor"))
79     throw std::invalid_argument("You must choose between network/latency-factor and callback configuration.");
80
81   lat_factor_cb_ = cb;
82 }
83
84 void NetworkModelFactors::set_bw_factor_cb(const std::function<NetworkFactorCb>& cb)
85 {
86   if (not cb)
87     throw std::invalid_argument("NetworkModelFactors: Invalid callback");
88   if (not simgrid::config::is_default("network/bandwidth-factor"))
89     throw std::invalid_argument("You must choose between network/bandwidth-factor and callback configuration.");
90
91   bw_factor_cb_ = cb;
92 }
93
94 } // namespace simgrid::kernel::resource