Logo AND Algorithmique Numérique Distribuée

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