Logo AND Algorithmique Numérique Distribuée

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