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 a = action;
71 GENERIC_LMM_ACTION(a).index_heap = i;
74 /* insert action on heap using a given key and a hat (heap_action_type)
75 * a hat can be of three types for communications:
77 * NORMAL = this is a normal heap entry stating the date to finish transmitting
78 * LATENCY = this is a heap entry to warn us when the latency is payed
79 * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
81 static void heap_insert(surf_action_network_CM02_t action, double key,
82 enum heap_action_type hat)
84 GENERIC_LMM_ACTION(action).hat = hat;
85 xbt_heap_push(net_action_heap, action, key);
88 static void heap_remove(surf_action_network_CM02_t action)
90 GENERIC_LMM_ACTION(action).hat = NOTSET;
91 if (GENERIC_LMM_ACTION(action).index_heap >= 0) {
92 xbt_heap_remove(net_action_heap, GENERIC_LMM_ACTION(action).index_heap);
96 /******************************************************************************/
97 /* Factors callbacks */
98 /******************************************************************************/
99 static double constant_latency_factor(double size)
101 return sg_latency_factor;
104 static double constant_bandwidth_factor(double size)
106 return sg_bandwidth_factor;
109 static double constant_bandwidth_constraint(double rate, double bound,
115 /**********************/
117 /**********************/
118 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
121 unsigned int iter = 0;
122 s_smpi_factor_t fact;
123 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
125 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
126 radical_elements = xbt_str_split(smpi_coef_string, ";");
127 xbt_dynar_foreach(radical_elements, iter, value) {
129 radical_elements2 = xbt_str_split(value, ":");
130 if (xbt_dynar_length(radical_elements2) != 2)
131 xbt_die("Malformed radical for smpi factor!");
132 fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
133 fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
134 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
135 XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
136 xbt_dynar_free(&radical_elements2);
138 xbt_dynar_free(&radical_elements);
142 static double smpi_bandwidth_factor(double size)
146 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/bw_factor"));
148 unsigned int iter = 0;
149 s_smpi_factor_t fact;
150 xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
151 if (size >= fact.factor) {
152 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
160 static double smpi_latency_factor(double size)
162 if (!smpi_lat_factor)
164 parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/lat_factor"));
166 unsigned int iter = 0;
167 s_smpi_factor_t fact;
168 xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
169 if (size >= fact.factor) {
170 XBT_DEBUG("%lf >= %ld return %f", size, fact.factor, fact.value);
178 /**--------- <copy/paste C code snippet in surf/network.c> -----------*/
180 static double smpi_bandwidth_constraint(double rate, double bound,
183 return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
186 static double (*latency_factor_callback) (double) =
187 &constant_latency_factor;
188 static double (*bandwidth_factor_callback) (double) =
189 &constant_bandwidth_factor;
190 static double (*bandwidth_constraint_callback) (double, double, double) =
191 &constant_bandwidth_constraint;
193 static void (*gap_append) (double, const link_CM02_t,
194 surf_action_network_CM02_t) = NULL;
195 static void (*gap_remove) (surf_action_network_CM02_t) = NULL;
197 static void *net_create_resource(const char *name,
199 tmgr_trace_t bw_trace,
201 tmgr_trace_t lat_trace,
202 e_surf_resource_state_t
204 tmgr_trace_t state_trace,
205 e_surf_link_sharing_policy_t
206 policy, xbt_dict_t properties)
208 link_CM02_t nw_link = (link_CM02_t)
209 surf_resource_lmm_new(sizeof(s_link_CM02_t),
210 surf_network_model, name, properties,
211 network_maxmin_system,
212 sg_bandwidth_factor * bw_initial,
214 state_initial, state_trace,
215 bw_initial, bw_trace);
217 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
218 "Link '%s' declared several times in the platform file.",
221 nw_link->lat_current = lat_initial;
224 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
226 if (policy == SURF_LINK_FATPIPE)
227 lmm_constraint_shared(nw_link->lmm_resource.constraint);
229 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
234 static void net_parse_link_init(sg_platf_link_cbarg_t link)
236 if (link->policy == SURF_LINK_FULLDUPLEX) {
238 link_id = bprintf("%s_UP", link->id);
239 net_create_resource(link_id,
241 link->bandwidth_trace,
245 link->state_trace, link->policy, link->properties);
247 link_id = bprintf("%s_DOWN", link->id);
248 net_create_resource(link_id,
250 link->bandwidth_trace,
254 link->state_trace, link->policy, link->properties);
257 net_create_resource(link->id,
259 link->bandwidth_trace,
263 link->state_trace, link->policy, link->properties);
267 static void net_add_traces(void)
269 xbt_dict_cursor_t cursor = NULL;
270 char *trace_name, *elm;
272 static int called = 0;
277 /* connect all traces relative to network */
278 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
279 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
280 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
282 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
285 "Cannot connect trace %s to link %s: trace undefined",
288 link->lmm_resource.state_event =
289 tmgr_history_add_trace(history, trace, 0.0, 0, link);
292 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
293 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
294 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
296 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
299 "Cannot connect trace %s to link %s: trace undefined",
302 link->lmm_resource.power.event =
303 tmgr_history_add_trace(history, trace, 0.0, 0, link);
306 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
307 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
308 link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
310 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
313 "Cannot connect trace %s to link %s: trace undefined",
316 link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
320 static void net_define_callbacks(void)
322 /* Figuring out the network links */
323 sg_platf_link_add_cb(net_parse_link_init);
324 sg_platf_postparse_add_cb(net_add_traces);
327 static int net_resource_used(void *resource_id)
329 return lmm_constraint_used(network_maxmin_system, ((surf_resource_lmm_t)
334 static int net_action_unref(surf_action_t action)
337 if (!action->refcount) {
338 xbt_swag_remove(action, action->state_set);
339 if (((surf_action_lmm_t)action)->variable) {
340 lmm_variable_free(network_maxmin_system,
341 ((surf_action_lmm_t) action)->variable);
343 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
344 heap_remove((surf_action_network_CM02_t) action);
345 xbt_swag_remove(action, net_modified_set);
348 xbt_free(((surf_action_network_CM02_t) action)->src_name);
349 xbt_free(((surf_action_network_CM02_t) action)->dst_name);
350 xbt_free(action->category);
352 surf_action_free(&action);
360 static void net_action_cancel(surf_action_t action)
362 XBT_DEBUG("cancel action %p", action);
363 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
364 if (network_update_mechanism == UM_LAZY) { // remove action from the heap
365 xbt_swag_remove(action, net_modified_set);
366 heap_remove((surf_action_network_CM02_t) action);
370 void net_action_recycle(surf_action_t action)
375 #ifdef HAVE_LATENCY_BOUND_TRACKING
376 int net_get_link_latency_limited(surf_action_t action)
378 return action->latency_limited;
382 double net_action_get_remains(surf_action_t action)
384 if (network_update_mechanism == UM_LAZY) /* update remains before return it */
385 update_action_remaining_lazy(surf_get_clock());
386 return action->remains;
389 static void update_action_remaining_lazy(double now)
391 surf_action_network_CM02_t action = NULL;
394 xbt_swag_foreach(action, net_modified_set) {
396 if (GENERIC_LMM_ACTION(action).suspended != 0) {
400 delta = now - GENERIC_LMM_ACTION(action).last_update;
402 double_update(&(((surf_action_t)action)->remains),
403 lmm_variable_getvalue(((surf_action_lmm_t) action)->variable) * delta);
405 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
406 double_update(&(((surf_action_t)action)->max_duration), delta);
408 if ((((surf_action_t)action)->remains <= 0) &&
409 (lmm_get_variable_weight(((surf_action_lmm_t)action)->variable) > 0)) {
410 ((surf_action_t)action)->finish = surf_get_clock();
411 surf_network_model->action_state_set((surf_action_t) action,
414 } else if (((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
415 && (((surf_action_t)action)->max_duration <= 0))) {
416 ((surf_action_t)action)->finish = surf_get_clock();
417 surf_network_model->action_state_set((surf_action_t) action,
422 GENERIC_LMM_ACTION(action).last_update = now;
426 static double net_share_resources_full(double now)
428 s_surf_action_lmm_t s_action;
429 surf_action_network_CM02_t action = NULL;
430 xbt_swag_t running_actions =
431 surf_network_model->states.running_action_set;
434 min = generic_maxmin_share_resources(running_actions,
435 xbt_swag_offset(s_action,
437 network_maxmin_system,
440 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable) )))
442 xbt_swag_foreach(action, running_actions) {
443 #ifdef HAVE_LATENCY_BOUND_TRACKING
444 if (lmm_is_variable_limited_by_latency(action->variable)) {
445 GENERIC_LMM_ACTION(action).latency_limited = 1;
447 GENERIC_LMM_ACTION(action).latency_limited = 0;
450 if (action->latency > 0) {
451 min = (min < 0) ? action->latency : min(min, action->latency);
455 XBT_DEBUG("Min of share resources %f", min);
460 static double net_share_resources_lazy(double now)
462 surf_action_network_CM02_t action = NULL;
467 ("Before share resources, the size of modified actions set is %d",
468 xbt_swag_size(net_modified_set));
469 update_action_remaining_lazy(now);
471 lmm_solve(network_maxmin_system);
474 ("After share resources, The size of modified actions set is %d",
475 xbt_swag_size(net_modified_set));
477 xbt_swag_foreach(action, net_modified_set) {
478 int max_dur_flag = 0;
480 if (GENERIC_ACTION(action).state_set !=
481 surf_network_model->states.running_action_set)
484 /* bogus priority, skip it */
485 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);
515 heap_insert(action, min, max_dur_flag ? MAX_DURATION : NORMAL);
516 XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min,
518 } else DIE_IMPOSSIBLE;
521 //hereafter must have already the min value for this resource model
522 if (xbt_heap_size(net_action_heap) > 0)
523 min = xbt_heap_maxkey(net_action_heap) - now;
527 XBT_DEBUG("The minimum with the HEAP %lf", min);
532 static void net_update_actions_state_full(double now, double delta)
535 surf_action_network_CM02_t action = NULL;
536 surf_action_network_CM02_t next_action = NULL;
537 xbt_swag_t running_actions =
538 surf_network_model->states.running_action_set;
540 xbt_swag_t failed_actions =
541 surf_network_model->states.failed_action_set;
544 xbt_swag_foreach_safe(action, next_action, running_actions) {
546 if (action->latency > 0) {
547 if (action->latency > deltap) {
548 double_update(&(action->latency), deltap);
551 double_update(&(deltap), action->latency);
552 action->latency = 0.0;
554 if ((action->latency == 0.0) && !(GENERIC_LMM_ACTION(action).suspended))
555 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
559 if (TRACE_is_enabled()) {
560 xbt_dynar_t route = NULL;
561 routing_get_route_and_latency(action->src_name, action->dst_name,
565 xbt_dynar_foreach(route, i, link) {
566 TRACE_surf_link_set_utilization(link->lmm_resource.
567 generic_resource.name,
568 GENERIC_ACTION(action).data,
569 (surf_action_t) action,
570 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
575 if (!lmm_get_number_of_cnst_from_var
576 (network_maxmin_system, GENERIC_LMM_ACTION(action).variable)) {
577 /* There is actually no link used, hence an infinite bandwidth.
578 * This happens often when using models like vivaldi.
579 * In such case, just make sure that the action completes immediately.
581 double_update(&(GENERIC_ACTION(action).remains),
582 GENERIC_ACTION(action).remains);
584 double_update(&(GENERIC_ACTION(action).remains),
585 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * deltap);
586 if (((surf_action_t)action)->max_duration != NO_MAX_DURATION)
587 double_update(&(((surf_action_t)action)->max_duration), delta);
589 if ((GENERIC_ACTION(action).remains <= 0) &&
590 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
591 ((surf_action_t)action)->finish = surf_get_clock();
592 surf_network_model->action_state_set((surf_action_t) action,
597 } else if ((((surf_action_t)action)->max_duration != NO_MAX_DURATION)
598 && (((surf_action_t)action)->max_duration <= 0)) {
599 ((surf_action_t)action)->finish = surf_get_clock();
600 surf_network_model->action_state_set((surf_action_t) action,
610 static void net_update_actions_state_lazy(double now, double delta)
612 surf_action_network_CM02_t action = NULL;
614 while ((xbt_heap_size(net_action_heap) > 0)
615 && (double_equals(xbt_heap_maxkey(net_action_heap), now))) {
616 action = xbt_heap_pop(net_action_heap);
617 XBT_DEBUG("Action %p: finish", action);
618 GENERIC_ACTION(action).finish = surf_get_clock();
620 // if I am wearing a latency hat
621 if (GENERIC_LMM_ACTION(action).hat == LATENCY) {
622 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
625 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
627 // if I am wearing a max_duration or normal hat
628 } else if (GENERIC_LMM_ACTION(action).hat == MAX_DURATION ||
629 GENERIC_LMM_ACTION(action).hat == NORMAL) {
630 // no need to communicate anymore
631 // assume that flows that reached max_duration have remaining of 0
632 GENERIC_ACTION(action).remains = 0;
633 ((surf_action_t)action)->finish = surf_get_clock();
634 surf_network_model->action_state_set((surf_action_t) action,
642 static void net_update_resource_state(void *id,
643 tmgr_trace_event_t event_type,
644 double value, double date)
646 link_CM02_t nw_link = id;
647 /* printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
648 /* "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
649 /* value, event_type); */
651 if (event_type == nw_link->lmm_resource.power.event) {
653 sg_weight_S_parameter / value - sg_weight_S_parameter /
654 (nw_link->lmm_resource.power.peak *
655 nw_link->lmm_resource.power.scale);
656 lmm_variable_t var = NULL;
657 lmm_element_t elem = NULL;
658 surf_action_network_CM02_t action = NULL;
660 nw_link->lmm_resource.power.peak = value;
661 lmm_update_constraint_bound(network_maxmin_system,
662 nw_link->lmm_resource.constraint,
663 sg_bandwidth_factor *
664 (nw_link->lmm_resource.power.peak *
665 nw_link->lmm_resource.power.scale));
667 TRACE_surf_link_set_bandwidth(date,
669 *) (((nw_link->lmm_resource).
670 generic_resource).name),
671 sg_bandwidth_factor *
672 (nw_link->lmm_resource.power.peak *
673 nw_link->lmm_resource.power.scale));
675 if (sg_weight_S_parameter > 0) {
676 while ((var = lmm_get_var_from_cnst
677 (network_maxmin_system, nw_link->lmm_resource.constraint,
679 action = lmm_variable_id(var);
680 action->weight += delta;
681 if (!(GENERIC_LMM_ACTION(action).suspended))
682 lmm_update_variable_weight(network_maxmin_system,
683 GENERIC_LMM_ACTION(action).variable, action->weight);
686 if (tmgr_trace_event_free(event_type))
687 nw_link->lmm_resource.power.event = NULL;
688 } else if (event_type == nw_link->lat_event) {
689 double delta = value - nw_link->lat_current;
690 lmm_variable_t var = NULL;
691 lmm_element_t elem = NULL;
692 surf_action_network_CM02_t action = NULL;
694 nw_link->lat_current = value;
695 while ((var = lmm_get_var_from_cnst
696 (network_maxmin_system, nw_link->lmm_resource.constraint,
698 action = lmm_variable_id(var);
699 action->lat_current += delta;
700 action->weight += delta;
701 if (action->rate < 0)
702 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
703 sg_tcp_gamma / (2.0 *
704 action->lat_current));
706 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
708 sg_tcp_gamma / (2.0 *
712 if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
713 XBT_INFO("Flow is limited BYBANDWIDTH");
715 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
716 action->lat_current);
719 if (!(GENERIC_LMM_ACTION(action).suspended))
720 lmm_update_variable_weight(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
724 if (tmgr_trace_event_free(event_type))
725 nw_link->lat_event = NULL;
726 } else if (event_type == nw_link->lmm_resource.state_event) {
728 nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
730 lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
731 lmm_variable_t var = NULL;
732 lmm_element_t elem = NULL;
734 nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
735 while ((var = lmm_get_var_from_cnst
736 (network_maxmin_system, cnst, &elem))) {
737 surf_action_t action = lmm_variable_id(var);
739 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
740 surf_action_state_get(action) == SURF_ACTION_READY) {
741 action->finish = date;
742 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
746 if (tmgr_trace_event_free(event_type))
747 nw_link->lmm_resource.state_event = NULL;
749 XBT_CRITICAL("Unknown event ! \n");
754 ("There were a resource state event, need to update actions related to the constraint (%p)",
755 nw_link->lmm_resource.constraint);
760 static surf_action_t net_communicate(const char *src_name,
761 const char *dst_name, double size,
767 surf_action_network_CM02_t action = NULL;
768 double bandwidth_bound;
769 double latency = 0.0;
770 xbt_dynar_t back_route = NULL;
771 int constraints_per_variable = 0;
773 xbt_dynar_t route = xbt_dynar_new(global_routing->size_of_link, NULL);
775 XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
777 routing_get_route_and_latency(src_name, dst_name, &route, &latency);
778 xbt_assert(!xbt_dynar_is_empty(route) || latency,
779 "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
782 xbt_dynar_foreach(route, i, link) {
783 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
788 if (sg_network_crosstraffic == 1) {
789 routing_get_route_and_latency(dst_name, src_name, &back_route, NULL);
790 xbt_dynar_foreach(back_route, i, link) {
791 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
799 surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
800 surf_network_model, failed);
801 #ifdef HAVE_LATENCY_BOUND_TRACKING
802 (GENERIC_LMM_ACTION(action)).latency_limited = 0;
804 action->weight = action->latency = latency;
806 xbt_swag_insert(action, ((surf_action_t)action)->state_set);
808 if (network_update_mechanism == UM_LAZY) {
809 GENERIC_LMM_ACTION(action).index_heap = -1;
810 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
813 bandwidth_bound = -1.0;
814 if (sg_weight_S_parameter > 0) {
815 xbt_dynar_foreach(route, i, link) {
817 sg_weight_S_parameter /
818 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
821 xbt_dynar_foreach(route, i, link) {
822 double bb = bandwidth_factor_callback(size) *
823 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
825 (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
828 action->lat_current = action->latency;
829 action->latency *= latency_factor_callback(size);
831 bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
833 xbt_assert(!xbt_dynar_is_empty(route),
834 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
836 link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
837 gap_append(size, link, action);
838 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
839 action, src_name, dst_name, action->sender.gap,
843 constraints_per_variable = xbt_dynar_length(route);
844 if (back_route != NULL)
845 constraints_per_variable += xbt_dynar_length(back_route);
847 if (action->latency > 0) {
848 GENERIC_LMM_ACTION(action).variable =
849 lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
850 constraints_per_variable);
851 if (network_update_mechanism == UM_LAZY) {
852 // add to the heap the event when the latency is payed
853 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
854 action->latency + GENERIC_LMM_ACTION(action).last_update);
855 heap_insert(action, action->latency + GENERIC_LMM_ACTION(action).last_update,
856 xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
859 GENERIC_LMM_ACTION(action).variable =
860 lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
861 constraints_per_variable);
863 if (action->rate < 0) {
864 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
865 (action->lat_current > 0) ?
866 sg_tcp_gamma / (2.0 *
867 action->lat_current) : -1.0);
869 lmm_update_variable_bound(network_maxmin_system, GENERIC_LMM_ACTION(action).variable,
870 (action->lat_current > 0) ?
872 sg_tcp_gamma / (2.0 *
873 action->lat_current))
877 xbt_dynar_foreach(route, i, link) {
878 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
879 GENERIC_LMM_ACTION(action).variable, 1.0);
882 if (sg_network_crosstraffic == 1) {
883 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
884 xbt_dynar_foreach(back_route, i, link) {
885 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
886 GENERIC_LMM_ACTION(action).variable, .05);
890 if (TRACE_is_enabled()) {
891 action->src_name = xbt_strdup(src_name);
892 action->dst_name = xbt_strdup(dst_name);
894 action->src_name = action->dst_name = NULL;
898 xbt_dynar_free(&route);
901 return (surf_action_t) action;
904 static xbt_dynar_t net_get_route(const char *src, const char *dst)
906 xbt_dynar_t route = NULL;
907 routing_get_route_and_latency(src, dst, &route, NULL);
911 static double net_get_link_bandwidth(const void *link)
913 surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
914 return lmm->power.peak * lmm->power.scale;
917 static double net_get_link_latency(const void *link)
919 return ((link_CM02_t) link)->lat_current;
922 static int net_link_shared(const void *link)
925 lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
928 static void net_action_suspend(surf_action_t action)
930 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 1;
931 lmm_update_variable_weight(network_maxmin_system,
932 ((surf_action_network_CM02_t)
933 action)->generic_lmm_action.variable, 0.0);
935 if (network_update_mechanism == UM_LAZY) // remove action from the heap
936 heap_remove((surf_action_network_CM02_t) action);
939 static void net_action_resume(surf_action_t action)
941 if (((surf_action_network_CM02_t) action)->generic_lmm_action.suspended) {
942 lmm_update_variable_weight(network_maxmin_system,
943 ((surf_action_network_CM02_t)
944 action)->generic_lmm_action.variable,
945 ((surf_action_network_CM02_t)
947 ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended = 0;
948 if (network_update_mechanism == UM_LAZY) // remove action from the heap
949 heap_remove((surf_action_network_CM02_t) action);
953 static int net_action_is_suspended(surf_action_t action)
955 return ((surf_action_network_CM02_t) action)->generic_lmm_action.suspended;
958 void net_action_set_max_duration(surf_action_t action, double duration)
960 action->max_duration = duration;
961 if (network_update_mechanism == UM_LAZY) // remove action from the heap
962 heap_remove((surf_action_network_CM02_t) action);
966 static void net_action_set_category(surf_action_t action,
967 const char *category)
969 action->category = xbt_strdup(category);
973 static void net_finalize(void)
975 surf_model_exit(surf_network_model);
976 surf_network_model = NULL;
978 lmm_system_free(network_maxmin_system);
979 network_maxmin_system = NULL;
981 if (network_update_mechanism == UM_LAZY) {
982 xbt_heap_free(net_action_heap);
983 xbt_swag_free(net_modified_set);
987 xbt_dynar_free(&smpi_bw_factor);
989 xbt_dynar_free(&smpi_lat_factor);
992 static void smpi_gap_append(double size, const link_CM02_t link,
993 surf_action_network_CM02_t action)
995 const char *src = link->lmm_resource.generic_resource.name;
997 surf_action_network_CM02_t last_action;
1000 if (sg_sender_gap > 0.0) {
1002 gap_lookup = xbt_dict_new();
1004 fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
1005 action->sender.gap = 0.0;
1006 if (fifo && xbt_fifo_size(fifo) > 0) {
1007 /* Compute gap from last send */
1009 (surf_action_network_CM02_t)
1010 xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
1011 bw = net_get_link_bandwidth(link);
1012 action->sender.gap =
1013 last_action->sender.gap + max(sg_sender_gap,
1014 last_action->sender.size / bw);
1015 action->latency += action->sender.gap;
1017 /* Append action as last send */
1018 action->sender.link_name = link->lmm_resource.generic_resource.name;
1020 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1021 action->sender.link_name);
1023 fifo = xbt_fifo_new();
1024 xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1026 action->sender.fifo_item = xbt_fifo_push(fifo, action);
1027 action->sender.size = size;
1031 static void smpi_gap_remove(surf_action_network_CM02_t action)
1036 if (sg_sender_gap > 0.0 && action->sender.link_name
1037 && action->sender.fifo_item) {
1039 (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
1040 action->sender.link_name);
1041 xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1042 size = xbt_fifo_size(fifo);
1044 xbt_fifo_free(fifo);
1045 xbt_dict_remove(gap_lookup, action->sender.link_name);
1046 size = xbt_dict_length(gap_lookup);
1048 xbt_dict_free(&gap_lookup);
1054 static void surf_network_model_init_internal(void)
1056 s_surf_action_network_CM02_t comm;
1057 surf_network_model = surf_model_init();
1059 surf_network_model->name = "network";
1060 surf_network_model->action_unref = net_action_unref;
1061 surf_network_model->action_cancel = net_action_cancel;
1062 surf_network_model->action_recycle = net_action_recycle;
1063 surf_network_model->get_remains = net_action_get_remains;
1064 #ifdef HAVE_LATENCY_BOUND_TRACKING
1065 surf_network_model->get_latency_limited = net_get_link_latency_limited;
1068 surf_network_model->set_category = net_action_set_category;
1071 surf_network_model->model_private->resource_used = net_resource_used;
1072 if (network_update_mechanism == UM_LAZY) {
1073 surf_network_model->model_private->share_resources =
1074 net_share_resources_lazy;
1075 surf_network_model->model_private->update_actions_state =
1076 net_update_actions_state_lazy;
1077 } else if (network_update_mechanism == UM_FULL) {
1078 surf_network_model->model_private->share_resources =
1079 net_share_resources_full;
1080 surf_network_model->model_private->update_actions_state =
1081 net_update_actions_state_full;
1084 surf_network_model->model_private->update_resource_state =
1085 net_update_resource_state;
1086 surf_network_model->model_private->finalize = net_finalize;
1088 surf_network_model->suspend = net_action_suspend;
1089 surf_network_model->resume = net_action_resume;
1090 surf_network_model->is_suspended = net_action_is_suspended;
1091 surf_cpu_model->set_max_duration = net_action_set_max_duration;
1093 surf_network_model->extension.network.communicate = net_communicate;
1094 surf_network_model->extension.network.get_route = net_get_route;
1095 surf_network_model->extension.network.get_link_bandwidth =
1096 net_get_link_bandwidth;
1097 surf_network_model->extension.network.get_link_latency =
1098 net_get_link_latency;
1099 surf_network_model->extension.network.link_shared = net_link_shared;
1100 surf_network_model->extension.network.add_traces = net_add_traces;
1101 surf_network_model->extension.network.create_resource =
1102 net_create_resource;
1104 if (!network_maxmin_system)
1105 network_maxmin_system = lmm_system_new(net_selective_update);
1107 routing_model_create(sizeof(link_CM02_t),
1108 net_create_resource("__loopback__",
1109 498000000, NULL, 0.000015, NULL,
1110 SURF_RESOURCE_ON, NULL,
1111 SURF_LINK_FATPIPE, NULL));
1113 if (network_update_mechanism == UM_LAZY) {
1114 net_action_heap = xbt_heap_new(8, NULL);
1115 xbt_heap_set_update_callback(net_action_heap,
1116 net_action_update_index_heap);
1118 xbt_swag_new(xbt_swag_offset(comm, generic_lmm_action.action_list_hookup));
1119 network_maxmin_system->keep_track = net_modified_set;
1123 static void set_update_mechanism(void)
1126 TRACE_set_network_update_mechanism();
1129 char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1131 xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
1133 if (!strcmp(optim, "Full")) {
1134 network_update_mechanism = UM_FULL;
1135 net_selective_update = select;
1136 } else if (!strcmp(optim, "Lazy")) {
1137 network_update_mechanism = UM_LAZY;
1138 net_selective_update = 1;
1139 xbt_assert((select == 1)
1141 (xbt_cfg_is_default_value
1142 (_surf_cfg_set, "network/maxmin_selective_update")),
1143 "Disabling selective update while using the lazy update mechanism is dumb!");
1145 xbt_die("Unsupported optimization (%s) for this model", optim);
1149 /************************************************************************/
1150 /* New model based on LV08 and experimental results of MPI ping-pongs */
1151 /************************************************************************/
1152 /* @Inproceedings{smpi_ipdps, */
1153 /* author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
1154 /* title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
1155 /* booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
1156 /* address={Anchorage (Alaska) USA}, */
1160 void surf_network_model_init_SMPI(void)
1163 if (surf_network_model)
1165 set_update_mechanism();
1167 surf_network_model_init_internal();
1168 latency_factor_callback = &smpi_latency_factor;
1169 bandwidth_factor_callback = &smpi_bandwidth_factor;
1170 bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1171 gap_append = &smpi_gap_append;
1172 gap_remove = &smpi_gap_remove;
1173 net_define_callbacks();
1174 xbt_dynar_push(model_list, &surf_network_model);
1175 network_solve = lmm_solve;
1177 xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1178 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1181 /************************************************************************/
1182 /* New model based on optimizations discussed during Pedro Velho's thesis*/
1183 /************************************************************************/
1184 /* @techreport{VELHO:2011:HAL-00646896:1, */
1185 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
1186 /* title = {{Flow-level network models: have we reached the limits?}}, */
1187 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
1188 /* type = {Rapport de recherche}, */
1189 /* institution = {INRIA}, */
1190 /* number = {RR-7821}, */
1191 /* year = {2011}, */
1193 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
1195 void surf_network_model_init_LegrandVelho(void)
1197 if (surf_network_model)
1200 set_update_mechanism();
1202 surf_network_model_init_internal();
1203 net_define_callbacks();
1204 xbt_dynar_push(model_list, &surf_network_model);
1205 network_solve = lmm_solve;
1207 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor",
1209 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1211 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 20537);
1214 /***************************************************************************/
1215 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1216 /***************************************************************************/
1217 /* @TechReport{ rr-lip2002-40, */
1218 /* author = {Henri Casanova and Loris Marchal}, */
1219 /* institution = {LIP}, */
1220 /* title = {A Network Model for Simulation of Grid Application}, */
1221 /* number = {2002-40}, */
1222 /* month = {oct}, */
1225 void surf_network_model_init_CM02(void)
1228 if (surf_network_model)
1231 set_update_mechanism();
1232 surf_network_model_init_internal();
1233 net_define_callbacks();
1234 xbt_dynar_push(model_list, &surf_network_model);
1235 network_solve = lmm_solve;
1237 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1238 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1240 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1243 /***************************************************************************/
1244 /* The models from Steven H. Low */
1245 /***************************************************************************/
1246 /* @article{Low03, */
1247 /* author={Steven H. Low}, */
1248 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
1250 /* journal={{IEEE/ACM} Transactions on Networking}, */
1251 /* volume={11}, number={4}, */
1253 void surf_network_model_init_Reno(void)
1255 if (surf_network_model)
1258 set_update_mechanism();
1259 surf_network_model_init_internal();
1260 net_define_callbacks();
1262 xbt_dynar_push(model_list, &surf_network_model);
1263 lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1265 network_solve = lagrange_solve;
1267 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1268 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1270 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1274 void surf_network_model_init_Reno2(void)
1276 if (surf_network_model)
1279 set_update_mechanism();
1280 surf_network_model_init_internal();
1281 net_define_callbacks();
1283 xbt_dynar_push(model_list, &surf_network_model);
1284 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1286 network_solve = lagrange_solve;
1288 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1289 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1291 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1295 void surf_network_model_init_Vegas(void)
1297 if (surf_network_model)
1300 set_update_mechanism();
1301 surf_network_model_init_internal();
1302 net_define_callbacks();
1304 xbt_dynar_push(model_list, &surf_network_model);
1305 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1307 network_solve = lagrange_solve;
1309 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1310 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1312 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);