Logo AND Algorithmique Numérique Distribuée

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