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"
27 #define GENERIC_ACTION(action) action->generic_action
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
31 "Logging specific to the SURF network module");
33 surf_model_t surf_network_model = NULL;
34 static lmm_system_t network_maxmin_system = NULL;
35 static void (*network_solve) (lmm_system_t) = NULL;
37 double sg_sender_gap = 0.0;
38 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
39 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
40 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
42 double sg_tcp_gamma = 0.0;
43 int sg_network_crosstraffic = 0;
45 xbt_dict_t gap_lookup = NULL;
47 e_UM_t network_update_mechanism = UM_UNDEFINED;
48 static int selective_update = 0;
50 static int net_action_is_suspended(surf_action_t action);
51 static void update_action_remaining(double now);
53 static xbt_swag_t net_modified_set = NULL;
54 static xbt_heap_t net_action_heap = NULL;
55 xbt_swag_t keep_track = NULL;
57 /* added to manage the communication action's heap */
58 static void net_action_update_index_heap(void *action, int i)
60 ((surf_action_network_CM02_t) action)->index_heap = i;
63 /* insert action on heap using a given key and a hat (heap_action_type)
64 * a hat can be of three types for communications:
66 * NORMAL = this is a normal heap entry stating the date to finish transmitting
67 * LATENCY = this is a heap entry to warn us when the latency is payed
68 * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
70 static void heap_insert(surf_action_network_CM02_t action, double key, enum heap_action_type hat){
72 xbt_heap_push(net_action_heap, action, key);
75 static void heap_remove(surf_action_network_CM02_t action){
77 if(((surf_action_network_CM02_t) action)->index_heap >= 0){
78 xbt_heap_remove(net_action_heap,action->index_heap);
82 /******************************************************************************/
83 /* Factors callbacks */
84 /******************************************************************************/
85 static double constant_latency_factor(double size)
87 return sg_latency_factor;
90 static double constant_bandwidth_factor(double size)
92 return sg_bandwidth_factor;
95 static double constant_bandwidth_constraint(double rate, double bound,
102 /**********************/
104 /**********************/
105 static double smpi_latency_factor(double size)
107 /* 1 B <= size <= 1 KiB */
108 if (size <= 1024.0) {
112 /* 2 KiB <= size <= 32 KiB */
113 if (size <= 32768.0) {
117 /* 64 KiB <= size <= 4 MiB */
121 static double smpi_bandwidth_factor(double size)
123 /* 1 B <= size <= 1 KiB */
124 if (size <= 1024.0) {
128 /* 2 KiB <= size <= 32 KiB */
129 if (size <= 32768.0) {
133 /* 64 KiB <= size <= 4 MiB */
137 static double smpi_bandwidth_constraint(double rate, double bound,
140 return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
143 static double (*latency_factor_callback) (double) =
144 &constant_latency_factor;
145 static double (*bandwidth_factor_callback) (double) =
146 &constant_bandwidth_factor;
147 static double (*bandwidth_constraint_callback) (double, double, double) =
148 &constant_bandwidth_constraint;
150 static void (*gap_append) (double, const link_CM02_t, surf_action_network_CM02_t) = NULL;
151 static void (*gap_remove) (surf_action_network_CM02_t) = NULL;
153 static void* net_create_resource(const char *name,
155 tmgr_trace_t bw_trace,
157 tmgr_trace_t lat_trace,
158 e_surf_resource_state_t
160 tmgr_trace_t state_trace,
161 e_surf_link_sharing_policy_t
162 policy, xbt_dict_t properties)
164 link_CM02_t nw_link = (link_CM02_t)
165 surf_resource_lmm_new(sizeof(s_link_CM02_t),
166 surf_network_model, name, properties,
167 network_maxmin_system,
168 sg_bandwidth_factor * bw_initial,
170 state_initial, state_trace,
171 bw_initial, bw_trace);
173 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
174 "Link '%s' declared several times in the platform file.",
177 nw_link->lat_current = lat_initial;
180 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
182 if (policy == SURF_LINK_FATPIPE)
183 lmm_constraint_shared(nw_link->lmm_resource.constraint);
185 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
190 static void net_parse_link_init(sg_platf_link_cbarg_t link)
192 if(link->policy == SURF_LINK_FULLDUPLEX){
194 link_id = bprintf("%s_UP", link->id);
195 net_create_resource(link_id,
197 link->bandwidth_trace,
205 link_id = bprintf("%s_DOWN", link->id);
206 net_create_resource(link_id,
208 link->bandwidth_trace,
218 net_create_resource(link->id,
220 link->bandwidth_trace,
230 static void net_add_traces(void)
232 xbt_dict_cursor_t cursor = NULL;
233 char *trace_name, *elm;
235 static int called = 0;
240 /* connect all traces relative to network */
241 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
242 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
244 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
246 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
249 "Cannot connect trace %s to link %s: trace undefined",
252 link->lmm_resource.state_event =
253 tmgr_history_add_trace(history, trace, 0.0, 0, link);
256 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
257 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
259 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
261 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
264 "Cannot connect trace %s to link %s: trace undefined",
267 link->lmm_resource.power.event =
268 tmgr_history_add_trace(history, trace, 0.0, 0, link);
271 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
272 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
274 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
276 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
279 "Cannot connect trace %s to link %s: trace undefined",
282 link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
286 static void net_define_callbacks(void)
288 /* Figuring out the network links */
289 sg_platf_link_add_cb(net_parse_link_init);
290 sg_platf_postparse_add_cb(net_add_traces);
293 static int net_resource_used(void *resource_id)
295 return lmm_constraint_used(network_maxmin_system,
296 ((surf_resource_lmm_t)
297 resource_id)->constraint);
300 static int net_action_unref(surf_action_t action)
303 if (!action->refcount) {
304 xbt_swag_remove(action, action->state_set);
305 if (((surf_action_network_CM02_t) action)->variable){
306 lmm_variable_free(network_maxmin_system,
307 ((surf_action_network_CM02_t) action)->variable);
309 if(network_update_mechanism == UM_LAZY){// remove action from the heap
310 heap_remove((surf_action_network_CM02_t) action);
311 xbt_swag_remove(action, net_modified_set);
314 xbt_free(((surf_action_network_CM02_t) action)->src_name);
315 xbt_free(((surf_action_network_CM02_t) action)->dst_name);
316 xbt_free(action->category);
318 surf_action_free(&action);
326 static void net_action_cancel(surf_action_t action)
328 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
329 if(network_update_mechanism == UM_LAZY){// remove action from the heap
330 xbt_swag_remove(action, net_modified_set);
331 heap_remove((surf_action_network_CM02_t) action);
335 void net_action_recycle(surf_action_t action)
340 #ifdef HAVE_LATENCY_BOUND_TRACKING
341 static int net_get_link_latency_limited(surf_action_t action)
343 return action->latency_limited;
347 double net_action_get_remains(surf_action_t action)
349 if(network_update_mechanism == UM_LAZY)/* update remains before return it */
350 update_action_remaining(surf_get_clock());
351 return action->remains;
354 static void update_action_remaining(double now){
355 surf_action_network_CM02_t action = NULL;
358 xbt_swag_foreach(action, net_modified_set) {
360 if(action->suspended != 0){
364 delta = now - action->last_update;
366 double_update(&(action->generic_action.remains),
367 lmm_variable_getvalue(action->variable) * delta);
369 if (action->generic_action.max_duration != NO_MAX_DURATION)
370 double_update(&(action->generic_action.max_duration), delta);
372 if ((action->generic_action.remains <= 0) &&
373 (lmm_get_variable_weight(action->variable) > 0)) {
374 action->generic_action.finish = surf_get_clock();
375 surf_network_model->action_state_set((surf_action_t) action,
378 } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
379 && (action->generic_action.max_duration <= 0)) {
380 action->generic_action.finish = surf_get_clock();
381 surf_network_model->action_state_set((surf_action_t) action,
386 action->last_update = now;
390 static double net_share_resources_full(double now)
392 s_surf_action_network_CM02_t s_action;
393 surf_action_network_CM02_t action = NULL;
394 xbt_swag_t running_actions =
395 surf_network_model->states.running_action_set;
398 min = generic_maxmin_share_resources(running_actions,
399 xbt_swag_offset(s_action,
401 network_maxmin_system,
404 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable) )))
406 xbt_swag_foreach(action, running_actions) {
407 #ifdef HAVE_LATENCY_BOUND_TRACKING
408 if (lmm_is_variable_limited_by_latency(action->variable)) {
409 (action->generic_action).latency_limited = 1;
411 (action->generic_action).latency_limited = 0;
414 if (action->latency > 0) {
415 min = (min<0)?action->latency:min(min,action->latency);
419 XBT_DEBUG("Min of share resources %f", min);
424 static double net_share_resources_lazy(double now)
426 surf_action_network_CM02_t action = NULL;
430 XBT_DEBUG("Before share resources, the size of modified actions set is %d", xbt_swag_size(net_modified_set));
431 update_action_remaining(now);
433 keep_track = net_modified_set;
434 lmm_solve(network_maxmin_system);
437 XBT_DEBUG("After share resources, The size of modified actions set is %d", xbt_swag_size(net_modified_set));
439 xbt_swag_foreach(action, net_modified_set) {
440 int max_dur_flag = 0;
442 if (GENERIC_ACTION(action).state_set != surf_network_model->states.running_action_set){
446 /* bogus priority, skip it */
447 if (GENERIC_ACTION(action).priority <= 0){
452 value = lmm_variable_getvalue(action->variable);
454 if (GENERIC_ACTION(action).remains > 0) {
455 value = GENERIC_ACTION(action).remains / value;
463 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
465 || GENERIC_ACTION(action).start +
466 GENERIC_ACTION(action).max_duration < min)){
467 min = GENERIC_ACTION(action).start +
468 GENERIC_ACTION(action).max_duration;
472 XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
473 GENERIC_ACTION(action).start, now + value,
474 GENERIC_ACTION(action).max_duration);
476 if (action->index_heap >= 0) {
477 heap_remove((surf_action_network_CM02_t) action);
481 heap_insert((surf_action_network_CM02_t) action, min, max_dur_flag?MAX_DURATION:NORMAL);
482 XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min, now);
486 //hereafter must have already the min value for this resource model
487 if(xbt_heap_size(net_action_heap) > 0 ){
488 min = xbt_heap_maxkey(net_action_heap) - now ;
493 XBT_DEBUG("The minimum with the HEAP %lf", min);
498 static void net_update_actions_state_full(double now, double delta)
501 surf_action_network_CM02_t action = NULL;
502 surf_action_network_CM02_t next_action = NULL;
503 xbt_swag_t running_actions =
504 surf_network_model->states.running_action_set;
506 xbt_swag_t failed_actions =
507 surf_network_model->states.failed_action_set;
510 xbt_swag_foreach_safe(action, next_action, running_actions) {
512 if (action->latency > 0) {
513 if (action->latency > deltap) {
514 double_update(&(action->latency), deltap);
517 double_update(&(deltap), action->latency);
518 action->latency = 0.0;
520 if ((action->latency == 0.0) && !(action->suspended))
521 lmm_update_variable_weight(network_maxmin_system, action->variable,
525 if (TRACE_is_enabled()) {
526 xbt_dynar_t route=NULL;
527 routing_get_route_and_latency(action->src_name, action->dst_name,&route,NULL);
530 xbt_dynar_foreach(route, i, link) {
531 TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
532 action->generic_action.data,
533 (surf_action_t) action,
534 lmm_variable_getvalue
535 (action->variable), now - delta,
540 if(!lmm_get_number_of_cnst_from_var(network_maxmin_system, action->variable)) {
541 /* There is actually no link used, hence an infinite bandwidth.
542 * This happens often when using models like vivaldi.
543 * In such case, just make sure that the action completes immediately.
545 double_update(&(action->generic_action.remains),
546 action->generic_action.remains);
548 double_update(&(action->generic_action.remains),
549 lmm_variable_getvalue(action->variable) * deltap);
550 if (action->generic_action.max_duration != NO_MAX_DURATION)
551 double_update(&(action->generic_action.max_duration), delta);
553 if ((action->generic_action.remains <= 0) &&
554 (lmm_get_variable_weight(action->variable) > 0)) {
555 action->generic_action.finish = surf_get_clock();
556 surf_network_model->action_state_set((surf_action_t) action,
559 if(gap_remove) gap_remove(action);
560 } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
561 && (action->generic_action.max_duration <= 0)) {
562 action->generic_action.finish = surf_get_clock();
563 surf_network_model->action_state_set((surf_action_t) action,
565 if(gap_remove) gap_remove(action);
572 static void net_update_actions_state_lazy(double now, double delta)
574 surf_action_network_CM02_t action = NULL;
576 while ((xbt_heap_size(net_action_heap) > 0)
577 && (double_equals(xbt_heap_maxkey(net_action_heap), now))) {
578 action = xbt_heap_pop(net_action_heap);
579 XBT_DEBUG("Action %p: finish", action);
580 GENERIC_ACTION(action).finish = surf_get_clock();
582 // if I am wearing a latency heat
583 if( action->hat == LATENCY){
584 lmm_update_variable_weight(network_maxmin_system, action->variable,
587 action->last_update = surf_get_clock();
589 // if I am wearing a max_duration or normal hat
590 }else if( action->hat == MAX_DURATION || action->hat == NORMAL ){
591 // no need to communicate anymore
592 // assume that flows that reached max_duration have remaining of 0
593 GENERIC_ACTION(action).remains = 0;
594 action->generic_action.finish = surf_get_clock();
595 surf_network_model->action_state_set((surf_action_t) action,
603 static void net_update_resource_state(void *id,
604 tmgr_trace_event_t event_type,
605 double value, double date)
607 link_CM02_t nw_link = id;
608 /* printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
609 /* "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
610 /* value, event_type); */
612 if (event_type == nw_link->lmm_resource.power.event) {
614 sg_weight_S_parameter / value - sg_weight_S_parameter /
615 (nw_link->lmm_resource.power.peak *
616 nw_link->lmm_resource.power.scale);
617 lmm_variable_t var = NULL;
618 lmm_element_t elem = NULL;
619 surf_action_network_CM02_t action = NULL;
621 nw_link->lmm_resource.power.peak = value;
622 lmm_update_constraint_bound(network_maxmin_system,
623 nw_link->lmm_resource.constraint,
624 sg_bandwidth_factor *
625 (nw_link->lmm_resource.power.peak *
626 nw_link->lmm_resource.power.scale));
628 TRACE_surf_link_set_bandwidth(date, (char *)(((nw_link->lmm_resource).generic_resource).name),
629 sg_bandwidth_factor *
630 (nw_link->lmm_resource.power.peak *
631 nw_link->lmm_resource.power.scale));
633 if (sg_weight_S_parameter > 0) {
634 while ((var = lmm_get_var_from_cnst
635 (network_maxmin_system, nw_link->lmm_resource.constraint,
637 action = lmm_variable_id(var);
638 action->weight += delta;
639 if (!(action->suspended))
640 lmm_update_variable_weight(network_maxmin_system,
641 action->variable, action->weight);
644 if (tmgr_trace_event_free(event_type))
645 nw_link->lmm_resource.power.event = NULL;
646 } else if (event_type == nw_link->lat_event) {
647 double delta = value - nw_link->lat_current;
648 lmm_variable_t var = NULL;
649 lmm_element_t elem = NULL;
650 surf_action_network_CM02_t action = NULL;
652 nw_link->lat_current = value;
653 while ((var = lmm_get_var_from_cnst
654 (network_maxmin_system, nw_link->lmm_resource.constraint,
656 action = lmm_variable_id(var);
657 action->lat_current += delta;
658 action->weight += delta;
659 if (action->rate < 0)
660 lmm_update_variable_bound(network_maxmin_system, action->variable,
661 sg_tcp_gamma / (2.0 *
662 action->lat_current));
664 lmm_update_variable_bound(network_maxmin_system, action->variable,
666 sg_tcp_gamma / (2.0 *
667 action->lat_current)));
669 if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
670 XBT_INFO("Flow is limited BYBANDWIDTH");
672 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
673 action->lat_current);
676 if (!(action->suspended))
677 lmm_update_variable_weight(network_maxmin_system, action->variable,
681 if (tmgr_trace_event_free(event_type))
682 nw_link->lat_event = NULL;
683 } else if (event_type == nw_link->lmm_resource.state_event) {
685 nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
687 lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
688 lmm_variable_t var = NULL;
689 lmm_element_t elem = NULL;
691 nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
692 while ((var = lmm_get_var_from_cnst
693 (network_maxmin_system, cnst, &elem))) {
694 surf_action_t action = lmm_variable_id(var);
696 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
697 surf_action_state_get(action) == SURF_ACTION_READY) {
698 action->finish = date;
699 surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
703 if (tmgr_trace_event_free(event_type))
704 nw_link->lmm_resource.state_event = NULL;
706 XBT_CRITICAL("Unknown event ! \n");
710 XBT_DEBUG("There were a resource state event, need to update actions related to the constraint (%p)", nw_link->lmm_resource.constraint);
715 static surf_action_t net_communicate(const char *src_name,
716 const char *dst_name, double size,
722 surf_action_network_CM02_t action = NULL;
723 double bandwidth_bound;
725 xbt_dynar_t back_route = NULL;
726 int constraints_per_variable = 0;
728 xbt_dynar_t route=xbt_dynar_new(global_routing->size_of_link,NULL);
730 XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
732 routing_get_route_and_latency(src_name, dst_name, &route, &latency);
733 xbt_assert(!xbt_dynar_is_empty(route) || latency,
734 "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
737 xbt_dynar_foreach(route, i, link) {
738 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
743 if (sg_network_crosstraffic == 1) {
744 routing_get_route_and_latency(dst_name, src_name, &back_route,NULL);
745 xbt_dynar_foreach(back_route, i, link) {
746 if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
754 surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
755 surf_network_model, failed);
756 #ifdef HAVE_LATENCY_BOUND_TRACKING
757 (action->generic_action).latency_limited = 0;
759 action->weight = action->latency = latency;
761 xbt_swag_insert(action, action->generic_action.state_set);
763 if(network_update_mechanism == UM_LAZY){
764 action->index_heap = -1;
765 action->last_update = surf_get_clock();
768 bandwidth_bound = -1.0;
769 if(sg_weight_S_parameter>0) {
770 xbt_dynar_foreach(route, i, link) {
772 sg_weight_S_parameter /
773 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
776 xbt_dynar_foreach(route, i, link) {
777 double bb = bandwidth_factor_callback(size) *
778 (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
779 bandwidth_bound = (bandwidth_bound < 0.0)?bb:min(bandwidth_bound,bb);
782 action->lat_current = action->latency;
783 action->latency *= latency_factor_callback(size);
785 bandwidth_constraint_callback(action->rate, bandwidth_bound,
788 xbt_assert(!xbt_dynar_is_empty(route),"Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
790 link = *(link_CM02_t*)xbt_dynar_get_ptr(route, 0);
791 gap_append(size, link, action);
792 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
793 action, src_name, dst_name, action->sender.gap, action->latency);
796 constraints_per_variable = xbt_dynar_length(route);
797 if (back_route != NULL)
798 constraints_per_variable += xbt_dynar_length(back_route);
800 if (action->latency > 0){
802 lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
803 constraints_per_variable);
804 if(network_update_mechanism == UM_LAZY){
805 // add to the heap the event when the latency is payed
806 XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency + action->last_update);
807 heap_insert(action, action->latency + action->last_update, LATENCY);
811 lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
812 constraints_per_variable);
814 if (action->rate < 0) {
815 lmm_update_variable_bound(network_maxmin_system, action->variable,
816 (action->lat_current > 0)?
817 sg_tcp_gamma / (2.0 * action->lat_current) :-1.0);
819 lmm_update_variable_bound(network_maxmin_system, action->variable,
820 (action->lat_current > 0)?
821 min(action->rate, sg_tcp_gamma / (2.0 * action->lat_current))
825 xbt_dynar_foreach(route, i, link) {
826 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
827 action->variable, 1.0);
830 if (sg_network_crosstraffic == 1) {
831 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
832 xbt_dynar_foreach(back_route, i, link) {
833 lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
834 action->variable, .05);
839 if (TRACE_is_enabled()) {
840 action->src_name = xbt_strdup(src_name);
841 action->dst_name = xbt_strdup(dst_name);
843 action->src_name = action->dst_name = NULL;
847 xbt_dynar_free(&route);
850 return (surf_action_t) action;
853 static xbt_dynar_t net_get_route(const char *src, const char *dst)
855 xbt_dynar_t route=NULL;
856 routing_get_route_and_latency(src, dst,&route,NULL);
860 static double net_get_link_bandwidth(const void *link)
862 surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
863 return lmm->power.peak * lmm->power.scale;
866 static double net_get_link_latency(const void *link)
868 return ((link_CM02_t) link)->lat_current;
871 static int net_link_shared(const void *link)
874 lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
877 static void net_action_suspend(surf_action_t action)
879 ((surf_action_network_CM02_t) action)->suspended = 1;
880 lmm_update_variable_weight(network_maxmin_system,
881 ((surf_action_network_CM02_t)
882 action)->variable, 0.0);
884 if(network_update_mechanism == UM_LAZY)// remove action from the heap
885 heap_remove((surf_action_network_CM02_t) action);
888 static void net_action_resume(surf_action_t action)
890 if (((surf_action_network_CM02_t) action)->suspended) {
891 lmm_update_variable_weight(network_maxmin_system,
892 ((surf_action_network_CM02_t)
894 ((surf_action_network_CM02_t)
896 ((surf_action_network_CM02_t) action)->suspended = 0;
897 if(network_update_mechanism == UM_LAZY)// remove action from the heap
898 heap_remove((surf_action_network_CM02_t) action);
902 static int net_action_is_suspended(surf_action_t action)
904 return ((surf_action_network_CM02_t) action)->suspended;
907 void net_action_set_max_duration(surf_action_t action, double duration)
909 action->max_duration = duration;
910 if(network_update_mechanism == UM_LAZY)// remove action from the heap
911 heap_remove((surf_action_network_CM02_t) action);
915 static void net_action_set_category(surf_action_t action, const char *category)
917 action->category = xbt_strdup (category);
921 static void net_finalize(void)
923 surf_model_exit(surf_network_model);
924 surf_network_model = NULL;
926 lmm_system_free(network_maxmin_system);
927 network_maxmin_system = NULL;
929 if(network_update_mechanism == UM_LAZY){
930 xbt_heap_free(net_action_heap);
931 xbt_swag_free(net_modified_set);
935 static void smpi_gap_append(double size, const link_CM02_t link, surf_action_network_CM02_t action) {
936 const char* src = link->lmm_resource.generic_resource.name;
938 surf_action_network_CM02_t last_action;
941 if(sg_sender_gap > 0.0) {
943 gap_lookup = xbt_dict_new();
945 fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, src);
946 action->sender.gap = 0.0;
947 if(fifo && xbt_fifo_size(fifo) > 0) {
948 /* Compute gap from last send */
949 last_action = (surf_action_network_CM02_t)xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
950 bw = net_get_link_bandwidth(link);
951 action->sender.gap = last_action->sender.gap + max(sg_sender_gap, last_action->sender.size / bw);
952 action->latency += action->sender.gap;
954 /* Append action as last send */
955 action->sender.link_name = link->lmm_resource.generic_resource.name;
956 fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
958 fifo = xbt_fifo_new();
959 xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
961 action->sender.fifo_item = xbt_fifo_push(fifo, action);
962 action->sender.size = size;
966 static void smpi_gap_remove(surf_action_network_CM02_t action) {
970 if(sg_sender_gap > 0.0 && action->sender.link_name && action->sender.fifo_item) {
971 fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
972 xbt_fifo_remove_item(fifo, action->sender.fifo_item);
973 size = xbt_fifo_size(fifo);
976 xbt_dict_remove(gap_lookup, action->sender.link_name);
977 size = xbt_dict_length(gap_lookup);
979 xbt_dict_free(&gap_lookup);
985 static void surf_network_model_init_internal(void)
987 s_surf_action_network_CM02_t comm;
988 surf_network_model = surf_model_init();
990 surf_network_model->name = "network";
991 surf_network_model->action_unref = net_action_unref;
992 surf_network_model->action_cancel = net_action_cancel;
993 surf_network_model->action_recycle = net_action_recycle;
994 surf_network_model->get_remains = net_action_get_remains;
995 #ifdef HAVE_LATENCY_BOUND_TRACKING
996 surf_network_model->get_latency_limited = net_get_link_latency_limited;
999 surf_network_model->model_private->resource_used = net_resource_used;
1000 if(network_update_mechanism == UM_LAZY) {
1001 surf_network_model->model_private->share_resources = net_share_resources_lazy;
1002 surf_network_model->model_private->update_actions_state = net_update_actions_state_lazy;
1003 } else if(network_update_mechanism == UM_FULL) {
1004 surf_network_model->model_private->share_resources = net_share_resources_full;
1005 surf_network_model->model_private->update_actions_state = net_update_actions_state_full;
1008 surf_network_model->model_private->update_resource_state =
1009 net_update_resource_state;
1010 surf_network_model->model_private->finalize = net_finalize;
1012 surf_network_model->suspend = net_action_suspend;
1013 surf_network_model->resume = net_action_resume;
1014 surf_network_model->is_suspended = net_action_is_suspended;
1015 surf_cpu_model->set_max_duration = net_action_set_max_duration;
1017 surf_network_model->extension.network.communicate = net_communicate;
1018 surf_network_model->extension.network.get_route = net_get_route;
1019 surf_network_model->extension.network.get_link_bandwidth =
1020 net_get_link_bandwidth;
1021 surf_network_model->extension.network.get_link_latency =
1022 net_get_link_latency;
1023 surf_network_model->extension.network.link_shared = net_link_shared;
1024 surf_network_model->extension.network.add_traces = net_add_traces;
1025 surf_network_model->extension.network.create_resource =
1026 net_create_resource;
1028 if (!network_maxmin_system)
1029 network_maxmin_system = lmm_system_new(selective_update);
1031 routing_model_create(sizeof(link_CM02_t),
1032 net_create_resource("__loopback__",
1033 498000000, NULL, 0.000015, NULL,
1034 SURF_RESOURCE_ON, NULL,
1035 SURF_LINK_FATPIPE, NULL));
1037 if(network_update_mechanism == UM_LAZY){
1038 net_action_heap = xbt_heap_new(8,NULL);
1039 xbt_heap_set_update_callback(net_action_heap, net_action_update_index_heap);
1041 xbt_swag_new(xbt_swag_offset(comm, action_list_hookup));
1045 static void set_update_mechanism(void) {
1046 char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1047 int select = xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
1049 if(!strcmp(optim,"Full")) {
1050 network_update_mechanism = UM_FULL;
1051 selective_update = select;
1052 } else if (!strcmp(optim,"Lazy")) {
1053 network_update_mechanism = UM_LAZY;
1054 selective_update = 1;
1055 xbt_assert((select==1) || (xbt_cfg_is_default_value(_surf_cfg_set,"network/maxmin_selective_update")),
1056 "Disabling selective update while using the lazy update mechanism is dumb!");
1058 xbt_die("Unsupported optimization (%s) for this model",optim);
1062 /************************************************************************/
1063 /* New model based on LV08 and experimental results of MPI ping-pongs */
1064 /************************************************************************/
1065 /* @Inproceedings{smpi_ipdps, */
1066 /* author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
1067 /* title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
1068 /* booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
1069 /* address={Anchorage (Alaska) USA}, */
1073 void surf_network_model_init_SMPI(void)
1076 if (surf_network_model)
1078 set_update_mechanism();
1080 surf_network_model_init_internal();
1081 latency_factor_callback = &smpi_latency_factor;
1082 bandwidth_factor_callback = &smpi_bandwidth_factor;
1083 bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1084 gap_append = &smpi_gap_append;
1085 gap_remove = &smpi_gap_remove;
1086 net_define_callbacks();
1087 xbt_dynar_push(model_list, &surf_network_model);
1088 network_solve = lmm_solve;
1090 xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1091 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1094 /************************************************************************/
1095 /* New model based on optimizations discussed during Pedro Velho's thesis*/
1096 /************************************************************************/
1097 /* @techreport{VELHO:2011:HAL-00646896:1, */
1098 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
1099 /* title = {{Flow-level network models: have we reached the limits?}}, */
1100 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
1101 /* type = {Rapport de recherche}, */
1102 /* institution = {INRIA}, */
1103 /* number = {RR-7821}, */
1104 /* year = {2011}, */
1106 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
1108 void surf_network_model_init_LegrandVelho(void)
1110 char *model = xbt_cfg_get_string(_surf_cfg_set, "network/model");
1112 if (surf_network_model)
1115 if(!strcmp(model,"LV08_fullupdate")) {
1116 XBT_WARN("[*Deprecated*. Use --cfg=network/model:LV08 with option --cfg=network/optim:Full instead.]");
1118 set_update_mechanism();
1120 surf_network_model_init_internal();
1121 net_define_callbacks();
1122 xbt_dynar_push(model_list, &surf_network_model);
1123 network_solve = lmm_solve;
1125 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4); // 13.01 when callibration is done without phase effects
1126 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",0.92);// 0.97 when callibration is done without phase effects
1127 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775); // 20537 when callibration is done without phase effects
1130 /***************************************************************************/
1131 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1132 /***************************************************************************/
1133 /* @TechReport{ rr-lip2002-40, */
1134 /* author = {Henri Casanova and Loris Marchal}, */
1135 /* institution = {LIP}, */
1136 /* title = {A Network Model for Simulation of Grid Application}, */
1137 /* number = {2002-40}, */
1138 /* month = {oct}, */
1141 void surf_network_model_init_CM02(void)
1144 if (surf_network_model)
1147 set_update_mechanism();
1148 surf_network_model_init_internal();
1149 net_define_callbacks();
1150 xbt_dynar_push(model_list, &surf_network_model);
1151 network_solve = lmm_solve;
1153 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1154 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor", 1.0);
1155 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1158 /***************************************************************************/
1159 /* The models from Steven H. Low */
1160 /***************************************************************************/
1161 /* @article{Low03, */
1162 /* author={Steven H. Low}, */
1163 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
1165 /* journal={{IEEE/ACM} Transactions on Networking}, */
1166 /* volume={11}, number={4}, */
1168 void surf_network_model_init_Reno(void)
1170 if (surf_network_model)
1173 set_update_mechanism();
1174 surf_network_model_init_internal();
1175 net_define_callbacks();
1177 xbt_dynar_push(model_list, &surf_network_model);
1178 lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1180 network_solve = lagrange_solve;
1182 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1183 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1185 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1189 void surf_network_model_init_Reno2(void)
1191 if (surf_network_model)
1194 set_update_mechanism();
1195 surf_network_model_init_internal();
1196 net_define_callbacks();
1198 xbt_dynar_push(model_list, &surf_network_model);
1199 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1201 network_solve = lagrange_solve;
1203 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1204 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1206 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1210 void surf_network_model_init_Vegas(void)
1212 if (surf_network_model)
1215 set_update_mechanism();
1216 surf_network_model_init_internal();
1217 net_define_callbacks();
1219 xbt_dynar_push(model_list, &surf_network_model);
1220 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1222 network_solve = lagrange_solve;
1224 xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1225 xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1227 xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);