Logo AND Algorithmique Numérique Distribuée

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