Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8dc9d2bd3c68b2cd029b82f6a65eaaa8db0eb9d2
[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
156   routing_model_create(createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE, nullptr));
157
158   if (updateMechanism_ == UM_LAZY) {
159     actionHeap_ = xbt_heap_new(8, nullptr);
160     xbt_heap_set_update_callback(actionHeap_, surf_action_lmm_update_index_heap);
161     modifiedSet_ = new ActionLmmList();
162     maxminSystem_->keep_track = modifiedSet_;
163   }
164 }
165 NetworkCm02Model::NetworkCm02Model(void (*specificSolveFun)(lmm_system_t self))
166   : NetworkCm02Model()
167 {
168   maxminSystem_->solve_fun = specificSolveFun;
169 }
170
171
172 NetworkCm02Model::~NetworkCm02Model() {}
173
174 Link* NetworkCm02Model::createLink(const char *name, double bandwidth, double latency, e_surf_link_sharing_policy_t policy,
175     xbt_dict_t properties)
176 {
177   return new NetworkCm02Link(this, name, properties, bandwidth, latency, policy, maxminSystem_);
178 }
179
180 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
181 {
182   while ((xbt_heap_size(actionHeap_) > 0)
183          && (double_equals(xbt_heap_maxkey(actionHeap_), now, sg_surf_precision))) {
184
185     NetworkCm02Action *action = static_cast<NetworkCm02Action*> (xbt_heap_pop(actionHeap_));
186     XBT_DEBUG("Something happened to action %p", action);
187     if (TRACE_is_enabled()) {
188       int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
189
190       for (int i = 0; i < n; i++){
191         lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
192         NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
193         double value = lmm_variable_getvalue(action->getVariable())*
194             lmm_get_cnst_weight_from_var(maxminSystem_, action->getVariable(), i);
195         TRACE_surf_link_set_utilization(link->getName(), action->getCategory(), value,
196            action->getLastUpdate(), now - action->getLastUpdate());
197       }
198     }
199
200     // if I am wearing a latency hat
201     if (action->getHat() == LATENCY) {
202       XBT_DEBUG("Latency paid for action %p. Activating", action);
203       lmm_update_variable_weight(maxminSystem_, action->getVariable(), action->weight_);
204       action->heapRemove(actionHeap_);
205       action->refreshLastUpdate();
206
207         // if I am wearing a max_duration or normal hat
208     } else if (action->getHat() == MAX_DURATION || action->getHat() == NORMAL) {
209         // no need to communicate anymore
210         // assume that flows that reached max_duration have remaining of 0
211       XBT_DEBUG("Action %p finished", action);
212       action->setRemains(0);
213       action->finish();
214       action->setState(Action::State::done);
215       action->heapRemove(actionHeap_);
216
217       action->gapRemove();
218     }
219   }
220   return;
221 }
222
223
224 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
225 {
226   ActionList *running_actions = getRunningActionSet();
227
228   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
229      ; it != itend ; it=itNext) {
230     ++itNext;
231
232     NetworkCm02Action *action = static_cast<NetworkCm02Action*> (&*it);
233     XBT_DEBUG("Something happened to action %p", action);
234       double deltap = delta;
235       if (action->latency_ > 0) {
236         if (action->latency_ > deltap) {
237           double_update(&(action->latency_), deltap, sg_surf_precision);
238           deltap = 0.0;
239         } else {
240           double_update(&(deltap), action->latency_, sg_surf_precision);
241           action->latency_ = 0.0;
242         }
243         if (action->latency_ == 0.0 && !(action->isSuspended()))
244           lmm_update_variable_weight(maxminSystem_, action->getVariable(),
245               action->weight_);
246       }
247       if (TRACE_is_enabled()) {
248         int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
249         for (int i = 0; i < n; i++){
250           lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
251
252           NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
253           TRACE_surf_link_set_utilization(link->getName(),
254                                         action->getCategory(),
255                                         (lmm_variable_getvalue(action->getVariable())*
256                                             lmm_get_cnst_weight_from_var(maxminSystem_,
257                                                 action->getVariable(),
258                                                 i)),
259                                         action->getLastUpdate(),
260                                         now - action->getLastUpdate());
261         }
262       }
263       if (!lmm_get_number_of_cnst_from_var (maxminSystem_, action->getVariable())) {
264         /* There is actually no link used, hence an infinite bandwidth.
265          * This happens often when using models like vivaldi.
266          * In such case, just make sure that the action completes immediately.
267          */
268         action->updateRemains(action->getRemains());
269       }
270     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
271                   
272     if (action->getMaxDuration() != NO_MAX_DURATION)
273       action->updateMaxDuration(delta);
274       
275     if ((action->getRemains() <= 0) &&
276         (lmm_get_variable_weight(action->getVariable()) > 0)) {
277       action->finish();
278       action->setState(Action::State::done);
279       action->gapRemove();
280     } else if (((action->getMaxDuration() != NO_MAX_DURATION)
281         && (action->getMaxDuration() <= 0))) {
282       action->finish();
283       action->setState(Action::State::done);
284       action->gapRemove();
285     }
286   }
287 }
288
289 Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, 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().c_str(), dst->name().c_str(), size, rate);
300
301   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard, 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().c_str(), dst->name().c_str());
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->pimpl_netcard, src->pimpl_netcard, 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().c_str(), dst->name().c_str(), action->senderGap_,
346               action->latency_);
347   }
348
349   constraints_per_variable = route->size();
350   if (back_route != nullptr)
351     constraints_per_variable += back_route->size();
352
353   if (action->latency_ > 0) {
354     action->variable_ = lmm_variable_new(maxminSystem_, action, 0.0, -1.0, constraints_per_variable);
355     if (updateMechanism_ == UM_LAZY) {
356       // add to the heap the event when the latency is payed
357       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->lastUpdate_);
358       action->heapInsert(actionHeap_, action->latency_ + action->lastUpdate_, route->empty() ? NORMAL : LATENCY);
359     }
360   } else
361     action->variable_ = lmm_variable_new(maxminSystem_, action, 1.0, -1.0, constraints_per_variable);
362
363   if (action->rate_ < 0) {
364     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
365   } else {
366     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)) : action->rate_);
367   }
368
369   for (auto link: *route)
370     lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), 1.0);
371
372   if (sg_network_crosstraffic == 1) {
373     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
374     for (auto link : *back_route)
375       lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), .05);
376      
377     //Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
378     //(You would also have to change lmm_element_concurrency())
379     //lmm_variable_concurrency_share_set(action->getVariable(),2);
380   }
381
382   delete route;
383   delete back_route;
384   XBT_OUT();
385
386   Link::onCommunicate(action, src, dst);
387   return action;
388 }
389
390 void NetworkCm02Model::gapAppend(double size, const Link* link, NetworkAction* action)
391 {
392   // Nothing
393 };
394
395 /************
396  * Resource *
397  ************/
398 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
399     double bandwidth,  double latency, e_surf_link_sharing_policy_t policy,
400     lmm_system_t system)
401 : Link(model, name, props, lmm_constraint_new(system, this, sg_bandwidth_factor * bandwidth))
402 {
403   m_bandwidth.scale = 1.0;
404   m_bandwidth.peak = bandwidth;
405
406   m_latency.scale = 1.0;
407   m_latency.peak = latency;
408
409   if (policy == SURF_LINK_FATPIPE)
410     lmm_constraint_shared(getConstraint());
411
412   Link::onCreation(this);
413 }
414
415
416
417 void NetworkCm02Link::apply_event(tmgr_trace_iterator_t triggered, double value)
418 {
419
420   /* Find out which of my iterators was triggered, and react accordingly */
421   if (triggered == m_bandwidth.event) {
422     updateBandwidth(value);
423     tmgr_trace_event_unref(&m_bandwidth.event);
424
425   } else if (triggered == m_latency.event) {
426     updateLatency(value);
427     tmgr_trace_event_unref(&m_latency.event);
428
429   } else if (triggered == m_stateEvent) {
430     if (value > 0)
431       turnOn();
432     else {
433       lmm_variable_t var = nullptr;
434       lmm_element_t elem = nullptr;
435       double now = surf_get_clock();
436
437       turnOff();
438       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
439         Action *action = static_cast<Action*>( lmm_variable_id(var) );
440
441         if (action->getState() == Action::State::running ||
442             action->getState() == Action::State::ready) {
443           action->setFinishTime(now);
444           action->setState(Action::State::failed);
445         }
446       }
447     }
448     tmgr_trace_event_unref(&m_stateEvent);
449   } else {
450     xbt_die("Unknown event!\n");
451   }
452
453   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
454        getConstraint());
455 }
456
457 void NetworkCm02Link::updateBandwidth(double value) {
458
459   m_bandwidth.peak = value;
460
461   lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
462       sg_bandwidth_factor * (m_bandwidth.peak * m_bandwidth.scale));
463   TRACE_surf_link_set_bandwidth(surf_get_clock(), getName(), sg_bandwidth_factor * m_bandwidth.peak * m_bandwidth.scale);
464
465   if (sg_weight_S_parameter > 0) {
466     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (m_bandwidth.peak * m_bandwidth.scale);
467
468     lmm_variable_t var;
469     lmm_element_t elem = nullptr, nextelem = nullptr;
470     int numelem = 0;
471     while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
472       NetworkCm02Action *action = (NetworkCm02Action*) lmm_variable_id(var);
473       action->weight_ += delta;
474       if (!action->isSuspended())
475         lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->weight_);
476     }
477   }
478 }
479
480 void NetworkCm02Link::updateLatency(double value){
481   double delta = value - m_latency.peak;
482   lmm_variable_t var = nullptr;
483   lmm_element_t elem = nullptr;
484   lmm_element_t nextelem = nullptr;
485   int numelem = 0;
486
487   m_latency.peak = value;
488
489   while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
490     NetworkCm02Action *action = (NetworkCm02Action*) lmm_variable_id(var);
491     action->latCurrent_ += delta;
492     action->weight_ += delta;
493     if (action->rate_ < 0)
494       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->latCurrent_));
495     else {
496       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
497                                 std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
498
499       if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
500         XBT_INFO("Flow is limited BYBANDWIDTH");
501       } else {
502         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
503       }
504     }
505     if (!action->isSuspended())
506       lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->weight_);
507   }
508 }
509
510 NetworkCm02Link::~NetworkCm02Link() {}
511
512 /**********
513  * Action *
514  **********/
515
516 NetworkCm02Action::~NetworkCm02Action() {}
517
518 void NetworkCm02Action::updateRemainingLazy(double now)
519 {
520   double delta = 0.0;
521
522   if (suspended_ != 0)
523     return;
524
525   delta = now - lastUpdate_;
526
527   if (remains_ > 0) {
528     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
529     double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
530
531     XBT_DEBUG("Updating action(%p): remains is now %f", this, remains_);
532   }
533
534   if (maxDuration_ != NO_MAX_DURATION)
535     double_update(&maxDuration_, delta, sg_surf_precision);
536
537   if (remains_ <= 0 &&
538       (lmm_get_variable_weight(getVariable()) > 0)) {
539     finish();
540     setState(Action::State::done);
541
542     heapRemove(getModel()->getActionHeap());
543   } else if (((maxDuration_ != NO_MAX_DURATION)
544       && (maxDuration_ <= 0))) {
545     finish();
546     setState(Action::State::done);
547     heapRemove(getModel()->getActionHeap());
548   }
549
550   lastUpdate_ = now;
551   lastValue_ = lmm_variable_getvalue(getVariable());
552 }
553
554 void NetworkCm02Link::gapAppend(double size, const Link* link, NetworkAction* action)
555 {
556   // Nothing
557 };
558
559 }
560 }