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 {
49 double sg_sender_gap = 0.0;
50 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
51 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
52 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
54 double sg_tcp_gamma = 0.0;
55 int sg_network_crosstraffic = 0;
57 xbt_dict_t gap_lookup = NULL;
59 e_UM_t network_update_mechanism = UM_UNDEFINED;
60 static int net_selective_update = 0;
62 static int net_action_is_suspended(surf_action_t action);
63 static void net_update_action_remaining_lazy(double now);
65 static xbt_swag_t net_modified_set = NULL;
66 static xbt_heap_t net_action_heap = NULL;
69 /******************************************************************************/
70 /* Factors callbacks */
71 /******************************************************************************/
72 static double constant_latency_factor(double size)
74 return sg_latency_factor;
77 static double constant_bandwidth_factor(double size)
79 return sg_bandwidth_factor;
82 static double constant_bandwidth_constraint(double rate, double bound,
88 /**********************/
90 /**********************/
91 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
94 unsigned int iter = 0;
96 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
98 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
99 radical_elements = xbt_str_split(smpi_coef_string, ";");
100 xbt_dynar_foreach(radical_elements, iter, value) {
102 radical_elements2 = xbt_str_split(value, ":");
103 if (xbt_dynar_length(radical_elements2) != 2)
104 xbt_die("Malformed radical for smpi factor!");
105 fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
106 fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
107 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
108 XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
109 xbt_dynar_free(&radical_elements2);
111 xbt_dynar_free(&radical_elements);
115 static double smpi_bandwidth_factor(double size)
119 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/bw_factor"));
121 unsigned int iter = 0;
122 s_smpi_factor_t fact;
123 xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
124 if (size >= fact.factor) {
125 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
133 static double smpi_latency_factor(double size)
135 if (!smpi_lat_factor)
137 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/lat_factor"));
139 unsigned int iter = 0;
140 s_smpi_factor_t fact;
141 xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
142 if (size >= fact.factor) {
143 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
151 /**--------- <copy/paste C code snippet in surf/network.c> -----------*/
153 static double smpi_bandwidth_constraint(double rate, double bound,
156 return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
159 static double (*latency_factor_callback) (double) =
160 &constant_latency_factor;
161 static double (*bandwidth_factor_callback) (double) =
162 &constant_bandwidth_factor;
163 static double (*bandwidth_constraint_callback) (double, double, double) =
164 &constant_bandwidth_constraint;
166 static void (*gap_append) (double, const link_CM02_t,
167 surf_action_network_CM02_t) = NULL;
168 static void (*gap_remove) (surf_action_network_CM02_t) = NULL;
170 static void *net_create_resource(const char *name,
172 tmgr_trace_t bw_trace,
174 tmgr_trace_t lat_trace,
175 e_surf_resource_state_t
177 tmgr_trace_t state_trace,
178 e_surf_link_sharing_policy_t
179 policy, xbt_dict_t properties)
181 link_CM02_t nw_link = (link_CM02_t)
182 surf_resource_lmm_new(sizeof(s_link_CM02_t),
183 surf_network_model, name, properties,
184 network_maxmin_system,
185 sg_bandwidth_factor * bw_initial,
187 state_initial, state_trace,
188 bw_initial, bw_trace);
190 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
191 "Link '%s' declared several times in the platform file.",
194 nw_link->lat_current = lat_initial;
197 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
199 if (policy == SURF_LINK_FATPIPE)
200 lmm_constraint_shared(nw_link->lmm_resource.constraint);
202 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
207 static void net_parse_link_init(sg_platf_link_cbarg_t link)
209 if (link->policy == SURF_LINK_FULLDUPLEX) {
211 link_id = bprintf("%s_UP", link->id);
212 net_create_resource(link_id,
214 link->bandwidth_trace,
218 link->state_trace, link->policy, link->properties);
220 link_id = bprintf("%s_DOWN", link->id);
221 net_create_resource(link_id,
223 link->bandwidth_trace,
227 link->state_trace, link->policy, link->properties);
230 net_create_resource(link->id,
232 link->bandwidth_trace,
236 link->state_trace, link->policy, link->properties);
240 static void net_add_traces(void)
242 xbt_dict_cursor_t cursor = NULL;
243 char *trace_name, *elm;
245 static int called = 0;
250 /* connect all traces relative to network */
251 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
252 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
253 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
255 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
258 "Cannot connect trace %s to link %s: trace undefined",
261 link->lmm_resource.state_event =
262 tmgr_history_add_trace(history, trace, 0.0, 0, link);
265 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
266 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
267 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
269 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
272 "Cannot connect trace %s to link %s: trace undefined",
275 link->lmm_resource.power.event =
276 tmgr_history_add_trace(history, trace, 0.0, 0, link);
279 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
280 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
281 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
283 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
286 "Cannot connect trace %s to link %s: trace undefined",
289 link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
293 static void net_define_callbacks(void)
295 /* Figuring out the network links */
296 sg_platf_link_add_cb(net_parse_link_init);
297 sg_platf_postparse_add_cb(net_add_traces);
300 static int net_resource_used(void *resource_id)
302 return lmm_constraint_used(network_maxmin_system, ((surf_resource_lmm_t)
307 static int net_action_unref(surf_action_t action)
310 if (!action->refcount) {
311 xbt_swag_remove(action, action->state_set);
312 if (((surf_action_lmm_t)action)->variable) {
313 lmm_variable_free(network_maxmin_system,
314 ((surf_action_lmm_t) action)->variable);
316 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
317 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t) action);
318 xbt_swag_remove(action, net_modified_set);
320 surf_action_free(&action);
328 static void net_action_cancel(surf_action_t action)
330 XBT_DEBUG("cancel action %p", action);
331 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
332 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
333 xbt_swag_remove(action, net_modified_set);
334 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t) action);
338 void net_action_recycle(surf_action_t action)
343 #ifdef HAVE_LATENCY_BOUND_TRACKING
344 int net_get_link_latency_limited(surf_action_t action)
346 return action->latency_limited;
350 double net_action_get_remains(surf_action_t action)
352 if (network_update_mechanism == UM_LAZY) /* update remains before return it */
353 net_update_action_remaining_lazy(surf_get_clock());
354 return action->remains;
357 static void net_update_action_remaining_lazy(double now)
359 surf_action_lmm_t action = NULL;
362 xbt_swag_foreach(action, net_modified_set) {
364 if (action->suspended != 0) {
368 delta = now - action->last_update;
370 double_update(&(((surf_action_t)action)->remains),
371 lmm_variable_getvalue(action->variable) * delta);
373 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
374 double_update(&(((surf_action_t)action)->max_duration), delta);
376 if ((((surf_action_t)action)->remains <= 0) &&
377 (lmm_get_variable_weight(action->variable) > 0)) {
378 ((surf_action_t)action)->finish = surf_get_clock();
379 surf_network_model->action_state_set((surf_action_t) action,
381 surf_action_lmm_heap_remove(net_action_heap,action);
382 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
383 && (((surf_action_t)action)->max_duration <= 0))) {
384 ((surf_action_t)action)->finish = surf_get_clock();
385 surf_network_model->action_state_set((surf_action_t) action,
387 surf_action_lmm_heap_remove(net_action_heap,action);
390 action->last_update = now;
394 static double net_share_resources_full(double now)
396 s_surf_action_lmm_t s_action;
397 surf_action_network_CM02_t action = NULL;
398 xbt_swag_t running_actions =
399 surf_network_model->states.running_action_set;
402 min = generic_maxmin_share_resources(running_actions,
403 xbt_swag_offset(s_action,
405 network_maxmin_system,
408 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable) )))
410 xbt_swag_foreach(action, running_actions) {
411 #ifdef HAVE_LATENCY_BOUND_TRACKING
412 if (lmm_is_variable_limited_by_latency(GENERIC_LMM_ACTION(action).variable)) {
413 action->latency_limited = 1;
415 action->latency_limited = 0;
418 if (action->latency > 0) {
419 min = (min < 0) ? action->latency : min(min, action->latency);
423 XBT_DEBUG("Min of share resources %f", min);
428 static double net_share_resources_lazy(double now)
430 surf_action_network_CM02_t action = NULL;
435 ("Before share resources, the size of modified actions set is %d",
436 xbt_swag_size(net_modified_set));
437 net_update_action_remaining_lazy(now);
439 lmm_solve(network_maxmin_system);
442 ("After share resources, The size of modified actions set is %d",
443 xbt_swag_size(net_modified_set));
445 xbt_swag_foreach(action, net_modified_set) {
446 int max_dur_flag = 0;
448 if (GENERIC_ACTION(action).state_set !=
449 surf_network_model->states.running_action_set)
452 /* bogus priority, skip it */
453 if (GENERIC_ACTION(action).priority <= 0)
457 value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
459 if (GENERIC_ACTION(action).remains > 0) {
460 value = GENERIC_ACTION(action).remains / value;
468 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
470 || GENERIC_ACTION(action).start +
471 GENERIC_ACTION(action).max_duration < min)) {
472 min = GENERIC_ACTION(action).start +
473 GENERIC_ACTION(action).max_duration;
477 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
478 GENERIC_ACTION(action).start, now + value,
479 GENERIC_ACTION(action).max_duration);
482 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
483 surf_action_lmm_heap_insert(net_action_heap,(surf_action_lmm_t)action, min, max_dur_flag ? MAX_DURATION : NORMAL);
484 XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min,
486 } else DIE_IMPOSSIBLE;
489 //hereafter must have already the min value for this resource model
490 if (xbt_heap_size(net_action_heap) > 0)
491 min = xbt_heap_maxkey(net_action_heap) - now;
495 XBT_DEBUG("The minimum with the HEAP %lf", min);
500 static void net_update_actions_state_full(double now, double delta)
503 surf_action_network_CM02_t action = NULL;
504 surf_action_network_CM02_t next_action = NULL;
505 xbt_swag_t running_actions =
506 surf_network_model->states.running_action_set;
508 xbt_swag_t failed_actions =
509 surf_network_model->states.failed_action_set;
512 xbt_swag_foreach_safe(action, next_action, running_actions) {
514 if (action->latency > 0) {
515 if (action->latency > deltap) {
516 double_update(&(action->latency), deltap);
519 double_update(&(deltap), action->latency);
520 action->latency = 0.0;
522 if ((action->latency == 0.0) && !(GENERIC_LMM_ACTION(action).suspended))
523 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
527 if (TRACE_is_enabled()) {
528 int n = lmm_get_number_of_cnst_from_var(network_maxmin_system, GENERIC_LMM_ACTION(action).variable);
530 for (i = 0; i < n; i++){
531 lmm_constraint_t constraint = lmm_get_cnst_from_var(network_maxmin_system,
532 GENERIC_LMM_ACTION(action).variable,
534 link_CM02_t link = lmm_constraint_id(constraint);
535 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
536 ((surf_action_t)action)->category,
537 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
543 if (!lmm_get_number_of_cnst_from_var
544 (network_maxmin_system, GENERIC_LMM_ACTION(action).variable)) {
545 /* There is actually no link used, hence an infinite bandwidth.
546 * This happens often when using models like vivaldi.
547 * In such case, just make sure that the action completes immediately.
549 double_update(&(GENERIC_ACTION(action).remains),
550 GENERIC_ACTION(action).remains);
552 double_update(&(GENERIC_ACTION(action).remains),
553 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * deltap);
554 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
555 double_update(&(((surf_action_t)action)->max_duration), delta);
557 if ((GENERIC_ACTION(action).remains <= 0) &&
558 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
559 ((surf_action_t)action)->finish = surf_get_clock();
560 surf_network_model->action_state_set((surf_action_t) action,
565 } else if ((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
566 && (((surf_action_t)action)->max_duration <= 0)) {
567 ((surf_action_t)action)->finish = surf_get_clock();
568 surf_network_model->action_state_set((surf_action_t) action,
578 static void net_update_actions_state_lazy(double now, double delta)
580 surf_action_network_CM02_t action = NULL;
582 while ((xbt_heap_size(net_action_heap) > 0)
583 && (double_equals(xbt_heap_maxkey(net_action_heap), now))) {
584 action = xbt_heap_pop(net_action_heap);
585 XBT_DEBUG("Action %p: finish", action);
586 GENERIC_ACTION(action).finish = surf_get_clock();
588 if (TRACE_is_enabled()) {
589 int n = lmm_get_number_of_cnst_from_var(network_maxmin_system, GENERIC_LMM_ACTION(action).variable);
591 for (i = 0; i < n; i++){
592 lmm_constraint_t constraint = lmm_get_cnst_from_var(network_maxmin_system,
593 GENERIC_LMM_ACTION(action).variable,
595 link_CM02_t link = lmm_constraint_id(constraint);
596 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
597 ((surf_action_t)action)->category,
598 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
599 GENERIC_LMM_ACTION(action).last_update,
600 now - GENERIC_LMM_ACTION(action).last_update);
605 // if I am wearing a latency hat
606 if (GENERIC_LMM_ACTION(action).hat == LATENCY) {
607 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
609 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
610 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
612 // if I am wearing a max_duration or normal hat
613 } else if (GENERIC_LMM_ACTION(action).hat == MAX_DURATION ||
614 GENERIC_LMM_ACTION(action).hat == NORMAL) {
615 // no need to communicate anymore
616 // assume that flows that reached max_duration have remaining of 0
617 GENERIC_ACTION(action).remains = 0;
618 ((surf_action_t)action)->finish = surf_get_clock();
619 surf_network_model->action_state_set((surf_action_t) action,
621 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
627 static void net_update_resource_state(void *id,
628 tmgr_trace_event_t event_type,
629 double value, double date)
631 link_CM02_t nw_link = id;
632 /* printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
633 /* "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
634 /* value, event_type); */
636 if (event_type == nw_link->lmm_resource.power.event) {
638 sg_weight_S_parameter / value - sg_weight_S_parameter /
639 (nw_link->lmm_resource.power.peak *
640 nw_link->lmm_resource.power.scale);
641 lmm_variable_t var = NULL;
642 lmm_element_t elem = NULL;
643 surf_action_network_CM02_t action = NULL;
645 nw_link->lmm_resource.power.peak = value;
646 lmm_update_constraint_bound(network_maxmin_system,
647 nw_link->lmm_resource.constraint,
648 sg_bandwidth_factor *
649 (nw_link->lmm_resource.power.peak *
650 nw_link->lmm_resource.power.scale));
652 TRACE_surf_link_set_bandwidth(date,
654 *) (((nw_link->lmm_resource).
655 generic_resource).name),
656 sg_bandwidth_factor *
657 (nw_link->lmm_resource.power.peak *
658 nw_link->lmm_resource.power.scale));
660 if (sg_weight_S_parameter > 0) {
661 while ((var = lmm_get_var_from_cnst
662 (network_maxmin_system, nw_link->lmm_resource.constraint,
664 action = lmm_variable_id(var);
665 action->weight += delta;
666 if (!(GENERIC_LMM_ACTION(action).suspended))
667 lmm_update_variable_weight(network_maxmin_system,
668 GENERIC_LMM_ACTION(action).variable, action->weight);
671 if (tmgr_trace_event_free(event_type))
672 nw_link->lmm_resource.power.event = NULL;
673 } else if (event_type == nw_link->lat_event) {
674 double delta = value - nw_link->lat_current;
675 lmm_variable_t var = NULL;
676 lmm_element_t elem = NULL;
677 surf_action_network_CM02_t action = NULL;
679 nw_link->lat_current = value;
680 while ((var = lmm_get_var_from_cnst
681 (network_maxmin_system, nw_link->lmm_resource.constraint,
683 action = lmm_variable_id(var);
684 action->lat_current += delta;
685 action->weight += delta;
686 if (action->rate < 0)
687 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
688 sg_tcp_gamma / (2.0 *
689 action->lat_current));
691 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
693 sg_tcp_gamma / (2.0 *
697 if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
698 XBT_INFO("Flow is limited BYBANDWIDTH");
700 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
701 action->lat_current);
704 if (!(GENERIC_LMM_ACTION(action).suspended))
705 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
709 if (tmgr_trace_event_free(event_type))
710 nw_link->lat_event = NULL;
711 } else if (event_type == nw_link->lmm_resource.state_event) {
713 nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
715 lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
716 lmm_variable_t var = NULL;
717 lmm_element_t elem = NULL;
719 nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
720 while ((var = lmm_get_var_from_cnst
721 (network_maxmin_system, cnst, &elem))) {
722 surf_action_t action = lmm_variable_id(var);
724 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
725 surf_action_state_get(action) == SURF_ACTION_READY) {
726 action->finish = date;
727 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
731 if (tmgr_trace_event_free(event_type))
732 nw_link->lmm_resource.state_event = NULL;
734 XBT_CRITICAL("Unknown event ! \n");
739 ("There were a resource state event, need to update actions related to the constraint (%p)",
740 nw_link->lmm_resource.constraint);
745 static surf_action_t net_communicate(void *src,
746 void *dst, double size,
752 surf_action_network_CM02_t action = NULL;
753 double bandwidth_bound;
754 double latency = 0.0;
755 xbt_dynar_t back_route = NULL;
756 int constraints_per_variable = 0;
758 xbt_dynar_t route = xbt_dynar_new(global_routing->size_of_link, NULL);
760 XBT_IN("(%s,%s,%g,%g)", ((network_element_t)src)->name, ((network_element_t)dst)->name, size, rate);
762 routing_get_route_and_latency((network_element_t)src, (network_element_t)dst, &route, &latency);
763 xbt_assert(!xbt_dynar_is_empty(route) || latency,
764 "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
765 ((network_element_t)src)->name, ((network_element_t)dst)->name);
767 xbt_dynar_foreach(route, i, link) {
768 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
773 if (sg_network_crosstraffic == 1) {
774 routing_get_route_and_latency((network_element_t)dst, (network_element_t)src, &back_route, NULL);
775 xbt_dynar_foreach(back_route, i, link) {
776 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
784 surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
785 surf_network_model, failed);
786 #ifdef HAVE_LATENCY_BOUND_TRACKING
787 action->latency_limited = 0;
789 action->weight = action->latency = latency;
791 xbt_swag_insert(action, ((surf_action_t)action)->state_set);
793 if (network_update_mechanism == UM_LAZY) {
794 GENERIC_LMM_ACTION(action).index_heap = -1;
795 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
798 bandwidth_bound = -1.0;
799 if (sg_weight_S_parameter > 0) {
800 xbt_dynar_foreach(route, i, link) {
802 sg_weight_S_parameter /
803 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
806 xbt_dynar_foreach(route, i, link) {
807 double bb = bandwidth_factor_callback(size) *
808 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
810 (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
813 action->lat_current = action->latency;
814 action->latency *= latency_factor_callback(size);
816 bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
818 xbt_assert(!xbt_dynar_is_empty(route),
819 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
821 link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
822 gap_append(size, link, action);
823 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
824 action, ((network_element_t)src)->name, ((network_element_t)dst)->name, action->sender.gap,
828 constraints_per_variable = xbt_dynar_length(route);
829 if (back_route != NULL)
830 constraints_per_variable += xbt_dynar_length(back_route);
832 if (action->latency > 0) {
833 GENERIC_LMM_ACTION(action).variable =
834 lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
835 constraints_per_variable);
836 if (network_update_mechanism == UM_LAZY) {
837 // add to the heap the event when the latency is payed
838 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
839 action->latency + GENERIC_LMM_ACTION(action).last_update);
840 surf_action_lmm_heap_insert(net_action_heap,(surf_action_lmm_t)action, action->latency + GENERIC_LMM_ACTION(action).last_update,
841 xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
844 GENERIC_LMM_ACTION(action).variable =
845 lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
846 constraints_per_variable);
848 if (action->rate < 0) {
849 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
850 (action->lat_current > 0) ?
851 sg_tcp_gamma / (2.0 *
852 action->lat_current) : -1.0);
854 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
855 (action->lat_current > 0) ?
857 sg_tcp_gamma / (2.0 *
858 action->lat_current))
862 xbt_dynar_foreach(route, i, link) {
863 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
864 GENERIC_LMM_ACTION(action).variable, 1.0);
867 if (sg_network_crosstraffic == 1) {
868 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
869 xbt_dynar_foreach(back_route, i, link) {
870 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
871 GENERIC_LMM_ACTION(action).variable, .05);
875 xbt_dynar_free(&route);
878 return (surf_action_t) action;
881 static xbt_dynar_t net_get_route(void *src, void *dst)
883 xbt_dynar_t route = NULL;
884 routing_get_route_and_latency(src, dst, &route, NULL);
888 static double net_get_link_bandwidth(const void *link)
890 surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
891 return lmm->power.peak * lmm->power.scale;
894 static double net_get_link_latency(const void *link)
896 return ((link_CM02_t) link)->lat_current;
899 static int net_link_shared(const void *link)
902 lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
905 static void net_action_suspend(surf_action_t action)
907 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 1;
908 lmm_update_variable_weight(network_maxmin_system,
909 ((surf_action_network_CM02_t)
910 action)->generic_lmm_action.variable, 0.0);
912 if (network_update_mechanism == UM_LAZY) // remove action from the heap
913 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
916 static void net_action_resume(surf_action_t action)
918 if (((surf_action_network_CM02_t) action)->generic_lmm_action.suspended) {
919 lmm_update_variable_weight(network_maxmin_system,
920 ((surf_action_network_CM02_t)
921 action)->generic_lmm_action.variable,
922 ((surf_action_network_CM02_t)
924 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 0;
925 if (network_update_mechanism == UM_LAZY) // remove action from the heap
926 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
930 static int net_action_is_suspended(surf_action_t action)
932 return ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended;
935 void net_action_set_max_duration(surf_action_t action, double duration)
937 action->max_duration = duration;
938 if (network_update_mechanism == UM_LAZY) // remove action from the heap
939 surf_action_lmm_heap_remove(net_action_heap,(surf_action_lmm_t)action);
943 static void net_action_set_category(surf_action_t action,
944 const char *category)
946 action->category = xbt_strdup(category);
950 static void net_finalize(void)
952 surf_model_exit(surf_network_model);
953 surf_network_model = NULL;
955 lmm_system_free(network_maxmin_system);
956 network_maxmin_system = NULL;
958 if (network_update_mechanism == UM_LAZY) {
959 xbt_heap_free(net_action_heap);
960 xbt_swag_free(net_modified_set);
964 xbt_dynar_free(&smpi_bw_factor);
966 xbt_dynar_free(&smpi_lat_factor);
969 static void smpi_gap_append(double size, const link_CM02_t link,
970 surf_action_network_CM02_t action)
972 const char *src = link->lmm_resource.generic_resource.name;
974 surf_action_network_CM02_t last_action;
977 if (sg_sender_gap > 0.0) {
979 gap_lookup = xbt_dict_new();
981 fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
982 action->sender.gap = 0.0;
983 if (fifo && xbt_fifo_size(fifo) > 0) {
984 /* Compute gap from last send */
986 (surf_action_network_CM02_t)
987 xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
988 bw = net_get_link_bandwidth(link);
990 last_action->sender.gap + max(sg_sender_gap,
991 last_action->sender.size / bw);
992 action->latency += action->sender.gap;
994 /* Append action as last send */
995 action->sender.link_name = link->lmm_resource.generic_resource.name;
997 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
998 action->sender.link_name);
1000 fifo = xbt_fifo_new();
1001 xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1003 action->sender.fifo_item = xbt_fifo_push(fifo, action);
1004 action->sender.size = size;
1008 static void smpi_gap_remove(surf_action_network_CM02_t action)
1013 if (sg_sender_gap > 0.0 && action->sender.link_name
1014 && action->sender.fifo_item) {
1016 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1017 action->sender.link_name);
1018 xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1019 size = xbt_fifo_size(fifo);
1021 xbt_fifo_free(fifo);
1022 xbt_dict_remove(gap_lookup, action->sender.link_name);
1023 size = xbt_dict_length(gap_lookup);
1025 xbt_dict_free(&gap_lookup);
1031 static void surf_network_model_init_internal(void)
1033 s_surf_action_network_CM02_t comm;
1034 surf_network_model = surf_model_init();
1036 surf_network_model->name = "network";
1037 surf_network_model->action_unref = net_action_unref;
1038 surf_network_model->action_cancel = net_action_cancel;
1039 surf_network_model->action_recycle = net_action_recycle;
1040 surf_network_model->get_remains = net_action_get_remains;
1041 #ifdef HAVE_LATENCY_BOUND_TRACKING
1042 surf_network_model->get_latency_limited = net_get_link_latency_limited;
1045 surf_network_model->set_category = net_action_set_category;
1048 surf_network_model->model_private->resource_used = net_resource_used;
1049 if (network_update_mechanism == UM_LAZY) {
1050 surf_network_model->model_private->share_resources =
1051 net_share_resources_lazy;
1052 surf_network_model->model_private->update_actions_state =
1053 net_update_actions_state_lazy;
1054 } else if (network_update_mechanism == UM_FULL) {
1055 surf_network_model->model_private->share_resources =
1056 net_share_resources_full;
1057 surf_network_model->model_private->update_actions_state =
1058 net_update_actions_state_full;
1061 surf_network_model->model_private->update_resource_state =
1062 net_update_resource_state;
1063 surf_network_model->model_private->finalize = net_finalize;
1065 surf_network_model->suspend = net_action_suspend;
1066 surf_network_model->resume = net_action_resume;
1067 surf_network_model->is_suspended = net_action_is_suspended;
1068 surf_cpu_model->set_max_duration = net_action_set_max_duration;
1070 surf_network_model->extension.network.communicate = net_communicate;
1071 surf_network_model->extension.network.get_route = net_get_route;
1072 surf_network_model->extension.network.get_link_bandwidth =
1073 net_get_link_bandwidth;
1074 surf_network_model->extension.network.get_link_latency =
1075 net_get_link_latency;
1076 surf_network_model->extension.network.link_shared = net_link_shared;
1077 surf_network_model->extension.network.add_traces = net_add_traces;
1078 surf_network_model->extension.network.create_resource =
1079 net_create_resource;
1081 if (!network_maxmin_system)
1082 network_maxmin_system = lmm_system_new(net_selective_update);
1084 routing_model_create(sizeof(link_CM02_t),
1085 net_create_resource("__loopback__",
1086 498000000, NULL, 0.000015, NULL,
1087 SURF_RESOURCE_ON, NULL,
1088 SURF_LINK_FATPIPE, NULL));
1090 if (network_update_mechanism == UM_LAZY) {
1091 net_action_heap = xbt_heap_new(8, NULL);
1092 xbt_heap_set_update_callback(net_action_heap,
1093 surf_action_lmm_update_index_heap);
1095 xbt_swag_new(xbt_swag_offset(comm, generic_lmm_action.action_list_hookup));
1096 network_maxmin_system->keep_track = net_modified_set;
1100 static void set_update_mechanism(void)
1102 char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1104 xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
1106 if (!strcmp(optim, "Full")) {
1107 network_update_mechanism = UM_FULL;
1108 net_selective_update = select;
1109 } else if (!strcmp(optim, "Lazy")) {
1110 network_update_mechanism = UM_LAZY;
1111 net_selective_update = 1;
1112 xbt_assert((select == 1)
1114 (xbt_cfg_is_default_value
1115 (_surf_cfg_set, "network/maxmin_selective_update")),
1116 "Disabling selective update while using the lazy update mechanism is dumb!");
1118 xbt_die("Unsupported optimization (%s) for this model", optim);
1122 /************************************************************************/
1123 /* New model based on LV08 and experimental results of MPI ping-pongs */
1124 /************************************************************************/
1125 /* @Inproceedings{smpi_ipdps, */
1126 /* author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
1127 /* title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
1128 /* booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
1129 /* address={Anchorage (Alaska) USA}, */
1133 void surf_network_model_init_SMPI(void)
1136 if (surf_network_model)
1138 set_update_mechanism();
1140 surf_network_model_init_internal();
1141 latency_factor_callback = &smpi_latency_factor;
1142 bandwidth_factor_callback = &smpi_bandwidth_factor;
1143 bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1144 gap_append = &smpi_gap_append;
1145 gap_remove = &smpi_gap_remove;
1146 net_define_callbacks();
1147 xbt_dynar_push(model_list, &surf_network_model);
1148 network_solve = lmm_solve;
1150 xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1151 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1154 /************************************************************************/
1155 /* New model based on optimizations discussed during Pedro Velho's thesis*/
1156 /************************************************************************/
1157 /* @techreport{VELHO:2011:HAL-00646896:1, */
1158 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
1159 /* title = {{Flow-level network models: have we reached the limits?}}, */
1160 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
1161 /* type = {Rapport de recherche}, */
1162 /* institution = {INRIA}, */
1163 /* number = {RR-7821}, */
1164 /* year = {2011}, */
1166 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
1168 void surf_network_model_init_LegrandVelho(void)
1170 if (surf_network_model)
1173 set_update_mechanism();
1175 surf_network_model_init_internal();
1176 net_define_callbacks();
1177 xbt_dynar_push(model_list, &surf_network_model);
1178 network_solve = lmm_solve;
1180 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor",
1182 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1184 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 20537);
1187 /***************************************************************************/
1188 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1189 /***************************************************************************/
1190 /* @TechReport{ rr-lip2002-40, */
1191 /* author = {Henri Casanova and Loris Marchal}, */
1192 /* institution = {LIP}, */
1193 /* title = {A Network Model for Simulation of Grid Application}, */
1194 /* number = {2002-40}, */
1195 /* month = {oct}, */
1198 void surf_network_model_init_CM02(void)
1201 if (surf_network_model)
1204 set_update_mechanism();
1205 surf_network_model_init_internal();
1206 net_define_callbacks();
1207 xbt_dynar_push(model_list, &surf_network_model);
1208 network_solve = lmm_solve;
1210 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1211 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1213 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1216 /***************************************************************************/
1217 /* The models from Steven H. Low */
1218 /***************************************************************************/
1219 /* @article{Low03, */
1220 /* author={Steven H. Low}, */
1221 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
1223 /* journal={{IEEE/ACM} Transactions on Networking}, */
1224 /* volume={11}, number={4}, */
1226 void surf_network_model_init_Reno(void)
1228 if (surf_network_model)
1231 set_update_mechanism();
1232 surf_network_model_init_internal();
1233 net_define_callbacks();
1235 xbt_dynar_push(model_list, &surf_network_model);
1236 lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1238 network_solve = lagrange_solve;
1240 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1241 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1243 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1247 void surf_network_model_init_Reno2(void)
1249 if (surf_network_model)
1252 set_update_mechanism();
1253 surf_network_model_init_internal();
1254 net_define_callbacks();
1256 xbt_dynar_push(model_list, &surf_network_model);
1257 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1259 network_solve = lagrange_solve;
1261 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1262 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1264 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1268 void surf_network_model_init_Vegas(void)
1270 if (surf_network_model)
1273 set_update_mechanism();
1274 surf_network_model_init_internal();
1275 net_define_callbacks();
1277 xbt_dynar_push(model_list, &surf_network_model);
1278 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1280 network_solve = lagrange_solve;
1282 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1283 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1285 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);