Logo AND Algorithmique Numérique Distribuée

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