Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
035ca869fec653562ec1e4ef47d8d926d6a4f7bd
[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   xbt_cfg_setdefault_double("network/latency-factor",      13.01);
42   xbt_cfg_setdefault_double("network/bandwidth-factor",     0.97);
43   xbt_cfg_setdefault_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   xbt_cfg_setdefault_double("network/latency-factor",   1.0);
64   xbt_cfg_setdefault_double("network/bandwidth-factor", 1.0);
65   xbt_cfg_setdefault_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   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
90   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
91   xbt_cfg_setdefault_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   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
107   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
108   xbt_cfg_setdefault_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   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
123   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
124   xbt_cfg_setdefault_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(xbt_cfg_get_string("network/optim") == "Full" ? Model::UpdateAlgo::Full : Model::UpdateAlgo::Lazy)
136 {
137   std::string optim = xbt_cfg_get_string("network/optim");
138   bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
139
140   if (optim == "Lazy") {
141     xbt_assert(select || xbt_cfg_is_default_value("network/maxmin-selective-update"),
142                "You cannot disable network selective update when using the lazy update mechanism");
143     select = true;
144   }
145
146   set_maxmin_system(make_new_lmm_system(select));
147   loopback_     = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
148 }
149
150 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
151                                        e_surf_link_sharing_policy_t policy)
152 {
153   return new NetworkCm02Link(this, name, bandwidth, latency, policy, get_maxmin_system());
154 }
155
156 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
157 {
158   while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
159
160     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(get_action_heap().pop());
161     XBT_DEBUG("Something happened to action %p", action);
162     if (TRACE_is_enabled()) {
163       int n = action->get_variable()->get_number_of_constraint();
164
165       for (int i = 0; i < n; i++){
166         kernel::lmm::Constraint* constraint = action->get_variable()->get_constraint(i);
167         NetworkCm02Link* link       = static_cast<NetworkCm02Link*>(constraint->get_id());
168         double value = action->get_variable()->get_value() * action->get_variable()->get_constraint_weight(i);
169         TRACE_surf_link_set_utilization(link->get_cname(), action->get_category(), value, action->get_last_update(),
170                                         now - action->get_last_update());
171       }
172     }
173
174     // if I am wearing a latency hat
175     if (action->get_type() == ActionHeap::Type::latency) {
176       XBT_DEBUG("Latency paid for action %p. Activating", action);
177       get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
178       get_action_heap().remove(action);
179       action->set_last_update();
180
181       // if I am wearing a max_duration or normal hat
182     } else if (action->get_type() == ActionHeap::Type::max_duration || action->get_type() == ActionHeap::Type::normal) {
183       // no need to communicate anymore
184       // assume that flows that reached max_duration have remaining of 0
185       XBT_DEBUG("Action %p finished", action);
186       action->finish(Action::State::done);
187       get_action_heap().remove(action);
188     }
189   }
190 }
191
192 void NetworkCm02Model::update_actions_state_full(double now, double delta)
193 {
194   for (auto it = std::begin(*get_running_action_set()); it != std::end(*get_running_action_set());) {
195     NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
196     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
197     XBT_DEBUG("Something happened to action %p", &action);
198     double deltap = delta;
199     if (action.latency_ > 0) {
200       if (action.latency_ > deltap) {
201         double_update(&action.latency_, deltap, sg_surf_precision);
202         deltap = 0.0;
203       } else {
204         double_update(&deltap, action.latency_, sg_surf_precision);
205         action.latency_ = 0.0;
206       }
207       if (action.latency_ <= 0.0 && not action.is_suspended())
208         get_maxmin_system()->update_variable_weight(action.get_variable(), action.weight_);
209     }
210     if (TRACE_is_enabled()) {
211       int n = action.get_variable()->get_number_of_constraint();
212       for (int i = 0; i < n; i++) {
213         kernel::lmm::Constraint* constraint = action.get_variable()->get_constraint(i);
214         NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
215         TRACE_surf_link_set_utilization(
216             link->get_cname(), action.get_category(),
217             (action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i)),
218             action.get_last_update(), now - action.get_last_update());
219       }
220     }
221     if (not action.get_variable()->get_number_of_constraint()) {
222       /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
223        * vivaldi. In such case, just make sure that the action completes immediately.
224        */
225       action.update_remains(action.get_remains());
226     }
227     action.update_remains(action.get_variable()->get_value() * delta);
228
229     if (action.get_max_duration() > NO_MAX_DURATION)
230       action.update_max_duration(delta);
231
232     if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
233         ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
234       action.finish(Action::State::done);
235     }
236   }
237 }
238
239 Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
240 {
241   int failed = 0;
242   double latency = 0.0;
243   std::vector<LinkImpl*> back_route;
244   std::vector<LinkImpl*> route;
245
246   XBT_IN("(%s,%s,%g,%g)", src->get_cname(), dst->get_cname(), size, rate);
247
248   src->routeTo(dst, route, &latency);
249   xbt_assert(not route.empty() || latency,
250              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
251              src->get_cname(), dst->get_cname());
252
253   for (auto const& link : route)
254     if (link->is_off())
255       failed = 1;
256
257   if (cfg_crosstraffic == 1) {
258     dst->routeTo(src, back_route, nullptr);
259     for (auto const& link : back_route)
260       if (link->is_off())
261         failed = 1;
262   }
263
264   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
265   action->weight_ = latency;
266   action->latency_ = latency;
267   action->rate_ = rate;
268   if (get_update_algorithm() == Model::UpdateAlgo::Lazy) {
269     action->set_last_update();
270   }
271
272   double bandwidth_bound = -1.0;
273   if (sg_weight_S_parameter > 0)
274     for (auto const& link : route)
275       action->weight_ += sg_weight_S_parameter / link->bandwidth();
276
277   for (auto const& link : route) {
278     double bb       = bandwidthFactor(size) * link->bandwidth();
279     bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
280   }
281
282   action->lat_current_ = action->latency_;
283   action->latency_ *= latencyFactor(size);
284   action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
285
286   int constraints_per_variable = route.size();
287   constraints_per_variable += back_route.size();
288
289   if (action->latency_ > 0) {
290     action->set_variable(get_maxmin_system()->variable_new(action, 0.0, -1.0, constraints_per_variable));
291     if (get_update_algorithm() == Model::UpdateAlgo::Lazy) {
292       // add to the heap the event when the latency is payed
293       double date = action->latency_ + action->get_last_update();
294       ActionHeap::Type type;
295       if (route.empty())
296         type = ActionHeap::Type::normal;
297       else
298         type = ActionHeap::Type::latency;
299
300       XBT_DEBUG("Added action (%p) one latency event at date %f", action, date);
301       get_action_heap().insert(action, date, type);
302     }
303   } else
304     action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
305
306   if (action->rate_ < 0) {
307     get_maxmin_system()->update_variable_bound(
308         action->get_variable(), (action->lat_current_ > 0) ? cfg_tcp_gamma / (2.0 * action->lat_current_) : -1.0);
309   } else {
310     get_maxmin_system()->update_variable_bound(
311         action->get_variable(), (action->lat_current_ > 0)
312                                     ? std::min(action->rate_, cfg_tcp_gamma / (2.0 * action->lat_current_))
313                                     : action->rate_);
314   }
315
316   for (auto const& link : route)
317     get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), 1.0);
318
319   if (not back_route.empty()) { //  cfg_crosstraffic was activated
320     XBT_DEBUG("Crosstraffic active adding backward flow using 5%%");
321     for (auto const& link : back_route)
322       get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), .05);
323
324     // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
325     // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
326     // action->getVariable()->set_concurrency_share(2)
327   }
328   XBT_OUT();
329
330   simgrid::s4u::Link::onCommunicate(action, src, dst);
331   return action;
332 }
333
334 /************
335  * Resource *
336  ************/
337 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
338                                  e_surf_link_sharing_policy_t policy, kernel::lmm::System* system)
339     : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
340 {
341   bandwidth_.scale = 1.0;
342   bandwidth_.peak  = bandwidth;
343
344   latency_.scale = 1.0;
345   latency_.peak  = latency;
346
347   if (policy == SURF_LINK_FATPIPE)
348     get_constraint()->unshare();
349
350   simgrid::s4u::Link::onCreation(this->piface_);
351 }
352
353 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
354 {
355   /* Find out which of my iterators was triggered, and react accordingly */
356   if (triggered == bandwidth_.event) {
357     setBandwidth(value);
358     tmgr_trace_event_unref(&bandwidth_.event);
359
360   } else if (triggered == latency_.event) {
361     setLatency(value);
362     tmgr_trace_event_unref(&latency_.event);
363
364   } else if (triggered == stateEvent_) {
365     if (value > 0)
366       turn_on();
367     else {
368       kernel::lmm::Variable* var = nullptr;
369       const kernel::lmm::Element* elem = nullptr;
370       double now               = surf_get_clock();
371
372       turn_off();
373       while ((var = get_constraint()->get_variable(&elem))) {
374         Action* action = static_cast<Action*>(var->get_id());
375
376         if (action->get_state() == Action::State::running || action->get_state() == Action::State::ready) {
377           action->set_finish_time(now);
378           action->set_state(Action::State::failed);
379         }
380       }
381     }
382     tmgr_trace_event_unref(&stateEvent_);
383   } else {
384     xbt_die("Unknown event!\n");
385   }
386
387   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
388             get_constraint());
389 }
390
391 void NetworkCm02Link::setBandwidth(double value)
392 {
393   bandwidth_.peak = value;
394
395   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
396                                                             sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
397   TRACE_surf_link_set_bandwidth(surf_get_clock(), get_cname(),
398                                 sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
399
400   if (sg_weight_S_parameter > 0) {
401     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
402
403     kernel::lmm::Variable* var;
404     const kernel::lmm::Element* elem     = nullptr;
405     const kernel::lmm::Element* nextelem = nullptr;
406     int numelem                  = 0;
407     while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
408       NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
409       action->weight_ += delta;
410       if (not action->is_suspended())
411         get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
412     }
413   }
414 }
415
416 void NetworkCm02Link::setLatency(double value)
417 {
418   double delta                 = value - latency_.peak;
419   kernel::lmm::Variable* var   = nullptr;
420   const kernel::lmm::Element* elem     = nullptr;
421   const kernel::lmm::Element* nextelem = nullptr;
422   int numelem                  = 0;
423
424   latency_.peak = value;
425
426   while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
427     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
428     action->lat_current_ += delta;
429     action->weight_ += delta;
430     if (action->rate_ < 0)
431       get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), NetworkModel::cfg_tcp_gamma /
432                                                                                           (2.0 * action->lat_current_));
433     else {
434       get_model()->get_maxmin_system()->update_variable_bound(
435           action->get_variable(), std::min(action->rate_, NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)));
436
437       if (action->rate_ < NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)) {
438         XBT_INFO("Flow is limited BYBANDWIDTH");
439       } else {
440         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->lat_current_);
441       }
442     }
443     if (not action->is_suspended())
444       get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
445   }
446 }
447
448 /**********
449  * Action *
450  **********/
451
452 void NetworkCm02Action::update_remains_lazy(double now)
453 {
454   if (suspended_ != Action::SuspendStates::not_suspended)
455     return;
456
457   double delta        = now - get_last_update();
458   double max_duration = get_max_duration();
459
460   if (get_remains_no_update() > 0) {
461     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
462               get_last_update());
463     update_remains(get_last_value() * delta);
464
465     XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
466   }
467
468   if (max_duration > NO_MAX_DURATION) {
469     double_update(&max_duration, delta, sg_surf_precision);
470     set_max_duration(max_duration);
471   }
472
473   if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
474       ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
475     finish(Action::State::done);
476     get_model()->get_action_heap().remove(this);
477   }
478
479   set_last_update();
480   set_last_value(get_variable()->get_value());
481 }
482
483 }
484 }
485 } // namespace simgrid