Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c7b6abcc47e34d12539ee704c9df4a1a5b580d3d
[simgrid.git] / src / surf / network_cm02.cpp
1 /* Copyright (c) 2013-2017. 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 "maxmin_private.hpp"
9 #include "network_cm02.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/sg_config.h"
12 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
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   lmm_set_default_protocol_function(func_reno_f, func_reno_fp, 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(&lagrange_solve);
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   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp, func_reno2_fpi);
107
108   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
109   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
110   xbt_cfg_setdefault_double("network/weight-S", 20537);
111
112   surf_network_model = new simgrid::surf::NetworkCm02Model(&lagrange_solve);
113   all_existing_models->push_back(surf_network_model);
114 }
115
116 void surf_network_model_init_Vegas()
117 {
118   if (surf_network_model)
119     return;
120
121   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
122
123   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
124   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
125   xbt_cfg_setdefault_double("network/weight-S", 20537);
126
127   surf_network_model = new simgrid::surf::NetworkCm02Model(&lagrange_solve);
128   all_existing_models->push_back(surf_network_model);
129 }
130
131 namespace simgrid {
132 namespace surf {
133
134 NetworkCm02Model::NetworkCm02Model()
135   :NetworkModel()
136 {
137   std::string optim = xbt_cfg_get_string("network/optim");
138   bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
139
140   if (optim == "Full") {
141     setUpdateMechanism(UM_FULL);
142     selectiveUpdate_ = select;
143   } else if (optim == "Lazy") {
144     setUpdateMechanism(UM_LAZY);
145     selectiveUpdate_ = true;
146     xbt_assert(select || (xbt_cfg_is_default_value("network/maxmin-selective-update")),
147                "You cannot disable selective update when using the lazy update mechanism");
148   } else {
149     xbt_die("Unsupported optimization (%s) for this model. Accepted: Full, Lazy.", optim.c_str());
150   }
151
152   maxminSystem_ = lmm_system_new(selectiveUpdate_);
153   loopback_     = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
154
155   if (getUpdateMechanism() == UM_LAZY) {
156     modifiedSet_ = new ActionLmmList();
157     maxminSystem_->keep_track = modifiedSet_;
158   }
159 }
160 NetworkCm02Model::NetworkCm02Model(void (*specificSolveFun)(lmm_system_t self))
161   : NetworkCm02Model()
162 {
163   maxminSystem_->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, maxminSystem_);
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 = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
180
181       for (int i = 0; i < n; i++){
182         lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
183         NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
184         double value = lmm_variable_getvalue(action->getVariable())*
185             lmm_get_cnst_weight_from_var(maxminSystem_, action->getVariable(), i);
186         TRACE_surf_link_set_utilization(link->getCname(), action->getCategory(), value, action->getLastUpdate(),
187                                         now - action->getLastUpdate());
188       }
189     }
190
191     // if I am wearing a latency hat
192     if (action->getHat() == LATENCY) {
193       XBT_DEBUG("Latency paid for action %p. Activating", action);
194       lmm_update_variable_weight(maxminSystem_, action->getVariable(), action->weight_);
195       action->heapRemove(getActionHeap());
196       action->refreshLastUpdate();
197
198         // if I am wearing a max_duration or normal hat
199     } else if (action->getHat() == MAX_DURATION || action->getHat() == 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->setRemains(0);
204       action->finish(Action::State::done);
205       action->heapRemove(getActionHeap());
206     }
207   }
208 }
209
210
211 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
212 {
213   ActionList *running_actions = getRunningActionSet();
214   ActionList::iterator it(running_actions->begin());
215   ActionList::iterator itend(running_actions->end());
216   while (it != itend) {
217     NetworkCm02Action *action = static_cast<NetworkCm02Action*> (&*it);
218     ++it;
219     XBT_DEBUG("Something happened to action %p", action);
220       double deltap = delta;
221       if (action->latency_ > 0) {
222         if (action->latency_ > deltap) {
223           double_update(&(action->latency_), deltap, sg_surf_precision);
224           deltap = 0.0;
225         } else {
226           double_update(&(deltap), action->latency_, sg_surf_precision);
227           action->latency_ = 0.0;
228         }
229         if (action->latency_ <= 0.0 && not action->isSuspended())
230           lmm_update_variable_weight(maxminSystem_, action->getVariable(), action->weight_);
231       }
232       if (TRACE_is_enabled()) {
233         int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
234         for (int i = 0; i < n; i++){
235           lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
236
237           NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
238           TRACE_surf_link_set_utilization(link->getCname(), action->getCategory(),
239                                           (lmm_variable_getvalue(action->getVariable()) *
240                                            lmm_get_cnst_weight_from_var(maxminSystem_, action->getVariable(), i)),
241                                           action->getLastUpdate(), now - action->getLastUpdate());
242         }
243       }
244       if (not lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable())) {
245         /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
246          * vivaldi. In such case, just make sure that the action completes immediately.
247          */
248         action->updateRemains(action->getRemains());
249       }
250     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
251
252     if (action->getMaxDuration() > NO_MAX_DURATION)
253       action->updateMaxDuration(delta);
254
255     if (((action->getRemains() <= 0) && (lmm_get_variable_weight(action->getVariable()) > 0)) ||
256         ((action->getMaxDuration() > NO_MAX_DURATION) && (action->getMaxDuration() <= 0))) {
257       action->finish(Action::State::done);
258     }
259   }
260 }
261
262 Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
263 {
264   int failed = 0;
265   double latency = 0.0;
266   std::vector<LinkImpl*> back_route;
267   std::vector<LinkImpl*> route;
268
269   XBT_IN("(%s,%s,%g,%g)", src->getCname(), dst->getCname(), size, rate);
270
271   src->routeTo(dst, route, &latency);
272   xbt_assert(not route.empty() || latency,
273              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
274              src->getCname(), dst->getCname());
275
276   for (auto const& link : route)
277     if (link->isOff())
278       failed = 1;
279
280   if (sg_network_crosstraffic == 1) {
281     dst->routeTo(src, back_route, nullptr);
282     for (auto const& link : back_route)
283       if (link->isOff())
284         failed = 1;
285   }
286
287   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
288   action->weight_ = latency;
289   action->latency_ = latency;
290   action->rate_ = rate;
291   if (getUpdateMechanism() == UM_LAZY) {
292     action->refreshLastUpdate();
293   }
294
295   double bandwidth_bound = -1.0;
296   if (sg_weight_S_parameter > 0)
297     for (auto const& link : route)
298       action->weight_ += sg_weight_S_parameter / link->bandwidth();
299
300   for (auto const& link : route) {
301     double bb       = bandwidthFactor(size) * link->bandwidth();
302     bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
303   }
304
305   action->latCurrent_ = action->latency_;
306   action->latency_ *= latencyFactor(size);
307   action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
308
309   int constraints_per_variable = route.size();
310   constraints_per_variable += back_route.size();
311
312   if (action->latency_ > 0) {
313     action->setVariable(lmm_variable_new(maxminSystem_, action, 0.0, -1.0, constraints_per_variable));
314     if (getUpdateMechanism() == UM_LAZY) {
315       // add to the heap the event when the latency is payed
316       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->getLastUpdate());
317       action->heapInsert(getActionHeap(), action->latency_ + action->getLastUpdate(), route.empty() ? NORMAL : LATENCY);
318     }
319   } else
320     action->setVariable(lmm_variable_new(maxminSystem_, action, 1.0, -1.0, constraints_per_variable));
321
322   if (action->rate_ < 0) {
323     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
324   } else {
325     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)) : action->rate_);
326   }
327
328   for (auto const& link : route)
329     lmm_expand(maxminSystem_, link->constraint(), action->getVariable(), 1.0);
330
331   if (not back_route.empty()) { //  sg_network_crosstraffic was activated
332     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
333     for (auto const& link : back_route)
334       lmm_expand(maxminSystem_, link->constraint(), action->getVariable(), .05);
335
336     //Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
337     //(You would also have to change lmm_element_concurrency())
338     //lmm_variable_concurrency_share_set(action->getVariable(),2);
339   }
340   XBT_OUT();
341
342   simgrid::s4u::Link::onCommunicate(action, src, dst);
343   return action;
344 }
345
346 /************
347  * Resource *
348  ************/
349 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
350                                  e_surf_link_sharing_policy_t policy, lmm_system_t system)
351     : LinkImpl(model, name, lmm_constraint_new(system, this, sg_bandwidth_factor * bandwidth))
352 {
353   bandwidth_.scale = 1.0;
354   bandwidth_.peak  = bandwidth;
355
356   latency_.scale = 1.0;
357   latency_.peak  = latency;
358
359   if (policy == SURF_LINK_FATPIPE)
360     lmm_constraint_shared(constraint());
361
362   simgrid::s4u::Link::onCreation(this->piface_);
363 }
364
365 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
366 {
367   /* Find out which of my iterators was triggered, and react accordingly */
368   if (triggered == bandwidth_.event) {
369     setBandwidth(value);
370     tmgr_trace_event_unref(&bandwidth_.event);
371
372   } else if (triggered == latency_.event) {
373     setLatency(value);
374     tmgr_trace_event_unref(&latency_.event);
375
376   } else if (triggered == stateEvent_) {
377     if (value > 0)
378       turnOn();
379     else {
380       lmm_variable_t var = nullptr;
381       lmm_element_t elem = nullptr;
382       double now = surf_get_clock();
383
384       turnOff();
385       while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
386         Action *action = static_cast<Action*>( lmm_variable_id(var) );
387
388         if (action->getState() == Action::State::running ||
389             action->getState() == Action::State::ready) {
390           action->setFinishTime(now);
391           action->setState(Action::State::failed);
392         }
393       }
394     }
395     tmgr_trace_event_unref(&stateEvent_);
396   } else {
397     xbt_die("Unknown event!\n");
398   }
399
400   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)", constraint());
401 }
402
403 void NetworkCm02Link::setBandwidth(double value)
404 {
405
406   bandwidth_.peak = value;
407
408   lmm_update_constraint_bound(model()->getMaxminSystem(), 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     lmm_variable_t var;
416     lmm_element_t elem = nullptr;
417     lmm_element_t nextelem = nullptr;
418     int numelem = 0;
419     while ((var = lmm_get_var_from_cnst_safe(model()->getMaxminSystem(), constraint(), &elem, &nextelem, &numelem))) {
420       NetworkCm02Action *action = static_cast<NetworkCm02Action*>(lmm_variable_id(var));
421       action->weight_ += delta;
422       if (not action->isSuspended())
423         lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), action->weight_);
424     }
425   }
426 }
427
428 void NetworkCm02Link::setLatency(double value)
429 {
430   double delta           = value - latency_.peak;
431   lmm_variable_t var = nullptr;
432   lmm_element_t elem = nullptr;
433   lmm_element_t nextelem = nullptr;
434   int numelem = 0;
435
436   latency_.peak = value;
437
438   while ((var = lmm_get_var_from_cnst_safe(model()->getMaxminSystem(), constraint(), &elem, &nextelem, &numelem))) {
439     NetworkCm02Action *action = static_cast<NetworkCm02Action*>(lmm_variable_id(var));
440     action->latCurrent_ += delta;
441     action->weight_ += delta;
442     if (action->rate_ < 0)
443       lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(),
444                                 sg_tcp_gamma / (2.0 * action->latCurrent_));
445     else {
446       lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(),
447                                 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->isSuspended())
456       lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), action->weight_);
457   }
458 }
459
460 /**********
461  * Action *
462  **********/
463
464 void NetworkCm02Action::updateRemainingLazy(double now)
465 {
466   if (suspended_ != 0)
467     return;
468
469   double delta        = now - getLastUpdate();
470   double max_duration = getMaxDuration();
471
472   if (getRemainsNoUpdate() > 0) {
473     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, getRemainsNoUpdate(), getLastUpdate());
474     updateRemains(getLastValue() * delta);
475
476     XBT_DEBUG("Updating action(%p): remains is now %f", this, getRemainsNoUpdate());
477   }
478
479   if (max_duration > NO_MAX_DURATION) {
480     double_update(&max_duration, delta, sg_surf_precision);
481     setMaxDuration(max_duration);
482   }
483
484   if ((getRemainsNoUpdate() <= 0 && (lmm_get_variable_weight(getVariable()) > 0)) ||
485       ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
486     finish(Action::State::done);
487     heapRemove(getModel()->getActionHeap());
488   }
489
490   refreshLastUpdate();
491   setLastValue(lmm_variable_getvalue(getVariable()));
492 }
493
494 }
495 }