Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
clang-format before starting changing files
[simgrid.git] / src / surf / network_cm02.cpp
1 /* Copyright (c) 2013-2021. 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/surf/network_cm02.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/resource/profile/Event.hpp"
10 #include "src/surf/network_wifi.hpp"
11 #include "src/surf/surf_interface.hpp"
12 #include "surf/surf.hpp"
13
14 #include <algorithm>
15 #include <numeric>
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
18
19 double sg_latency_factor     = 1.0; /* default value; can be set by model or from command line */
20 double sg_bandwidth_factor   = 1.0; /* default value; can be set by model or from command line */
21 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
22
23 /************************************************************************/
24 /* New model based on optimizations discussed during Pedro Velho's thesis*/
25 /************************************************************************/
26 /* @techreport{VELHO:2011:HAL-00646896:1, */
27 /*      url = {http://hal.inria.fr/hal-00646896/en/}, */
28 /*      title = {{Flow-level network models: have we reached the limits?}}, */
29 /*      author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
30 /*      type = {Rapport de recherche}, */
31 /*      institution = {INRIA}, */
32 /*      number = {RR-7821}, */
33 /*      year = {2011}, */
34 /*      month = Nov, */
35 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
36 /*  } */
37 void surf_network_model_init_LegrandVelho()
38 {
39   xbt_assert(surf_network_model == nullptr, "Cannot set the network model twice");
40
41   surf_network_model = new simgrid::kernel::resource::NetworkCm02Model();
42
43   simgrid::config::set_default<double>("network/latency-factor", 13.01);
44   simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
45   simgrid::config::set_default<double>("network/weight-S", 20537);
46 }
47
48 /***************************************************************************/
49 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
50 /***************************************************************************/
51 /* @TechReport{      rr-lip2002-40, */
52 /*   author        = {Henri Casanova and Loris Marchal}, */
53 /*   institution   = {LIP}, */
54 /*   title         = {A Network Model for Simulation of Grid Application}, */
55 /*   number        = {2002-40}, */
56 /*   month         = {oct}, */
57 /*   year          = {2002} */
58 /* } */
59 void surf_network_model_init_CM02()
60 {
61   xbt_assert(surf_network_model == nullptr, "Cannot set the network model twice");
62
63   simgrid::config::set_default<double>("network/latency-factor", 1.0);
64   simgrid::config::set_default<double>("network/bandwidth-factor", 1.0);
65   simgrid::config::set_default<double>("network/weight-S", 0.0);
66
67   surf_network_model = new simgrid::kernel::resource::NetworkCm02Model();
68 }
69
70 namespace simgrid {
71 namespace kernel {
72 namespace resource {
73
74 NetworkCm02Model::NetworkCm02Model()
75     : NetworkModel(config::get_value<std::string>("network/optim") == "Full" ? Model::UpdateAlgo::FULL
76                                                                              : Model::UpdateAlgo::LAZY)
77 {
78   all_existing_models.push_back(this);
79
80   std::string optim = config::get_value<std::string>("network/optim");
81   bool select       = config::get_value<bool>("network/maxmin-selective-update");
82
83   if (optim == "Lazy") {
84     xbt_assert(select || config::is_default("network/maxmin-selective-update"),
85                "You cannot disable network selective update when using the lazy update mechanism");
86     select = true;
87   }
88
89   set_maxmin_system(new lmm::System(select));
90   loopback_ = NetworkCm02Model::create_link("__loopback__",
91                                             std::vector<double>{config::get_value<double>("network/loopback-bw")},
92                                             s4u::Link::SharingPolicy::FATPIPE)
93                   ->set_latency(config::get_value<double>("network/loopback-lat"));
94   loopback_->seal();
95 }
96
97 LinkImpl* NetworkCm02Model::create_link(const std::string& name, const std::vector<double>& bandwidths,
98                                         s4u::Link::SharingPolicy policy)
99 {
100   if (policy == s4u::Link::SharingPolicy::WIFI)
101     return (new NetworkWifiLink(name, bandwidths, get_maxmin_system()))->set_model(this);
102
103   xbt_assert(bandwidths.size() == 1, "Non-WIFI links must use only 1 bandwidth.");
104   return (new NetworkCm02Link(name, bandwidths[0], policy, get_maxmin_system()))->set_model(this);
105 }
106
107 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
108 {
109   while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
110     auto* action = static_cast<NetworkCm02Action*>(get_action_heap().pop());
111     XBT_DEBUG("Something happened to action %p", action);
112
113     // if I am wearing a latency hat
114     if (action->get_type() == ActionHeap::Type::latency) {
115       XBT_DEBUG("Latency paid for action %p. Activating", action);
116       get_maxmin_system()->update_variable_penalty(action->get_variable(), action->sharing_penalty_);
117       get_action_heap().remove(action);
118       action->set_last_update();
119
120       // if I am wearing a max_duration or normal hat
121     } else if (action->get_type() == ActionHeap::Type::max_duration || action->get_type() == ActionHeap::Type::normal) {
122       // no need to communicate anymore
123       // assume that flows that reached max_duration have remaining of 0
124       XBT_DEBUG("Action %p finished", action);
125       action->finish(Action::State::FINISHED);
126       get_action_heap().remove(action);
127     }
128   }
129 }
130
131 void NetworkCm02Model::update_actions_state_full(double /*now*/, double delta)
132 {
133   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
134     auto& action = static_cast<NetworkCm02Action&>(*it);
135     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
136     XBT_DEBUG("Something happened to action %p", &action);
137     double deltap = delta;
138     if (action.latency_ > 0) {
139       if (action.latency_ > deltap) {
140         double_update(&action.latency_, deltap, sg_surf_precision);
141         deltap = 0.0;
142       } else {
143         double_update(&deltap, action.latency_, sg_surf_precision);
144         action.latency_ = 0.0;
145       }
146       if (action.latency_ <= 0.0 && not action.is_suspended())
147         get_maxmin_system()->update_variable_penalty(action.get_variable(), action.sharing_penalty_);
148     }
149
150     if (not action.get_variable()->get_number_of_constraint()) {
151       /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
152        * vivaldi. In such case, just make sure that the action completes immediately.
153        */
154       action.update_remains(action.get_remains());
155     }
156     action.update_remains(action.get_variable()->get_value() * delta);
157
158     if (action.get_max_duration() != NO_MAX_DURATION)
159       action.update_max_duration(delta);
160
161     if (((action.get_remains() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
162         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
163       action.finish(Action::State::FINISHED);
164     }
165   }
166 }
167
168 Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
169 {
170   double latency = 0.0;
171   std::vector<LinkImpl*> back_route;
172   std::vector<LinkImpl*> route;
173
174   XBT_IN("(%s,%s,%g,%g)", src->get_cname(), dst->get_cname(), size, rate);
175
176   src->route_to(dst, route, &latency);
177   xbt_assert(not route.empty() || latency > 0,
178              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
179              src->get_cname(), dst->get_cname());
180
181   bool failed = std::any_of(route.begin(), route.end(), [](const LinkImpl* link) { return not link->is_on(); });
182
183   if (cfg_crosstraffic) {
184     dst->route_to(src, back_route, nullptr);
185     if (not failed)
186       failed =
187           std::any_of(back_route.begin(), back_route.end(), [](const LinkImpl* link) { return not link->is_on(); });
188   }
189
190   NetworkWifiLink* src_wifi_link = nullptr;
191   NetworkWifiLink* dst_wifi_link = nullptr;
192   if (not route.empty() && route.front()->get_sharing_policy() == s4u::Link::SharingPolicy::WIFI) {
193     src_wifi_link = static_cast<NetworkWifiLink*>(route.front());
194     xbt_assert(src_wifi_link->get_host_rate(src) != -1,
195                "The route from %s to %s begins with the WIFI link %s, but the host %s does not seem attached to that "
196                "WIFI link. Did you call link->set_host_rate()?",
197                src->get_cname(), dst->get_cname(), src_wifi_link->get_cname(), src->get_cname());
198   }
199   if (route.size() > 1 && route.back()->get_sharing_policy() == s4u::Link::SharingPolicy::WIFI) {
200     dst_wifi_link = static_cast<NetworkWifiLink*>(route.back());
201     xbt_assert(dst_wifi_link->get_host_rate(dst) != -1,
202                "The route from %s to %s ends with the WIFI link %s, but the host %s does not seem attached to that "
203                "WIFI link. Did you call link->set_host_rate()?",
204                src->get_cname(), dst->get_cname(), dst_wifi_link->get_cname(), dst->get_cname());
205   }
206   if (route.size() > 2)
207     for (unsigned i = 1; i < route.size() - 1; i++)
208       xbt_assert(route[i]->get_sharing_policy() != s4u::Link::SharingPolicy::WIFI,
209                  "Link '%s' is a WIFI link. It can only be at the beginning or the end of the route from '%s' to '%s', "
210                  "not in between (it is at position %u out of %zu). "
211                  "Did you declare an access_point in your WIFI zones?",
212                  route[i]->get_cname(), src->get_cname(), dst->get_cname(), i + 1, route.size());
213
214   NetworkCm02Action* action;
215   if (src_wifi_link == nullptr && dst_wifi_link == nullptr)
216     action = new NetworkCm02Action(this, *src, *dst, size, failed);
217   else
218     action = new NetworkWifiAction(this, *src, *dst, size, failed, src_wifi_link, dst_wifi_link);
219   action->sharing_penalty_ = latency;
220   action->latency_         = latency;
221   action->set_user_bound(rate);
222
223   if (is_update_lazy()) {
224     action->set_last_update();
225   }
226
227   if (sg_weight_S_parameter > 0) {
228     action->sharing_penalty_ =
229         std::accumulate(route.begin(), route.end(), action->sharing_penalty_, [](double total, LinkImpl* const& link) {
230           return total + sg_weight_S_parameter / link->get_bandwidth();
231         });
232   }
233
234   double bandwidth_bound = route.empty() ? -1.0 : get_bandwidth_factor(size) * route.front()->get_bandwidth();
235
236   for (auto const& link : route)
237     bandwidth_bound = std::min(bandwidth_bound, get_bandwidth_factor(size) * link->get_bandwidth());
238
239   action->lat_current_ = action->latency_;
240   action->latency_ *= get_latency_factor(size);
241   action->set_user_bound(get_bandwidth_constraint(action->get_user_bound(), bandwidth_bound, size));
242
243   size_t constraints_per_variable = route.size();
244   constraints_per_variable += back_route.size();
245
246   if (action->latency_ > 0) {
247     action->set_variable(get_maxmin_system()->variable_new(action, 0.0, -1.0, constraints_per_variable));
248     if (is_update_lazy()) {
249       // add to the heap the event when the latency is paid
250       double date = action->latency_ + action->get_last_update();
251
252       ActionHeap::Type type = route.empty() ? ActionHeap::Type::normal : ActionHeap::Type::latency;
253
254       XBT_DEBUG("Added action (%p) one latency event at date %f", action, date);
255       get_action_heap().insert(action, date, type);
256     }
257   } else
258     action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
259
260   if (action->get_user_bound() < 0) {
261     get_maxmin_system()->update_variable_bound(
262         action->get_variable(), (action->lat_current_ > 0) ? cfg_tcp_gamma / (2.0 * action->lat_current_) : -1.0);
263   } else {
264     get_maxmin_system()->update_variable_bound(
265         action->get_variable(), (action->lat_current_ > 0)
266                                     ? std::min(action->get_user_bound(), cfg_tcp_gamma / (2.0 * action->lat_current_))
267                                     : action->get_user_bound());
268   }
269
270   if (src_wifi_link != nullptr)
271     get_maxmin_system()->expand(src_wifi_link->get_constraint(), action->get_variable(),
272                                 1.0 / src_wifi_link->get_host_rate(src));
273   if (dst_wifi_link != nullptr)
274     get_maxmin_system()->expand(dst_wifi_link->get_constraint(), action->get_variable(),
275                                 1.0 / dst_wifi_link->get_host_rate(dst));
276
277   for (auto const* link : route) {
278     // WIFI links are handled manually just above, so skip them now
279     if (link->get_sharing_policy() == s4u::Link::SharingPolicy::WIFI) {
280       xbt_assert(link == src_wifi_link || link == dst_wifi_link,
281                  "Wifi links can only occur at the beginning of the route (meaning that it's attached to the src) or "
282                  "at its end (meaning that it's attached to the dst");
283     } else {
284       get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), 1.0);
285     }
286   }
287
288   if (cfg_crosstraffic) {
289     XBT_DEBUG("Crosstraffic active: adding backward flow using 5%% of the available bandwidth");
290     if (dst_wifi_link != nullptr)
291       get_maxmin_system()->expand(dst_wifi_link->get_constraint(), action->get_variable(),
292                                   .05 / dst_wifi_link->get_host_rate(dst));
293     if (src_wifi_link != nullptr)
294       get_maxmin_system()->expand(src_wifi_link->get_constraint(), action->get_variable(),
295                                   .05 / src_wifi_link->get_host_rate(src));
296     for (auto const* link : back_route)
297       if (link->get_sharing_policy() != s4u::Link::SharingPolicy::WIFI)
298         get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), .05);
299     // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
300     // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
301     // action->getVariable()->set_concurrency_share(2)
302   }
303   XBT_OUT();
304
305   simgrid::s4u::Link::on_communicate(*action);
306   return action;
307 }
308
309 /************
310  * Resource *
311  ************/
312 NetworkCm02Link::NetworkCm02Link(const std::string& name, double bandwidth, s4u::Link::SharingPolicy policy,
313                                  kernel::lmm::System* system)
314     : LinkImpl(name)
315 {
316   bandwidth_.scale = 1.0;
317   bandwidth_.peak  = bandwidth;
318   this->set_constraint(system->constraint_new(this, sg_bandwidth_factor * bandwidth));
319
320   if (policy == s4u::Link::SharingPolicy::FATPIPE)
321     get_constraint()->unshare();
322 }
323
324 void NetworkCm02Link::apply_event(kernel::profile::Event* triggered, double value)
325 {
326   /* Find out which of my iterators was triggered, and react accordingly */
327   if (triggered == bandwidth_.event) {
328     set_bandwidth(value);
329     tmgr_trace_event_unref(&bandwidth_.event);
330
331   } else if (triggered == latency_.event) {
332     set_latency(value);
333     tmgr_trace_event_unref(&latency_.event);
334
335   } else if (triggered == state_event_) {
336     if (value > 0)
337       turn_on();
338     else
339       turn_off();
340     tmgr_trace_event_unref(&state_event_);
341   } else {
342     xbt_die("Unknown event!\n");
343   }
344
345   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
346             get_constraint());
347 }
348
349 void NetworkCm02Link::set_bandwidth(double value)
350 {
351   bandwidth_.peak = value;
352
353   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
354                                                             sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
355
356   LinkImpl::on_bandwidth_change();
357
358   if (sg_weight_S_parameter > 0) {
359     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
360
361     const kernel::lmm::Variable* var;
362     const kernel::lmm::Element* elem     = nullptr;
363     const kernel::lmm::Element* nextelem = nullptr;
364     int numelem                          = 0;
365     while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
366       auto* action = static_cast<NetworkCm02Action*>(var->get_id());
367       action->sharing_penalty_ += delta;
368       if (not action->is_suspended())
369         get_model()->get_maxmin_system()->update_variable_penalty(action->get_variable(), action->sharing_penalty_);
370     }
371   }
372 }
373
374 LinkImpl* NetworkCm02Link::set_latency(double value)
375 {
376   latency_check(value);
377
378   double delta = value - latency_.peak;
379   const kernel::lmm::Variable* var;
380   const kernel::lmm::Element* elem     = nullptr;
381   const kernel::lmm::Element* nextelem = nullptr;
382   int numelem                          = 0;
383
384   latency_.scale = 1.0;
385   latency_.peak  = value;
386
387   while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
388     auto* action = static_cast<NetworkCm02Action*>(var->get_id());
389     action->lat_current_ += delta;
390     action->sharing_penalty_ += delta;
391     if (action->get_user_bound() < 0)
392       get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), NetworkModel::cfg_tcp_gamma /
393                                                                                           (2.0 * action->lat_current_));
394     else {
395       get_model()->get_maxmin_system()->update_variable_bound(
396           action->get_variable(),
397           std::min(action->get_user_bound(), NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)));
398
399       if (action->get_user_bound() < NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)) {
400         XBT_INFO("Flow is limited BYBANDWIDTH");
401       } else {
402         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->lat_current_);
403       }
404     }
405     if (not action->is_suspended())
406       get_model()->get_maxmin_system()->update_variable_penalty(action->get_variable(), action->sharing_penalty_);
407   }
408   return this;
409 }
410
411 /**********
412  * Action *
413  **********/
414
415 void NetworkCm02Action::update_remains_lazy(double now)
416 {
417   if (not is_running())
418     return;
419
420   double delta = now - get_last_update();
421
422   if (get_remains_no_update() > 0) {
423     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
424               get_last_update());
425     update_remains(get_last_value() * delta);
426
427     XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
428   }
429
430   update_max_duration(delta);
431
432   if ((get_remains_no_update() <= 0 && (get_variable()->get_penalty() > 0)) ||
433       ((get_max_duration() != NO_MAX_DURATION) && (get_max_duration() <= 0))) {
434     finish(Action::State::FINISHED);
435     get_model()->get_action_heap().remove(this);
436   }
437
438   set_last_update();
439   set_last_value(get_variable()->get_value());
440 }
441
442 } // namespace resource
443 } // namespace kernel
444 } // namespace simgrid