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 typedef struct s_net_card *net_card_t;
49 typedef struct s_net_card {
54 double sg_sender_gap = 0.0;
55 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
56 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
57 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
59 double sg_tcp_gamma = 0.0;
60 int sg_network_crosstraffic = 0;
62 xbt_dict_t gap_lookup = NULL;
64 e_UM_t network_update_mechanism = UM_UNDEFINED;
65 static int net_selective_update = 0;
67 static int net_action_is_suspended(surf_action_t action);
68 static void net_update_action_remaining_lazy(double now);
70 static xbt_swag_t net_modified_set = NULL;
71 static xbt_heap_t net_action_heap = NULL;
74 /******************************************************************************/
75 /* Factors callbacks */
76 /******************************************************************************/
77 static double constant_latency_factor(double size)
79 return sg_latency_factor;
82 static double constant_bandwidth_factor(double size)
84 return sg_bandwidth_factor;
87 static double constant_bandwidth_constraint(double rate, double bound,
93 /**********************/
95 /**********************/
96 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
99 unsigned int iter = 0;
100 s_smpi_factor_t fact;
101 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
103 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
104 radical_elements = xbt_str_split(smpi_coef_string, ";");
105 xbt_dynar_foreach(radical_elements, iter, value) {
107 radical_elements2 = xbt_str_split(value, ":");
108 if (xbt_dynar_length(radical_elements2) != 2)
109 xbt_die("Malformed radical for smpi factor!");
110 fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
111 fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
112 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
113 XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
114 xbt_dynar_free(&radical_elements2);
116 xbt_dynar_free(&radical_elements);
120 static double smpi_bandwidth_factor(double size)
124 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/bw_factor"));
126 unsigned int iter = 0;
127 s_smpi_factor_t fact;
128 xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
129 if (size >= fact.factor) {
130 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
138 static double smpi_latency_factor(double size)
140 if (!smpi_lat_factor)
142 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/lat_factor"));
144 unsigned int iter = 0;
145 s_smpi_factor_t fact;
146 xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
147 if (size >= fact.factor) {
148 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
156 /**--------- <copy/paste C code snippet in surf/network.c> -----------*/
158 static double smpi_bandwidth_constraint(double rate, double bound,
161 return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
164 static double (*latency_factor_callback) (double) =
165 &constant_latency_factor;
166 static double (*bandwidth_factor_callback) (double) =
167 &constant_bandwidth_factor;
168 static double (*bandwidth_constraint_callback) (double, double, double) =
169 &constant_bandwidth_constraint;
171 static void (*gap_append) (double, const link_CM02_t,
172 surf_action_network_CM02_t) = NULL;
173 static void (*gap_remove) (surf_action_network_CM02_t) = NULL;
175 static void *net_create_resource(const char *name,
177 tmgr_trace_t bw_trace,
179 tmgr_trace_t lat_trace,
180 e_surf_resource_state_t
182 tmgr_trace_t state_trace,
183 e_surf_link_sharing_policy_t
184 policy, xbt_dict_t properties)
186 link_CM02_t nw_link = (link_CM02_t)
187 surf_resource_lmm_new(sizeof(s_link_CM02_t),
188 surf_network_model, name, properties,
189 network_maxmin_system,
190 sg_bandwidth_factor * bw_initial,
192 state_initial, state_trace,
193 bw_initial, bw_trace);
195 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
196 "Link '%s' declared several times in the platform file.",
199 nw_link->lat_current = lat_initial;
202 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
204 if (policy == SURF_LINK_FATPIPE)
205 lmm_constraint_shared(nw_link->lmm_resource.constraint);
207 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
212 static void net_parse_link_init(sg_platf_link_cbarg_t link)
214 if (link->policy == SURF_LINK_FULLDUPLEX) {
216 link_id = bprintf("%s_UP", link->id);
217 net_create_resource(link_id,
219 link->bandwidth_trace,
223 link->state_trace, link->policy, link->properties);
225 link_id = bprintf("%s_DOWN", link->id);
226 net_create_resource(link_id,
228 link->bandwidth_trace,
232 link->state_trace, link->policy, link->properties);
235 net_create_resource(link->id,
237 link->bandwidth_trace,
241 link->state_trace, link->policy, link->properties);
245 static void net_add_traces(void)
247 xbt_dict_cursor_t cursor = NULL;
248 char *trace_name, *elm;
250 static int called = 0;
255 /* connect all traces relative to network */
256 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
257 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
258 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
260 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
263 "Cannot connect trace %s to link %s: trace undefined",
266 link->lmm_resource.state_event =
267 tmgr_history_add_trace(history, trace, 0.0, 0, link);
270 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
271 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
272 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
274 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
277 "Cannot connect trace %s to link %s: trace undefined",
280 link->lmm_resource.power.event =
281 tmgr_history_add_trace(history, trace, 0.0, 0, link);
284 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
285 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
286 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
288 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
291 "Cannot connect trace %s to link %s: trace undefined",
294 link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
298 static void net_define_callbacks(void)
300 /* Figuring out the network links */
301 sg_platf_link_add_cb(net_parse_link_init);
302 sg_platf_postparse_add_cb(net_add_traces);
305 static int net_resource_used(void *resource_id)
307 return lmm_constraint_used(network_maxmin_system, ((surf_resource_lmm_t)
312 static int net_action_unref(surf_action_t action)
315 if (!action->refcount) {
316 xbt_swag_remove(action, action->state_set);
317 if (((surf_action_lmm_t)action)->variable) {
318 lmm_variable_free(network_maxmin_system,
319 ((surf_action_lmm_t) action)->variable);
321 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
322 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t) action);
323 xbt_swag_remove(action, net_modified_set);
325 surf_action_free(&action);
333 static void net_action_cancel(surf_action_t action)
335 XBT_DEBUG("cancel action %p", action);
336 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
337 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
338 xbt_swag_remove(action, net_modified_set);
339 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t) action);
343 void net_action_recycle(surf_action_t action)
348 #ifdef HAVE_LATENCY_BOUND_TRACKING
349 int net_get_link_latency_limited(surf_action_t action)
351 return action->latency_limited;
355 double net_action_get_remains(surf_action_t action)
357 if (network_update_mechanism == UM_LAZY) /* update remains before return it */
358 net_update_action_remaining_lazy(surf_get_clock());
359 return action->remains;
362 static void net_update_action_remaining_lazy(double now)
364 surf_action_lmm_t action = NULL;
367 xbt_swag_foreach(action, net_modified_set) {
369 if (action->suspended != 0) {
373 delta = now - action->last_update;
375 double_update(&(((surf_action_t)action)->remains),
376 lmm_variable_getvalue(action->variable) * delta);
378 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
379 double_update(&(((surf_action_t)action)->max_duration), delta);
381 if ((((surf_action_t)action)->remains <= 0) &&
382 (lmm_get_variable_weight(action->variable) > 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);
387 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
388 && (((surf_action_t)action)->max_duration <= 0))) {
389 ((surf_action_t)action)->finish = surf_get_clock();
390 surf_network_model->action_state_set((surf_action_t) action,
392 surf_action_lmm_heap_remove(net_action_heap,action);
395 action->last_update = now;
399 static double net_share_resources_full(double now)
401 s_surf_action_lmm_t s_action;
402 surf_action_network_CM02_t action = NULL;
403 xbt_swag_t running_actions =
404 surf_network_model->states.running_action_set;
407 min = generic_maxmin_share_resources(running_actions,
408 xbt_swag_offset(s_action,
410 network_maxmin_system,
413 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable) )))
415 xbt_swag_foreach(action, running_actions) {
416 #ifdef HAVE_LATENCY_BOUND_TRACKING
417 if (lmm_is_variable_limited_by_latency(GENERIC_LMM_ACTION(action).variable)) {
418 action->latency_limited = 1;
420 action->latency_limited = 0;
423 if (action->latency > 0) {
424 min = (min < 0) ? action->latency : min(min, action->latency);
428 XBT_DEBUG("Min of share resources %f", min);
433 static double net_share_resources_lazy(double now)
435 surf_action_network_CM02_t action = NULL;
440 ("Before share resources, the size of modified actions set is %d",
441 xbt_swag_size(net_modified_set));
442 net_update_action_remaining_lazy(now);
444 lmm_solve(network_maxmin_system);
447 ("After share resources, The size of modified actions set is %d",
448 xbt_swag_size(net_modified_set));
450 xbt_swag_foreach(action, net_modified_set) {
451 int max_dur_flag = 0;
453 if (GENERIC_ACTION(action).state_set !=
454 surf_network_model->states.running_action_set)
457 /* bogus priority, skip it */
458 if (GENERIC_ACTION(action).priority <= 0)
462 value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
464 if (GENERIC_ACTION(action).remains > 0) {
465 value = GENERIC_ACTION(action).remains / value;
473 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
475 || GENERIC_ACTION(action).start +
476 GENERIC_ACTION(action).max_duration < min)) {
477 min = GENERIC_ACTION(action).start +
478 GENERIC_ACTION(action).max_duration;
482 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
483 GENERIC_ACTION(action).start, now + value,
484 GENERIC_ACTION(action).max_duration);
487 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
488 surf_action_lmm_heap_insert(net_action_heap,(surf_action_lmm_t)action, min, max_dur_flag ? MAX_DURATION : NORMAL);
489 XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min,
491 } else DIE_IMPOSSIBLE;
494 //hereafter must have already the min value for this resource model
495 if (xbt_heap_size(net_action_heap) > 0)
496 min = xbt_heap_maxkey(net_action_heap) - now;
500 XBT_DEBUG("The minimum with the HEAP %lf", min);
505 static void net_update_actions_state_full(double now, double delta)
508 surf_action_network_CM02_t action = NULL;
509 surf_action_network_CM02_t next_action = NULL;
510 xbt_swag_t running_actions =
511 surf_network_model->states.running_action_set;
513 xbt_swag_t failed_actions =
514 surf_network_model->states.failed_action_set;
517 xbt_swag_foreach_safe(action, next_action, running_actions) {
519 if (action->latency > 0) {
520 if (action->latency > deltap) {
521 double_update(&(action->latency), deltap);
524 double_update(&(deltap), action->latency);
525 action->latency = 0.0;
527 if ((action->latency == 0.0) && !(GENERIC_LMM_ACTION(action).suspended))
528 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
532 if (TRACE_is_enabled()) {
533 int n = lmm_get_number_of_cnst_from_var(network_maxmin_system, GENERIC_LMM_ACTION(action).variable);
535 for (i = 0; i < n; i++){
536 lmm_constraint_t constraint = lmm_get_cnst_from_var(network_maxmin_system,
537 GENERIC_LMM_ACTION(action).variable,
539 link_CM02_t link = lmm_constraint_id(constraint);
540 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
541 ((surf_action_t)action)->category,
542 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
548 if (!lmm_get_number_of_cnst_from_var
549 (network_maxmin_system, GENERIC_LMM_ACTION(action).variable)) {
550 /* There is actually no link used, hence an infinite bandwidth.
551 * This happens often when using models like vivaldi.
552 * In such case, just make sure that the action completes immediately.
554 double_update(&(GENERIC_ACTION(action).remains),
555 GENERIC_ACTION(action).remains);
557 double_update(&(GENERIC_ACTION(action).remains),
558 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * deltap);
559 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
560 double_update(&(((surf_action_t)action)->max_duration), delta);
562 if ((GENERIC_ACTION(action).remains <= 0) &&
563 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
564 ((surf_action_t)action)->finish = surf_get_clock();
565 surf_network_model->action_state_set((surf_action_t) action,
570 } else if ((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
571 && (((surf_action_t)action)->max_duration <= 0)) {
572 ((surf_action_t)action)->finish = surf_get_clock();
573 surf_network_model->action_state_set((surf_action_t) action,
583 static void net_update_actions_state_lazy(double now, double delta)
585 surf_action_network_CM02_t action = NULL;
587 while ((xbt_heap_size(net_action_heap) > 0)
588 && (double_equals(xbt_heap_maxkey(net_action_heap), now))) {
589 action = xbt_heap_pop(net_action_heap);
590 XBT_DEBUG("Action %p: finish", action);
591 GENERIC_ACTION(action).finish = surf_get_clock();
593 if (TRACE_is_enabled()) {
594 int n = lmm_get_number_of_cnst_from_var(network_maxmin_system, GENERIC_LMM_ACTION(action).variable);
596 for (i = 0; i < n; i++){
597 lmm_constraint_t constraint = lmm_get_cnst_from_var(network_maxmin_system,
598 GENERIC_LMM_ACTION(action).variable,
600 link_CM02_t link = lmm_constraint_id(constraint);
601 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
602 ((surf_action_t)action)->category,
603 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
604 GENERIC_LMM_ACTION(action).last_update,
605 now - GENERIC_LMM_ACTION(action).last_update);
610 // if I am wearing a latency hat
611 if (GENERIC_LMM_ACTION(action).hat == LATENCY) {
612 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
614 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
615 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
617 // if I am wearing a max_duration or normal hat
618 } else if (GENERIC_LMM_ACTION(action).hat == MAX_DURATION ||
619 GENERIC_LMM_ACTION(action).hat == NORMAL) {
620 // no need to communicate anymore
621 // assume that flows that reached max_duration have remaining of 0
622 GENERIC_ACTION(action).remains = 0;
623 ((surf_action_t)action)->finish = surf_get_clock();
624 surf_network_model->action_state_set((surf_action_t) action,
626 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
632 static void net_update_resource_state(void *id,
633 tmgr_trace_event_t event_type,
634 double value, double date)
636 link_CM02_t nw_link = id;
637 /* printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
638 /* "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
639 /* value, event_type); */
641 if (event_type == nw_link->lmm_resource.power.event) {
643 sg_weight_S_parameter / value - sg_weight_S_parameter /
644 (nw_link->lmm_resource.power.peak *
645 nw_link->lmm_resource.power.scale);
646 lmm_variable_t var = NULL;
647 lmm_element_t elem = NULL;
648 surf_action_network_CM02_t action = NULL;
650 nw_link->lmm_resource.power.peak = value;
651 lmm_update_constraint_bound(network_maxmin_system,
652 nw_link->lmm_resource.constraint,
653 sg_bandwidth_factor *
654 (nw_link->lmm_resource.power.peak *
655 nw_link->lmm_resource.power.scale));
657 TRACE_surf_link_set_bandwidth(date,
659 *) (((nw_link->lmm_resource).
660 generic_resource).name),
661 sg_bandwidth_factor *
662 (nw_link->lmm_resource.power.peak *
663 nw_link->lmm_resource.power.scale));
665 if (sg_weight_S_parameter > 0) {
666 while ((var = lmm_get_var_from_cnst
667 (network_maxmin_system, nw_link->lmm_resource.constraint,
669 action = lmm_variable_id(var);
670 action->weight += delta;
671 if (!(GENERIC_LMM_ACTION(action).suspended))
672 lmm_update_variable_weight(network_maxmin_system,
673 GENERIC_LMM_ACTION(action).variable, action->weight);
676 if (tmgr_trace_event_free(event_type))
677 nw_link->lmm_resource.power.event = NULL;
678 } else if (event_type == nw_link->lat_event) {
679 double delta = value - nw_link->lat_current;
680 lmm_variable_t var = NULL;
681 lmm_element_t elem = NULL;
682 surf_action_network_CM02_t action = NULL;
684 nw_link->lat_current = value;
685 while ((var = lmm_get_var_from_cnst
686 (network_maxmin_system, nw_link->lmm_resource.constraint,
688 action = lmm_variable_id(var);
689 action->lat_current += delta;
690 action->weight += delta;
691 if (action->rate < 0)
692 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
693 sg_tcp_gamma / (2.0 *
694 action->lat_current));
696 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
698 sg_tcp_gamma / (2.0 *
702 if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
703 XBT_INFO("Flow is limited BYBANDWIDTH");
705 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
706 action->lat_current);
709 if (!(GENERIC_LMM_ACTION(action).suspended))
710 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
714 if (tmgr_trace_event_free(event_type))
715 nw_link->lat_event = NULL;
716 } else if (event_type == nw_link->lmm_resource.state_event) {
718 nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
720 lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
721 lmm_variable_t var = NULL;
722 lmm_element_t elem = NULL;
724 nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
725 while ((var = lmm_get_var_from_cnst
726 (network_maxmin_system, cnst, &elem))) {
727 surf_action_t action = lmm_variable_id(var);
729 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
730 surf_action_state_get(action) == SURF_ACTION_READY) {
731 action->finish = date;
732 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
736 if (tmgr_trace_event_free(event_type))
737 nw_link->lmm_resource.state_event = NULL;
739 XBT_CRITICAL("Unknown event ! \n");
744 ("There were a resource state event, need to update actions related to the constraint (%p)",
745 nw_link->lmm_resource.constraint);
750 static surf_action_t net_communicate(const char *src_name,
751 const char *dst_name, double size,
757 surf_action_network_CM02_t action = NULL;
758 double bandwidth_bound;
759 double latency = 0.0;
760 xbt_dynar_t back_route = NULL;
761 int constraints_per_variable = 0;
763 xbt_dynar_t route = xbt_dynar_new(global_routing->size_of_link, NULL);
765 XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
767 routing_get_route_and_latency(src_name, dst_name, &route, &latency);
768 xbt_assert(!xbt_dynar_is_empty(route) || latency,
769 "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
772 xbt_dynar_foreach(route, i, link) {
773 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
778 if (sg_network_crosstraffic == 1) {
779 routing_get_route_and_latency(dst_name, src_name, &back_route, NULL);
780 xbt_dynar_foreach(back_route, i, link) {
781 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
789 surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
790 surf_network_model, failed);
791 #ifdef HAVE_LATENCY_BOUND_TRACKING
792 action->latency_limited = 0;
794 action->weight = action->latency = latency;
796 xbt_swag_insert(action, ((surf_action_t)action)->state_set);
798 if (network_update_mechanism == UM_LAZY) {
799 GENERIC_LMM_ACTION(action).index_heap = -1;
800 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
803 bandwidth_bound = -1.0;
804 if (sg_weight_S_parameter > 0) {
805 xbt_dynar_foreach(route, i, link) {
807 sg_weight_S_parameter /
808 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
811 xbt_dynar_foreach(route, i, link) {
812 double bb = bandwidth_factor_callback(size) *
813 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
815 (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
818 action->lat_current = action->latency;
819 action->latency *= latency_factor_callback(size);
821 bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
823 xbt_assert(!xbt_dynar_is_empty(route),
824 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
826 link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
827 gap_append(size, link, action);
828 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
829 action, src_name, dst_name, action->sender.gap,
833 constraints_per_variable = xbt_dynar_length(route);
834 if (back_route != NULL)
835 constraints_per_variable += xbt_dynar_length(back_route);
837 if (action->latency > 0) {
838 GENERIC_LMM_ACTION(action).variable =
839 lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
840 constraints_per_variable);
841 if (network_update_mechanism == UM_LAZY) {
842 // add to the heap the event when the latency is payed
843 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
844 action->latency + GENERIC_LMM_ACTION(action).last_update);
845 surf_action_lmm_heap_insert(net_action_heap,(surf_action_lmm_t)action, action->latency + GENERIC_LMM_ACTION(action).last_update,
846 xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
849 GENERIC_LMM_ACTION(action).variable =
850 lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
851 constraints_per_variable);
853 if (action->rate < 0) {
854 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
855 (action->lat_current > 0) ?
856 sg_tcp_gamma / (2.0 *
857 action->lat_current) : -1.0);
859 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
860 (action->lat_current > 0) ?
862 sg_tcp_gamma / (2.0 *
863 action->lat_current))
867 xbt_dynar_foreach(route, i, link) {
868 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
869 GENERIC_LMM_ACTION(action).variable, 1.0);
872 if (sg_network_crosstraffic == 1) {
873 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
874 xbt_dynar_foreach(back_route, i, link) {
875 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
876 GENERIC_LMM_ACTION(action).variable, .05);
880 xbt_dynar_free(&route);
883 return (surf_action_t) action;
886 static xbt_dynar_t net_get_route(const char *src, const char *dst)
888 xbt_dynar_t route = NULL;
889 routing_get_route_and_latency(src, dst, &route, NULL);
893 static double net_get_link_bandwidth(const void *link)
895 surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
896 return lmm->power.peak * lmm->power.scale;
899 static double net_get_link_latency(const void *link)
901 return ((link_CM02_t) link)->lat_current;
904 static int net_link_shared(const void *link)
907 lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
910 static void net_action_suspend(surf_action_t action)
912 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 1;
913 lmm_update_variable_weight(network_maxmin_system,
914 ((surf_action_network_CM02_t)
915 action)->generic_lmm_action.variable, 0.0);
917 if (network_update_mechanism == UM_LAZY) // remove action from the heap
918 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
921 static void net_action_resume(surf_action_t action)
923 if (((surf_action_network_CM02_t) action)->generic_lmm_action.suspended) {
924 lmm_update_variable_weight(network_maxmin_system,
925 ((surf_action_network_CM02_t)
926 action)->generic_lmm_action.variable,
927 ((surf_action_network_CM02_t)
929 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 0;
930 if (network_update_mechanism == UM_LAZY) // remove action from the heap
931 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
935 static int net_action_is_suspended(surf_action_t action)
937 return ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended;
940 void net_action_set_max_duration(surf_action_t action, double duration)
942 action->max_duration = duration;
943 if (network_update_mechanism == UM_LAZY) // remove action from the heap
944 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
948 static void net_action_set_category(surf_action_t action,
949 const char *category)
951 action->category = xbt_strdup(category);
955 static void net_finalize(void)
957 surf_model_exit(surf_network_model);
958 surf_network_model = NULL;
960 lmm_system_free(network_maxmin_system);
961 network_maxmin_system = NULL;
963 if (network_update_mechanism == UM_LAZY) {
964 xbt_heap_free(net_action_heap);
965 xbt_swag_free(net_modified_set);
969 xbt_dynar_free(&smpi_bw_factor);
971 xbt_dynar_free(&smpi_lat_factor);
974 static void smpi_gap_append(double size, const link_CM02_t link,
975 surf_action_network_CM02_t action)
977 const char *src = link->lmm_resource.generic_resource.name;
979 surf_action_network_CM02_t last_action;
982 if (sg_sender_gap > 0.0) {
984 gap_lookup = xbt_dict_new();
986 fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
987 action->sender.gap = 0.0;
988 if (fifo && xbt_fifo_size(fifo) > 0) {
989 /* Compute gap from last send */
991 (surf_action_network_CM02_t)
992 xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
993 bw = net_get_link_bandwidth(link);
995 last_action->sender.gap + max(sg_sender_gap,
996 last_action->sender.size / bw);
997 action->latency += action->sender.gap;
999 /* Append action as last send */
1000 action->sender.link_name = link->lmm_resource.generic_resource.name;
1002 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1003 action->sender.link_name);
1005 fifo = xbt_fifo_new();
1006 xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1008 action->sender.fifo_item = xbt_fifo_push(fifo, action);
1009 action->sender.size = size;
1013 static void smpi_gap_remove(surf_action_network_CM02_t action)
1018 if (sg_sender_gap > 0.0 && action->sender.link_name
1019 && action->sender.fifo_item) {
1021 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1022 action->sender.link_name);
1023 xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1024 size = xbt_fifo_size(fifo);
1026 xbt_fifo_free(fifo);
1027 xbt_dict_remove(gap_lookup, action->sender.link_name);
1028 size = xbt_dict_length(gap_lookup);
1030 xbt_dict_free(&gap_lookup);
1036 static void surf_network_model_init_internal(void)
1038 s_surf_action_network_CM02_t comm;
1039 surf_network_model = surf_model_init();
1041 surf_network_model->name = "network";
1042 surf_network_model->action_unref = net_action_unref;
1043 surf_network_model->action_cancel = net_action_cancel;
1044 surf_network_model->action_recycle = net_action_recycle;
1045 surf_network_model->get_remains = net_action_get_remains;
1046 #ifdef HAVE_LATENCY_BOUND_TRACKING
1047 surf_network_model->get_latency_limited = net_get_link_latency_limited;
1050 surf_network_model->set_category = net_action_set_category;
1053 surf_network_model->model_private->resource_used = net_resource_used;
1054 if (network_update_mechanism == UM_LAZY) {
1055 surf_network_model->model_private->share_resources =
1056 net_share_resources_lazy;
1057 surf_network_model->model_private->update_actions_state =
1058 net_update_actions_state_lazy;
1059 } else if (network_update_mechanism == UM_FULL) {
1060 surf_network_model->model_private->share_resources =
1061 net_share_resources_full;
1062 surf_network_model->model_private->update_actions_state =
1063 net_update_actions_state_full;
1066 surf_network_model->model_private->update_resource_state =
1067 net_update_resource_state;
1068 surf_network_model->model_private->finalize = net_finalize;
1070 surf_network_model->suspend = net_action_suspend;
1071 surf_network_model->resume = net_action_resume;
1072 surf_network_model->is_suspended = net_action_is_suspended;
1073 surf_cpu_model->set_max_duration = net_action_set_max_duration;
1075 surf_network_model->extension.network.communicate = net_communicate;
1076 surf_network_model->extension.network.get_route = net_get_route;
1077 surf_network_model->extension.network.get_link_bandwidth =
1078 net_get_link_bandwidth;
1079 surf_network_model->extension.network.get_link_latency =
1080 net_get_link_latency;
1081 surf_network_model->extension.network.link_shared = net_link_shared;
1082 surf_network_model->extension.network.add_traces = net_add_traces;
1083 surf_network_model->extension.network.create_resource =
1084 net_create_resource;
1086 if (!network_maxmin_system)
1087 network_maxmin_system = lmm_system_new(net_selective_update);
1089 routing_model_create(sizeof(link_CM02_t),
1090 net_create_resource("__loopback__",
1091 498000000, NULL, 0.000015, NULL,
1092 SURF_RESOURCE_ON, NULL,
1093 SURF_LINK_FATPIPE, NULL));
1095 if (network_update_mechanism == UM_LAZY) {
1096 net_action_heap = xbt_heap_new(8, NULL);
1097 xbt_heap_set_update_callback(net_action_heap,
1098 surf_action_lmm_update_index_heap);
1100 xbt_swag_new(xbt_swag_offset(comm, generic_lmm_action.action_list_hookup));
1101 network_maxmin_system->keep_track = net_modified_set;
1105 static void set_update_mechanism(void)
1107 char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1109 xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
1111 if (!strcmp(optim, "Full")) {
1112 network_update_mechanism = UM_FULL;
1113 net_selective_update = select;
1114 } else if (!strcmp(optim, "Lazy")) {
1115 network_update_mechanism = UM_LAZY;
1116 net_selective_update = 1;
1117 xbt_assert((select == 1)
1119 (xbt_cfg_is_default_value
1120 (_surf_cfg_set, "network/maxmin_selective_update")),
1121 "Disabling selective update while using the lazy update mechanism is dumb!");
1123 xbt_die("Unsupported optimization (%s) for this model", optim);
1127 /************************************************************************/
1128 /* New model based on LV08 and experimental results of MPI ping-pongs */
1129 /************************************************************************/
1130 /* @Inproceedings{smpi_ipdps, */
1131 /* author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
1132 /* title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
1133 /* booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
1134 /* address={Anchorage (Alaska) USA}, */
1138 void surf_network_model_init_SMPI(void)
1141 if (surf_network_model)
1143 set_update_mechanism();
1145 surf_network_model_init_internal();
1146 latency_factor_callback = &smpi_latency_factor;
1147 bandwidth_factor_callback = &smpi_bandwidth_factor;
1148 bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1149 gap_append = &smpi_gap_append;
1150 gap_remove = &smpi_gap_remove;
1151 net_define_callbacks();
1152 xbt_dynar_push(model_list, &surf_network_model);
1153 network_solve = lmm_solve;
1155 xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1156 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1159 /************************************************************************/
1160 /* New model based on optimizations discussed during Pedro Velho's thesis*/
1161 /************************************************************************/
1162 /* @techreport{VELHO:2011:HAL-00646896:1, */
1163 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
1164 /* title = {{Flow-level network models: have we reached the limits?}}, */
1165 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
1166 /* type = {Rapport de recherche}, */
1167 /* institution = {INRIA}, */
1168 /* number = {RR-7821}, */
1169 /* year = {2011}, */
1171 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
1173 void surf_network_model_init_LegrandVelho(void)
1175 if (surf_network_model)
1178 set_update_mechanism();
1180 surf_network_model_init_internal();
1181 net_define_callbacks();
1182 xbt_dynar_push(model_list, &surf_network_model);
1183 network_solve = lmm_solve;
1185 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor",
1187 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1189 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 20537);
1192 /***************************************************************************/
1193 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1194 /***************************************************************************/
1195 /* @TechReport{ rr-lip2002-40, */
1196 /* author = {Henri Casanova and Loris Marchal}, */
1197 /* institution = {LIP}, */
1198 /* title = {A Network Model for Simulation of Grid Application}, */
1199 /* number = {2002-40}, */
1200 /* month = {oct}, */
1203 void surf_network_model_init_CM02(void)
1206 if (surf_network_model)
1209 set_update_mechanism();
1210 surf_network_model_init_internal();
1211 net_define_callbacks();
1212 xbt_dynar_push(model_list, &surf_network_model);
1213 network_solve = lmm_solve;
1215 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1216 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1218 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1221 /***************************************************************************/
1222 /* The models from Steven H. Low */
1223 /***************************************************************************/
1224 /* @article{Low03, */
1225 /* author={Steven H. Low}, */
1226 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
1228 /* journal={{IEEE/ACM} Transactions on Networking}, */
1229 /* volume={11}, number={4}, */
1231 void surf_network_model_init_Reno(void)
1233 if (surf_network_model)
1236 set_update_mechanism();
1237 surf_network_model_init_internal();
1238 net_define_callbacks();
1240 xbt_dynar_push(model_list, &surf_network_model);
1241 lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1243 network_solve = lagrange_solve;
1245 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1246 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1248 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1252 void surf_network_model_init_Reno2(void)
1254 if (surf_network_model)
1257 set_update_mechanism();
1258 surf_network_model_init_internal();
1259 net_define_callbacks();
1261 xbt_dynar_push(model_list, &surf_network_model);
1262 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1264 network_solve = lagrange_solve;
1266 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1267 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1269 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1273 void surf_network_model_init_Vegas(void)
1275 if (surf_network_model)
1278 set_update_mechanism();
1279 surf_network_model_init_internal();
1280 net_define_callbacks();
1282 xbt_dynar_push(model_list, &surf_network_model);
1283 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1285 network_solve = lagrange_solve;
1287 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1288 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1290 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);