Logo AND Algorithmique Numérique Distribuée

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