Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
check that disk has been given as argument before accessing its pimpl
[simgrid.git] / src / kernel / resource / NetworkModelFactors.cpp
1 /* Copyright (c) 2013-2022. 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 "simgrid/sg_config.hpp"
8 #include "src/kernel/resource/FactorSet.hpp"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
11
12 /*********
13  * Model *
14  *********/
15
16 namespace simgrid::kernel::resource {
17 static FactorSet cfg_latency_factor("network/latency-factor");
18 static FactorSet cfg_bandwidth_factor("network/bandwidth-factor");
19
20 config::Flag<std::string> cfg_latency_factor_str(
21     "network/latency-factor", std::initializer_list<const char*>{"smpi/lat-factor"},
22     "Correction factor to apply to the provided latency (default value overridden by network model)", "1.0");
23 static config::Flag<std::string> cfg_bandwidth_factor_str(
24     "network/bandwidth-factor", std::initializer_list<const char*>{"smpi/bw-factor"},
25     "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0");
26
27 double NetworkModelFactors::get_bandwidth_factor()
28 {
29   xbt_assert(not bw_factor_cb_,
30              "Cannot access the global bandwidth factor since a callback is used. Please go for the advanced API.");
31
32   if (not cfg_bandwidth_factor.is_initialized())
33     cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
34
35   return cfg_bandwidth_factor(0);
36 }
37
38 double NetworkModelFactors::get_latency_factor()
39 {
40   xbt_assert(not lat_factor_cb_,
41              "Cannot access the global latency factor since a callback is used. Please go for the advanced API.");
42
43   if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
44     cfg_latency_factor.parse(cfg_latency_factor_str.get());
45
46   return cfg_latency_factor(0);
47 }
48
49 #if 0
50 double NetworkModelFactors::get_latency_factor(double size, const s4u::Host* src, const s4u::Host* dst,
51                                                const std::vector<s4u::Link*>& links,
52                                                const std::unordered_set<s4u::NetZone*>& netzones)
53 {
54   if (lat_factor_cb_)
55     return lat_factor_cb_(size, src, dst, links, netzones);
56
57   if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
58     cfg_latency_factor.parse(cfg_latency_factor_str.get());
59
60   return cfg_latency_factor(size);
61 }
62
63 double NetworkModelFactors::get_bandwidth_factor(double size, const s4u::Host* src, const s4u::Host* dst,
64                                                  const std::vector<s4u::Link*>& links,
65                                                  const std::unordered_set<s4u::NetZone*>& netzones)
66 {
67   if (bw_factor_cb_)
68     return bw_factor_cb_(size, src, dst, links, netzones);
69
70   if (not cfg_bandwidth_factor.is_initialized())
71     cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
72
73   return cfg_bandwidth_factor(size);
74 }
75 #endif
76
77 double NetworkModelFactors::get_latency_factor(double size, const s4u::Host* src, const s4u::Host* dst,
78                                                const std::vector<s4u::Link*>& links,
79                                                const std::unordered_set<s4u::NetZone*>& netzones)
80 {
81   if (lat_factor_cb_)
82     return lat_factor_cb_(size, src, dst, links, netzones);
83
84   if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
85     cfg_latency_factor.parse(cfg_latency_factor_str.get());
86
87   return cfg_latency_factor(size);
88 }
89 double NetworkModelFactors::get_bandwidth_factor(double size, const s4u::Host* src, const s4u::Host* dst,
90                                                  const std::vector<s4u::Link*>& links,
91                                                  const std::unordered_set<s4u::NetZone*>& netzones)
92 {
93   if (bw_factor_cb_)
94     return bw_factor_cb_(size, src, dst, links, netzones);
95
96   if (not cfg_bandwidth_factor.is_initialized())
97     cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
98
99   return cfg_bandwidth_factor(size);
100 }
101
102 void NetworkModelFactors::set_lat_factor_cb(const std::function<NetworkFactorCb>& cb)
103 {
104   if (not cb)
105     throw std::invalid_argument("NetworkModelFactors: Invalid callback");
106   if (not simgrid::config::is_default("network/latency-factor"))
107     throw std::invalid_argument("You must choose between network/latency-factor and callback configuration.");
108
109   lat_factor_cb_ = cb;
110 }
111
112 void NetworkModelFactors::set_bw_factor_cb(const std::function<NetworkFactorCb>& cb)
113 {
114   if (not cb)
115     throw std::invalid_argument("NetworkModelFactors: Invalid callback");
116   if (not simgrid::config::is_default("network/bandwidth-factor"))
117     throw std::invalid_argument("You must choose between network/bandwidth-factor and callback configuration.");
118
119   bw_factor_cb_ = cb;
120 }
121
122 } // namespace simgrid::kernel::resource