Logo AND Algorithmique Numérique Distribuée

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