Logo AND Algorithmique Numérique Distribuée

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