Logo AND Algorithmique Numérique Distribuée

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