Logo AND Algorithmique Numérique Distribuée

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