Logo AND Algorithmique Numérique Distribuée

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