Logo AND Algorithmique Numérique Distribuée

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