Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
have the maxmin system create by itself what it needs for selective update
[simgrid.git] / src / surf / network_cm02.cpp
1 /* Copyright (c) 2013-2018. 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 "network_cm02.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "simgrid/sg_config.hpp"
11 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
12 #include "src/kernel/lmm/maxmin.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
15
16 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
17 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
18 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
19
20 double sg_tcp_gamma = 0.0;
21 int sg_network_crosstraffic = 0;
22
23 /************************************************************************/
24 /* New model based on optimizations discussed during Pedro Velho's thesis*/
25 /************************************************************************/
26 /* @techreport{VELHO:2011:HAL-00646896:1, */
27 /*      url = {http://hal.inria.fr/hal-00646896/en/}, */
28 /*      title = {{Flow-level network models: have we reached the limits?}}, */
29 /*      author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
30 /*      type = {Rapport de recherche}, */
31 /*      institution = {INRIA}, */
32 /*      number = {RR-7821}, */
33 /*      year = {2011}, */
34 /*      month = Nov, */
35 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
36 /*  } */
37 void surf_network_model_init_LegrandVelho()
38 {
39   if (surf_network_model)
40     return;
41
42   surf_network_model = new simgrid::surf::NetworkCm02Model();
43   all_existing_models->push_back(surf_network_model);
44
45   xbt_cfg_setdefault_double("network/latency-factor",      13.01);
46   xbt_cfg_setdefault_double("network/bandwidth-factor",     0.97);
47   xbt_cfg_setdefault_double("network/weight-S",         20537);
48 }
49
50 /***************************************************************************/
51 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
52 /***************************************************************************/
53 /* @TechReport{      rr-lip2002-40, */
54 /*   author        = {Henri Casanova and Loris Marchal}, */
55 /*   institution   = {LIP}, */
56 /*   title         = {A Network Model for Simulation of Grid Application}, */
57 /*   number        = {2002-40}, */
58 /*   month         = {oct}, */
59 /*   year          = {2002} */
60 /* } */
61 void surf_network_model_init_CM02()
62 {
63
64   if (surf_network_model)
65     return;
66
67   xbt_cfg_setdefault_double("network/latency-factor",   1.0);
68   xbt_cfg_setdefault_double("network/bandwidth-factor", 1.0);
69   xbt_cfg_setdefault_double("network/weight-S",         0.0);
70
71   surf_network_model = new simgrid::surf::NetworkCm02Model();
72   all_existing_models->push_back(surf_network_model);
73 }
74
75 /***************************************************************************/
76 /* The models from Steven H. Low                                           */
77 /***************************************************************************/
78 /* @article{Low03,                                                         */
79 /*   author={Steven H. Low},                                               */
80 /*   title={A Duality Model of {TCP} and Queue Management Algorithms},     */
81 /*   year={2003},                                                          */
82 /*   journal={{IEEE/ACM} Transactions on Networking},                      */
83 /*    volume={11}, number={4},                                             */
84 /*  }                                                                      */
85 void surf_network_model_init_Reno()
86 {
87   if (surf_network_model)
88     return;
89
90   using namespace simgrid::kernel;
91   lmm::Lagrange::set_default_protocol_function(lmm::func_reno_f, lmm::func_reno_fp, lmm::func_reno_fpi);
92
93   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
94   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
95   xbt_cfg_setdefault_double("network/weight-S", 20537);
96
97   surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
98   all_existing_models->push_back(surf_network_model);
99 }
100
101
102 void surf_network_model_init_Reno2()
103 {
104   if (surf_network_model)
105     return;
106
107   using namespace simgrid::kernel;
108   lmm::Lagrange::set_default_protocol_function(lmm::func_reno2_f, lmm::func_reno2_fp, lmm::func_reno2_fpi);
109
110   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
111   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
112   xbt_cfg_setdefault_double("network/weight-S", 20537);
113
114   surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
115   all_existing_models->push_back(surf_network_model);
116 }
117
118 void surf_network_model_init_Vegas()
119 {
120   if (surf_network_model)
121     return;
122
123   using namespace simgrid::kernel;
124   lmm::Lagrange::set_default_protocol_function(lmm::func_vegas_f, lmm::func_vegas_fp, lmm::func_vegas_fpi);
125
126   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
127   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
128   xbt_cfg_setdefault_double("network/weight-S", 20537);
129
130   surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
131   all_existing_models->push_back(surf_network_model);
132 }
133
134 namespace simgrid {
135 namespace surf {
136
137 NetworkCm02Model::NetworkCm02Model(kernel::lmm::System* (*make_new_lmm_system)(bool)) : NetworkModel()
138 {
139   std::string optim = xbt_cfg_get_string("network/optim");
140   bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
141
142   if (optim == "Full") {
143     setUpdateMechanism(kernel::resource::Model::UpdateAlgo::Full);
144   } else if (optim == "Lazy") {
145     xbt_assert(select || xbt_cfg_is_default_value("network/maxmin-selective-update"),
146                "You cannot disable network selective update when using the lazy update mechanism");
147     select = true;
148     setUpdateMechanism(kernel::resource::Model::UpdateAlgo::Lazy);
149   } else {
150     xbt_die("Unsupported optimization (%s) for this model. Accepted: Full, Lazy.", optim.c_str());
151   }
152
153   set_maxmin_system(make_new_lmm_system(select));
154   loopback_     = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
155 }
156
157 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
158                                        e_surf_link_sharing_policy_t policy)
159 {
160   return new NetworkCm02Link(this, name, bandwidth, latency, policy, get_maxmin_system());
161 }
162
163 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
164 {
165   while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
166
167     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(actionHeapPop());
168     XBT_DEBUG("Something happened to action %p", action);
169     if (TRACE_is_enabled()) {
170       int n = action->get_variable()->get_number_of_constraint();
171
172       for (int i = 0; i < n; i++){
173         kernel::lmm::Constraint* constraint = action->get_variable()->get_constraint(i);
174         NetworkCm02Link* link       = static_cast<NetworkCm02Link*>(constraint->get_id());
175         double value = action->get_variable()->get_value() * action->get_variable()->get_constraint_weight(i);
176         TRACE_surf_link_set_utilization(link->getCname(), action->get_category(), value, action->get_last_update(),
177                                         now - action->get_last_update());
178       }
179     }
180
181     // if I am wearing a latency hat
182     if (action->get_type() == kernel::resource::Action::Type::LATENCY) {
183       XBT_DEBUG("Latency paid for action %p. Activating", action);
184       get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
185       action->heapRemove();
186       action->set_last_update();
187
188       // if I am wearing a max_duration or normal hat
189     } else if (action->get_type() == kernel::resource::Action::Type::MAX_DURATION ||
190                action->get_type() == kernel::resource::Action::Type::NORMAL) {
191       // no need to communicate anymore
192       // assume that flows that reached max_duration have remaining of 0
193       XBT_DEBUG("Action %p finished", action);
194       action->finish(kernel::resource::Action::State::done);
195       action->heapRemove();
196     }
197   }
198 }
199
200 void NetworkCm02Model::update_actions_state_full(double now, double delta)
201 {
202   for (auto it = std::begin(*get_running_action_set()); it != std::end(*get_running_action_set());) {
203     NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
204     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
205     XBT_DEBUG("Something happened to action %p", &action);
206     double deltap = delta;
207     if (action.latency_ > 0) {
208       if (action.latency_ > deltap) {
209         double_update(&action.latency_, deltap, sg_surf_precision);
210         deltap = 0.0;
211       } else {
212         double_update(&deltap, action.latency_, sg_surf_precision);
213         action.latency_ = 0.0;
214       }
215       if (action.latency_ <= 0.0 && not action.is_suspended())
216         get_maxmin_system()->update_variable_weight(action.get_variable(), action.weight_);
217     }
218     if (TRACE_is_enabled()) {
219       int n = action.get_variable()->get_number_of_constraint();
220       for (int i = 0; i < n; i++) {
221         kernel::lmm::Constraint* constraint = action.get_variable()->get_constraint(i);
222         NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
223         TRACE_surf_link_set_utilization(
224             link->getCname(), action.get_category(),
225             (action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i)),
226             action.get_last_update(), now - action.get_last_update());
227       }
228     }
229     if (not action.get_variable()->get_number_of_constraint()) {
230       /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
231        * vivaldi. In such case, just make sure that the action completes immediately.
232        */
233       action.update_remains(action.get_remains());
234     }
235     action.update_remains(action.get_variable()->get_value() * delta);
236
237     if (action.get_max_duration() > NO_MAX_DURATION)
238       action.update_max_duration(delta);
239
240     if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
241         ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
242       action.finish(kernel::resource::Action::State::done);
243     }
244   }
245 }
246
247 kernel::resource::Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
248 {
249   int failed = 0;
250   double latency = 0.0;
251   std::vector<LinkImpl*> back_route;
252   std::vector<LinkImpl*> route;
253
254   XBT_IN("(%s,%s,%g,%g)", src->getCname(), dst->getCname(), size, rate);
255
256   src->routeTo(dst, route, &latency);
257   xbt_assert(not route.empty() || latency,
258              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
259              src->getCname(), dst->getCname());
260
261   for (auto const& link : route)
262     if (link->isOff())
263       failed = 1;
264
265   if (sg_network_crosstraffic == 1) {
266     dst->routeTo(src, back_route, nullptr);
267     for (auto const& link : back_route)
268       if (link->isOff())
269         failed = 1;
270   }
271
272   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
273   action->weight_ = latency;
274   action->latency_ = latency;
275   action->rate_ = rate;
276   if (getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
277     action->set_last_update();
278   }
279
280   double bandwidth_bound = -1.0;
281   if (sg_weight_S_parameter > 0)
282     for (auto const& link : route)
283       action->weight_ += sg_weight_S_parameter / link->bandwidth();
284
285   for (auto const& link : route) {
286     double bb       = bandwidthFactor(size) * link->bandwidth();
287     bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
288   }
289
290   action->latCurrent_ = action->latency_;
291   action->latency_ *= latencyFactor(size);
292   action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
293
294   int constraints_per_variable = route.size();
295   constraints_per_variable += back_route.size();
296
297   if (action->latency_ > 0) {
298     action->set_variable(get_maxmin_system()->variable_new(action, 0.0, -1.0, constraints_per_variable));
299     if (getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
300       // add to the heap the event when the latency is payed
301       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->get_last_update());
302       action->heapInsert(action->latency_ + action->get_last_update(), route.empty()
303                                                                            ? kernel::resource::Action::Type::NORMAL
304                                                                            : kernel::resource::Action::Type::LATENCY);
305     }
306   } else
307     action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
308
309   if (action->rate_ < 0) {
310     get_maxmin_system()->update_variable_bound(
311         action->get_variable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
312   } else {
313     get_maxmin_system()->update_variable_bound(action->get_variable(),
314                                                (action->latCurrent_ > 0)
315                                                    ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_))
316                                                    : action->rate_);
317   }
318
319   for (auto const& link : route)
320     get_maxmin_system()->expand(link->constraint(), action->get_variable(), 1.0);
321
322   if (not back_route.empty()) { //  sg_network_crosstraffic was activated
323     XBT_DEBUG("Crosstraffic active adding backward flow using 5%%");
324     for (auto const& link : back_route)
325       get_maxmin_system()->expand(link->constraint(), action->get_variable(), .05);
326
327     // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
328     // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
329     // action->getVariable()->set_concurrency_share(2)
330   }
331   XBT_OUT();
332
333   simgrid::s4u::Link::onCommunicate(action, src, dst);
334   return action;
335 }
336
337 /************
338  * Resource *
339  ************/
340 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
341                                  e_surf_link_sharing_policy_t policy, kernel::lmm::System* system)
342     : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
343 {
344   bandwidth_.scale = 1.0;
345   bandwidth_.peak  = bandwidth;
346
347   latency_.scale = 1.0;
348   latency_.peak  = latency;
349
350   if (policy == SURF_LINK_FATPIPE)
351     constraint()->unshare();
352
353   simgrid::s4u::Link::onCreation(this->piface_);
354 }
355
356 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
357 {
358   /* Find out which of my iterators was triggered, and react accordingly */
359   if (triggered == bandwidth_.event) {
360     setBandwidth(value);
361     tmgr_trace_event_unref(&bandwidth_.event);
362
363   } else if (triggered == latency_.event) {
364     setLatency(value);
365     tmgr_trace_event_unref(&latency_.event);
366
367   } else if (triggered == stateEvent_) {
368     if (value > 0)
369       turnOn();
370     else {
371       kernel::lmm::Variable* var = nullptr;
372       const_lmm_element_t elem = nullptr;
373       double now               = surf_get_clock();
374
375       turnOff();
376       while ((var = constraint()->get_variable(&elem))) {
377         kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
378
379         if (action->get_state() == kernel::resource::Action::State::running ||
380             action->get_state() == kernel::resource::Action::State::ready) {
381           action->set_finish_time(now);
382           action->set_state(kernel::resource::Action::State::failed);
383         }
384       }
385     }
386     tmgr_trace_event_unref(&stateEvent_);
387   } else {
388     xbt_die("Unknown event!\n");
389   }
390
391   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)", constraint());
392 }
393
394 void NetworkCm02Link::setBandwidth(double value)
395 {
396   bandwidth_.peak = value;
397
398   model()->get_maxmin_system()->update_constraint_bound(constraint(),
399                                                         sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
400   TRACE_surf_link_set_bandwidth(surf_get_clock(), getCname(), sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
401
402   if (sg_weight_S_parameter > 0) {
403     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
404
405     kernel::lmm::Variable* var;
406     const_lmm_element_t elem     = nullptr;
407     const_lmm_element_t nextelem = nullptr;
408     int numelem                  = 0;
409     while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
410       NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
411       action->weight_ += delta;
412       if (not action->is_suspended())
413         model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
414     }
415   }
416 }
417
418 void NetworkCm02Link::setLatency(double value)
419 {
420   double delta                 = value - latency_.peak;
421   kernel::lmm::Variable* var   = nullptr;
422   const_lmm_element_t elem     = nullptr;
423   const_lmm_element_t nextelem = nullptr;
424   int numelem                  = 0;
425
426   latency_.peak = value;
427
428   while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
429     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
430     action->latCurrent_ += delta;
431     action->weight_ += delta;
432     if (action->rate_ < 0)
433       model()->get_maxmin_system()->update_variable_bound(action->get_variable(),
434                                                           sg_tcp_gamma / (2.0 * action->latCurrent_));
435     else {
436       model()->get_maxmin_system()->update_variable_bound(
437           action->get_variable(), std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
438
439       if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
440         XBT_INFO("Flow is limited BYBANDWIDTH");
441       } else {
442         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
443       }
444     }
445     if (not action->is_suspended())
446       model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
447   }
448 }
449
450 /**********
451  * Action *
452  **********/
453
454 void NetworkCm02Action::update_remains_lazy(double now)
455 {
456   if (suspended_ != Action::SuspendStates::not_suspended)
457     return;
458
459   double delta        = now - get_last_update();
460   double max_duration = get_max_duration();
461
462   if (get_remains_no_update() > 0) {
463     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
464               get_last_update());
465     update_remains(get_last_value() * delta);
466
467     XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
468   }
469
470   if (max_duration > NO_MAX_DURATION) {
471     double_update(&max_duration, delta, sg_surf_precision);
472     set_max_duration(max_duration);
473   }
474
475   if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
476       ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
477     finish(Action::State::done);
478     heapRemove();
479   }
480
481   set_last_update();
482   set_last_value(get_variable()->get_value());
483 }
484
485 }
486 }