Logo AND Algorithmique Numérique Distribuée

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