Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sort out the Link::onCommunicate signal
[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(void)
39 {
40   if (surf_network_model)
41     return;
42
43   simgrid::surf::on_link.connect(netlink_parse_init);
44   surf_network_model = new simgrid::surf::NetworkCm02Model();
45   xbt_dynar_push(all_existing_models, &surf_network_model);
46
47   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",      13.01);
48   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",     0.97);
49   xbt_cfg_setdefault_double(_sg_cfg_set, "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(void)
64 {
65
66   if (surf_network_model)
67     return;
68
69   simgrid::surf::on_link.connect(netlink_parse_init);
70   surf_network_model = new simgrid::surf::NetworkCm02Model();
71   xbt_dynar_push(all_existing_models, &surf_network_model);
72
73   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",   1.0);
74   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 1.0);
75   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S",         0.0);
76 }
77
78 /***************************************************************************/
79 /* The models from Steven H. Low                                           */
80 /***************************************************************************/
81 /* @article{Low03,                                                         */
82 /*   author={Steven H. Low},                                               */
83 /*   title={A Duality Model of {TCP} and Queue Management Algorithms},     */
84 /*   year={2003},                                                          */
85 /*   journal={{IEEE/ACM} Transactions on Networking},                      */
86 /*    volume={11}, number={4},                                             */
87 /*  }                                                                      */
88 void surf_network_model_init_Reno(void)
89 {
90   if (surf_network_model)
91     return;
92
93   simgrid::surf::on_link.connect(netlink_parse_init);
94   surf_network_model = new simgrid::surf::NetworkCm02Model();
95   xbt_dynar_push(all_existing_models, &surf_network_model);
96
97   lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
98   surf_network_model->f_networkSolve = lagrange_solve;
99
100   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",     10.4);
101   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",    0.92);
102   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S",         8775);
103 }
104
105
106 void surf_network_model_init_Reno2(void)
107 {
108   if (surf_network_model)
109     return;
110
111   simgrid::surf::on_link.connect(netlink_parse_init);
112   surf_network_model = new simgrid::surf::NetworkCm02Model();
113   xbt_dynar_push(all_existing_models, &surf_network_model);
114
115   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp, func_reno2_fpi);
116   surf_network_model->f_networkSolve = lagrange_solve;
117
118   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",    10.4);
119   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",   0.92);
120   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S",        8775);
121 }
122
123 void surf_network_model_init_Vegas(void)
124 {
125   if (surf_network_model)
126     return;
127
128   simgrid::surf::on_link.connect(netlink_parse_init);
129   surf_network_model = new simgrid::surf::NetworkCm02Model();
130   xbt_dynar_push(all_existing_models, &surf_network_model);
131
132   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
133   surf_network_model->f_networkSolve = lagrange_solve;
134
135   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",    10.4);
136   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",   0.92);
137   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S",        8775);
138 }
139
140 namespace simgrid {
141 namespace surf {
142
143 NetworkCm02Model::NetworkCm02Model()
144   :NetworkModel()
145 {
146   char *optim = xbt_cfg_get_string(_sg_cfg_set, "network/optim");
147   int select = xbt_cfg_get_boolean(_sg_cfg_set, "network/maxmin_selective_update");
148
149   if (!strcmp(optim, "Full")) {
150     updateMechanism_ = UM_FULL;
151     selectiveUpdate_ = select;
152   } else if (!strcmp(optim, "Lazy")) {
153     updateMechanism_ = UM_LAZY;
154     selectiveUpdate_ = 1;
155     xbt_assert((select == 1) || (xbt_cfg_is_default_value(_sg_cfg_set, "network/maxmin_selective_update")),
156                "Disabling selective update while using the lazy update mechanism is dumb!");
157   } else {
158     xbt_die("Unsupported optimization (%s) for this model", optim);
159   }
160
161   if (!maxminSystem_)
162     maxminSystem_ = lmm_system_new(selectiveUpdate_);
163
164   routing_model_create(createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE, NULL));
165
166   if (updateMechanism_ == UM_LAZY) {
167   actionHeap_ = xbt_heap_new(8, NULL);
168   xbt_heap_set_update_callback(actionHeap_, surf_action_lmm_update_index_heap);
169   modifiedSet_ = new ActionLmmList();
170   maxminSystem_->keep_track = modifiedSet_;
171   }
172 }
173
174 Link* NetworkCm02Model::createLink(const char *name, double bandwidth, double latency, e_surf_link_sharing_policy_t policy, xbt_dict_t properties)
175 {
176   return new NetworkCm02Link(this, name, properties, maxminSystem_, sg_bandwidth_factor * bandwidth, bandwidth, latency, policy);
177 }
178
179 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
180 {
181   NetworkCm02Action *action;
182   while ((xbt_heap_size(actionHeap_) > 0)
183          && (double_equals(xbt_heap_maxkey(actionHeap_), now, sg_surf_precision))) {
184     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         TRACE_surf_link_set_utilization(link->getName(),
193                                         action->getCategory(),
194                                         (lmm_variable_getvalue(action->getVariable())*
195                                             lmm_get_cnst_weight_from_var(maxminSystem_,
196                                                 action->getVariable(),
197                                                 i)),
198                                         action->getLastUpdate(),
199                                         now - action->getLastUpdate());
200       }
201     }
202
203     // if I am wearing a latency hat
204     if (action->getHat() == LATENCY) {
205       XBT_DEBUG("Latency paid for action %p. Activating", action);
206       lmm_update_variable_weight(maxminSystem_, action->getVariable(), action->weight_);
207       action->heapRemove(actionHeap_);
208       action->refreshLastUpdate();
209
210         // if I am wearing a max_duration or normal hat
211     } else if (action->getHat() == MAX_DURATION ||
212         action->getHat() == NORMAL) {
213         // no need to communicate anymore
214         // assume that flows that reached max_duration have remaining of 0
215       XBT_DEBUG("Action %p finished", action);
216       action->setRemains(0);
217       action->finish();
218       action->setState(Action::State::done);
219       action->heapRemove(actionHeap_);
220
221       action->gapRemove();
222     }
223   }
224   return;
225 }
226
227
228 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
229 {
230   NetworkCm02Action *action;
231   ActionList *running_actions = getRunningActionSet();
232
233   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
234      ; it != itend ; it=itNext) {
235   ++itNext;
236
237     action = static_cast<NetworkCm02Action*> (&*it);
238     XBT_DEBUG("Something happened to action %p", action);
239       double deltap = delta;
240       if (action->latency_ > 0) {
241         if (action->latency_ > deltap) {
242           double_update(&(action->latency_), deltap, sg_surf_precision);
243           deltap = 0.0;
244         } else {
245           double_update(&(deltap), action->latency_, sg_surf_precision);
246           action->latency_ = 0.0;
247         }
248         if (action->latency_ == 0.0 && !(action->isSuspended()))
249           lmm_update_variable_weight(maxminSystem_, action->getVariable(),
250               action->weight_);
251       }
252       if (TRACE_is_enabled()) {
253         int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
254         for (int i = 0; i < n; i++){
255           lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
256
257           NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
258           TRACE_surf_link_set_utilization(link->getName(),
259                                         action->getCategory(),
260                                         (lmm_variable_getvalue(action->getVariable())*
261                                             lmm_get_cnst_weight_from_var(maxminSystem_,
262                                                 action->getVariable(),
263                                                 i)),
264                                         action->getLastUpdate(),
265                                         now - action->getLastUpdate());
266         }
267       }
268       if (!lmm_get_number_of_cnst_from_var (maxminSystem_, action->getVariable())) {
269         /* There is actually no link used, hence an infinite bandwidth.
270          * This happens often when using models like vivaldi.
271          * In such case, just make sure that the action completes immediately.
272          */
273         action->updateRemains(action->getRemains());
274       }
275     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
276                   
277     if (action->getMaxDuration() != NO_MAX_DURATION)
278       action->updateMaxDuration(delta);
279       
280     if ((action->getRemains() <= 0) &&
281         (lmm_get_variable_weight(action->getVariable()) > 0)) {
282       action->finish();
283       action->setState(Action::State::done);
284       action->gapRemove();
285     } else if (((action->getMaxDuration() != NO_MAX_DURATION)
286         && (action->getMaxDuration() <= 0))) {
287       action->finish();
288       action->setState(Action::State::done);
289       action->gapRemove();
290     }
291   }
292 }
293
294 Action *NetworkCm02Model::communicate(NetCard *src, NetCard *dst, double size, double rate)
295 {
296   int failed = 0;
297   double bandwidth_bound;
298   double latency = 0.0;
299   std::vector<Link*> * back_route = NULL;
300   int constraints_per_variable = 0;
301
302   std::vector<Link*> *route = new std::vector<Link*>();
303
304   XBT_IN("(%s,%s,%g,%g)", src->name(), dst->name(), size, rate);
305
306   routing_platf->getRouteAndLatency(src, dst, route, &latency);
307   xbt_assert(! route->empty() || latency,
308              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
309              src->name(), dst->name());
310
311   for (auto link: *route)
312     if (link->isOff())
313       failed = 1;
314
315   if (sg_network_crosstraffic == 1) {
316     back_route = new std::vector<Link*>();
317     routing_platf->getRouteAndLatency(dst, src, back_route, NULL);
318     for (auto link: *back_route)
319       if (link->isOff())
320         failed = 1;
321   }
322
323   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
324   action->weight_ = action->latency_ = latency;
325
326   action->rate_ = rate;
327   if (updateMechanism_ == UM_LAZY) {
328     action->m_indexHeap = -1;
329     action->m_lastUpdate = surf_get_clock();
330   }
331
332   bandwidth_bound = -1.0;
333   if (sg_weight_S_parameter > 0)
334     for (auto link : *route)
335       action->weight_ += sg_weight_S_parameter / link->getBandwidth();
336
337   for (auto link : *route) {
338     double bb = bandwidthFactor(size) * link->getBandwidth();
339     bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
340   }
341
342   action->latCurrent_ = action->latency_;
343   action->latency_ *= latencyFactor(size);
344   action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
345   if (haveGap_) {
346     xbt_assert(! route->empty(),
347                "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
348
349     gapAppend(size, route->at(0), action);
350     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)", action, src->name(), dst->name(), action->senderGap_, action->latency_);
351   }
352
353   constraints_per_variable = route->size();
354   if (back_route != NULL)
355     constraints_per_variable += back_route->size();
356
357   if (action->latency_ > 0) {
358     action->p_variable = lmm_variable_new(maxminSystem_, action, 0.0, -1.0, constraints_per_variable);
359     if (updateMechanism_ == UM_LAZY) {
360       // add to the heap the event when the latency is payed
361       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->m_lastUpdate);
362       action->heapInsert(actionHeap_, action->latency_ + action->m_lastUpdate, route->empty() ? NORMAL : LATENCY);
363     }
364   } else
365     action->p_variable = lmm_variable_new(maxminSystem_, action, 1.0, -1.0, constraints_per_variable);
366
367   if (action->rate_ < 0) {
368     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
369   } else {
370     lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)) : action->rate_);
371   }
372
373   for (auto link: *route)
374     lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), 1.0);
375
376   if (sg_network_crosstraffic == 1) {
377     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
378     for (auto link : *back_route)
379       lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), .05);
380      
381     //Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
382     //(You would also have to change lmm_element_concurrency())
383     //lmm_variable_concurrency_share_set(action->getVariable(),2);
384   }
385
386   delete route;
387   XBT_OUT();
388
389   Link::onCommunicate(action, src, dst);
390   return action;
391 }
392
393 /************
394  * Resource *
395  ************/
396 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
397     lmm_system_t system,
398     double constraint_value,
399     double bandwidth,  double latency,
400     e_surf_link_sharing_policy_t policy)
401 : Link(model, name, props, lmm_constraint_new(system, this, constraint_value))
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 = NULL;
434       lmm_element_t elem = NULL;
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 = NULL, nextelem = NULL;
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 = NULL;
483   lmm_element_t elem = NULL;
484   lmm_element_t nextelem = NULL;
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 /**********
511  * Action *
512  **********/
513 void NetworkCm02Action::updateRemainingLazy(double now)
514 {
515   double delta = 0.0;
516
517   if (m_suspended != 0)
518     return;
519
520   delta = now - m_lastUpdate;
521
522   if (m_remains > 0) {
523     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
524     double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
525
526     XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
527   }
528
529   if (m_maxDuration != NO_MAX_DURATION)
530     double_update(&m_maxDuration, delta, sg_surf_precision);
531
532   if (m_remains <= 0 &&
533       (lmm_get_variable_weight(getVariable()) > 0)) {
534     finish();
535     setState(Action::State::done);
536
537     heapRemove(getModel()->getActionHeap());
538   } else if (((m_maxDuration != NO_MAX_DURATION)
539       && (m_maxDuration <= 0))) {
540     finish();
541     setState(Action::State::done);
542     heapRemove(getModel()->getActionHeap());
543   }
544
545   m_lastUpdate = now;
546   m_lastValue = lmm_variable_getvalue(getVariable());
547 }
548
549 }
550 }