Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f3e83138a0a1d713ed20405945939d9fb9b88b65
[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.h"
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   set_default_protocol_function(simgrid::kernel::lmm::func_reno_f, simgrid::kernel::lmm::func_reno_fp,
91                                 simgrid::kernel::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::lagrange_solve);
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   set_default_protocol_function(simgrid::kernel::lmm::func_reno2_f, simgrid::kernel::lmm::func_reno2_fp,
108                                 simgrid::kernel::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::lagrange_solve);
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   set_default_protocol_function(simgrid::kernel::lmm::func_vegas_f, simgrid::kernel::lmm::func_vegas_fp,
124                                 simgrid::kernel::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::lagrange_solve);
131   all_existing_models->push_back(surf_network_model);
132 }
133
134 namespace simgrid {
135 namespace surf {
136
137 NetworkCm02Model::NetworkCm02Model()
138   :NetworkModel()
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 == "Full") {
144     setUpdateMechanism(UM_FULL);
145   } else if (optim == "Lazy") {
146     select = true;
147     setUpdateMechanism(UM_LAZY);
148     xbt_assert(select || (xbt_cfg_is_default_value("network/maxmin-selective-update")),
149                "You cannot disable selective update when using the lazy update mechanism");
150   } else {
151     xbt_die("Unsupported optimization (%s) for this model. Accepted: Full, Lazy.", optim.c_str());
152   }
153
154   maxmin_system_ = new simgrid::kernel::lmm::System(select);
155   loopback_     = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
156
157   if (getUpdateMechanism() == UM_LAZY)
158     maxmin_system_->modified_set_ = new kernel::resource::Action::ModifiedSet();
159 }
160
161 NetworkCm02Model::NetworkCm02Model(void (*specificSolveFun)(kernel::lmm::System* self)) : NetworkCm02Model()
162 {
163   maxmin_system_->solve_fun = specificSolveFun;
164 }
165
166 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
167                                        e_surf_link_sharing_policy_t policy)
168 {
169   return new NetworkCm02Link(this, name, bandwidth, latency, policy, maxmin_system_);
170 }
171
172 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
173 {
174   while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
175
176     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(actionHeapPop());
177     XBT_DEBUG("Something happened to action %p", action);
178     if (TRACE_is_enabled()) {
179       int n = action->get_variable()->get_number_of_constraint();
180
181       for (int i = 0; i < n; i++){
182         kernel::lmm::Constraint* constraint = action->get_variable()->get_constraint(i);
183         NetworkCm02Link* link       = static_cast<NetworkCm02Link*>(constraint->get_id());
184         double value = action->get_variable()->get_value() * action->get_variable()->get_constraint_weight(i);
185         TRACE_surf_link_set_utilization(link->getCname(), action->get_category(), value, action->get_last_update(),
186                                         now - action->get_last_update());
187       }
188     }
189
190     // if I am wearing a latency hat
191     if (action->get_type() == kernel::resource::Action::Type::LATENCY) {
192       XBT_DEBUG("Latency paid for action %p. Activating", action);
193       maxmin_system_->update_variable_weight(action->get_variable(), action->weight_);
194       action->heapRemove(getActionHeap());
195       action->set_last_update();
196
197       // if I am wearing a max_duration or normal hat
198     } else if (action->get_type() == kernel::resource::Action::Type::MAX_DURATION ||
199                action->get_type() == kernel::resource::Action::Type::NORMAL) {
200       // no need to communicate anymore
201       // assume that flows that reached max_duration have remaining of 0
202       XBT_DEBUG("Action %p finished", action);
203       action->finish(kernel::resource::Action::State::done);
204       action->heapRemove(getActionHeap());
205     }
206   }
207 }
208
209
210 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
211 {
212   for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
213     NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
214     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
215     XBT_DEBUG("Something happened to action %p", &action);
216     double deltap = delta;
217     if (action.latency_ > 0) {
218       if (action.latency_ > deltap) {
219         double_update(&action.latency_, deltap, sg_surf_precision);
220         deltap = 0.0;
221       } else {
222         double_update(&deltap, action.latency_, sg_surf_precision);
223         action.latency_ = 0.0;
224       }
225       if (action.latency_ <= 0.0 && not action.is_suspended())
226         maxmin_system_->update_variable_weight(action.get_variable(), action.weight_);
227     }
228     if (TRACE_is_enabled()) {
229       int n = action.get_variable()->get_number_of_constraint();
230       for (int i = 0; i < n; i++) {
231         kernel::lmm::Constraint* constraint = action.get_variable()->get_constraint(i);
232         NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
233         TRACE_surf_link_set_utilization(
234             link->getCname(), action.get_category(),
235             (action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i)),
236             action.get_last_update(), now - action.get_last_update());
237       }
238     }
239     if (not action.get_variable()->get_number_of_constraint()) {
240       /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
241        * vivaldi. In such case, just make sure that the action completes immediately.
242        */
243       action.update_remains(action.get_remains());
244     }
245     action.update_remains(action.get_variable()->get_value() * delta);
246
247     if (action.get_max_duration() > NO_MAX_DURATION)
248       action.update_max_duration(delta);
249
250     if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
251         ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
252       action.finish(kernel::resource::Action::State::done);
253     }
254   }
255 }
256
257 kernel::resource::Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
258 {
259   int failed = 0;
260   double latency = 0.0;
261   std::vector<LinkImpl*> back_route;
262   std::vector<LinkImpl*> route;
263
264   XBT_IN("(%s,%s,%g,%g)", src->getCname(), dst->getCname(), size, rate);
265
266   src->routeTo(dst, route, &latency);
267   xbt_assert(not route.empty() || latency,
268              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
269              src->getCname(), dst->getCname());
270
271   for (auto const& link : route)
272     if (link->isOff())
273       failed = 1;
274
275   if (sg_network_crosstraffic == 1) {
276     dst->routeTo(src, back_route, nullptr);
277     for (auto const& link : back_route)
278       if (link->isOff())
279         failed = 1;
280   }
281
282   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
283   action->weight_ = latency;
284   action->latency_ = latency;
285   action->rate_ = rate;
286   if (getUpdateMechanism() == UM_LAZY) {
287     action->set_last_update();
288   }
289
290   double bandwidth_bound = -1.0;
291   if (sg_weight_S_parameter > 0)
292     for (auto const& link : route)
293       action->weight_ += sg_weight_S_parameter / link->bandwidth();
294
295   for (auto const& link : route) {
296     double bb       = bandwidthFactor(size) * link->bandwidth();
297     bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
298   }
299
300   action->latCurrent_ = action->latency_;
301   action->latency_ *= latencyFactor(size);
302   action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
303
304   int constraints_per_variable = route.size();
305   constraints_per_variable += back_route.size();
306
307   if (action->latency_ > 0) {
308     action->set_variable(maxmin_system_->variable_new(action, 0.0, -1.0, constraints_per_variable));
309     if (getUpdateMechanism() == UM_LAZY) {
310       // add to the heap the event when the latency is payed
311       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->get_last_update());
312       action->heapInsert(getActionHeap(), action->latency_ + action->get_last_update(),
313                          route.empty() ? kernel::resource::Action::Type::NORMAL
314                                        : kernel::resource::Action::Type::LATENCY);
315     }
316   } else
317     action->set_variable(maxmin_system_->variable_new(action, 1.0, -1.0, constraints_per_variable));
318
319   if (action->rate_ < 0) {
320     maxmin_system_->update_variable_bound(
321         action->get_variable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
322   } else {
323     maxmin_system_->update_variable_bound(action->get_variable(),
324                                           (action->latCurrent_ > 0)
325                                               ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_))
326                                               : action->rate_);
327   }
328
329   for (auto const& link : route)
330     maxmin_system_->expand(link->constraint(), action->get_variable(), 1.0);
331
332   if (not back_route.empty()) { //  sg_network_crosstraffic was activated
333     XBT_DEBUG("Crosstraffic active adding backward flow using 5%%");
334     for (auto const& link : back_route)
335       maxmin_system_->expand(link->constraint(), action->get_variable(), .05);
336
337     // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
338     // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
339     // action->getVariable()->set_concurrency_share(2)
340   }
341   XBT_OUT();
342
343   simgrid::s4u::Link::onCommunicate(action, src, dst);
344   return action;
345 }
346
347 /************
348  * Resource *
349  ************/
350 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
351                                  e_surf_link_sharing_policy_t policy, kernel::lmm::System* system)
352     : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
353 {
354   bandwidth_.scale = 1.0;
355   bandwidth_.peak  = bandwidth;
356
357   latency_.scale = 1.0;
358   latency_.peak  = latency;
359
360   if (policy == SURF_LINK_FATPIPE)
361     constraint()->unshare();
362
363   simgrid::s4u::Link::onCreation(this->piface_);
364 }
365
366 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
367 {
368   /* Find out which of my iterators was triggered, and react accordingly */
369   if (triggered == bandwidth_.event) {
370     setBandwidth(value);
371     tmgr_trace_event_unref(&bandwidth_.event);
372
373   } else if (triggered == latency_.event) {
374     setLatency(value);
375     tmgr_trace_event_unref(&latency_.event);
376
377   } else if (triggered == stateEvent_) {
378     if (value > 0)
379       turnOn();
380     else {
381       kernel::lmm::Variable* var = nullptr;
382       const_lmm_element_t elem = nullptr;
383       double now               = surf_get_clock();
384
385       turnOff();
386       while ((var = constraint()->get_variable(&elem))) {
387         kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
388
389         if (action->get_state() == kernel::resource::Action::State::running ||
390             action->get_state() == kernel::resource::Action::State::ready) {
391           action->set_finish_time(now);
392           action->set_state(kernel::resource::Action::State::failed);
393         }
394       }
395     }
396     tmgr_trace_event_unref(&stateEvent_);
397   } else {
398     xbt_die("Unknown event!\n");
399   }
400
401   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)", constraint());
402 }
403
404 void NetworkCm02Link::setBandwidth(double value)
405 {
406   bandwidth_.peak = value;
407
408   model()->getMaxminSystem()->update_constraint_bound(constraint(),
409                                                       sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
410   TRACE_surf_link_set_bandwidth(surf_get_clock(), getCname(), sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
411
412   if (sg_weight_S_parameter > 0) {
413     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
414
415     kernel::lmm::Variable* var;
416     const_lmm_element_t elem     = nullptr;
417     const_lmm_element_t nextelem = nullptr;
418     int numelem                  = 0;
419     while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
420       NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
421       action->weight_ += delta;
422       if (not action->is_suspended())
423         model()->getMaxminSystem()->update_variable_weight(action->get_variable(), action->weight_);
424     }
425   }
426 }
427
428 void NetworkCm02Link::setLatency(double value)
429 {
430   double delta                 = value - latency_.peak;
431   kernel::lmm::Variable* var   = nullptr;
432   const_lmm_element_t elem     = nullptr;
433   const_lmm_element_t nextelem = nullptr;
434   int numelem                  = 0;
435
436   latency_.peak = value;
437
438   while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
439     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
440     action->latCurrent_ += delta;
441     action->weight_ += delta;
442     if (action->rate_ < 0)
443       model()->getMaxminSystem()->update_variable_bound(action->get_variable(),
444                                                         sg_tcp_gamma / (2.0 * action->latCurrent_));
445     else {
446       model()->getMaxminSystem()->update_variable_bound(
447           action->get_variable(), std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
448
449       if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
450         XBT_INFO("Flow is limited BYBANDWIDTH");
451       } else {
452         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
453       }
454     }
455     if (not action->is_suspended())
456       model()->getMaxminSystem()->update_variable_weight(action->get_variable(), action->weight_);
457   }
458 }
459
460 /**********
461  * Action *
462  **********/
463
464 void NetworkCm02Action::update_remains_lazy(double now)
465 {
466   if (suspended_ != Action::SuspendStates::not_suspended)
467     return;
468
469   double delta        = now - get_last_update();
470   double max_duration = get_max_duration();
471
472   if (get_remains_no_update() > 0) {
473     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
474               get_last_update());
475     update_remains(get_last_value() * delta);
476
477     XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
478   }
479
480   if (max_duration > NO_MAX_DURATION) {
481     double_update(&max_duration, delta, sg_surf_precision);
482     set_max_duration(max_duration);
483   }
484
485   if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
486       ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
487     finish(Action::State::done);
488     heapRemove(get_model()->getActionHeap());
489   }
490
491   set_last_update();
492   set_last_value(get_variable()->get_value());
493 }
494
495 }
496 }