Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tiny code simplification
[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(lagrange_solve);
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
96   xbt_cfg_setdefault_double("network/latency-factor",     10.4);
97   xbt_cfg_setdefault_double("network/bandwidth-factor",    0.92);
98   xbt_cfg_setdefault_double("network/weight-S",         8775);
99 }
100
101
102 void surf_network_model_init_Reno2()
103 {
104   if (surf_network_model)
105     return;
106
107   surf_network_model = new simgrid::surf::NetworkCm02Model(lagrange_solve);
108   all_existing_models->push_back(surf_network_model);
109
110   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp, func_reno2_fpi);
111
112   xbt_cfg_setdefault_double("network/latency-factor",    10.4);
113   xbt_cfg_setdefault_double("network/bandwidth-factor",   0.92);
114   xbt_cfg_setdefault_double("network/weight-S",        8775);
115 }
116
117 void surf_network_model_init_Vegas()
118 {
119   if (surf_network_model)
120     return;
121
122   surf_network_model = new simgrid::surf::NetworkCm02Model(lagrange_solve);
123   all_existing_models->push_back(surf_network_model);
124
125   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
126
127   xbt_cfg_setdefault_double("network/latency-factor",    10.4);
128   xbt_cfg_setdefault_double("network/bandwidth-factor",   0.92);
129   xbt_cfg_setdefault_double("network/weight-S",        8775);
130 }
131
132 namespace simgrid {
133 namespace surf {
134
135 NetworkCm02Model::NetworkCm02Model()
136   :NetworkModel()
137 {
138   char *optim = xbt_cfg_get_string("network/optim");
139   bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
140
141   if (!strcmp(optim, "Full")) {
142     updateMechanism_ = UM_FULL;
143     selectiveUpdate_ = select;
144   } else if (!strcmp(optim, "Lazy")) {
145     updateMechanism_ = UM_LAZY;
146     selectiveUpdate_ = true;
147     xbt_assert(select || (xbt_cfg_is_default_value("network/maxmin-selective-update")),
148                "You cannot disable selective update when using the lazy update mechanism");
149   } else {
150     xbt_die("Unsupported optimization (%s) for this model. Accepted: Full, Lazy.", optim);
151   }
152
153   maxminSystem_ = lmm_system_new(selectiveUpdate_);
154
155   routing_model_create(createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE, nullptr));
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 Link* NetworkCm02Model::createLink(const char *name, double bandwidth, double latency, e_surf_link_sharing_policy_t policy,
174     xbt_dict_t properties)
175 {
176   return new NetworkCm02Link(this, name, properties, 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   return;
220 }
221
222
223 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
224 {
225   ActionList *running_actions = getRunningActionSet();
226
227   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
228      ; it != itend ; it=itNext) {
229     ++itNext;
230
231     NetworkCm02Action *action = static_cast<NetworkCm02Action*> (&*it);
232     XBT_DEBUG("Something happened to action %p", action);
233       double deltap = delta;
234       if (action->latency_ > 0) {
235         if (action->latency_ > deltap) {
236           double_update(&(action->latency_), deltap, sg_surf_precision);
237           deltap = 0.0;
238         } else {
239           double_update(&(deltap), action->latency_, sg_surf_precision);
240           action->latency_ = 0.0;
241         }
242         if (action->latency_ == 0.0 && !(action->isSuspended()))
243           lmm_update_variable_weight(maxminSystem_, action->getVariable(),
244               action->weight_);
245       }
246       if (TRACE_is_enabled()) {
247         int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
248         for (int i = 0; i < n; i++){
249           lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
250
251           NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
252           TRACE_surf_link_set_utilization(link->getName(),
253                                         action->getCategory(),
254                                         (lmm_variable_getvalue(action->getVariable())*
255                                             lmm_get_cnst_weight_from_var(maxminSystem_,
256                                                 action->getVariable(),
257                                                 i)),
258                                         action->getLastUpdate(),
259                                         now - action->getLastUpdate());
260         }
261       }
262       if (!lmm_get_number_of_cnst_from_var (maxminSystem_, action->getVariable())) {
263         /* There is actually no link used, hence an infinite bandwidth.
264          * This happens often when using models like vivaldi.
265          * In such case, just make sure that the action completes immediately.
266          */
267         action->updateRemains(action->getRemains());
268       }
269     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
270                   
271     if (action->getMaxDuration() != NO_MAX_DURATION)
272       action->updateMaxDuration(delta);
273       
274     if ((action->getRemains() <= 0) &&
275         (lmm_get_variable_weight(action->getVariable()) > 0)) {
276       action->finish();
277       action->setState(Action::State::done);
278       action->gapRemove();
279     } else if (((action->getMaxDuration() != NO_MAX_DURATION)
280         && (action->getMaxDuration() <= 0))) {
281       action->finish();
282       action->setState(Action::State::done);
283       action->gapRemove();
284     }
285   }
286 }
287
288 Action *NetworkCm02Model::communicate(kernel::routing::NetCard *src, kernel::routing::NetCard *dst,
289     double size, double rate)
290 {
291   int failed = 0;
292   double bandwidth_bound;
293   double latency = 0.0;
294   std::vector<Link*> * back_route = nullptr;
295   int constraints_per_variable = 0;
296
297   std::vector<Link*> *route = new std::vector<Link*>();
298
299   XBT_IN("(%s,%s,%g,%g)", src->name(), dst->name(), size, rate);
300
301   routing_platf->getRouteAndLatency(src, dst, route, &latency);
302   xbt_assert(! route->empty() || latency,
303              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
304              src->name(), dst->name());
305
306   for (auto link: *route)
307     if (link->isOff())
308       failed = 1;
309
310   if (sg_network_crosstraffic == 1) {
311     back_route = new std::vector<Link*>();
312     routing_platf->getRouteAndLatency(dst, src, back_route, nullptr);
313     for (auto link: *back_route)
314       if (link->isOff())
315         failed = 1;
316   }
317
318   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
319   action->weight_ = action->latency_ = latency;
320
321   action->rate_ = rate;
322   if (updateMechanism_ == UM_LAZY) {
323     action->indexHeap_ = -1;
324     action->lastUpdate_ = surf_get_clock();
325   }
326
327   bandwidth_bound = -1.0;
328   if (sg_weight_S_parameter > 0)
329     for (auto link : *route)
330       action->weight_ += sg_weight_S_parameter / link->getBandwidth();
331
332   for (auto link : *route) {
333     double bb = bandwidthFactor(size) * link->getBandwidth();
334     bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
335   }
336
337   action->latCurrent_ = action->latency_;
338   action->latency_ *= latencyFactor(size);
339   action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
340   if (haveGap_) {
341     xbt_assert(! route->empty(),
342                "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
343
344     gapAppend(size, route->at(0), action);
345     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)", action, src->name(), dst->name(), action->senderGap_, action->latency_);
346   }
347
348   constraints_per_variable = route->size();
349   if (back_route != nullptr)
350     constraints_per_variable += back_route->size();
351
352   if (action->latency_ > 0) {
353     action->variable_ = lmm_variable_new(maxminSystem_, action, 0.0, -1.0, constraints_per_variable);
354     if (updateMechanism_ == UM_LAZY) {
355       // add to the heap the event when the latency is payed
356       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->lastUpdate_);
357       action->heapInsert(actionHeap_, action->latency_ + action->lastUpdate_, route->empty() ? NORMAL : LATENCY);
358     }
359   } else
360     action->variable_ = lmm_variable_new(maxminSystem_, action, 1.0, -1.0, constraints_per_variable);
361
362   if (action->rate_ < 0) {
363     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
364   } else {
365     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)) : action->rate_);
366   }
367
368   for (auto link: *route)
369     lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), 1.0);
370
371   if (sg_network_crosstraffic == 1) {
372     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
373     for (auto link : *back_route)
374       lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), .05);
375      
376     //Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
377     //(You would also have to change lmm_element_concurrency())
378     //lmm_variable_concurrency_share_set(action->getVariable(),2);
379   }
380
381   delete route;
382   delete back_route;
383   XBT_OUT();
384
385   Link::onCommunicate(action, src, dst);
386   return action;
387 }
388
389 bool NetworkCm02Model::next_occuring_event_isIdempotent()
390 {
391   return true;
392 }
393
394 void NetworkCm02Model::gapAppend(double size, const Link* link, NetworkAction* action)
395 {
396   // Nothing
397 };
398
399 /************
400  * Resource *
401  ************/
402 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
403     double bandwidth,  double latency, e_surf_link_sharing_policy_t policy,
404     lmm_system_t system)
405 : Link(model, name, props, lmm_constraint_new(system, this, sg_bandwidth_factor * bandwidth))
406 {
407   m_bandwidth.scale = 1.0;
408   m_bandwidth.peak = bandwidth;
409
410   m_latency.scale = 1.0;
411   m_latency.peak = latency;
412
413   if (policy == SURF_LINK_FATPIPE)
414     lmm_constraint_shared(getConstraint());
415
416   Link::onCreation(this);
417 }
418
419
420
421 void NetworkCm02Link::apply_event(tmgr_trace_iterator_t triggered, double value)
422 {
423
424   /* Find out which of my iterators was triggered, and react accordingly */
425   if (triggered == m_bandwidth.event) {
426     updateBandwidth(value);
427     tmgr_trace_event_unref(&m_bandwidth.event);
428
429   } else if (triggered == m_latency.event) {
430     updateLatency(value);
431     tmgr_trace_event_unref(&m_latency.event);
432
433   } else if (triggered == m_stateEvent) {
434     if (value > 0)
435       turnOn();
436     else {
437       lmm_variable_t var = nullptr;
438       lmm_element_t elem = nullptr;
439       double now = surf_get_clock();
440
441       turnOff();
442       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
443         Action *action = static_cast<Action*>( lmm_variable_id(var) );
444
445         if (action->getState() == Action::State::running ||
446             action->getState() == Action::State::ready) {
447           action->setFinishTime(now);
448           action->setState(Action::State::failed);
449         }
450       }
451     }
452     tmgr_trace_event_unref(&m_stateEvent);
453   } else {
454     xbt_die("Unknown event!\n");
455   }
456
457   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
458        getConstraint());
459 }
460
461 void NetworkCm02Link::updateBandwidth(double value) {
462
463   m_bandwidth.peak = value;
464
465   lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
466       sg_bandwidth_factor * (m_bandwidth.peak * m_bandwidth.scale));
467   TRACE_surf_link_set_bandwidth(surf_get_clock(), getName(), sg_bandwidth_factor * m_bandwidth.peak * m_bandwidth.scale);
468
469   if (sg_weight_S_parameter > 0) {
470     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (m_bandwidth.peak * m_bandwidth.scale);
471
472     lmm_variable_t var;
473     lmm_element_t elem = nullptr, nextelem = nullptr;
474     int numelem = 0;
475     while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
476       NetworkCm02Action *action = (NetworkCm02Action*) lmm_variable_id(var);
477       action->weight_ += delta;
478       if (!action->isSuspended())
479         lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->weight_);
480     }
481   }
482 }
483
484 void NetworkCm02Link::updateLatency(double value){
485   double delta = value - m_latency.peak;
486   lmm_variable_t var = nullptr;
487   lmm_element_t elem = nullptr;
488   lmm_element_t nextelem = nullptr;
489   int numelem = 0;
490
491   m_latency.peak = value;
492
493   while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
494     NetworkCm02Action *action = (NetworkCm02Action*) lmm_variable_id(var);
495     action->latCurrent_ += delta;
496     action->weight_ += delta;
497     if (action->rate_ < 0)
498       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->latCurrent_));
499     else {
500       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
501                                 std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
502
503       if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
504         XBT_INFO("Flow is limited BYBANDWIDTH");
505       } else {
506         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
507       }
508     }
509     if (!action->isSuspended())
510       lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->weight_);
511   }
512 }
513
514 NetworkCm02Link::~NetworkCm02Link() {}
515
516 /**********
517  * Action *
518  **********/
519
520 NetworkCm02Action::~NetworkCm02Action() {}
521
522 void NetworkCm02Action::updateRemainingLazy(double now)
523 {
524   double delta = 0.0;
525
526   if (suspended_ != 0)
527     return;
528
529   delta = now - lastUpdate_;
530
531   if (remains_ > 0) {
532     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
533     double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
534
535     XBT_DEBUG("Updating action(%p): remains is now %f", this, remains_);
536   }
537
538   if (maxDuration_ != NO_MAX_DURATION)
539     double_update(&maxDuration_, delta, sg_surf_precision);
540
541   if (remains_ <= 0 &&
542       (lmm_get_variable_weight(getVariable()) > 0)) {
543     finish();
544     setState(Action::State::done);
545
546     heapRemove(getModel()->getActionHeap());
547   } else if (((maxDuration_ != NO_MAX_DURATION)
548       && (maxDuration_ <= 0))) {
549     finish();
550     setState(Action::State::done);
551     heapRemove(getModel()->getActionHeap());
552   }
553
554   lastUpdate_ = now;
555   lastValue_ = lmm_variable_getvalue(getVariable());
556 }
557
558 void NetworkCm02Link::gapAppend(double size, const Link* link, NetworkAction* action)
559 {
560   // Nothing
561 };
562
563 }
564 }