1 /* Copyright (c) 2013-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "network_cm02.hpp"
10 #include "maxmin_private.hpp"
11 #include "simgrid/sg_config.h"
12 #include "src/instr/instr_private.h" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
16 double sg_sender_gap = 0.0;
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 */
21 double sg_tcp_gamma = 0.0;
22 int sg_network_crosstraffic = 0;
24 /************************************************************************/
25 /* New model based on optimizations discussed during Pedro Velho's thesis*/
26 /************************************************************************/
27 /* @techreport{VELHO:2011:HAL-00646896:1, */
28 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
29 /* title = {{Flow-level network models: have we reached the limits?}}, */
30 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
31 /* type = {Rapport de recherche}, */
32 /* institution = {INRIA}, */
33 /* number = {RR-7821}, */
36 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
38 void surf_network_model_init_LegrandVelho(void)
40 if (surf_network_model)
43 surf_network_model = new simgrid::surf::NetworkCm02Model();
44 xbt_dynar_push(all_existing_models, &surf_network_model);
46 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 13.01);
47 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.97);
48 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 20537);
51 /***************************************************************************/
52 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
53 /***************************************************************************/
54 /* @TechReport{ rr-lip2002-40, */
55 /* author = {Henri Casanova and Loris Marchal}, */
56 /* institution = {LIP}, */
57 /* title = {A Network Model for Simulation of Grid Application}, */
58 /* number = {2002-40}, */
62 void surf_network_model_init_CM02(void)
65 if (surf_network_model)
68 surf_network_model = new simgrid::surf::NetworkCm02Model();
69 xbt_dynar_push(all_existing_models, &surf_network_model);
71 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 1.0);
72 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 1.0);
73 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 0.0);
76 /***************************************************************************/
77 /* The models from Steven H. Low */
78 /***************************************************************************/
80 /* author={Steven H. Low}, */
81 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
83 /* journal={{IEEE/ACM} Transactions on Networking}, */
84 /* volume={11}, number={4}, */
86 void surf_network_model_init_Reno(void)
88 if (surf_network_model)
91 surf_network_model = new simgrid::surf::NetworkCm02Model();
92 xbt_dynar_push(all_existing_models, &surf_network_model);
94 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
95 surf_network_model->f_networkSolve = lagrange_solve;
97 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
98 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
99 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
103 void surf_network_model_init_Reno2(void)
105 if (surf_network_model)
108 surf_network_model = new simgrid::surf::NetworkCm02Model();
109 xbt_dynar_push(all_existing_models, &surf_network_model);
111 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp, func_reno2_fpi);
112 surf_network_model->f_networkSolve = lagrange_solve;
114 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
115 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
116 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
119 void surf_network_model_init_Vegas(void)
121 if (surf_network_model)
124 surf_network_model = new simgrid::surf::NetworkCm02Model();
125 xbt_dynar_push(all_existing_models, &surf_network_model);
127 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
128 surf_network_model->f_networkSolve = lagrange_solve;
130 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
131 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
132 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
138 NetworkCm02Model::NetworkCm02Model()
141 char *optim = xbt_cfg_get_string(_sg_cfg_set, "network/optim");
142 int select = xbt_cfg_get_boolean(_sg_cfg_set, "network/maxmin_selective_update");
144 if (!strcmp(optim, "Full")) {
145 updateMechanism_ = UM_FULL;
146 selectiveUpdate_ = select;
147 } else if (!strcmp(optim, "Lazy")) {
148 updateMechanism_ = UM_LAZY;
149 selectiveUpdate_ = 1;
150 xbt_assert((select == 1) || (xbt_cfg_is_default_value(_sg_cfg_set, "network/maxmin_selective_update")),
151 "Disabling selective update while using the lazy update mechanism is dumb!");
153 xbt_die("Unsupported optimization (%s) for this model", optim);
157 maxminSystem_ = lmm_system_new(selectiveUpdate_);
159 routing_model_create(createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE, NULL));
161 if (updateMechanism_ == UM_LAZY) {
162 actionHeap_ = xbt_heap_new(8, NULL);
163 xbt_heap_set_update_callback(actionHeap_, surf_action_lmm_update_index_heap);
164 modifiedSet_ = new ActionLmmList();
165 maxminSystem_->keep_track = modifiedSet_;
169 Link* NetworkCm02Model::createLink(const char *name, double bandwidth, double latency, e_surf_link_sharing_policy_t policy, xbt_dict_t properties)
171 return new NetworkCm02Link(this, name, properties, maxminSystem_, sg_bandwidth_factor * bandwidth, bandwidth, latency, policy);
174 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
176 NetworkCm02Action *action;
177 while ((xbt_heap_size(actionHeap_) > 0)
178 && (double_equals(xbt_heap_maxkey(actionHeap_), now, sg_surf_precision))) {
179 action = static_cast<NetworkCm02Action*> (xbt_heap_pop(actionHeap_));
180 XBT_DEBUG("Something happened to action %p", action);
181 if (TRACE_is_enabled()) {
182 int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
184 for (int i = 0; i < n; i++){
185 lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
186 NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
187 TRACE_surf_link_set_utilization(link->getName(),
188 action->getCategory(),
189 (lmm_variable_getvalue(action->getVariable())*
190 lmm_get_cnst_weight_from_var(maxminSystem_,
191 action->getVariable(),
193 action->getLastUpdate(),
194 now - action->getLastUpdate());
198 // if I am wearing a latency hat
199 if (action->getHat() == LATENCY) {
200 XBT_DEBUG("Latency paid for action %p. Activating", action);
201 lmm_update_variable_weight(maxminSystem_, action->getVariable(), action->weight_);
202 action->heapRemove(actionHeap_);
203 action->refreshLastUpdate();
205 // if I am wearing a max_duration or normal hat
206 } else if (action->getHat() == MAX_DURATION ||
207 action->getHat() == NORMAL) {
208 // no need to communicate anymore
209 // assume that flows that reached max_duration have remaining of 0
210 XBT_DEBUG("Action %p finished", action);
211 action->setRemains(0);
213 action->setState(Action::State::done);
214 action->heapRemove(actionHeap_);
223 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
225 NetworkCm02Action *action;
226 ActionList *running_actions = getRunningActionSet();
228 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
229 ; it != itend ; it=itNext) {
232 action = static_cast<NetworkCm02Action*> (&*it);
233 XBT_DEBUG("Something happened to action %p", action);
234 double deltap = delta;
235 if (action->latency_ > 0) {
236 if (action->latency_ > deltap) {
237 double_update(&(action->latency_), deltap, sg_surf_precision);
240 double_update(&(deltap), action->latency_, sg_surf_precision);
241 action->latency_ = 0.0;
243 if (action->latency_ == 0.0 && !(action->isSuspended()))
244 lmm_update_variable_weight(maxminSystem_, action->getVariable(),
247 if (TRACE_is_enabled()) {
248 int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
249 for (int i = 0; i < n; i++){
250 lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
252 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
253 TRACE_surf_link_set_utilization(link->getName(),
254 action->getCategory(),
255 (lmm_variable_getvalue(action->getVariable())*
256 lmm_get_cnst_weight_from_var(maxminSystem_,
257 action->getVariable(),
259 action->getLastUpdate(),
260 now - action->getLastUpdate());
263 if (!lmm_get_number_of_cnst_from_var (maxminSystem_, action->getVariable())) {
264 /* There is actually no link used, hence an infinite bandwidth.
265 * This happens often when using models like vivaldi.
266 * In such case, just make sure that the action completes immediately.
268 action->updateRemains(action->getRemains());
270 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
272 if (action->getMaxDuration() != NO_MAX_DURATION)
273 action->updateMaxDuration(delta);
275 if ((action->getRemains() <= 0) &&
276 (lmm_get_variable_weight(action->getVariable()) > 0)) {
278 action->setState(Action::State::done);
280 } else if (((action->getMaxDuration() != NO_MAX_DURATION)
281 && (action->getMaxDuration() <= 0))) {
283 action->setState(Action::State::done);
289 Action *NetworkCm02Model::communicate(NetCard *src, NetCard *dst, double size, double rate)
292 double bandwidth_bound;
293 double latency = 0.0;
294 std::vector<Link*> * back_route = NULL;
295 int constraints_per_variable = 0;
297 std::vector<Link*> *route = new std::vector<Link*>();
299 XBT_IN("(%s,%s,%g,%g)", src->name(), dst->name(), size, rate);
301 routing_platf->getRouteAndLatency(src, dst, route, &latency);
302 xbt_assert(! route->empty() || latency,
303 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
304 src->name(), dst->name());
306 for (auto link: *route)
310 if (sg_network_crosstraffic == 1) {
311 back_route = new std::vector<Link*>();
312 routing_platf->getRouteAndLatency(dst, src, back_route, NULL);
313 for (auto link: *back_route)
318 NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
319 action->weight_ = action->latency_ = latency;
321 action->rate_ = rate;
322 if (updateMechanism_ == UM_LAZY) {
323 action->indexHeap_ = -1;
324 action->lastUpdate_ = surf_get_clock();
327 bandwidth_bound = -1.0;
328 if (sg_weight_S_parameter > 0)
329 for (auto link : *route)
330 action->weight_ += sg_weight_S_parameter / link->getBandwidth();
332 for (auto link : *route) {
333 double bb = bandwidthFactor(size) * link->getBandwidth();
334 bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
337 action->latCurrent_ = action->latency_;
338 action->latency_ *= latencyFactor(size);
339 action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
341 xbt_assert(! route->empty(),
342 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
344 gapAppend(size, route->at(0), action);
345 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)", action, src->name(), dst->name(), action->senderGap_, action->latency_);
348 constraints_per_variable = route->size();
349 if (back_route != NULL)
350 constraints_per_variable += back_route->size();
352 if (action->latency_ > 0) {
353 action->variable_ = lmm_variable_new(maxminSystem_, action, 0.0, -1.0, constraints_per_variable);
354 if (updateMechanism_ == UM_LAZY) {
355 // add to the heap the event when the latency is payed
356 XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->lastUpdate_);
357 action->heapInsert(actionHeap_, action->latency_ + action->lastUpdate_, route->empty() ? NORMAL : LATENCY);
360 action->variable_ = lmm_variable_new(maxminSystem_, action, 1.0, -1.0, constraints_per_variable);
362 if (action->rate_ < 0) {
363 lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
365 lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)) : action->rate_);
368 for (auto link: *route)
369 lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), 1.0);
371 if (sg_network_crosstraffic == 1) {
372 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
373 for (auto link : *back_route)
374 lmm_expand(maxminSystem_, link->getConstraint(), action->getVariable(), .05);
376 //Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
377 //(You would also have to change lmm_element_concurrency())
378 //lmm_variable_concurrency_share_set(action->getVariable(),2);
384 Link::onCommunicate(action, src, dst);
391 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
393 double constraint_value,
394 double bandwidth, double latency,
395 e_surf_link_sharing_policy_t policy)
396 : Link(model, name, props, lmm_constraint_new(system, this, constraint_value))
398 m_bandwidth.scale = 1.0;
399 m_bandwidth.peak = bandwidth;
401 m_latency.scale = 1.0;
402 m_latency.peak = latency;
404 if (policy == SURF_LINK_FATPIPE)
405 lmm_constraint_shared(getConstraint());
407 Link::onCreation(this);
412 void NetworkCm02Link::apply_event(tmgr_trace_iterator_t triggered, double value)
415 /* Find out which of my iterators was triggered, and react accordingly */
416 if (triggered == m_bandwidth.event) {
417 updateBandwidth(value);
418 tmgr_trace_event_unref(&m_bandwidth.event);
420 } else if (triggered == m_latency.event) {
421 updateLatency(value);
422 tmgr_trace_event_unref(&m_latency.event);
424 } else if (triggered == m_stateEvent) {
428 lmm_variable_t var = NULL;
429 lmm_element_t elem = NULL;
430 double now = surf_get_clock();
433 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
434 Action *action = static_cast<Action*>( lmm_variable_id(var) );
436 if (action->getState() == Action::State::running ||
437 action->getState() == Action::State::ready) {
438 action->setFinishTime(now);
439 action->setState(Action::State::failed);
443 tmgr_trace_event_unref(&m_stateEvent);
445 xbt_die("Unknown event!\n");
448 XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
452 void NetworkCm02Link::updateBandwidth(double value) {
454 m_bandwidth.peak = value;
456 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
457 sg_bandwidth_factor * (m_bandwidth.peak * m_bandwidth.scale));
458 TRACE_surf_link_set_bandwidth(surf_get_clock(), getName(), sg_bandwidth_factor * m_bandwidth.peak * m_bandwidth.scale);
460 if (sg_weight_S_parameter > 0) {
461 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (m_bandwidth.peak * m_bandwidth.scale);
464 lmm_element_t elem = NULL, nextelem = NULL;
466 while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
467 NetworkCm02Action *action = (NetworkCm02Action*) lmm_variable_id(var);
468 action->weight_ += delta;
469 if (!action->isSuspended())
470 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->weight_);
475 void NetworkCm02Link::updateLatency(double value){
476 double delta = value - m_latency.peak;
477 lmm_variable_t var = NULL;
478 lmm_element_t elem = NULL;
479 lmm_element_t nextelem = NULL;
482 m_latency.peak = value;
484 while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
485 NetworkCm02Action *action = (NetworkCm02Action*) lmm_variable_id(var);
486 action->latCurrent_ += delta;
487 action->weight_ += delta;
488 if (action->rate_ < 0)
489 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->latCurrent_));
491 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
492 std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
494 if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
495 XBT_INFO("Flow is limited BYBANDWIDTH");
497 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
500 if (!action->isSuspended())
501 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->weight_);
508 void NetworkCm02Action::updateRemainingLazy(double now)
515 delta = now - lastUpdate_;
518 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
519 double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
521 XBT_DEBUG("Updating action(%p): remains is now %f", this, remains_);
524 if (maxDuration_ != NO_MAX_DURATION)
525 double_update(&maxDuration_, delta, sg_surf_precision);
528 (lmm_get_variable_weight(getVariable()) > 0)) {
530 setState(Action::State::done);
532 heapRemove(getModel()->getActionHeap());
533 } else if (((maxDuration_ != NO_MAX_DURATION)
534 && (maxDuration_ <= 0))) {
536 setState(Action::State::done);
537 heapRemove(getModel()->getActionHeap());
541 lastValue_ = lmm_variable_getvalue(getVariable());