3 * Network with improved management of tasks, IM (Improved Management).
4 * Uses a heap to store actions so that the share_resources is faster.
5 * This model automatically sets the selective update flag to 1 and is
6 * highly dependent on the maxmin lmm module.
9 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
10 * All rights reserved. */
12 /* This program is free software; you can redistribute it and/or modify it
13 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "network_private.h"
19 #include "surf_private.h"
21 #include "maxmin_private.h"
22 #include "surf/surfxml_parse_values.h"
23 #include "surf/surf_resource.h"
24 #include "surf/surf_resource_lmm.h"
26 #undef GENERIC_LMM_ACTION
28 #define GENERIC_LMM_ACTION(action) (action)->generic_lmm_action
29 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
33 "Logging specific to the SURF network module");
35 surf_model_t surf_network_model = NULL;
36 static lmm_system_t network_maxmin_system = NULL;
37 static void (*network_solve) (lmm_system_t) = NULL;
39 xbt_dynar_t smpi_bw_factor = NULL;
40 xbt_dynar_t smpi_lat_factor = NULL;
42 typedef struct s_smpi_factor *smpi_factor_t;
43 typedef struct s_smpi_factor {
48 double sg_sender_gap = 0.0;
49 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
50 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
51 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
53 double sg_tcp_gamma = 0.0;
54 int sg_network_crosstraffic = 0;
56 xbt_dict_t gap_lookup = NULL;
58 e_UM_t network_update_mechanism = UM_UNDEFINED;
59 static int net_selective_update = 0;
61 static int net_action_is_suspended(surf_action_t action);
62 static void net_update_action_remaining_lazy(double now);
64 static xbt_swag_t net_modified_set = NULL;
65 static xbt_heap_t net_action_heap = NULL;
68 /******************************************************************************/
69 /* Factors callbacks */
70 /******************************************************************************/
71 static double constant_latency_factor(double size)
73 return sg_latency_factor;
76 static double constant_bandwidth_factor(double size)
78 return sg_bandwidth_factor;
81 static double constant_bandwidth_constraint(double rate, double bound,
87 /**********************/
89 /**********************/
90 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
93 unsigned int iter = 0;
95 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
97 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
98 radical_elements = xbt_str_split(smpi_coef_string, ";");
99 xbt_dynar_foreach(radical_elements, iter, value) {
101 radical_elements2 = xbt_str_split(value, ":");
102 if (xbt_dynar_length(radical_elements2) != 2)
103 xbt_die("Malformed radical for smpi factor!");
104 fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
105 fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
106 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
107 XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
108 xbt_dynar_free(&radical_elements2);
110 xbt_dynar_free(&radical_elements);
114 static double smpi_bandwidth_factor(double size)
118 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/bw_factor"));
120 unsigned int iter = 0;
121 s_smpi_factor_t fact;
122 xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
123 if (size >= fact.factor) {
124 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
132 static double smpi_latency_factor(double size)
134 if (!smpi_lat_factor)
136 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/lat_factor"));
138 unsigned int iter = 0;
139 s_smpi_factor_t fact;
140 xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
141 if (size >= fact.factor) {
142 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
150 /**--------- <copy/paste C code snippet in surf/network.c> -----------*/
152 static double smpi_bandwidth_constraint(double rate, double bound,
155 return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
158 static double (*latency_factor_callback) (double) =
159 &constant_latency_factor;
160 static double (*bandwidth_factor_callback) (double) =
161 &constant_bandwidth_factor;
162 static double (*bandwidth_constraint_callback) (double, double, double) =
163 &constant_bandwidth_constraint;
165 static void (*gap_append) (double, const link_CM02_t,
166 surf_action_network_CM02_t) = NULL;
167 static void (*gap_remove) (surf_action_network_CM02_t) = NULL;
169 static void *net_create_resource(const char *name,
171 tmgr_trace_t bw_trace,
173 tmgr_trace_t lat_trace,
174 e_surf_resource_state_t
176 tmgr_trace_t state_trace,
177 e_surf_link_sharing_policy_t
178 policy, xbt_dict_t properties)
180 link_CM02_t nw_link = (link_CM02_t)
181 surf_resource_lmm_new(sizeof(s_link_CM02_t),
182 surf_network_model, name, properties,
183 network_maxmin_system,
184 sg_bandwidth_factor * bw_initial,
186 state_initial, state_trace,
187 bw_initial, bw_trace);
189 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
190 "Link '%s' declared several times in the platform file.",
193 nw_link->lat_current = lat_initial;
196 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
198 if (policy == SURF_LINK_FATPIPE)
199 lmm_constraint_shared(nw_link->lmm_resource.constraint);
201 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
206 static void net_parse_link_init(sg_platf_link_cbarg_t link)
208 if (link->policy == SURF_LINK_FULLDUPLEX) {
210 link_id = bprintf("%s_UP", link->id);
211 net_create_resource(link_id,
213 link->bandwidth_trace,
217 link->state_trace, link->policy, link->properties);
219 link_id = bprintf("%s_DOWN", link->id);
220 net_create_resource(link_id,
222 link->bandwidth_trace,
226 link->state_trace, link->policy, link->properties);
229 net_create_resource(link->id,
231 link->bandwidth_trace,
235 link->state_trace, link->policy, link->properties);
239 static void net_add_traces(void)
241 xbt_dict_cursor_t cursor = NULL;
242 char *trace_name, *elm;
244 static int called = 0;
249 /* connect all traces relative to network */
250 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
251 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
252 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
254 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
257 "Cannot connect trace %s to link %s: trace undefined",
260 link->lmm_resource.state_event =
261 tmgr_history_add_trace(history, trace, 0.0, 0, link);
264 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
265 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
266 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
268 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
271 "Cannot connect trace %s to link %s: trace undefined",
274 link->lmm_resource.power.event =
275 tmgr_history_add_trace(history, trace, 0.0, 0, link);
278 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
279 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
280 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
282 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
285 "Cannot connect trace %s to link %s: trace undefined",
288 link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
292 static void net_define_callbacks(void)
294 /* Figuring out the network links */
295 sg_platf_link_add_cb(net_parse_link_init);
296 sg_platf_postparse_add_cb(net_add_traces);
299 static int net_resource_used(void *resource_id)
301 return lmm_constraint_used(network_maxmin_system, ((surf_resource_lmm_t)
306 static int net_action_unref(surf_action_t action)
309 if (!action->refcount) {
310 xbt_swag_remove(action, action->state_set);
311 if (((surf_action_lmm_t)action)->variable) {
312 lmm_variable_free(network_maxmin_system,
313 ((surf_action_lmm_t) action)->variable);
315 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
316 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t) action);
317 xbt_swag_remove(action, net_modified_set);
319 surf_action_free(&action);
327 static void net_action_cancel(surf_action_t action)
329 XBT_DEBUG("cancel action %p", action);
330 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
331 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
332 xbt_swag_remove(action, net_modified_set);
333 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t) action);
337 void net_action_recycle(surf_action_t action)
342 #ifdef HAVE_LATENCY_BOUND_TRACKING
343 int net_get_link_latency_limited(surf_action_t action)
345 return action->latency_limited;
349 double net_action_get_remains(surf_action_t action)
351 if (network_update_mechanism == UM_LAZY) /* update remains before return it */
352 net_update_action_remaining_lazy(surf_get_clock());
353 return action->remains;
356 static void net_update_action_remaining_lazy(double now)
358 surf_action_lmm_t action = NULL;
361 xbt_swag_foreach(action, net_modified_set) {
363 if (action->suspended != 0) {
367 delta = now - action->last_update;
369 double_update(&(((surf_action_t)action)->remains),
370 lmm_variable_getvalue(action->variable) * delta);
372 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
373 double_update(&(((surf_action_t)action)->max_duration), delta);
375 if ((((surf_action_t)action)->remains <= 0) &&
376 (lmm_get_variable_weight(action->variable) > 0)) {
377 ((surf_action_t)action)->finish = surf_get_clock();
378 surf_network_model->action_state_set((surf_action_t) action,
380 surf_action_lmm_heap_remove(net_action_heap,action);
381 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
382 && (((surf_action_t)action)->max_duration <= 0))) {
383 ((surf_action_t)action)->finish = surf_get_clock();
384 surf_network_model->action_state_set((surf_action_t) action,
386 surf_action_lmm_heap_remove(net_action_heap,action);
389 action->last_update = now;
393 static double net_share_resources_full(double now)
395 s_surf_action_lmm_t s_action;
396 surf_action_network_CM02_t action = NULL;
397 xbt_swag_t running_actions =
398 surf_network_model->states.running_action_set;
401 min = generic_maxmin_share_resources(running_actions,
402 xbt_swag_offset(s_action,
404 network_maxmin_system,
407 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable) )))
409 xbt_swag_foreach(action, running_actions) {
410 #ifdef HAVE_LATENCY_BOUND_TRACKING
411 if (lmm_is_variable_limited_by_latency(GENERIC_LMM_ACTION(action).variable)) {
412 action->latency_limited = 1;
414 action->latency_limited = 0;
417 if (action->latency > 0) {
418 min = (min < 0) ? action->latency : min(min, action->latency);
422 XBT_DEBUG("Min of share resources %f", min);
427 static double net_share_resources_lazy(double now)
429 surf_action_network_CM02_t action = NULL;
434 ("Before share resources, the size of modified actions set is %d",
435 xbt_swag_size(net_modified_set));
436 net_update_action_remaining_lazy(now);
438 lmm_solve(network_maxmin_system);
441 ("After share resources, The size of modified actions set is %d",
442 xbt_swag_size(net_modified_set));
444 xbt_swag_foreach(action, net_modified_set) {
445 int max_dur_flag = 0;
447 if (GENERIC_ACTION(action).state_set !=
448 surf_network_model->states.running_action_set)
451 /* bogus priority, skip it */
452 if (GENERIC_ACTION(action).priority <= 0)
456 value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
458 if (GENERIC_ACTION(action).remains > 0) {
459 value = GENERIC_ACTION(action).remains / value;
467 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
469 || GENERIC_ACTION(action).start +
470 GENERIC_ACTION(action).max_duration < min)) {
471 min = GENERIC_ACTION(action).start +
472 GENERIC_ACTION(action).max_duration;
476 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
477 GENERIC_ACTION(action).start, now + value,
478 GENERIC_ACTION(action).max_duration);
481 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
482 surf_action_lmm_heap_insert(net_action_heap,(surf_action_lmm_t)action, min, max_dur_flag ? MAX_DURATION : NORMAL);
483 XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min,
485 } else DIE_IMPOSSIBLE;
488 //hereafter must have already the min value for this resource model
489 if (xbt_heap_size(net_action_heap) > 0)
490 min = xbt_heap_maxkey(net_action_heap) - now;
494 XBT_DEBUG("The minimum with the HEAP %lf", min);
499 static void net_update_actions_state_full(double now, double delta)
502 surf_action_network_CM02_t action = NULL;
503 surf_action_network_CM02_t next_action = NULL;
504 xbt_swag_t running_actions =
505 surf_network_model->states.running_action_set;
507 xbt_swag_t failed_actions =
508 surf_network_model->states.failed_action_set;
511 xbt_swag_foreach_safe(action, next_action, running_actions) {
513 if (action->latency > 0) {
514 if (action->latency > deltap) {
515 double_update(&(action->latency), deltap);
518 double_update(&(deltap), action->latency);
519 action->latency = 0.0;
521 if ((action->latency == 0.0) && !(GENERIC_LMM_ACTION(action).suspended))
522 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
526 if (TRACE_is_enabled()) {
527 int n = lmm_get_number_of_cnst_from_var(network_maxmin_system, GENERIC_LMM_ACTION(action).variable);
529 for (i = 0; i < n; i++){
530 lmm_constraint_t constraint = lmm_get_cnst_from_var(network_maxmin_system,
531 GENERIC_LMM_ACTION(action).variable,
533 link_CM02_t link = lmm_constraint_id(constraint);
534 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
535 ((surf_action_t)action)->category,
536 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
542 if (!lmm_get_number_of_cnst_from_var
543 (network_maxmin_system, GENERIC_LMM_ACTION(action).variable)) {
544 /* There is actually no link used, hence an infinite bandwidth.
545 * This happens often when using models like vivaldi.
546 * In such case, just make sure that the action completes immediately.
548 double_update(&(GENERIC_ACTION(action).remains),
549 GENERIC_ACTION(action).remains);
551 double_update(&(GENERIC_ACTION(action).remains),
552 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * deltap);
553 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
554 double_update(&(((surf_action_t)action)->max_duration), delta);
556 if ((GENERIC_ACTION(action).remains <= 0) &&
557 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
558 ((surf_action_t)action)->finish = surf_get_clock();
559 surf_network_model->action_state_set((surf_action_t) action,
564 } else if ((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
565 && (((surf_action_t)action)->max_duration <= 0)) {
566 ((surf_action_t)action)->finish = surf_get_clock();
567 surf_network_model->action_state_set((surf_action_t) action,
577 static void net_update_actions_state_lazy(double now, double delta)
579 surf_action_network_CM02_t action = NULL;
581 while ((xbt_heap_size(net_action_heap) > 0)
582 && (double_equals(xbt_heap_maxkey(net_action_heap), now))) {
583 action = xbt_heap_pop(net_action_heap);
584 XBT_DEBUG("Action %p: finish", action);
585 GENERIC_ACTION(action).finish = surf_get_clock();
587 if (TRACE_is_enabled()) {
588 int n = lmm_get_number_of_cnst_from_var(network_maxmin_system, GENERIC_LMM_ACTION(action).variable);
590 for (i = 0; i < n; i++){
591 lmm_constraint_t constraint = lmm_get_cnst_from_var(network_maxmin_system,
592 GENERIC_LMM_ACTION(action).variable,
594 link_CM02_t link = lmm_constraint_id(constraint);
595 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
596 ((surf_action_t)action)->category,
597 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
598 GENERIC_LMM_ACTION(action).last_update,
599 now - GENERIC_LMM_ACTION(action).last_update);
604 // if I am wearing a latency hat
605 if (GENERIC_LMM_ACTION(action).hat == LATENCY) {
606 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
608 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
609 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
611 // if I am wearing a max_duration or normal hat
612 } else if (GENERIC_LMM_ACTION(action).hat == MAX_DURATION ||
613 GENERIC_LMM_ACTION(action).hat == NORMAL) {
614 // no need to communicate anymore
615 // assume that flows that reached max_duration have remaining of 0
616 GENERIC_ACTION(action).remains = 0;
617 ((surf_action_t)action)->finish = surf_get_clock();
618 surf_network_model->action_state_set((surf_action_t) action,
620 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
626 static void net_update_resource_state(void *id,
627 tmgr_trace_event_t event_type,
628 double value, double date)
630 link_CM02_t nw_link = id;
631 /* printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
632 /* "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
633 /* value, event_type); */
635 if (event_type == nw_link->lmm_resource.power.event) {
637 sg_weight_S_parameter / value - sg_weight_S_parameter /
638 (nw_link->lmm_resource.power.peak *
639 nw_link->lmm_resource.power.scale);
640 lmm_variable_t var = NULL;
641 lmm_element_t elem = NULL;
642 surf_action_network_CM02_t action = NULL;
644 nw_link->lmm_resource.power.peak = value;
645 lmm_update_constraint_bound(network_maxmin_system,
646 nw_link->lmm_resource.constraint,
647 sg_bandwidth_factor *
648 (nw_link->lmm_resource.power.peak *
649 nw_link->lmm_resource.power.scale));
651 TRACE_surf_link_set_bandwidth(date,
653 *) (((nw_link->lmm_resource).
654 generic_resource).name),
655 sg_bandwidth_factor *
656 (nw_link->lmm_resource.power.peak *
657 nw_link->lmm_resource.power.scale));
659 if (sg_weight_S_parameter > 0) {
660 while ((var = lmm_get_var_from_cnst
661 (network_maxmin_system, nw_link->lmm_resource.constraint,
663 action = lmm_variable_id(var);
664 action->weight += delta;
665 if (!(GENERIC_LMM_ACTION(action).suspended))
666 lmm_update_variable_weight(network_maxmin_system,
667 GENERIC_LMM_ACTION(action).variable, action->weight);
670 if (tmgr_trace_event_free(event_type))
671 nw_link->lmm_resource.power.event = NULL;
672 } else if (event_type == nw_link->lat_event) {
673 double delta = value - nw_link->lat_current;
674 lmm_variable_t var = NULL;
675 lmm_element_t elem = NULL;
676 surf_action_network_CM02_t action = NULL;
678 nw_link->lat_current = value;
679 while ((var = lmm_get_var_from_cnst
680 (network_maxmin_system, nw_link->lmm_resource.constraint,
682 action = lmm_variable_id(var);
683 action->lat_current += delta;
684 action->weight += delta;
685 if (action->rate < 0)
686 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
687 sg_tcp_gamma / (2.0 *
688 action->lat_current));
690 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
692 sg_tcp_gamma / (2.0 *
696 if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
697 XBT_INFO("Flow is limited BYBANDWIDTH");
699 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
700 action->lat_current);
703 if (!(GENERIC_LMM_ACTION(action).suspended))
704 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
708 if (tmgr_trace_event_free(event_type))
709 nw_link->lat_event = NULL;
710 } else if (event_type == nw_link->lmm_resource.state_event) {
712 nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
714 lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
715 lmm_variable_t var = NULL;
716 lmm_element_t elem = NULL;
718 nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
719 while ((var = lmm_get_var_from_cnst
720 (network_maxmin_system, cnst, &elem))) {
721 surf_action_t action = lmm_variable_id(var);
723 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
724 surf_action_state_get(action) == SURF_ACTION_READY) {
725 action->finish = date;
726 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
730 if (tmgr_trace_event_free(event_type))
731 nw_link->lmm_resource.state_event = NULL;
733 XBT_CRITICAL("Unknown event ! \n");
738 ("There were a resource state event, need to update actions related to the constraint (%p)",
739 nw_link->lmm_resource.constraint);
744 static surf_action_t net_communicate(const char *src_name,
745 const char *dst_name, double size,
751 surf_action_network_CM02_t action = NULL;
752 double bandwidth_bound;
753 double latency = 0.0;
754 xbt_dynar_t back_route = NULL;
755 int constraints_per_variable = 0;
757 xbt_dynar_t route = xbt_dynar_new(global_routing->size_of_link, NULL);
759 XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
761 routing_get_route_and_latency(src_name, dst_name, &route, &latency);
762 xbt_assert(!xbt_dynar_is_empty(route) || latency,
763 "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
766 xbt_dynar_foreach(route, i, link) {
767 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
772 if (sg_network_crosstraffic == 1) {
773 routing_get_route_and_latency(dst_name, src_name, &back_route, NULL);
774 xbt_dynar_foreach(back_route, i, link) {
775 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
783 surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
784 surf_network_model, failed);
785 #ifdef HAVE_LATENCY_BOUND_TRACKING
786 action->latency_limited = 0;
788 action->weight = action->latency = latency;
790 xbt_swag_insert(action, ((surf_action_t)action)->state_set);
792 if (network_update_mechanism == UM_LAZY) {
793 GENERIC_LMM_ACTION(action).index_heap = -1;
794 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
797 bandwidth_bound = -1.0;
798 if (sg_weight_S_parameter > 0) {
799 xbt_dynar_foreach(route, i, link) {
801 sg_weight_S_parameter /
802 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
805 xbt_dynar_foreach(route, i, link) {
806 double bb = bandwidth_factor_callback(size) *
807 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
809 (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
812 action->lat_current = action->latency;
813 action->latency *= latency_factor_callback(size);
815 bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
817 xbt_assert(!xbt_dynar_is_empty(route),
818 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
820 link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
821 gap_append(size, link, action);
822 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
823 action, src_name, dst_name, action->sender.gap,
827 constraints_per_variable = xbt_dynar_length(route);
828 if (back_route != NULL)
829 constraints_per_variable += xbt_dynar_length(back_route);
831 if (action->latency > 0) {
832 GENERIC_LMM_ACTION(action).variable =
833 lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
834 constraints_per_variable);
835 if (network_update_mechanism == UM_LAZY) {
836 // add to the heap the event when the latency is payed
837 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
838 action->latency + GENERIC_LMM_ACTION(action).last_update);
839 surf_action_lmm_heap_insert(net_action_heap,(surf_action_lmm_t)action, action->latency + GENERIC_LMM_ACTION(action).last_update,
840 xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
843 GENERIC_LMM_ACTION(action).variable =
844 lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
845 constraints_per_variable);
847 if (action->rate < 0) {
848 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
849 (action->lat_current > 0) ?
850 sg_tcp_gamma / (2.0 *
851 action->lat_current) : -1.0);
853 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
854 (action->lat_current > 0) ?
856 sg_tcp_gamma / (2.0 *
857 action->lat_current))
861 xbt_dynar_foreach(route, i, link) {
862 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
863 GENERIC_LMM_ACTION(action).variable, 1.0);
866 if (sg_network_crosstraffic == 1) {
867 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
868 xbt_dynar_foreach(back_route, i, link) {
869 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
870 GENERIC_LMM_ACTION(action).variable, .05);
874 xbt_dynar_free(&route);
877 return (surf_action_t) action;
880 static xbt_dynar_t net_get_route(const char *src, const char *dst)
882 xbt_dynar_t route = NULL;
883 routing_get_route_and_latency(src, dst, &route, NULL);
887 static double net_get_link_bandwidth(const void *link)
889 surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
890 return lmm->power.peak * lmm->power.scale;
893 static double net_get_link_latency(const void *link)
895 return ((link_CM02_t) link)->lat_current;
898 static int net_link_shared(const void *link)
901 lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
904 static void net_action_suspend(surf_action_t action)
906 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 1;
907 lmm_update_variable_weight(network_maxmin_system,
908 ((surf_action_network_CM02_t)
909 action)->generic_lmm_action.variable, 0.0);
911 if (network_update_mechanism == UM_LAZY) // remove action from the heap
912 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
915 static void net_action_resume(surf_action_t action)
917 if (((surf_action_network_CM02_t) action)->generic_lmm_action.suspended) {
918 lmm_update_variable_weight(network_maxmin_system,
919 ((surf_action_network_CM02_t)
920 action)->generic_lmm_action.variable,
921 ((surf_action_network_CM02_t)
923 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 0;
924 if (network_update_mechanism == UM_LAZY) // remove action from the heap
925 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
929 static int net_action_is_suspended(surf_action_t action)
931 return ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended;
934 void net_action_set_max_duration(surf_action_t action, double duration)
936 action->max_duration = duration;
937 if (network_update_mechanism == UM_LAZY) // remove action from the heap
938 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
942 static void net_action_set_category(surf_action_t action,
943 const char *category)
945 action->category = xbt_strdup(category);
949 static void net_finalize(void)
951 surf_model_exit(surf_network_model);
952 surf_network_model = NULL;
954 lmm_system_free(network_maxmin_system);
955 network_maxmin_system = NULL;
957 if (network_update_mechanism == UM_LAZY) {
958 xbt_heap_free(net_action_heap);
959 xbt_swag_free(net_modified_set);
963 xbt_dynar_free(&smpi_bw_factor);
965 xbt_dynar_free(&smpi_lat_factor);
968 static void smpi_gap_append(double size, const link_CM02_t link,
969 surf_action_network_CM02_t action)
971 const char *src = link->lmm_resource.generic_resource.name;
973 surf_action_network_CM02_t last_action;
976 if (sg_sender_gap > 0.0) {
978 gap_lookup = xbt_dict_new();
980 fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
981 action->sender.gap = 0.0;
982 if (fifo && xbt_fifo_size(fifo) > 0) {
983 /* Compute gap from last send */
985 (surf_action_network_CM02_t)
986 xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
987 bw = net_get_link_bandwidth(link);
989 last_action->sender.gap + max(sg_sender_gap,
990 last_action->sender.size / bw);
991 action->latency += action->sender.gap;
993 /* Append action as last send */
994 action->sender.link_name = link->lmm_resource.generic_resource.name;
996 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
997 action->sender.link_name);
999 fifo = xbt_fifo_new();
1000 xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1002 action->sender.fifo_item = xbt_fifo_push(fifo, action);
1003 action->sender.size = size;
1007 static void smpi_gap_remove(surf_action_network_CM02_t action)
1012 if (sg_sender_gap > 0.0 && action->sender.link_name
1013 && action->sender.fifo_item) {
1015 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1016 action->sender.link_name);
1017 xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1018 size = xbt_fifo_size(fifo);
1020 xbt_fifo_free(fifo);
1021 xbt_dict_remove(gap_lookup, action->sender.link_name);
1022 size = xbt_dict_length(gap_lookup);
1024 xbt_dict_free(&gap_lookup);
1030 static void surf_network_model_init_internal(void)
1032 s_surf_action_network_CM02_t comm;
1033 surf_network_model = surf_model_init();
1035 surf_network_model->name = "network";
1036 surf_network_model->action_unref = net_action_unref;
1037 surf_network_model->action_cancel = net_action_cancel;
1038 surf_network_model->action_recycle = net_action_recycle;
1039 surf_network_model->get_remains = net_action_get_remains;
1040 #ifdef HAVE_LATENCY_BOUND_TRACKING
1041 surf_network_model->get_latency_limited = net_get_link_latency_limited;
1044 surf_network_model->set_category = net_action_set_category;
1047 surf_network_model->model_private->resource_used = net_resource_used;
1048 if (network_update_mechanism == UM_LAZY) {
1049 surf_network_model->model_private->share_resources =
1050 net_share_resources_lazy;
1051 surf_network_model->model_private->update_actions_state =
1052 net_update_actions_state_lazy;
1053 } else if (network_update_mechanism == UM_FULL) {
1054 surf_network_model->model_private->share_resources =
1055 net_share_resources_full;
1056 surf_network_model->model_private->update_actions_state =
1057 net_update_actions_state_full;
1060 surf_network_model->model_private->update_resource_state =
1061 net_update_resource_state;
1062 surf_network_model->model_private->finalize = net_finalize;
1064 surf_network_model->suspend = net_action_suspend;
1065 surf_network_model->resume = net_action_resume;
1066 surf_network_model->is_suspended = net_action_is_suspended;
1067 surf_cpu_model->set_max_duration = net_action_set_max_duration;
1069 surf_network_model->extension.network.communicate = net_communicate;
1070 surf_network_model->extension.network.get_route = net_get_route;
1071 surf_network_model->extension.network.get_link_bandwidth =
1072 net_get_link_bandwidth;
1073 surf_network_model->extension.network.get_link_latency =
1074 net_get_link_latency;
1075 surf_network_model->extension.network.link_shared = net_link_shared;
1076 surf_network_model->extension.network.add_traces = net_add_traces;
1077 surf_network_model->extension.network.create_resource =
1078 net_create_resource;
1080 if (!network_maxmin_system)
1081 network_maxmin_system = lmm_system_new(net_selective_update);
1083 routing_model_create(sizeof(link_CM02_t),
1084 net_create_resource("__loopback__",
1085 498000000, NULL, 0.000015, NULL,
1086 SURF_RESOURCE_ON, NULL,
1087 SURF_LINK_FATPIPE, NULL));
1089 if (network_update_mechanism == UM_LAZY) {
1090 net_action_heap = xbt_heap_new(8, NULL);
1091 xbt_heap_set_update_callback(net_action_heap,
1092 surf_action_lmm_update_index_heap);
1094 xbt_swag_new(xbt_swag_offset(comm, generic_lmm_action.action_list_hookup));
1095 network_maxmin_system->keep_track = net_modified_set;
1099 static void set_update_mechanism(void)
1101 char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1103 xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
1105 if (!strcmp(optim, "Full")) {
1106 network_update_mechanism = UM_FULL;
1107 net_selective_update = select;
1108 } else if (!strcmp(optim, "Lazy")) {
1109 network_update_mechanism = UM_LAZY;
1110 net_selective_update = 1;
1111 xbt_assert((select == 1)
1113 (xbt_cfg_is_default_value
1114 (_surf_cfg_set, "network/maxmin_selective_update")),
1115 "Disabling selective update while using the lazy update mechanism is dumb!");
1117 xbt_die("Unsupported optimization (%s) for this model", optim);
1121 /************************************************************************/
1122 /* New model based on LV08 and experimental results of MPI ping-pongs */
1123 /************************************************************************/
1124 /* @Inproceedings{smpi_ipdps, */
1125 /* author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
1126 /* title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
1127 /* booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
1128 /* address={Anchorage (Alaska) USA}, */
1132 void surf_network_model_init_SMPI(void)
1135 if (surf_network_model)
1137 set_update_mechanism();
1139 surf_network_model_init_internal();
1140 latency_factor_callback = &smpi_latency_factor;
1141 bandwidth_factor_callback = &smpi_bandwidth_factor;
1142 bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1143 gap_append = &smpi_gap_append;
1144 gap_remove = &smpi_gap_remove;
1145 net_define_callbacks();
1146 xbt_dynar_push(model_list, &surf_network_model);
1147 network_solve = lmm_solve;
1149 xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1150 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1153 /************************************************************************/
1154 /* New model based on optimizations discussed during Pedro Velho's thesis*/
1155 /************************************************************************/
1156 /* @techreport{VELHO:2011:HAL-00646896:1, */
1157 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
1158 /* title = {{Flow-level network models: have we reached the limits?}}, */
1159 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
1160 /* type = {Rapport de recherche}, */
1161 /* institution = {INRIA}, */
1162 /* number = {RR-7821}, */
1163 /* year = {2011}, */
1165 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
1167 void surf_network_model_init_LegrandVelho(void)
1169 if (surf_network_model)
1172 set_update_mechanism();
1174 surf_network_model_init_internal();
1175 net_define_callbacks();
1176 xbt_dynar_push(model_list, &surf_network_model);
1177 network_solve = lmm_solve;
1179 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor",
1181 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1183 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 20537);
1186 /***************************************************************************/
1187 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1188 /***************************************************************************/
1189 /* @TechReport{ rr-lip2002-40, */
1190 /* author = {Henri Casanova and Loris Marchal}, */
1191 /* institution = {LIP}, */
1192 /* title = {A Network Model for Simulation of Grid Application}, */
1193 /* number = {2002-40}, */
1194 /* month = {oct}, */
1197 void surf_network_model_init_CM02(void)
1200 if (surf_network_model)
1203 set_update_mechanism();
1204 surf_network_model_init_internal();
1205 net_define_callbacks();
1206 xbt_dynar_push(model_list, &surf_network_model);
1207 network_solve = lmm_solve;
1209 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1210 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1212 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1215 /***************************************************************************/
1216 /* The models from Steven H. Low */
1217 /***************************************************************************/
1218 /* @article{Low03, */
1219 /* author={Steven H. Low}, */
1220 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
1222 /* journal={{IEEE/ACM} Transactions on Networking}, */
1223 /* volume={11}, number={4}, */
1225 void surf_network_model_init_Reno(void)
1227 if (surf_network_model)
1230 set_update_mechanism();
1231 surf_network_model_init_internal();
1232 net_define_callbacks();
1234 xbt_dynar_push(model_list, &surf_network_model);
1235 lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1237 network_solve = lagrange_solve;
1239 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1240 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1242 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1246 void surf_network_model_init_Reno2(void)
1248 if (surf_network_model)
1251 set_update_mechanism();
1252 surf_network_model_init_internal();
1253 net_define_callbacks();
1255 xbt_dynar_push(model_list, &surf_network_model);
1256 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1258 network_solve = lagrange_solve;
1260 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1261 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1263 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1267 void surf_network_model_init_Vegas(void)
1269 if (surf_network_model)
1272 set_update_mechanism();
1273 surf_network_model_init_internal();
1274 net_define_callbacks();
1276 xbt_dynar_push(model_list, &surf_network_model);
1277 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1279 network_solve = lagrange_solve;
1281 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1282 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1284 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);