Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename surf::Link into surf::LinkImpl to make room for s4u
[simgrid.git] / src / surf / network_cm02.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <algorithm>
8
9 #include "maxmin_private.hpp"
10 #include "network_cm02.hpp"
11 #include "simgrid/s4u/host.hpp"
12 #include "simgrid/sg_config.h"
13 #include "src/instr/instr_private.h" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
16
17 double sg_sender_gap = 0.0;
18 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
19 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
20 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
21
22 double sg_tcp_gamma = 0.0;
23 int sg_network_crosstraffic = 0;
24
25 /************************************************************************/
26 /* New model based on optimizations discussed during Pedro Velho's thesis*/
27 /************************************************************************/
28 /* @techreport{VELHO:2011:HAL-00646896:1, */
29 /*      url = {http://hal.inria.fr/hal-00646896/en/}, */
30 /*      title = {{Flow-level network models: have we reached the limits?}}, */
31 /*      author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
32 /*      type = {Rapport de recherche}, */
33 /*      institution = {INRIA}, */
34 /*      number = {RR-7821}, */
35 /*      year = {2011}, */
36 /*      month = Nov, */
37 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
38 /*  } */
39 void surf_network_model_init_LegrandVelho()
40 {
41   if (surf_network_model)
42     return;
43
44   surf_network_model = new simgrid::surf::NetworkCm02Model();
45   all_existing_models->push_back(surf_network_model);
46
47   xbt_cfg_setdefault_double("network/latency-factor",      13.01);
48   xbt_cfg_setdefault_double("network/bandwidth-factor",     0.97);
49   xbt_cfg_setdefault_double("network/weight-S",         20537);
50 }
51
52 /***************************************************************************/
53 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
54 /***************************************************************************/
55 /* @TechReport{      rr-lip2002-40, */
56 /*   author        = {Henri Casanova and Loris Marchal}, */
57 /*   institution   = {LIP}, */
58 /*   title         = {A Network Model for Simulation of Grid Application}, */
59 /*   number        = {2002-40}, */
60 /*   month         = {oct}, */
61 /*   year          = {2002} */
62 /* } */
63 void surf_network_model_init_CM02()
64 {
65
66   if (surf_network_model)
67     return;
68
69   surf_network_model = new simgrid::surf::NetworkCm02Model();
70   all_existing_models->push_back(surf_network_model);
71
72   xbt_cfg_setdefault_double("network/latency-factor",   1.0);
73   xbt_cfg_setdefault_double("network/bandwidth-factor", 1.0);
74   xbt_cfg_setdefault_double("network/weight-S",         0.0);
75 }
76
77 /***************************************************************************/
78 /* The models from Steven H. Low                                           */
79 /***************************************************************************/
80 /* @article{Low03,                                                         */
81 /*   author={Steven H. Low},                                               */
82 /*   title={A Duality Model of {TCP} and Queue Management Algorithms},     */
83 /*   year={2003},                                                          */
84 /*   journal={{IEEE/ACM} Transactions on Networking},                      */
85 /*    volume={11}, number={4},                                             */
86 /*  }                                                                      */
87 void surf_network_model_init_Reno()
88 {
89   if (surf_network_model)
90     return;
91
92   surf_network_model = new simgrid::surf::NetworkCm02Model(lagrange_solve);
93   all_existing_models->push_back(surf_network_model);
94
95   lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
96
97   xbt_cfg_setdefault_double("network/latency-factor",     10.4);
98   xbt_cfg_setdefault_double("network/bandwidth-factor",    0.92);
99   xbt_cfg_setdefault_double("network/weight-S",         8775);
100 }
101
102
103 void surf_network_model_init_Reno2()
104 {
105   if (surf_network_model)
106     return;
107
108   surf_network_model = new simgrid::surf::NetworkCm02Model(lagrange_solve);
109   all_existing_models->push_back(surf_network_model);
110
111   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp, func_reno2_fpi);
112
113   xbt_cfg_setdefault_double("network/latency-factor",    10.4);
114   xbt_cfg_setdefault_double("network/bandwidth-factor",   0.92);
115   xbt_cfg_setdefault_double("network/weight-S",        8775);
116 }
117
118 void surf_network_model_init_Vegas()
119 {
120   if (surf_network_model)
121     return;
122
123   surf_network_model = new simgrid::surf::NetworkCm02Model(lagrange_solve);
124   all_existing_models->push_back(surf_network_model);
125
126   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
127
128   xbt_cfg_setdefault_double("network/latency-factor",    10.4);
129   xbt_cfg_setdefault_double("network/bandwidth-factor",   0.92);
130   xbt_cfg_setdefault_double("network/weight-S",        8775);
131 }
132
133 namespace simgrid {
134 namespace surf {
135
136 NetworkCm02Model::NetworkCm02Model()
137   :NetworkModel()
138 {
139   char *optim = xbt_cfg_get_string("network/optim");
140   bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
141
142   if (!strcmp(optim, "Full")) {
143     updateMechanism_ = UM_FULL;
144     selectiveUpdate_ = select;
145   } else if (!strcmp(optim, "Lazy")) {
146     updateMechanism_ = UM_LAZY;
147     selectiveUpdate_ = true;
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);
152   }
153
154   maxminSystem_ = lmm_system_new(selectiveUpdate_);
155   loopback_     = createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
156
157   if (updateMechanism_ == UM_LAZY) {
158     actionHeap_ = xbt_heap_new(8, nullptr);
159     xbt_heap_set_update_callback(actionHeap_, surf_action_lmm_update_index_heap);
160     modifiedSet_ = new ActionLmmList();
161     maxminSystem_->keep_track = modifiedSet_;
162   }
163 }
164 NetworkCm02Model::NetworkCm02Model(void (*specificSolveFun)(lmm_system_t self))
165   : NetworkCm02Model()
166 {
167   maxminSystem_->solve_fun = specificSolveFun;
168 }
169
170
171 NetworkCm02Model::~NetworkCm02Model() {}
172
173 LinkImpl* NetworkCm02Model::createLink(const char* name, double bandwidth, double latency,
174                                        e_surf_link_sharing_policy_t policy)
175 {
176   return new NetworkCm02Link(this, name, bandwidth, latency, policy, maxminSystem_);
177 }
178
179 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
180 {
181   while ((xbt_heap_size(actionHeap_) > 0)
182          && (double_equals(xbt_heap_maxkey(actionHeap_), now, sg_surf_precision))) {
183
184     NetworkCm02Action *action = static_cast<NetworkCm02Action*> (xbt_heap_pop(actionHeap_));
185     XBT_DEBUG("Something happened to action %p", action);
186     if (TRACE_is_enabled()) {
187       int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
188
189       for (int i = 0; i < n; i++){
190         lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
191         NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
192         double value = lmm_variable_getvalue(action->getVariable())*
193             lmm_get_cnst_weight_from_var(maxminSystem_, action->getVariable(), i);
194         TRACE_surf_link_set_utilization(link->getName(), action->getCategory(), value,
195            action->getLastUpdate(), now - action->getLastUpdate());
196       }
197     }
198
199     // if I am wearing a latency hat
200     if (action->getHat() == LATENCY) {
201       XBT_DEBUG("Latency paid for action %p. Activating", action);
202       lmm_update_variable_weight(maxminSystem_, action->getVariable(), action->weight_);
203       action->heapRemove(actionHeap_);
204       action->refreshLastUpdate();
205
206         // if I am wearing a max_duration or normal hat
207     } else if (action->getHat() == MAX_DURATION || action->getHat() == NORMAL) {
208         // no need to communicate anymore
209         // assume that flows that reached max_duration have remaining of 0
210       XBT_DEBUG("Action %p finished", action);
211       action->setRemains(0);
212       action->finish();
213       action->setState(Action::State::done);
214       action->heapRemove(actionHeap_);
215
216       action->gapRemove();
217     }
218   }
219 }
220
221
222 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
223 {
224   ActionList *running_actions = getRunningActionSet();
225
226   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
227      ; it != itend ; it=itNext) {
228     ++itNext;
229
230     NetworkCm02Action *action = static_cast<NetworkCm02Action*> (&*it);
231     XBT_DEBUG("Something happened to action %p", action);
232       double deltap = delta;
233       if (action->latency_ > 0) {
234         if (action->latency_ > deltap) {
235           double_update(&(action->latency_), deltap, sg_surf_precision);
236           deltap = 0.0;
237         } else {
238           double_update(&(deltap), action->latency_, sg_surf_precision);
239           action->latency_ = 0.0;
240         }
241         if (action->latency_ == 0.0 && !(action->isSuspended()))
242           lmm_update_variable_weight(maxminSystem_, action->getVariable(),
243               action->weight_);
244       }
245       if (TRACE_is_enabled()) {
246         int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
247         for (int i = 0; i < n; i++){
248           lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
249
250           NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
251           TRACE_surf_link_set_utilization(link->getName(),
252                                         action->getCategory(),
253                                         (lmm_variable_getvalue(action->getVariable())*
254                                             lmm_get_cnst_weight_from_var(maxminSystem_,
255                                                 action->getVariable(),
256                                                 i)),
257                                         action->getLastUpdate(),
258                                         now - action->getLastUpdate());
259         }
260       }
261       if (!lmm_get_number_of_cnst_from_var (maxminSystem_, action->getVariable())) {
262         /* There is actually no link used, hence an infinite bandwidth.
263          * This happens often when using models like vivaldi.
264          * In such case, just make sure that the action completes immediately.
265          */
266         action->updateRemains(action->getRemains());
267       }
268     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
269                   
270     if (action->getMaxDuration() != NO_MAX_DURATION)
271       action->updateMaxDuration(delta);
272       
273     if ((action->getRemains() <= 0) &&
274         (lmm_get_variable_weight(action->getVariable()) > 0)) {
275       action->finish();
276       action->setState(Action::State::done);
277       action->gapRemove();
278     } else if (((action->getMaxDuration() != NO_MAX_DURATION)
279         && (action->getMaxDuration() <= 0))) {
280       action->finish();
281       action->setState(Action::State::done);
282       action->gapRemove();
283     }
284   }
285 }
286
287 Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
288 {
289   int failed = 0;
290   double bandwidth_bound;
291   double latency = 0.0;
292   std::vector<LinkImpl*>* back_route = nullptr;
293   int constraints_per_variable = 0;
294
295   std::vector<LinkImpl*>* route = new std::vector<LinkImpl*>();
296
297   XBT_IN("(%s,%s,%g,%g)", src->cname(), dst->cname(), size, rate);
298
299   src->routeTo(dst, route, &latency);
300   xbt_assert(!route->empty() || latency,
301              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
302              src->cname(), dst->cname());
303
304   for (auto link: *route)
305     if (link->isOff())
306       failed = 1;
307
308   if (sg_network_crosstraffic == 1) {
309     back_route = new std::vector<LinkImpl*>();
310     dst->routeTo(src, back_route, nullptr);
311     for (auto link: *back_route)
312       if (link->isOff())
313         failed = 1;
314   }
315
316   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
317   action->weight_ = action->latency_ = latency;
318
319   action->rate_ = rate;
320   if (updateMechanism_ == UM_LAZY) {
321     action->indexHeap_ = -1;
322     action->lastUpdate_ = surf_get_clock();
323   }
324
325   bandwidth_bound = -1.0;
326   if (sg_weight_S_parameter > 0)
327     for (auto link : *route)
328       action->weight_ += sg_weight_S_parameter / link->bandwidth();
329
330   for (auto link : *route) {
331     double bb       = bandwidthFactor(size) * link->bandwidth();
332     bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
333   }
334
335   action->latCurrent_ = action->latency_;
336   action->latency_ *= latencyFactor(size);
337   action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
338   if (haveGap_) {
339     xbt_assert(! route->empty(),
340                "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
341
342     gapAppend(size, route->at(0), action);
343     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)", action, src->cname(), dst->cname(), action->senderGap_,
344               action->latency_);
345   }
346
347   constraints_per_variable = route->size();
348   if (back_route != nullptr)
349     constraints_per_variable += back_route->size();
350
351   if (action->latency_ > 0) {
352     action->variable_ = lmm_variable_new(maxminSystem_, action, 0.0, -1.0, constraints_per_variable);
353     if (updateMechanism_ == UM_LAZY) {
354       // add to the heap the event when the latency is payed
355       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->lastUpdate_);
356       action->heapInsert(actionHeap_, action->latency_ + action->lastUpdate_, route->empty() ? NORMAL : LATENCY);
357     }
358   } else
359     action->variable_ = lmm_variable_new(maxminSystem_, action, 1.0, -1.0, constraints_per_variable);
360
361   if (action->rate_ < 0) {
362     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
363   } else {
364     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)) : action->rate_);
365   }
366
367   for (auto link: *route)
368     lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), 1.0);
369
370   if (back_route != nullptr) { //  sg_network_crosstraffic was activated
371     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
372     for (auto link : *back_route)
373       lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), .05);
374      
375     //Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
376     //(You would also have to change lmm_element_concurrency())
377     //lmm_variable_concurrency_share_set(action->getVariable(),2);
378   }
379
380   delete route;
381   delete back_route;
382   XBT_OUT();
383
384   LinkImpl::onCommunicate(action, src, dst);
385   return action;
386 }
387
388 void NetworkCm02Model::gapAppend(double size, const LinkImpl* link, NetworkAction* action){
389     // Nothing
390 };
391
392 /************
393  * Resource *
394  ************/
395 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const char* name, double bandwidth, double latency,
396                                  e_surf_link_sharing_policy_t policy, lmm_system_t system)
397     : LinkImpl(model, name, lmm_constraint_new(system, this, sg_bandwidth_factor * bandwidth))
398 {
399   bandwidth_.scale = 1.0;
400   bandwidth_.peak  = bandwidth;
401
402   latency_.scale = 1.0;
403   latency_.peak  = latency;
404
405   if (policy == SURF_LINK_FATPIPE)
406     lmm_constraint_shared(getConstraint());
407
408   LinkImpl::onCreation(this);
409 }
410
411
412
413 void NetworkCm02Link::apply_event(tmgr_trace_iterator_t triggered, double value)
414 {
415
416   /* Find out which of my iterators was triggered, and react accordingly */
417   if (triggered == bandwidth_.event) {
418     setBandwidth(value);
419     tmgr_trace_event_unref(&bandwidth_.event);
420
421   } else if (triggered == latency_.event) {
422     setLatency(value);
423     tmgr_trace_event_unref(&latency_.event);
424
425   } else if (triggered == stateEvent_) {
426     if (value > 0)
427       turnOn();
428     else {
429       lmm_variable_t var = nullptr;
430       lmm_element_t elem = nullptr;
431       double now = surf_get_clock();
432
433       turnOff();
434       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
435         Action *action = static_cast<Action*>( lmm_variable_id(var) );
436
437         if (action->getState() == Action::State::running ||
438             action->getState() == Action::State::ready) {
439           action->setFinishTime(now);
440           action->setState(Action::State::failed);
441         }
442       }
443     }
444     tmgr_trace_event_unref(&stateEvent_);
445   } else {
446     xbt_die("Unknown event!\n");
447   }
448
449   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
450        getConstraint());
451 }
452
453 void NetworkCm02Link::setBandwidth(double value)
454 {
455
456   bandwidth_.peak = value;
457
458   lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
459                               sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
460   TRACE_surf_link_set_bandwidth(surf_get_clock(), getName(), sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
461
462   if (sg_weight_S_parameter > 0) {
463     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
464
465     lmm_variable_t var;
466     lmm_element_t elem = nullptr, nextelem = nullptr;
467     int numelem = 0;
468     while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
469       NetworkCm02Action *action = (NetworkCm02Action*) lmm_variable_id(var);
470       action->weight_ += delta;
471       if (!action->isSuspended())
472         lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->weight_);
473     }
474   }
475 }
476
477 void NetworkCm02Link::setLatency(double value)
478 {
479   double delta           = value - latency_.peak;
480   lmm_variable_t var = nullptr;
481   lmm_element_t elem = nullptr;
482   lmm_element_t nextelem = nullptr;
483   int numelem = 0;
484
485   latency_.peak = value;
486
487   while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
488     NetworkCm02Action *action = (NetworkCm02Action*) lmm_variable_id(var);
489     action->latCurrent_ += delta;
490     action->weight_ += delta;
491     if (action->rate_ < 0)
492       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->latCurrent_));
493     else {
494       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
495                                 std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
496
497       if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
498         XBT_INFO("Flow is limited BYBANDWIDTH");
499       } else {
500         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
501       }
502     }
503     if (!action->isSuspended())
504       lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->weight_);
505   }
506 }
507
508 NetworkCm02Link::~NetworkCm02Link() {}
509
510 /**********
511  * Action *
512  **********/
513
514 NetworkCm02Action::~NetworkCm02Action() {}
515
516 void NetworkCm02Action::updateRemainingLazy(double now)
517 {
518   double delta = 0.0;
519
520   if (suspended_ != 0)
521     return;
522
523   delta = now - lastUpdate_;
524
525   if (remains_ > 0) {
526     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
527     double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
528
529     XBT_DEBUG("Updating action(%p): remains is now %f", this, remains_);
530   }
531
532   if (maxDuration_ != NO_MAX_DURATION)
533     double_update(&maxDuration_, delta, sg_surf_precision);
534
535   if (remains_ <= 0 &&
536       (lmm_get_variable_weight(getVariable()) > 0)) {
537     finish();
538     setState(Action::State::done);
539
540     heapRemove(getModel()->getActionHeap());
541   } else if (((maxDuration_ != NO_MAX_DURATION)
542       && (maxDuration_ <= 0))) {
543     finish();
544     setState(Action::State::done);
545     heapRemove(getModel()->getActionHeap());
546   }
547
548   lastUpdate_ = now;
549   lastValue_ = lmm_variable_getvalue(getVariable());
550 }
551
552 void NetworkCm02Link::gapAppend(double size, const LinkImpl* link, NetworkAction* action){
553     // Nothing
554 };
555 }
556 }