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 update_action_remaining_lazy(double now);
70 static xbt_swag_t net_modified_set = NULL;
71 static xbt_heap_t net_action_heap = NULL;
73 /* added to manage the communication action's heap */
74 static void net_action_update_index_heap(void *action, int i)
76 surf_action_network_CM02_t a = action;
77 GENERIC_LMM_ACTION(a).index_heap = i;
80 /* insert action on heap using a given key and a hat (heap_action_type)
81 * a hat can be of three types for communications:
83 * NORMAL = this is a normal heap entry stating the date to finish transmitting
84 * LATENCY = this is a heap entry to warn us when the latency is payed
85 * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
87 static void heap_insert(surf_action_network_CM02_t action, double key,
88 enum heap_action_type hat)
90 GENERIC_LMM_ACTION(action).hat = hat;
91 xbt_heap_push(net_action_heap, action, key);
94 static void heap_remove(surf_action_network_CM02_t action)
96 GENERIC_LMM_ACTION(action).hat = NOTSET;
97 if (GENERIC_LMM_ACTION(action).index_heap >= 0) {
98 xbt_heap_remove(net_action_heap, GENERIC_LMM_ACTION(action).index_heap);
102 /******************************************************************************/
103 /* Factors callbacks */
104 /******************************************************************************/
105 static double constant_latency_factor(double size)
107 return sg_latency_factor;
110 static double constant_bandwidth_factor(double size)
112 return sg_bandwidth_factor;
115 static double constant_bandwidth_constraint(double rate, double bound,
121 /**********************/
123 /**********************/
124 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
127 unsigned int iter = 0;
128 s_smpi_factor_t fact;
129 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
131 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
132 radical_elements = xbt_str_split(smpi_coef_string, ";");
133 xbt_dynar_foreach(radical_elements, iter, value) {
135 radical_elements2 = xbt_str_split(value, ":");
136 if (xbt_dynar_length(radical_elements2) != 2)
137 xbt_die("Malformed radical for smpi factor!");
138 fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
139 fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
140 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
141 XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
142 xbt_dynar_free(&radical_elements2);
144 xbt_dynar_free(&radical_elements);
148 static double smpi_bandwidth_factor(double size)
152 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/bw_factor"));
154 unsigned int iter = 0;
155 s_smpi_factor_t fact;
156 xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
157 if (size >= fact.factor) {
158 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
166 static double smpi_latency_factor(double size)
168 if (!smpi_lat_factor)
170 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/lat_factor"));
172 unsigned int iter = 0;
173 s_smpi_factor_t fact;
174 xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
175 if (size >= fact.factor) {
176 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
184 /**--------- <copy/paste C code snippet in surf/network.c> -----------*/
186 static double smpi_bandwidth_constraint(double rate, double bound,
189 return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
192 static double (*latency_factor_callback) (double) =
193 &constant_latency_factor;
194 static double (*bandwidth_factor_callback) (double) =
195 &constant_bandwidth_factor;
196 static double (*bandwidth_constraint_callback) (double, double, double) =
197 &constant_bandwidth_constraint;
199 static void (*gap_append) (double, const link_CM02_t,
200 surf_action_network_CM02_t) = NULL;
201 static void (*gap_remove) (surf_action_network_CM02_t) = NULL;
203 static void *net_create_resource(const char *name,
205 tmgr_trace_t bw_trace,
207 tmgr_trace_t lat_trace,
208 e_surf_resource_state_t
210 tmgr_trace_t state_trace,
211 e_surf_link_sharing_policy_t
212 policy, xbt_dict_t properties)
214 link_CM02_t nw_link = (link_CM02_t)
215 surf_resource_lmm_new(sizeof(s_link_CM02_t),
216 surf_network_model, name, properties,
217 network_maxmin_system,
218 sg_bandwidth_factor * bw_initial,
220 state_initial, state_trace,
221 bw_initial, bw_trace);
223 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
224 "Link '%s' declared several times in the platform file.",
227 nw_link->lat_current = lat_initial;
230 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
232 if (policy == SURF_LINK_FATPIPE)
233 lmm_constraint_shared(nw_link->lmm_resource.constraint);
235 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
240 static void net_parse_link_init(sg_platf_link_cbarg_t link)
242 if (link->policy == SURF_LINK_FULLDUPLEX) {
244 link_id = bprintf("%s_UP", link->id);
245 net_create_resource(link_id,
247 link->bandwidth_trace,
251 link->state_trace, link->policy, link->properties);
253 link_id = bprintf("%s_DOWN", link->id);
254 net_create_resource(link_id,
256 link->bandwidth_trace,
260 link->state_trace, link->policy, link->properties);
263 net_create_resource(link->id,
265 link->bandwidth_trace,
269 link->state_trace, link->policy, link->properties);
273 static void net_add_traces(void)
275 xbt_dict_cursor_t cursor = NULL;
276 char *trace_name, *elm;
278 static int called = 0;
283 /* connect all traces relative to network */
284 xbt_dict_foreach(trace_connect_list_link_avail, 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->lmm_resource.state_event =
295 tmgr_history_add_trace(history, trace, 0.0, 0, link);
298 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
299 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
300 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
302 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
305 "Cannot connect trace %s to link %s: trace undefined",
308 link->lmm_resource.power.event =
309 tmgr_history_add_trace(history, trace, 0.0, 0, link);
312 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
313 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
314 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
316 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
319 "Cannot connect trace %s to link %s: trace undefined",
322 link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
326 static void net_define_callbacks(void)
328 /* Figuring out the network links */
329 sg_platf_link_add_cb(net_parse_link_init);
330 sg_platf_postparse_add_cb(net_add_traces);
333 static int net_resource_used(void *resource_id)
335 return lmm_constraint_used(network_maxmin_system, ((surf_resource_lmm_t)
340 static int net_action_unref(surf_action_t action)
343 if (!action->refcount) {
344 xbt_swag_remove(action, action->state_set);
345 if (((surf_action_lmm_t)action)->variable) {
346 lmm_variable_free(network_maxmin_system,
347 ((surf_action_lmm_t) action)->variable);
349 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
350 heap_remove((surf_action_network_CM02_t) action);
351 xbt_swag_remove(action, net_modified_set);
353 surf_action_free(&action);
361 static void net_action_cancel(surf_action_t action)
363 XBT_DEBUG("cancel action %p", action);
364 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
365 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
366 xbt_swag_remove(action, net_modified_set);
367 heap_remove((surf_action_network_CM02_t) action);
371 void net_action_recycle(surf_action_t action)
376 #ifdef HAVE_LATENCY_BOUND_TRACKING
377 int net_get_link_latency_limited(surf_action_t action)
379 return action->latency_limited;
383 double net_action_get_remains(surf_action_t action)
385 if (network_update_mechanism == UM_LAZY) /* update remains before return it */
386 update_action_remaining_lazy(surf_get_clock());
387 return action->remains;
390 static void update_action_remaining_lazy(double now)
392 surf_action_network_CM02_t action = NULL;
395 xbt_swag_foreach(action, net_modified_set) {
397 if (GENERIC_LMM_ACTION(action).suspended != 0) {
401 delta = now - GENERIC_LMM_ACTION(action).last_update;
403 double_update(&(((surf_action_t)action)->remains),
404 lmm_variable_getvalue(((surf_action_lmm_t) action)->variable) * delta);
406 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
407 double_update(&(((surf_action_t)action)->max_duration), delta);
409 if ((((surf_action_t)action)->remains <= 0) &&
410 (lmm_get_variable_weight(((surf_action_lmm_t)action)->variable) > 0)) {
411 ((surf_action_t)action)->finish = surf_get_clock();
412 surf_network_model->action_state_set((surf_action_t) action,
415 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
416 && (((surf_action_t)action)->max_duration <= 0))) {
417 ((surf_action_t)action)->finish = surf_get_clock();
418 surf_network_model->action_state_set((surf_action_t) action,
423 GENERIC_LMM_ACTION(action).last_update = now;
427 static double net_share_resources_full(double now)
429 s_surf_action_lmm_t s_action;
430 surf_action_network_CM02_t action = NULL;
431 xbt_swag_t running_actions =
432 surf_network_model->states.running_action_set;
435 min = generic_maxmin_share_resources(running_actions,
436 xbt_swag_offset(s_action,
438 network_maxmin_system,
441 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable) )))
443 xbt_swag_foreach(action, running_actions) {
444 #ifdef HAVE_LATENCY_BOUND_TRACKING
445 if (lmm_is_variable_limited_by_latency(GENERIC_LMM_ACTION(action).variable)) {
446 action->latency_limited = 1;
448 action->latency_limited = 0;
451 if (action->latency > 0) {
452 min = (min < 0) ? action->latency : min(min, action->latency);
456 XBT_DEBUG("Min of share resources %f", min);
461 static double net_share_resources_lazy(double now)
463 surf_action_network_CM02_t action = NULL;
468 ("Before share resources, the size of modified actions set is %d",
469 xbt_swag_size(net_modified_set));
470 update_action_remaining_lazy(now);
472 lmm_solve(network_maxmin_system);
475 ("After share resources, The size of modified actions set is %d",
476 xbt_swag_size(net_modified_set));
478 xbt_swag_foreach(action, net_modified_set) {
479 int max_dur_flag = 0;
481 if (GENERIC_ACTION(action).state_set !=
482 surf_network_model->states.running_action_set)
485 /* bogus priority, skip it */
486 if (GENERIC_ACTION(action).priority <= 0)
490 value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
492 if (GENERIC_ACTION(action).remains > 0) {
493 value = GENERIC_ACTION(action).remains / value;
501 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
503 || GENERIC_ACTION(action).start +
504 GENERIC_ACTION(action).max_duration < min)) {
505 min = GENERIC_ACTION(action).start +
506 GENERIC_ACTION(action).max_duration;
510 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
511 GENERIC_ACTION(action).start, now + value,
512 GENERIC_ACTION(action).max_duration);
516 heap_insert(action, min, max_dur_flag ? MAX_DURATION : NORMAL);
517 XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min,
519 } else DIE_IMPOSSIBLE;
522 //hereafter must have already the min value for this resource model
523 if (xbt_heap_size(net_action_heap) > 0)
524 min = xbt_heap_maxkey(net_action_heap) - now;
528 XBT_DEBUG("The minimum with the HEAP %lf", min);
533 static void net_update_actions_state_full(double now, double delta)
536 surf_action_network_CM02_t action = NULL;
537 surf_action_network_CM02_t next_action = NULL;
538 xbt_swag_t running_actions =
539 surf_network_model->states.running_action_set;
541 xbt_swag_t failed_actions =
542 surf_network_model->states.failed_action_set;
545 xbt_swag_foreach_safe(action, next_action, running_actions) {
547 if (action->latency > 0) {
548 if (action->latency > deltap) {
549 double_update(&(action->latency), deltap);
552 double_update(&(deltap), action->latency);
553 action->latency = 0.0;
555 if ((action->latency == 0.0) && !(GENERIC_LMM_ACTION(action).suspended))
556 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
560 if (TRACE_is_enabled()) {
561 int n = lmm_get_number_of_cnst_from_var(network_maxmin_system, GENERIC_LMM_ACTION(action).variable);
563 for (i = 0; i < n; i++){
564 lmm_constraint_t constraint = lmm_get_cnst_from_var(network_maxmin_system,
565 GENERIC_LMM_ACTION(action).variable,
567 link_CM02_t link = lmm_constraint_id(constraint);
568 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
569 ((surf_action_t)action)->category,
570 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
576 if (!lmm_get_number_of_cnst_from_var
577 (network_maxmin_system, GENERIC_LMM_ACTION(action).variable)) {
578 /* There is actually no link used, hence an infinite bandwidth.
579 * This happens often when using models like vivaldi.
580 * In such case, just make sure that the action completes immediately.
582 double_update(&(GENERIC_ACTION(action).remains),
583 GENERIC_ACTION(action).remains);
585 double_update(&(GENERIC_ACTION(action).remains),
586 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * deltap);
587 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
588 double_update(&(((surf_action_t)action)->max_duration), delta);
590 if ((GENERIC_ACTION(action).remains <= 0) &&
591 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
592 ((surf_action_t)action)->finish = surf_get_clock();
593 surf_network_model->action_state_set((surf_action_t) action,
598 } else if ((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
599 && (((surf_action_t)action)->max_duration <= 0)) {
600 ((surf_action_t)action)->finish = surf_get_clock();
601 surf_network_model->action_state_set((surf_action_t) action,
611 static void net_update_actions_state_lazy(double now, double delta)
613 surf_action_network_CM02_t action = NULL;
615 while ((xbt_heap_size(net_action_heap) > 0)
616 && (double_equals(xbt_heap_maxkey(net_action_heap), now))) {
617 action = xbt_heap_pop(net_action_heap);
618 XBT_DEBUG("Action %p: finish", action);
619 GENERIC_ACTION(action).finish = surf_get_clock();
621 if (TRACE_is_enabled()) {
622 int n = lmm_get_number_of_cnst_from_var(network_maxmin_system, GENERIC_LMM_ACTION(action).variable);
624 for (i = 0; i < n; i++){
625 lmm_constraint_t constraint = lmm_get_cnst_from_var(network_maxmin_system,
626 GENERIC_LMM_ACTION(action).variable,
628 link_CM02_t link = lmm_constraint_id(constraint);
629 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
630 ((surf_action_t)action)->category,
631 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
632 GENERIC_LMM_ACTION(action).last_update,
633 now - GENERIC_LMM_ACTION(action).last_update);
638 // if I am wearing a latency hat
639 if (GENERIC_LMM_ACTION(action).hat == LATENCY) {
640 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
643 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
645 // if I am wearing a max_duration or normal hat
646 } else if (GENERIC_LMM_ACTION(action).hat == MAX_DURATION ||
647 GENERIC_LMM_ACTION(action).hat == NORMAL) {
648 // no need to communicate anymore
649 // assume that flows that reached max_duration have remaining of 0
650 GENERIC_ACTION(action).remains = 0;
651 ((surf_action_t)action)->finish = surf_get_clock();
652 surf_network_model->action_state_set((surf_action_t) action,
660 static void net_update_resource_state(void *id,
661 tmgr_trace_event_t event_type,
662 double value, double date)
664 link_CM02_t nw_link = id;
665 /* printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
666 /* "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
667 /* value, event_type); */
669 if (event_type == nw_link->lmm_resource.power.event) {
671 sg_weight_S_parameter / value - sg_weight_S_parameter /
672 (nw_link->lmm_resource.power.peak *
673 nw_link->lmm_resource.power.scale);
674 lmm_variable_t var = NULL;
675 lmm_element_t elem = NULL;
676 surf_action_network_CM02_t action = NULL;
678 nw_link->lmm_resource.power.peak = value;
679 lmm_update_constraint_bound(network_maxmin_system,
680 nw_link->lmm_resource.constraint,
681 sg_bandwidth_factor *
682 (nw_link->lmm_resource.power.peak *
683 nw_link->lmm_resource.power.scale));
685 TRACE_surf_link_set_bandwidth(date,
687 *) (((nw_link->lmm_resource).
688 generic_resource).name),
689 sg_bandwidth_factor *
690 (nw_link->lmm_resource.power.peak *
691 nw_link->lmm_resource.power.scale));
693 if (sg_weight_S_parameter > 0) {
694 while ((var = lmm_get_var_from_cnst
695 (network_maxmin_system, nw_link->lmm_resource.constraint,
697 action = lmm_variable_id(var);
698 action->weight += delta;
699 if (!(GENERIC_LMM_ACTION(action).suspended))
700 lmm_update_variable_weight(network_maxmin_system,
701 GENERIC_LMM_ACTION(action).variable, action->weight);
704 if (tmgr_trace_event_free(event_type))
705 nw_link->lmm_resource.power.event = NULL;
706 } else if (event_type == nw_link->lat_event) {
707 double delta = value - nw_link->lat_current;
708 lmm_variable_t var = NULL;
709 lmm_element_t elem = NULL;
710 surf_action_network_CM02_t action = NULL;
712 nw_link->lat_current = value;
713 while ((var = lmm_get_var_from_cnst
714 (network_maxmin_system, nw_link->lmm_resource.constraint,
716 action = lmm_variable_id(var);
717 action->lat_current += delta;
718 action->weight += delta;
719 if (action->rate < 0)
720 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
721 sg_tcp_gamma / (2.0 *
722 action->lat_current));
724 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
726 sg_tcp_gamma / (2.0 *
730 if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
731 XBT_INFO("Flow is limited BYBANDWIDTH");
733 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
734 action->lat_current);
737 if (!(GENERIC_LMM_ACTION(action).suspended))
738 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
742 if (tmgr_trace_event_free(event_type))
743 nw_link->lat_event = NULL;
744 } else if (event_type == nw_link->lmm_resource.state_event) {
746 nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
748 lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
749 lmm_variable_t var = NULL;
750 lmm_element_t elem = NULL;
752 nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
753 while ((var = lmm_get_var_from_cnst
754 (network_maxmin_system, cnst, &elem))) {
755 surf_action_t action = lmm_variable_id(var);
757 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
758 surf_action_state_get(action) == SURF_ACTION_READY) {
759 action->finish = date;
760 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
764 if (tmgr_trace_event_free(event_type))
765 nw_link->lmm_resource.state_event = NULL;
767 XBT_CRITICAL("Unknown event ! \n");
772 ("There were a resource state event, need to update actions related to the constraint (%p)",
773 nw_link->lmm_resource.constraint);
778 static surf_action_t net_communicate(const char *src_name,
779 const char *dst_name, double size,
785 surf_action_network_CM02_t action = NULL;
786 double bandwidth_bound;
787 double latency = 0.0;
788 xbt_dynar_t back_route = NULL;
789 int constraints_per_variable = 0;
791 xbt_dynar_t route = xbt_dynar_new(global_routing->size_of_link, NULL);
793 XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
795 routing_get_route_and_latency(src_name, dst_name, &route, &latency);
796 xbt_assert(!xbt_dynar_is_empty(route) || latency,
797 "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
800 xbt_dynar_foreach(route, i, link) {
801 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
806 if (sg_network_crosstraffic == 1) {
807 routing_get_route_and_latency(dst_name, src_name, &back_route, NULL);
808 xbt_dynar_foreach(back_route, i, link) {
809 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
817 surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
818 surf_network_model, failed);
819 #ifdef HAVE_LATENCY_BOUND_TRACKING
820 action->latency_limited = 0;
822 action->weight = action->latency = latency;
824 xbt_swag_insert(action, ((surf_action_t)action)->state_set);
826 if (network_update_mechanism == UM_LAZY) {
827 GENERIC_LMM_ACTION(action).index_heap = -1;
828 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
831 bandwidth_bound = -1.0;
832 if (sg_weight_S_parameter > 0) {
833 xbt_dynar_foreach(route, i, link) {
835 sg_weight_S_parameter /
836 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
839 xbt_dynar_foreach(route, i, link) {
840 double bb = bandwidth_factor_callback(size) *
841 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
843 (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
846 action->lat_current = action->latency;
847 action->latency *= latency_factor_callback(size);
849 bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
851 xbt_assert(!xbt_dynar_is_empty(route),
852 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
854 link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
855 gap_append(size, link, action);
856 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
857 action, src_name, dst_name, action->sender.gap,
861 constraints_per_variable = xbt_dynar_length(route);
862 if (back_route != NULL)
863 constraints_per_variable += xbt_dynar_length(back_route);
865 if (action->latency > 0) {
866 GENERIC_LMM_ACTION(action).variable =
867 lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
868 constraints_per_variable);
869 if (network_update_mechanism == UM_LAZY) {
870 // add to the heap the event when the latency is payed
871 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
872 action->latency + GENERIC_LMM_ACTION(action).last_update);
873 heap_insert(action, action->latency + GENERIC_LMM_ACTION(action).last_update,
874 xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
877 GENERIC_LMM_ACTION(action).variable =
878 lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
879 constraints_per_variable);
881 if (action->rate < 0) {
882 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
883 (action->lat_current > 0) ?
884 sg_tcp_gamma / (2.0 *
885 action->lat_current) : -1.0);
887 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
888 (action->lat_current > 0) ?
890 sg_tcp_gamma / (2.0 *
891 action->lat_current))
895 xbt_dynar_foreach(route, i, link) {
896 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
897 GENERIC_LMM_ACTION(action).variable, 1.0);
900 if (sg_network_crosstraffic == 1) {
901 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
902 xbt_dynar_foreach(back_route, i, link) {
903 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
904 GENERIC_LMM_ACTION(action).variable, .05);
908 xbt_dynar_free(&route);
911 return (surf_action_t) action;
914 static xbt_dynar_t net_get_route(const char *src, const char *dst)
916 xbt_dynar_t route = NULL;
917 routing_get_route_and_latency(src, dst, &route, NULL);
921 static double net_get_link_bandwidth(const void *link)
923 surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
924 return lmm->power.peak * lmm->power.scale;
927 static double net_get_link_latency(const void *link)
929 return ((link_CM02_t) link)->lat_current;
932 static int net_link_shared(const void *link)
935 lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
938 static void net_action_suspend(surf_action_t action)
940 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 1;
941 lmm_update_variable_weight(network_maxmin_system,
942 ((surf_action_network_CM02_t)
943 action)->generic_lmm_action.variable, 0.0);
945 if (network_update_mechanism == UM_LAZY) // remove action from the heap
946 heap_remove((surf_action_network_CM02_t) action);
949 static void net_action_resume(surf_action_t action)
951 if (((surf_action_network_CM02_t) action)->generic_lmm_action.suspended) {
952 lmm_update_variable_weight(network_maxmin_system,
953 ((surf_action_network_CM02_t)
954 action)->generic_lmm_action.variable,
955 ((surf_action_network_CM02_t)
957 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 0;
958 if (network_update_mechanism == UM_LAZY) // remove action from the heap
959 heap_remove((surf_action_network_CM02_t) action);
963 static int net_action_is_suspended(surf_action_t action)
965 return ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended;
968 void net_action_set_max_duration(surf_action_t action, double duration)
970 action->max_duration = duration;
971 if (network_update_mechanism == UM_LAZY) // remove action from the heap
972 heap_remove((surf_action_network_CM02_t) action);
976 static void net_action_set_category(surf_action_t action,
977 const char *category)
979 action->category = xbt_strdup(category);
983 static void net_finalize(void)
985 surf_model_exit(surf_network_model);
986 surf_network_model = NULL;
988 lmm_system_free(network_maxmin_system);
989 network_maxmin_system = NULL;
991 if (network_update_mechanism == UM_LAZY) {
992 xbt_heap_free(net_action_heap);
993 xbt_swag_free(net_modified_set);
997 xbt_dynar_free(&smpi_bw_factor);
999 xbt_dynar_free(&smpi_lat_factor);
1002 static void smpi_gap_append(double size, const link_CM02_t link,
1003 surf_action_network_CM02_t action)
1005 const char *src = link->lmm_resource.generic_resource.name;
1007 surf_action_network_CM02_t last_action;
1010 if (sg_sender_gap > 0.0) {
1012 gap_lookup = xbt_dict_new();
1014 fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
1015 action->sender.gap = 0.0;
1016 if (fifo && xbt_fifo_size(fifo) > 0) {
1017 /* Compute gap from last send */
1019 (surf_action_network_CM02_t)
1020 xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
1021 bw = net_get_link_bandwidth(link);
1022 action->sender.gap =
1023 last_action->sender.gap + max(sg_sender_gap,
1024 last_action->sender.size / bw);
1025 action->latency += action->sender.gap;
1027 /* Append action as last send */
1028 action->sender.link_name = link->lmm_resource.generic_resource.name;
1030 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1031 action->sender.link_name);
1033 fifo = xbt_fifo_new();
1034 xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1036 action->sender.fifo_item = xbt_fifo_push(fifo, action);
1037 action->sender.size = size;
1041 static void smpi_gap_remove(surf_action_network_CM02_t action)
1046 if (sg_sender_gap > 0.0 && action->sender.link_name
1047 && action->sender.fifo_item) {
1049 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1050 action->sender.link_name);
1051 xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1052 size = xbt_fifo_size(fifo);
1054 xbt_fifo_free(fifo);
1055 xbt_dict_remove(gap_lookup, action->sender.link_name);
1056 size = xbt_dict_length(gap_lookup);
1058 xbt_dict_free(&gap_lookup);
1064 static void surf_network_model_init_internal(void)
1066 s_surf_action_network_CM02_t comm;
1067 surf_network_model = surf_model_init();
1069 surf_network_model->name = "network";
1070 surf_network_model->action_unref = net_action_unref;
1071 surf_network_model->action_cancel = net_action_cancel;
1072 surf_network_model->action_recycle = net_action_recycle;
1073 surf_network_model->get_remains = net_action_get_remains;
1074 #ifdef HAVE_LATENCY_BOUND_TRACKING
1075 surf_network_model->get_latency_limited = net_get_link_latency_limited;
1078 surf_network_model->set_category = net_action_set_category;
1081 surf_network_model->model_private->resource_used = net_resource_used;
1082 if (network_update_mechanism == UM_LAZY) {
1083 surf_network_model->model_private->share_resources =
1084 net_share_resources_lazy;
1085 surf_network_model->model_private->update_actions_state =
1086 net_update_actions_state_lazy;
1087 } else if (network_update_mechanism == UM_FULL) {
1088 surf_network_model->model_private->share_resources =
1089 net_share_resources_full;
1090 surf_network_model->model_private->update_actions_state =
1091 net_update_actions_state_full;
1094 surf_network_model->model_private->update_resource_state =
1095 net_update_resource_state;
1096 surf_network_model->model_private->finalize = net_finalize;
1098 surf_network_model->suspend = net_action_suspend;
1099 surf_network_model->resume = net_action_resume;
1100 surf_network_model->is_suspended = net_action_is_suspended;
1101 surf_cpu_model->set_max_duration = net_action_set_max_duration;
1103 surf_network_model->extension.network.communicate = net_communicate;
1104 surf_network_model->extension.network.get_route = net_get_route;
1105 surf_network_model->extension.network.get_link_bandwidth =
1106 net_get_link_bandwidth;
1107 surf_network_model->extension.network.get_link_latency =
1108 net_get_link_latency;
1109 surf_network_model->extension.network.link_shared = net_link_shared;
1110 surf_network_model->extension.network.add_traces = net_add_traces;
1111 surf_network_model->extension.network.create_resource =
1112 net_create_resource;
1114 if (!network_maxmin_system)
1115 network_maxmin_system = lmm_system_new(net_selective_update);
1117 routing_model_create(sizeof(link_CM02_t),
1118 net_create_resource("__loopback__",
1119 498000000, NULL, 0.000015, NULL,
1120 SURF_RESOURCE_ON, NULL,
1121 SURF_LINK_FATPIPE, NULL));
1123 if (network_update_mechanism == UM_LAZY) {
1124 net_action_heap = xbt_heap_new(8, NULL);
1125 xbt_heap_set_update_callback(net_action_heap,
1126 net_action_update_index_heap);
1128 xbt_swag_new(xbt_swag_offset(comm, generic_lmm_action.action_list_hookup));
1129 network_maxmin_system->keep_track = net_modified_set;
1133 static void set_update_mechanism(void)
1135 char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1137 xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
1139 if (!strcmp(optim, "Full")) {
1140 network_update_mechanism = UM_FULL;
1141 net_selective_update = select;
1142 } else if (!strcmp(optim, "Lazy")) {
1143 network_update_mechanism = UM_LAZY;
1144 net_selective_update = 1;
1145 xbt_assert((select == 1)
1147 (xbt_cfg_is_default_value
1148 (_surf_cfg_set, "network/maxmin_selective_update")),
1149 "Disabling selective update while using the lazy update mechanism is dumb!");
1151 xbt_die("Unsupported optimization (%s) for this model", optim);
1155 /************************************************************************/
1156 /* New model based on LV08 and experimental results of MPI ping-pongs */
1157 /************************************************************************/
1158 /* @Inproceedings{smpi_ipdps, */
1159 /* author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
1160 /* title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
1161 /* booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
1162 /* address={Anchorage (Alaska) USA}, */
1166 void surf_network_model_init_SMPI(void)
1169 if (surf_network_model)
1171 set_update_mechanism();
1173 surf_network_model_init_internal();
1174 latency_factor_callback = &smpi_latency_factor;
1175 bandwidth_factor_callback = &smpi_bandwidth_factor;
1176 bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1177 gap_append = &smpi_gap_append;
1178 gap_remove = &smpi_gap_remove;
1179 net_define_callbacks();
1180 xbt_dynar_push(model_list, &surf_network_model);
1181 network_solve = lmm_solve;
1183 xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1184 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1187 /************************************************************************/
1188 /* New model based on optimizations discussed during Pedro Velho's thesis*/
1189 /************************************************************************/
1190 /* @techreport{VELHO:2011:HAL-00646896:1, */
1191 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
1192 /* title = {{Flow-level network models: have we reached the limits?}}, */
1193 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
1194 /* type = {Rapport de recherche}, */
1195 /* institution = {INRIA}, */
1196 /* number = {RR-7821}, */
1197 /* year = {2011}, */
1199 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
1201 void surf_network_model_init_LegrandVelho(void)
1203 if (surf_network_model)
1206 set_update_mechanism();
1208 surf_network_model_init_internal();
1209 net_define_callbacks();
1210 xbt_dynar_push(model_list, &surf_network_model);
1211 network_solve = lmm_solve;
1213 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor",
1215 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1217 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 20537);
1220 /***************************************************************************/
1221 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1222 /***************************************************************************/
1223 /* @TechReport{ rr-lip2002-40, */
1224 /* author = {Henri Casanova and Loris Marchal}, */
1225 /* institution = {LIP}, */
1226 /* title = {A Network Model for Simulation of Grid Application}, */
1227 /* number = {2002-40}, */
1228 /* month = {oct}, */
1231 void surf_network_model_init_CM02(void)
1234 if (surf_network_model)
1237 set_update_mechanism();
1238 surf_network_model_init_internal();
1239 net_define_callbacks();
1240 xbt_dynar_push(model_list, &surf_network_model);
1241 network_solve = lmm_solve;
1243 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1244 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1246 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1249 /***************************************************************************/
1250 /* The models from Steven H. Low */
1251 /***************************************************************************/
1252 /* @article{Low03, */
1253 /* author={Steven H. Low}, */
1254 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
1256 /* journal={{IEEE/ACM} Transactions on Networking}, */
1257 /* volume={11}, number={4}, */
1259 void surf_network_model_init_Reno(void)
1261 if (surf_network_model)
1264 set_update_mechanism();
1265 surf_network_model_init_internal();
1266 net_define_callbacks();
1268 xbt_dynar_push(model_list, &surf_network_model);
1269 lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1271 network_solve = lagrange_solve;
1273 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1274 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1276 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1280 void surf_network_model_init_Reno2(void)
1282 if (surf_network_model)
1285 set_update_mechanism();
1286 surf_network_model_init_internal();
1287 net_define_callbacks();
1289 xbt_dynar_push(model_list, &surf_network_model);
1290 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1292 network_solve = lagrange_solve;
1294 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1295 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1297 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1301 void surf_network_model_init_Vegas(void)
1303 if (surf_network_model)
1306 set_update_mechanism();
1307 surf_network_model_init_internal();
1308 net_define_callbacks();
1310 xbt_dynar_push(model_list, &surf_network_model);
1311 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1313 network_solve = lagrange_solve;
1315 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1316 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1318 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);