Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make option 'network/crosstraffic' clean and nice
[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/kernel/lmm/maxmin.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
15
16 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
17 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
18 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
19
20 /************************************************************************/
21 /* New model based on optimizations discussed during Pedro Velho's thesis*/
22 /************************************************************************/
23 /* @techreport{VELHO:2011:HAL-00646896:1, */
24 /*      url = {http://hal.inria.fr/hal-00646896/en/}, */
25 /*      title = {{Flow-level network models: have we reached the limits?}}, */
26 /*      author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
27 /*      type = {Rapport de recherche}, */
28 /*      institution = {INRIA}, */
29 /*      number = {RR-7821}, */
30 /*      year = {2011}, */
31 /*      month = Nov, */
32 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
33 /*  } */
34 void surf_network_model_init_LegrandVelho()
35 {
36   if (surf_network_model)
37     return;
38
39   surf_network_model = new simgrid::surf::NetworkCm02Model();
40   all_existing_models->push_back(surf_network_model);
41
42   xbt_cfg_setdefault_double("network/latency-factor",      13.01);
43   xbt_cfg_setdefault_double("network/bandwidth-factor",     0.97);
44   xbt_cfg_setdefault_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
61   if (surf_network_model)
62     return;
63
64   xbt_cfg_setdefault_double("network/latency-factor",   1.0);
65   xbt_cfg_setdefault_double("network/bandwidth-factor", 1.0);
66   xbt_cfg_setdefault_double("network/weight-S",         0.0);
67
68   surf_network_model = new simgrid::surf::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   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
91   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
92   xbt_cfg_setdefault_double("network/weight-S", 20537);
93
94   surf_network_model = new simgrid::surf::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   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
108   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
109   xbt_cfg_setdefault_double("network/weight-S", 20537);
110
111   surf_network_model = new simgrid::surf::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   xbt_cfg_setdefault_double("network/latency-factor", 13.01);
124   xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
125   xbt_cfg_setdefault_double("network/weight-S", 20537);
126
127   surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
128   all_existing_models->push_back(surf_network_model);
129 }
130
131 namespace simgrid {
132 namespace surf {
133
134 NetworkCm02Model::NetworkCm02Model(kernel::lmm::System* (*make_new_lmm_system)(bool))
135     : NetworkModel(xbt_cfg_get_string("network/optim") == "Full" ? kernel::resource::Model::UpdateAlgo::Full
136                                                                  : kernel::resource::Model::UpdateAlgo::Lazy)
137 {
138   std::string optim = xbt_cfg_get_string("network/optim");
139   bool select = xbt_cfg_get_boolean("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, SURF_LINK_FATPIPE);
149 }
150
151 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
152                                        e_surf_link_sharing_policy_t 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() == kernel::resource::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() == kernel::resource::ActionHeap::Type::max_duration ||
184                action->get_type() == kernel::resource::ActionHeap::Type::normal) {
185       // no need to communicate anymore
186       // assume that flows that reached max_duration have remaining of 0
187       XBT_DEBUG("Action %p finished", action);
188       action->finish(kernel::resource::Action::State::done);
189       get_action_heap().remove(action);
190     }
191   }
192 }
193
194 void NetworkCm02Model::update_actions_state_full(double now, double delta)
195 {
196   for (auto it = std::begin(*get_running_action_set()); it != std::end(*get_running_action_set());) {
197     NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
198     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
199     XBT_DEBUG("Something happened to action %p", &action);
200     double deltap = delta;
201     if (action.latency_ > 0) {
202       if (action.latency_ > deltap) {
203         double_update(&action.latency_, deltap, sg_surf_precision);
204         deltap = 0.0;
205       } else {
206         double_update(&deltap, action.latency_, sg_surf_precision);
207         action.latency_ = 0.0;
208       }
209       if (action.latency_ <= 0.0 && not action.is_suspended())
210         get_maxmin_system()->update_variable_weight(action.get_variable(), action.weight_);
211     }
212     if (TRACE_is_enabled()) {
213       int n = action.get_variable()->get_number_of_constraint();
214       for (int i = 0; i < n; i++) {
215         kernel::lmm::Constraint* constraint = action.get_variable()->get_constraint(i);
216         NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
217         TRACE_surf_link_set_utilization(
218             link->get_cname(), action.get_category(),
219             (action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i)),
220             action.get_last_update(), now - action.get_last_update());
221       }
222     }
223     if (not action.get_variable()->get_number_of_constraint()) {
224       /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
225        * vivaldi. In such case, just make sure that the action completes immediately.
226        */
227       action.update_remains(action.get_remains());
228     }
229     action.update_remains(action.get_variable()->get_value() * delta);
230
231     if (action.get_max_duration() > NO_MAX_DURATION)
232       action.update_max_duration(delta);
233
234     if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
235         ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
236       action.finish(kernel::resource::Action::State::done);
237     }
238   }
239 }
240
241 kernel::resource::Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
242 {
243   int failed = 0;
244   double latency = 0.0;
245   std::vector<LinkImpl*> back_route;
246   std::vector<LinkImpl*> route;
247
248   XBT_IN("(%s,%s,%g,%g)", src->get_cname(), dst->get_cname(), size, rate);
249
250   src->routeTo(dst, route, &latency);
251   xbt_assert(not route.empty() || latency,
252              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
253              src->get_cname(), dst->get_cname());
254
255   for (auto const& link : route)
256     if (link->is_off())
257       failed = 1;
258
259   if (cfg_crosstraffic == 1) {
260     dst->routeTo(src, back_route, nullptr);
261     for (auto const& link : back_route)
262       if (link->is_off())
263         failed = 1;
264   }
265
266   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
267   action->weight_ = latency;
268   action->latency_ = latency;
269   action->rate_ = rate;
270   if (get_update_algorithm() == kernel::resource::Model::UpdateAlgo::Lazy) {
271     action->set_last_update();
272   }
273
274   double bandwidth_bound = -1.0;
275   if (sg_weight_S_parameter > 0)
276     for (auto const& link : route)
277       action->weight_ += sg_weight_S_parameter / link->bandwidth();
278
279   for (auto const& link : route) {
280     double bb       = bandwidthFactor(size) * link->bandwidth();
281     bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
282   }
283
284   action->lat_current_ = action->latency_;
285   action->latency_ *= latencyFactor(size);
286   action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
287
288   int constraints_per_variable = route.size();
289   constraints_per_variable += back_route.size();
290
291   if (action->latency_ > 0) {
292     action->set_variable(get_maxmin_system()->variable_new(action, 0.0, -1.0, constraints_per_variable));
293     if (get_update_algorithm() == kernel::resource::Model::UpdateAlgo::Lazy) {
294       // add to the heap the event when the latency is payed
295       double date = action->latency_ + action->get_last_update();
296       kernel::resource::ActionHeap::Type type;
297       if (route.empty())
298         type = kernel::resource::ActionHeap::Type::normal;
299       else
300         type = kernel::resource::ActionHeap::Type::latency;
301
302       XBT_DEBUG("Added action (%p) one latency event at date %f", action, date);
303       get_action_heap().insert(action, date, type);
304     }
305   } else
306     action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
307
308   if (action->rate_ < 0) {
309     get_maxmin_system()->update_variable_bound(
310         action->get_variable(), (action->lat_current_ > 0) ? cfg_tcp_gamma / (2.0 * action->lat_current_) : -1.0);
311   } else {
312     get_maxmin_system()->update_variable_bound(
313         action->get_variable(), (action->lat_current_ > 0)
314                                     ? std::min(action->rate_, cfg_tcp_gamma / (2.0 * action->lat_current_))
315                                     : action->rate_);
316   }
317
318   for (auto const& link : route)
319     get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), 1.0);
320
321   if (not back_route.empty()) { //  cfg_crosstraffic was activated
322     XBT_DEBUG("Crosstraffic active adding backward flow using 5%%");
323     for (auto const& link : back_route)
324       get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), .05);
325
326     // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
327     // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
328     // action->getVariable()->set_concurrency_share(2)
329   }
330   XBT_OUT();
331
332   simgrid::s4u::Link::onCommunicate(action, src, dst);
333   return action;
334 }
335
336 /************
337  * Resource *
338  ************/
339 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
340                                  e_surf_link_sharing_policy_t policy, kernel::lmm::System* system)
341     : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
342 {
343   bandwidth_.scale = 1.0;
344   bandwidth_.peak  = bandwidth;
345
346   latency_.scale = 1.0;
347   latency_.peak  = latency;
348
349   if (policy == SURF_LINK_FATPIPE)
350     get_constraint()->unshare();
351
352   simgrid::s4u::Link::onCreation(this->piface_);
353 }
354
355 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
356 {
357   /* Find out which of my iterators was triggered, and react accordingly */
358   if (triggered == bandwidth_.event) {
359     setBandwidth(value);
360     tmgr_trace_event_unref(&bandwidth_.event);
361
362   } else if (triggered == latency_.event) {
363     setLatency(value);
364     tmgr_trace_event_unref(&latency_.event);
365
366   } else if (triggered == stateEvent_) {
367     if (value > 0)
368       turn_on();
369     else {
370       kernel::lmm::Variable* var = nullptr;
371       const kernel::lmm::Element* elem = nullptr;
372       double now               = surf_get_clock();
373
374       turn_off();
375       while ((var = get_constraint()->get_variable(&elem))) {
376         kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
377
378         if (action->get_state() == kernel::resource::Action::State::running ||
379             action->get_state() == kernel::resource::Action::State::ready) {
380           action->set_finish_time(now);
381           action->set_state(kernel::resource::Action::State::failed);
382         }
383       }
384     }
385     tmgr_trace_event_unref(&stateEvent_);
386   } else {
387     xbt_die("Unknown event!\n");
388   }
389
390   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
391             get_constraint());
392 }
393
394 void NetworkCm02Link::setBandwidth(double value)
395 {
396   bandwidth_.peak = value;
397
398   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
399                                                             sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
400   TRACE_surf_link_set_bandwidth(surf_get_clock(), get_cname(),
401                                 sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
402
403   if (sg_weight_S_parameter > 0) {
404     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
405
406     kernel::lmm::Variable* var;
407     const kernel::lmm::Element* elem     = nullptr;
408     const kernel::lmm::Element* nextelem = nullptr;
409     int numelem                  = 0;
410     while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
411       NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
412       action->weight_ += delta;
413       if (not action->is_suspended())
414         get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
415     }
416   }
417 }
418
419 void NetworkCm02Link::setLatency(double value)
420 {
421   double delta                 = value - latency_.peak;
422   kernel::lmm::Variable* var   = nullptr;
423   const kernel::lmm::Element* elem     = nullptr;
424   const kernel::lmm::Element* nextelem = nullptr;
425   int numelem                  = 0;
426
427   latency_.peak = value;
428
429   while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
430     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
431     action->lat_current_ += delta;
432     action->weight_ += delta;
433     if (action->rate_ < 0)
434       get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), NetworkModel::cfg_tcp_gamma /
435                                                                                           (2.0 * action->lat_current_));
436     else {
437       get_model()->get_maxmin_system()->update_variable_bound(
438           action->get_variable(), std::min(action->rate_, NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)));
439
440       if (action->rate_ < NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)) {
441         XBT_INFO("Flow is limited BYBANDWIDTH");
442       } else {
443         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->lat_current_);
444       }
445     }
446     if (not action->is_suspended())
447       get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
448   }
449 }
450
451 /**********
452  * Action *
453  **********/
454
455 void NetworkCm02Action::update_remains_lazy(double now)
456 {
457   if (suspended_ != Action::SuspendStates::not_suspended)
458     return;
459
460   double delta        = now - get_last_update();
461   double max_duration = get_max_duration();
462
463   if (get_remains_no_update() > 0) {
464     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
465               get_last_update());
466     update_remains(get_last_value() * delta);
467
468     XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
469   }
470
471   if (max_duration > NO_MAX_DURATION) {
472     double_update(&max_duration, delta, sg_surf_precision);
473     set_max_duration(max_duration);
474   }
475
476   if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
477       ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
478     finish(Action::State::done);
479     get_model()->get_action_heap().remove(this);
480   }
481
482   set_last_update();
483   set_last_value(get_variable()->get_value());
484 }
485
486 }
487 }