3 * Network with improved management of tasks, IM (Improved Management).
4 * Uses a heap to store actions so that the share_resources is faster.
5 * This model automatically sets the selective update flag to 1 and is
6 * highly dependent on the maxmin lmm module.
9 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
10 * All rights reserved. */
12 /* This program is free software; you can redistribute it and/or modify it
13 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "network_private.h"
19 #include "surf_private.h"
21 #include "maxmin_private.h"
22 #include "surf/surfxml_parse_values.h"
23 #include "surf/surf_resource.h"
24 #include "surf/surf_resource_lmm.h"
26 #undef GENERIC_LMM_ACTION
28 #define GENERIC_LMM_ACTION(action) (action)->generic_lmm_action
29 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
33 "Logging specific to the SURF network module");
35 surf_model_t surf_network_model = NULL;
36 static lmm_system_t network_maxmin_system = NULL;
37 static void (*network_solve) (lmm_system_t) = NULL;
39 xbt_dynar_t smpi_bw_factor = NULL;
40 xbt_dynar_t smpi_lat_factor = NULL;
42 typedef struct s_smpi_factor *smpi_factor_t;
43 typedef struct s_smpi_factor {
48 double sg_sender_gap = 0.0;
49 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
50 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
51 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
53 double sg_tcp_gamma = 0.0;
54 int sg_network_crosstraffic = 0;
56 xbt_dict_t gap_lookup = NULL;
58 e_UM_t network_update_mechanism = UM_UNDEFINED;
59 static int net_selective_update = 0;
61 static int net_action_is_suspended(surf_action_t action);
62 static void update_action_remaining_lazy(double now);
64 static xbt_swag_t net_modified_set = NULL;
65 static xbt_heap_t net_action_heap = NULL;
67 /* added to manage the communication action's heap */
68 static void net_action_update_index_heap(void *action, int i)
70 ((surf_action_network_CM02_t) action)->index_heap = i;
73 /* insert action on heap using a given key and a hat (heap_action_type)
74 * a hat can be of three types for communications:
76 * NORMAL = this is a normal heap entry stating the date to finish transmitting
77 * LATENCY = this is a heap entry to warn us when the latency is payed
78 * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
80 static void heap_insert(surf_action_network_CM02_t action, double key,
81 enum heap_action_type hat)
84 xbt_heap_push(net_action_heap, action, key);
87 static void heap_remove(surf_action_network_CM02_t action)
90 if (((surf_action_network_CM02_t) action)->index_heap >= 0) {
91 xbt_heap_remove(net_action_heap, action->index_heap);
95 /******************************************************************************/
96 /* Factors callbacks */
97 /******************************************************************************/
98 static double constant_latency_factor(double size)
100 return sg_latency_factor;
103 static double constant_bandwidth_factor(double size)
105 return sg_bandwidth_factor;
108 static double constant_bandwidth_constraint(double rate, double bound,
114 /**********************/
116 /**********************/
117 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
120 unsigned int iter = 0;
121 s_smpi_factor_t fact;
122 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
124 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
125 radical_elements = xbt_str_split(smpi_coef_string, ";");
126 xbt_dynar_foreach(radical_elements, iter, value) {
128 radical_elements2 = xbt_str_split(value, ":");
129 if (xbt_dynar_length(radical_elements2) != 2)
130 xbt_die("Malformed radical for smpi factor!");
131 fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
132 fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
133 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
134 XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
135 xbt_dynar_free(&radical_elements2);
137 xbt_dynar_free(&radical_elements);
141 static double smpi_bandwidth_factor(double size)
145 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/bw_factor"));
147 unsigned int iter = 0;
148 s_smpi_factor_t fact;
149 xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
150 if (size >= fact.factor) {
151 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
159 static double smpi_latency_factor(double size)
161 if (!smpi_lat_factor)
163 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/lat_factor"));
165 unsigned int iter = 0;
166 s_smpi_factor_t fact;
167 xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
168 if (size >= fact.factor) {
169 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
177 /**--------- <copy/paste C code snippet in surf/network.c> -----------*/
179 static double smpi_bandwidth_constraint(double rate, double bound,
182 return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
185 static double (*latency_factor_callback) (double) =
186 &constant_latency_factor;
187 static double (*bandwidth_factor_callback) (double) =
188 &constant_bandwidth_factor;
189 static double (*bandwidth_constraint_callback) (double, double, double) =
190 &constant_bandwidth_constraint;
192 static void (*gap_append) (double, const link_CM02_t,
193 surf_action_network_CM02_t) = NULL;
194 static void (*gap_remove) (surf_action_network_CM02_t) = NULL;
196 static void *net_create_resource(const char *name,
198 tmgr_trace_t bw_trace,
200 tmgr_trace_t lat_trace,
201 e_surf_resource_state_t
203 tmgr_trace_t state_trace,
204 e_surf_link_sharing_policy_t
205 policy, xbt_dict_t properties)
207 link_CM02_t nw_link = (link_CM02_t)
208 surf_resource_lmm_new(sizeof(s_link_CM02_t),
209 surf_network_model, name, properties,
210 network_maxmin_system,
211 sg_bandwidth_factor * bw_initial,
213 state_initial, state_trace,
214 bw_initial, bw_trace);
216 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
217 "Link '%s' declared several times in the platform file.",
220 nw_link->lat_current = lat_initial;
223 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
225 if (policy == SURF_LINK_FATPIPE)
226 lmm_constraint_shared(nw_link->lmm_resource.constraint);
228 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
233 static void net_parse_link_init(sg_platf_link_cbarg_t link)
235 if (link->policy == SURF_LINK_FULLDUPLEX) {
237 link_id = bprintf("%s_UP", link->id);
238 net_create_resource(link_id,
240 link->bandwidth_trace,
244 link->state_trace, link->policy, link->properties);
246 link_id = bprintf("%s_DOWN", link->id);
247 net_create_resource(link_id,
249 link->bandwidth_trace,
253 link->state_trace, link->policy, link->properties);
256 net_create_resource(link->id,
258 link->bandwidth_trace,
262 link->state_trace, link->policy, link->properties);
266 static void net_add_traces(void)
268 xbt_dict_cursor_t cursor = NULL;
269 char *trace_name, *elm;
271 static int called = 0;
276 /* connect all traces relative to network */
277 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
278 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
279 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
281 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
284 "Cannot connect trace %s to link %s: trace undefined",
287 link->lmm_resource.state_event =
288 tmgr_history_add_trace(history, trace, 0.0, 0, link);
291 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
292 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
293 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
295 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
298 "Cannot connect trace %s to link %s: trace undefined",
301 link->lmm_resource.power.event =
302 tmgr_history_add_trace(history, trace, 0.0, 0, link);
305 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
306 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
307 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
309 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
312 "Cannot connect trace %s to link %s: trace undefined",
315 link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
319 static void net_define_callbacks(void)
321 /* Figuring out the network links */
322 sg_platf_link_add_cb(net_parse_link_init);
323 sg_platf_postparse_add_cb(net_add_traces);
326 static int net_resource_used(void *resource_id)
328 return lmm_constraint_used(network_maxmin_system, ((surf_resource_lmm_t)
333 static int net_action_unref(surf_action_t action)
336 if (!action->refcount) {
337 xbt_swag_remove(action, action->state_set);
338 if (((surf_action_lmm_t)action)->variable) {
339 lmm_variable_free(network_maxmin_system,
340 ((surf_action_lmm_t) action)->variable);
342 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
343 heap_remove((surf_action_network_CM02_t) action);
344 xbt_swag_remove(action, net_modified_set);
347 xbt_free(((surf_action_network_CM02_t) action)->src_name);
348 xbt_free(((surf_action_network_CM02_t) action)->dst_name);
349 xbt_free(action->category);
351 surf_action_free(&action);
359 static void net_action_cancel(surf_action_t action)
361 XBT_DEBUG("cancel action %p", action);
362 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
363 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
364 xbt_swag_remove(action, net_modified_set);
365 heap_remove((surf_action_network_CM02_t) action);
369 void net_action_recycle(surf_action_t action)
374 #ifdef HAVE_LATENCY_BOUND_TRACKING
375 int net_get_link_latency_limited(surf_action_t action)
377 return action->latency_limited;
381 double net_action_get_remains(surf_action_t action)
383 if (network_update_mechanism == UM_LAZY) /* update remains before return it */
384 update_action_remaining_lazy(surf_get_clock());
385 return action->remains;
388 static void update_action_remaining_lazy(double now)
390 surf_action_network_CM02_t action = NULL;
393 xbt_swag_foreach(action, net_modified_set) {
395 if (GENERIC_LMM_ACTION(action).suspended != 0) {
399 delta = now - action->last_update;
401 double_update(&(((surf_action_t)action)->remains),
402 lmm_variable_getvalue(((surf_action_lmm_t) action)->variable) * delta);
404 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
405 double_update(&(((surf_action_t)action)->max_duration), delta);
407 if ((((surf_action_t)action)->remains <= 0) &&
408 (lmm_get_variable_weight(((surf_action_lmm_t)action)->variable) > 0)) {
409 ((surf_action_t)action)->finish = surf_get_clock();
410 surf_network_model->action_state_set((surf_action_t) action,
413 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
414 && (((surf_action_t)action)->max_duration <= 0))) {
415 ((surf_action_t)action)->finish = surf_get_clock();
416 surf_network_model->action_state_set((surf_action_t) action,
421 action->last_update = now;
425 static double net_share_resources_full(double now)
427 s_surf_action_lmm_t s_action;
428 surf_action_network_CM02_t action = NULL;
429 xbt_swag_t running_actions =
430 surf_network_model->states.running_action_set;
433 min = generic_maxmin_share_resources(running_actions,
434 xbt_swag_offset(s_action,
436 network_maxmin_system,
439 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable) )))
441 xbt_swag_foreach(action, running_actions) {
442 #ifdef HAVE_LATENCY_BOUND_TRACKING
443 if (lmm_is_variable_limited_by_latency(action->variable)) {
444 GENERIC_LMM_ACTION(action).latency_limited = 1;
446 GENERIC_LMM_ACTION(action).latency_limited = 0;
449 if (action->latency > 0) {
450 min = (min < 0) ? action->latency : min(min, action->latency);
454 XBT_DEBUG("Min of share resources %f", min);
459 static double net_share_resources_lazy(double now)
461 surf_action_network_CM02_t action = NULL;
466 ("Before share resources, the size of modified actions set is %d",
467 xbt_swag_size(net_modified_set));
468 update_action_remaining_lazy(now);
470 lmm_solve(network_maxmin_system);
473 ("After share resources, The size of modified actions set is %d",
474 xbt_swag_size(net_modified_set));
476 xbt_swag_foreach(action, net_modified_set) {
477 int max_dur_flag = 0;
479 if (GENERIC_ACTION(action).state_set !=
480 surf_network_model->states.running_action_set)
483 /* bogus priority, skip it */
484 if (GENERIC_ACTION(action).priority <= 0) {
489 value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
491 if (GENERIC_ACTION(action).remains > 0) {
492 value = GENERIC_ACTION(action).remains / value;
500 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
502 || GENERIC_ACTION(action).start +
503 GENERIC_ACTION(action).max_duration < min)) {
504 min = GENERIC_ACTION(action).start +
505 GENERIC_ACTION(action).max_duration;
509 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
510 GENERIC_ACTION(action).start, now + value,
511 GENERIC_ACTION(action).max_duration);
513 if (action->index_heap >= 0) {
514 heap_remove((surf_action_network_CM02_t) action);
518 heap_insert((surf_action_network_CM02_t) action, min,
519 max_dur_flag ? MAX_DURATION : NORMAL);
520 XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min,
525 //hereafter must have already the min value for this resource model
526 if (xbt_heap_size(net_action_heap) > 0) {
527 min = xbt_heap_maxkey(net_action_heap) - now;
532 XBT_DEBUG("The minimum with the HEAP %lf", min);
537 static void net_update_actions_state_full(double now, double delta)
540 surf_action_network_CM02_t action = NULL;
541 surf_action_network_CM02_t next_action = NULL;
542 xbt_swag_t running_actions =
543 surf_network_model->states.running_action_set;
545 xbt_swag_t failed_actions =
546 surf_network_model->states.failed_action_set;
549 xbt_swag_foreach_safe(action, next_action, running_actions) {
551 if (action->latency > 0) {
552 if (action->latency > deltap) {
553 double_update(&(action->latency), deltap);
556 double_update(&(deltap), action->latency);
557 action->latency = 0.0;
559 if ((action->latency == 0.0) && !(GENERIC_LMM_ACTION(action).suspended))
560 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
564 if (TRACE_is_enabled()) {
565 xbt_dynar_t route = NULL;
566 routing_get_route_and_latency(action->src_name, action->dst_name,
570 xbt_dynar_foreach(route, i, link) {
571 TRACE_surf_link_set_utilization(link->lmm_resource.
572 generic_resource.name,
573 (((surf_action_t)action)->data,
574 (surf_action_t) action,
575 lmm_variable_getvalue(action->
581 if (!lmm_get_number_of_cnst_from_var
582 (network_maxmin_system, GENERIC_LMM_ACTION(action).variable)) {
583 /* There is actually no link used, hence an infinite bandwidth.
584 * This happens often when using models like vivaldi.
585 * In such case, just make sure that the action completes immediately.
587 double_update(&(GENERIC_ACTION(action).remains),
588 GENERIC_ACTION(action).remains);
590 double_update(&(GENERIC_ACTION(action).remains),
591 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * deltap);
592 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
593 double_update(&(((surf_action_t)action)->max_duration), delta);
595 if ((GENERIC_ACTION(action).remains <= 0) &&
596 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
597 ((surf_action_t)action)->finish = surf_get_clock();
598 surf_network_model->action_state_set((surf_action_t) action,
603 } else if ((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
604 && (((surf_action_t)action)->max_duration <= 0)) {
605 ((surf_action_t)action)->finish = surf_get_clock();
606 surf_network_model->action_state_set((surf_action_t) action,
616 static void net_update_actions_state_lazy(double now, double delta)
618 surf_action_network_CM02_t action = NULL;
620 while ((xbt_heap_size(net_action_heap) > 0)
621 && (double_equals(xbt_heap_maxkey(net_action_heap), now))) {
622 action = xbt_heap_pop(net_action_heap);
623 XBT_DEBUG("Action %p: finish", action);
624 GENERIC_ACTION(action).finish = surf_get_clock();
626 // if I am wearing a latency hat
627 if (action->hat == LATENCY) {
628 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
631 action->last_update = surf_get_clock();
633 // if I am wearing a max_duration or normal hat
634 } else if (action->hat == MAX_DURATION || action->hat == NORMAL) {
635 // no need to communicate anymore
636 // assume that flows that reached max_duration have remaining of 0
637 GENERIC_ACTION(action).remains = 0;
638 ((surf_action_t)action)->finish = surf_get_clock();
639 surf_network_model->action_state_set((surf_action_t) action,
647 static void net_update_resource_state(void *id,
648 tmgr_trace_event_t event_type,
649 double value, double date)
651 link_CM02_t nw_link = id;
652 /* printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
653 /* "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
654 /* value, event_type); */
656 if (event_type == nw_link->lmm_resource.power.event) {
658 sg_weight_S_parameter / value - sg_weight_S_parameter /
659 (nw_link->lmm_resource.power.peak *
660 nw_link->lmm_resource.power.scale);
661 lmm_variable_t var = NULL;
662 lmm_element_t elem = NULL;
663 surf_action_network_CM02_t action = NULL;
665 nw_link->lmm_resource.power.peak = value;
666 lmm_update_constraint_bound(network_maxmin_system,
667 nw_link->lmm_resource.constraint,
668 sg_bandwidth_factor *
669 (nw_link->lmm_resource.power.peak *
670 nw_link->lmm_resource.power.scale));
672 TRACE_surf_link_set_bandwidth(date,
674 *) (((nw_link->lmm_resource).
675 generic_resource).name),
676 sg_bandwidth_factor *
677 (nw_link->lmm_resource.power.peak *
678 nw_link->lmm_resource.power.scale));
680 if (sg_weight_S_parameter > 0) {
681 while ((var = lmm_get_var_from_cnst
682 (network_maxmin_system, nw_link->lmm_resource.constraint,
684 action = lmm_variable_id(var);
685 action->weight += delta;
686 if (!(GENERIC_LMM_ACTION(action).suspended))
687 lmm_update_variable_weight(network_maxmin_system,
688 GENERIC_LMM_ACTION(action).variable, action->weight);
691 if (tmgr_trace_event_free(event_type))
692 nw_link->lmm_resource.power.event = NULL;
693 } else if (event_type == nw_link->lat_event) {
694 double delta = value - nw_link->lat_current;
695 lmm_variable_t var = NULL;
696 lmm_element_t elem = NULL;
697 surf_action_network_CM02_t action = NULL;
699 nw_link->lat_current = value;
700 while ((var = lmm_get_var_from_cnst
701 (network_maxmin_system, nw_link->lmm_resource.constraint,
703 action = lmm_variable_id(var);
704 action->lat_current += delta;
705 action->weight += delta;
706 if (action->rate < 0)
707 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
708 sg_tcp_gamma / (2.0 *
709 action->lat_current));
711 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
713 sg_tcp_gamma / (2.0 *
717 if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
718 XBT_INFO("Flow is limited BYBANDWIDTH");
720 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
721 action->lat_current);
724 if (!(GENERIC_LMM_ACTION(action).suspended))
725 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
729 if (tmgr_trace_event_free(event_type))
730 nw_link->lat_event = NULL;
731 } else if (event_type == nw_link->lmm_resource.state_event) {
733 nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
735 lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
736 lmm_variable_t var = NULL;
737 lmm_element_t elem = NULL;
739 nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
740 while ((var = lmm_get_var_from_cnst
741 (network_maxmin_system, cnst, &elem))) {
742 surf_action_t action = lmm_variable_id(var);
744 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
745 surf_action_state_get(action) == SURF_ACTION_READY) {
746 action->finish = date;
747 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
751 if (tmgr_trace_event_free(event_type))
752 nw_link->lmm_resource.state_event = NULL;
754 XBT_CRITICAL("Unknown event ! \n");
759 ("There were a resource state event, need to update actions related to the constraint (%p)",
760 nw_link->lmm_resource.constraint);
765 static surf_action_t net_communicate(const char *src_name,
766 const char *dst_name, double size,
772 surf_action_network_CM02_t action = NULL;
773 double bandwidth_bound;
774 double latency = 0.0;
775 xbt_dynar_t back_route = NULL;
776 int constraints_per_variable = 0;
778 xbt_dynar_t route = xbt_dynar_new(global_routing->size_of_link, NULL);
780 XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
782 routing_get_route_and_latency(src_name, dst_name, &route, &latency);
783 xbt_assert(!xbt_dynar_is_empty(route) || latency,
784 "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
787 xbt_dynar_foreach(route, i, link) {
788 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
793 if (sg_network_crosstraffic == 1) {
794 routing_get_route_and_latency(dst_name, src_name, &back_route, NULL);
795 xbt_dynar_foreach(back_route, i, link) {
796 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
804 surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
805 surf_network_model, failed);
806 #ifdef HAVE_LATENCY_BOUND_TRACKING
807 (GENERIC_LMM_ACTION(action)).latency_limited = 0;
809 action->weight = action->latency = latency;
811 xbt_swag_insert(action, ((surf_action_t)action)->state_set);
813 if (network_update_mechanism == UM_LAZY) {
814 action->index_heap = -1;
815 action->last_update = surf_get_clock();
818 bandwidth_bound = -1.0;
819 if (sg_weight_S_parameter > 0) {
820 xbt_dynar_foreach(route, i, link) {
822 sg_weight_S_parameter /
823 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
826 xbt_dynar_foreach(route, i, link) {
827 double bb = bandwidth_factor_callback(size) *
828 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
830 (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
833 action->lat_current = action->latency;
834 action->latency *= latency_factor_callback(size);
836 bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
838 xbt_assert(!xbt_dynar_is_empty(route),
839 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
841 link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
842 gap_append(size, link, action);
843 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
844 action, src_name, dst_name, action->sender.gap,
848 constraints_per_variable = xbt_dynar_length(route);
849 if (back_route != NULL)
850 constraints_per_variable += xbt_dynar_length(back_route);
852 if (action->latency > 0) {
853 GENERIC_LMM_ACTION(action).variable =
854 lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
855 constraints_per_variable);
856 if (network_update_mechanism == UM_LAZY) {
857 // add to the heap the event when the latency is payed
858 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
859 action->latency + action->last_update);
860 heap_insert(action, action->latency + action->last_update,
861 xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
864 GENERIC_LMM_ACTION(action).variable =
865 lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
866 constraints_per_variable);
868 if (action->rate < 0) {
869 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
870 (action->lat_current > 0) ?
871 sg_tcp_gamma / (2.0 *
872 action->lat_current) : -1.0);
874 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
875 (action->lat_current > 0) ?
877 sg_tcp_gamma / (2.0 *
878 action->lat_current))
882 xbt_dynar_foreach(route, i, link) {
883 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
884 GENERIC_LMM_ACTION(action).variable, 1.0);
887 if (sg_network_crosstraffic == 1) {
888 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
889 xbt_dynar_foreach(back_route, i, link) {
890 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
891 GENERIC_LMM_ACTION(action).variable, .05);
895 if (TRACE_is_enabled()) {
896 action->src_name = xbt_strdup(src_name);
897 action->dst_name = xbt_strdup(dst_name);
899 action->src_name = action->dst_name = NULL;
903 xbt_dynar_free(&route);
906 return (surf_action_t) action;
909 static xbt_dynar_t net_get_route(const char *src, const char *dst)
911 xbt_dynar_t route = NULL;
912 routing_get_route_and_latency(src, dst, &route, NULL);
916 static double net_get_link_bandwidth(const void *link)
918 surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
919 return lmm->power.peak * lmm->power.scale;
922 static double net_get_link_latency(const void *link)
924 return ((link_CM02_t) link)->lat_current;
927 static int net_link_shared(const void *link)
930 lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
933 static void net_action_suspend(surf_action_t action)
935 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 1;
936 lmm_update_variable_weight(network_maxmin_system,
937 ((surf_action_network_CM02_t)
938 action)->generic_lmm_action.variable, 0.0);
940 if (network_update_mechanism == UM_LAZY) // remove action from the heap
941 heap_remove((surf_action_network_CM02_t) action);
944 static void net_action_resume(surf_action_t action)
946 if (((surf_action_network_CM02_t) action)->generic_lmm_action.suspended) {
947 lmm_update_variable_weight(network_maxmin_system,
948 ((surf_action_network_CM02_t)
949 action)->generic_lmm_action.variable,
950 ((surf_action_network_CM02_t)
952 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 0;
953 if (network_update_mechanism == UM_LAZY) // remove action from the heap
954 heap_remove((surf_action_network_CM02_t) action);
958 static int net_action_is_suspended(surf_action_t action)
960 return ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended;
963 void net_action_set_max_duration(surf_action_t action, double duration)
965 action->max_duration = duration;
966 if (network_update_mechanism == UM_LAZY) // remove action from the heap
967 heap_remove((surf_action_network_CM02_t) action);
971 static void net_action_set_category(surf_action_t action,
972 const char *category)
974 action->category = xbt_strdup(category);
978 static void net_finalize(void)
980 surf_model_exit(surf_network_model);
981 surf_network_model = NULL;
983 lmm_system_free(network_maxmin_system);
984 network_maxmin_system = NULL;
986 if (network_update_mechanism == UM_LAZY) {
987 xbt_heap_free(net_action_heap);
988 xbt_swag_free(net_modified_set);
992 xbt_dynar_free(&smpi_bw_factor);
994 xbt_dynar_free(&smpi_lat_factor);
997 static void smpi_gap_append(double size, const link_CM02_t link,
998 surf_action_network_CM02_t action)
1000 const char *src = link->lmm_resource.generic_resource.name;
1002 surf_action_network_CM02_t last_action;
1005 if (sg_sender_gap > 0.0) {
1007 gap_lookup = xbt_dict_new();
1009 fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
1010 action->sender.gap = 0.0;
1011 if (fifo && xbt_fifo_size(fifo) > 0) {
1012 /* Compute gap from last send */
1014 (surf_action_network_CM02_t)
1015 xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
1016 bw = net_get_link_bandwidth(link);
1017 action->sender.gap =
1018 last_action->sender.gap + max(sg_sender_gap,
1019 last_action->sender.size / bw);
1020 action->latency += action->sender.gap;
1022 /* Append action as last send */
1023 action->sender.link_name = link->lmm_resource.generic_resource.name;
1025 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1026 action->sender.link_name);
1028 fifo = xbt_fifo_new();
1029 xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1031 action->sender.fifo_item = xbt_fifo_push(fifo, action);
1032 action->sender.size = size;
1036 static void smpi_gap_remove(surf_action_network_CM02_t action)
1041 if (sg_sender_gap > 0.0 && action->sender.link_name
1042 && action->sender.fifo_item) {
1044 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1045 action->sender.link_name);
1046 xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1047 size = xbt_fifo_size(fifo);
1049 xbt_fifo_free(fifo);
1050 xbt_dict_remove(gap_lookup, action->sender.link_name);
1051 size = xbt_dict_length(gap_lookup);
1053 xbt_dict_free(&gap_lookup);
1059 static void surf_network_model_init_internal(void)
1061 s_surf_action_network_CM02_t comm;
1062 surf_network_model = surf_model_init();
1064 surf_network_model->name = "network";
1065 surf_network_model->action_unref = net_action_unref;
1066 surf_network_model->action_cancel = net_action_cancel;
1067 surf_network_model->action_recycle = net_action_recycle;
1068 surf_network_model->get_remains = net_action_get_remains;
1069 #ifdef HAVE_LATENCY_BOUND_TRACKING
1070 surf_network_model->get_latency_limited = net_get_link_latency_limited;
1073 surf_network_model->set_category = net_action_set_category;
1076 surf_network_model->model_private->resource_used = net_resource_used;
1077 if (network_update_mechanism == UM_LAZY) {
1078 surf_network_model->model_private->share_resources =
1079 net_share_resources_lazy;
1080 surf_network_model->model_private->update_actions_state =
1081 net_update_actions_state_lazy;
1082 } else if (network_update_mechanism == UM_FULL) {
1083 surf_network_model->model_private->share_resources =
1084 net_share_resources_full;
1085 surf_network_model->model_private->update_actions_state =
1086 net_update_actions_state_full;
1089 surf_network_model->model_private->update_resource_state =
1090 net_update_resource_state;
1091 surf_network_model->model_private->finalize = net_finalize;
1093 surf_network_model->suspend = net_action_suspend;
1094 surf_network_model->resume = net_action_resume;
1095 surf_network_model->is_suspended = net_action_is_suspended;
1096 surf_cpu_model->set_max_duration = net_action_set_max_duration;
1098 surf_network_model->extension.network.communicate = net_communicate;
1099 surf_network_model->extension.network.get_route = net_get_route;
1100 surf_network_model->extension.network.get_link_bandwidth =
1101 net_get_link_bandwidth;
1102 surf_network_model->extension.network.get_link_latency =
1103 net_get_link_latency;
1104 surf_network_model->extension.network.link_shared = net_link_shared;
1105 surf_network_model->extension.network.add_traces = net_add_traces;
1106 surf_network_model->extension.network.create_resource =
1107 net_create_resource;
1109 if (!network_maxmin_system)
1110 network_maxmin_system = lmm_system_new(net_selective_update);
1112 routing_model_create(sizeof(link_CM02_t),
1113 net_create_resource("__loopback__",
1114 498000000, NULL, 0.000015, NULL,
1115 SURF_RESOURCE_ON, NULL,
1116 SURF_LINK_FATPIPE, NULL));
1118 if (network_update_mechanism == UM_LAZY) {
1119 net_action_heap = xbt_heap_new(8, NULL);
1120 xbt_heap_set_update_callback(net_action_heap,
1121 net_action_update_index_heap);
1123 xbt_swag_new(xbt_swag_offset(comm, action_list_hookup));
1124 network_maxmin_system->keep_track = net_modified_set;
1128 static void set_update_mechanism(void)
1131 TRACE_set_network_update_mechanism();
1134 char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1136 xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
1138 if (!strcmp(optim, "Full")) {
1139 network_update_mechanism = UM_FULL;
1140 net_selective_update = select;
1141 } else if (!strcmp(optim, "Lazy")) {
1142 network_update_mechanism = UM_LAZY;
1143 net_selective_update = 1;
1144 xbt_assert((select == 1)
1146 (xbt_cfg_is_default_value
1147 (_surf_cfg_set, "network/maxmin_selective_update")),
1148 "Disabling selective update while using the lazy update mechanism is dumb!");
1150 xbt_die("Unsupported optimization (%s) for this model", optim);
1154 /************************************************************************/
1155 /* New model based on LV08 and experimental results of MPI ping-pongs */
1156 /************************************************************************/
1157 /* @Inproceedings{smpi_ipdps, */
1158 /* author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
1159 /* title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
1160 /* booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
1161 /* address={Anchorage (Alaska) USA}, */
1165 void surf_network_model_init_SMPI(void)
1168 if (surf_network_model)
1170 set_update_mechanism();
1172 surf_network_model_init_internal();
1173 latency_factor_callback = &smpi_latency_factor;
1174 bandwidth_factor_callback = &smpi_bandwidth_factor;
1175 bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1176 gap_append = &smpi_gap_append;
1177 gap_remove = &smpi_gap_remove;
1178 net_define_callbacks();
1179 xbt_dynar_push(model_list, &surf_network_model);
1180 network_solve = lmm_solve;
1182 xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1183 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1186 /************************************************************************/
1187 /* New model based on optimizations discussed during Pedro Velho's thesis*/
1188 /************************************************************************/
1189 /* @techreport{VELHO:2011:HAL-00646896:1, */
1190 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
1191 /* title = {{Flow-level network models: have we reached the limits?}}, */
1192 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
1193 /* type = {Rapport de recherche}, */
1194 /* institution = {INRIA}, */
1195 /* number = {RR-7821}, */
1196 /* year = {2011}, */
1198 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
1200 void surf_network_model_init_LegrandVelho(void)
1202 if (surf_network_model)
1205 set_update_mechanism();
1207 surf_network_model_init_internal();
1208 net_define_callbacks();
1209 xbt_dynar_push(model_list, &surf_network_model);
1210 network_solve = lmm_solve;
1212 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor",
1214 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1216 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 20537);
1219 /***************************************************************************/
1220 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1221 /***************************************************************************/
1222 /* @TechReport{ rr-lip2002-40, */
1223 /* author = {Henri Casanova and Loris Marchal}, */
1224 /* institution = {LIP}, */
1225 /* title = {A Network Model for Simulation of Grid Application}, */
1226 /* number = {2002-40}, */
1227 /* month = {oct}, */
1230 void surf_network_model_init_CM02(void)
1233 if (surf_network_model)
1236 set_update_mechanism();
1237 surf_network_model_init_internal();
1238 net_define_callbacks();
1239 xbt_dynar_push(model_list, &surf_network_model);
1240 network_solve = lmm_solve;
1242 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1243 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1245 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1248 /***************************************************************************/
1249 /* The models from Steven H. Low */
1250 /***************************************************************************/
1251 /* @article{Low03, */
1252 /* author={Steven H. Low}, */
1253 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
1255 /* journal={{IEEE/ACM} Transactions on Networking}, */
1256 /* volume={11}, number={4}, */
1258 void surf_network_model_init_Reno(void)
1260 if (surf_network_model)
1263 set_update_mechanism();
1264 surf_network_model_init_internal();
1265 net_define_callbacks();
1267 xbt_dynar_push(model_list, &surf_network_model);
1268 lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1270 network_solve = lagrange_solve;
1272 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1273 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1275 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1279 void surf_network_model_init_Reno2(void)
1281 if (surf_network_model)
1284 set_update_mechanism();
1285 surf_network_model_init_internal();
1286 net_define_callbacks();
1288 xbt_dynar_push(model_list, &surf_network_model);
1289 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1291 network_solve = lagrange_solve;
1293 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1294 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1296 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1300 void surf_network_model_init_Vegas(void)
1302 if (surf_network_model)
1305 set_update_mechanism();
1306 surf_network_model_init_internal();
1307 net_define_callbacks();
1309 xbt_dynar_push(model_list, &surf_network_model);
1310 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1312 network_solve = lagrange_solve;
1314 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1315 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1317 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);