Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Apply the optimization mechanism as requested in config flag.
[simgrid.git] / src / surf / network_im.c
1
2 /*
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.
7  */
8
9 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
10  * All rights reserved.                                                     */
11
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. */
14
15 #include "network_private.h"
16 #include "xbt/log.h"
17 #include "xbt/str.h"
18
19 #include "surf_private.h"
20 #include "xbt/dict.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"
25
26
27 #undef GENERIC_ACTION
28 #define GENERIC_ACTION(action) action->generic_action
29
30
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
32                                 "Logging specific to the SURF network module");
33
34 surf_model_t surf_network_model = NULL;
35 static lmm_system_t network_im_maxmin_system = NULL;
36 static void (*network_im_solve) (lmm_system_t) = NULL;
37
38 double sg_sender_gap = 0.0;
39 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
40 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
41 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
42
43 double sg_tcp_gamma = 0.0;
44 int sg_network_crosstraffic = 0;
45
46 xbt_dict_t gap_lookup = NULL;
47
48 e_UM_t network_update_mechanism = UM_FULL;
49
50 typedef struct network_link_CM02_im {
51   s_surf_resource_lmm_t lmm_resource;   /* must remain first to be added to a trace */
52
53   /* Using this object with the public part of
54      model does not make sense */
55   double lat_current;
56   tmgr_trace_event_t lat_event;
57 } s_link_CM02_im_t, *link_CM02_im_t;
58
59 static int im_net_action_is_suspended(surf_action_t action);
60 static void im_net_update_actions_state(double now, double delta);
61 static void update_action_remaining(double now);
62
63 static xbt_swag_t im_net_modified_set = NULL;
64 static xbt_heap_t im_net_action_heap = NULL;
65 xbt_swag_t keep_track = NULL;
66 extern int sg_maxmin_selective_update;
67
68 #ifdef HAVE_SMPI
69 static void gap_append(double size, const link_CM02_im_t link, surf_action_network_CM02_im_t action);
70 static void gap_unknown(surf_action_network_CM02_im_t action);
71 static void gap_remove(surf_action_network_CM02_im_t action);
72 #endif
73
74 /* added to manage the communication action's heap */
75 static void im_net_action_update_index_heap(void *action, int i)
76 {
77   ((surf_action_network_CM02_im_t) action)->index_heap = i;
78 }
79
80 /* insert action on heap using a given key and a hat (heap_action_type)
81  * a hat can be of three types for communications:
82  *
83  * NORMAL = this is a normal heap entry stating the date to finish transmitting
84  * LATENCY = this is a heap entry to warn us when the latency is payed
85  * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
86  */
87 static void heap_insert(surf_action_network_CM02_im_t    action, double key, enum heap_action_type hat){
88   action->hat = hat;
89   xbt_heap_push(im_net_action_heap, action, key);
90 }
91
92 static void heap_remove(surf_action_network_CM02_im_t action){
93   action->hat = NONE;
94   if(((surf_action_network_CM02_im_t) action)->index_heap >= 0){
95       xbt_heap_remove(im_net_action_heap,action->index_heap);
96   }
97 }
98
99 /******************************************************************************/
100 /*                           Factors callbacks                                */
101 /******************************************************************************/
102 static double im_constant_latency_factor(double size)
103 {
104   return sg_latency_factor;
105 }
106
107 static double im_constant_bandwidth_factor(double size)
108 {
109   return sg_bandwidth_factor;
110 }
111
112 static double im_constant_bandwidth_constraint(double rate, double bound,
113                                             double size)
114 {
115   return rate;
116 }
117
118
119 /**********************/
120 /*   SMPI callbacks   */
121 /**********************/
122 static double smpi_latency_factor(double size)
123 {
124   /* 1 B <= size <= 1 KiB */
125   if (size <= 1024.0) {
126     return 1.0056;
127   }
128
129   /* 2 KiB <= size <= 32 KiB */
130   if (size <= 32768.0) {
131     return 1.8805;
132   }
133
134   /* 64 KiB <= size <= 4 MiB */
135   return 22.7111;
136 }
137
138 static double smpi_bandwidth_factor(double size)
139 {
140   /* 1 B <= size <= 1 KiB */
141   if (size <= 1024.0) {
142     return 0.2758;
143   }
144
145   /* 2 KiB <= size <= 32 KiB */
146   if (size <= 32768.0) {
147     return 0.5477;
148   }
149
150   /* 64 KiB <= size <= 4 MiB */
151   return 0.9359;
152 }
153
154 static double smpi_bandwidth_constraint(double rate, double bound,
155                                         double size)
156 {
157   return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
158 }
159
160 static double (*im_latency_factor_callback) (double) =
161     &im_constant_latency_factor;
162 static double (*im_bandwidth_factor_callback) (double) =
163     &im_constant_bandwidth_factor;
164 static double (*im_bandwidth_constraint_callback) (double, double, double) =
165     &im_constant_bandwidth_constraint;
166
167
168 static void* im_net_create_resource(const char *name,
169                                 double bw_initial,
170                                 tmgr_trace_t bw_trace,
171                                 double lat_initial,
172                                 tmgr_trace_t lat_trace,
173                                 e_surf_resource_state_t
174                                 state_initial,
175                                 tmgr_trace_t state_trace,
176                                 e_surf_link_sharing_policy_t
177                                 policy, xbt_dict_t properties)
178 {
179   link_CM02_im_t nw_link = (link_CM02_im_t)
180       surf_resource_lmm_new(sizeof(s_link_CM02_im_t),
181                             surf_network_model, name, properties,
182                             network_im_maxmin_system,
183                             sg_bandwidth_factor * bw_initial,
184                             history,
185                             state_initial, state_trace,
186                             bw_initial, bw_trace);
187
188   xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
189               "Link '%s' declared several times in the platform file.",
190               name);
191
192   nw_link->lat_current = lat_initial;
193   if (lat_trace)
194     nw_link->lat_event =
195         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
196
197   if (policy == SURF_LINK_FATPIPE)
198     lmm_constraint_shared(nw_link->lmm_resource.constraint);
199
200   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
201
202   return nw_link;
203 }
204
205 static void im_net_parse_link_init(sg_platf_link_cbarg_t link)
206 {
207   if(link->policy == SURF_LINK_FULLDUPLEX){
208     char *link_id;
209     link_id = bprintf("%s_UP", link->id);
210           im_net_create_resource(link_id,
211                            link->bandwidth,
212                            link->bandwidth_trace,
213                            link->latency,
214                            link->latency_trace,
215                            link->state,
216                            link->state_trace,
217                            link->policy,
218                            link->properties);
219           xbt_free(link_id);
220     link_id = bprintf("%s_DOWN", link->id);
221     im_net_create_resource(link_id,
222                         link->bandwidth,
223                         link->bandwidth_trace,
224                         link->latency,
225                         link->latency_trace,
226                         link->state,
227                         link->state_trace,
228                         link->policy,
229                         link->properties);
230     xbt_free(link_id);
231   }
232   else{
233     im_net_create_resource(link->id,
234                         link->bandwidth,
235                         link->bandwidth_trace,
236                         link->latency,
237                         link->latency_trace,
238                         link->state,
239                         link->state_trace,
240                         link->policy,
241                         link->properties);
242   }
243 }
244
245 static void im_net_add_traces(void)
246 {
247   xbt_dict_cursor_t cursor = NULL;
248   char *trace_name, *elm;
249
250   static int called = 0;
251   if (called)
252     return;
253   called = 1;
254
255   /* connect all traces relative to network */
256   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
257     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
258     link_CM02_im_t link =
259                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
260
261     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
262                 trace_name, elm);
263     xbt_assert(trace,
264                 "Cannot connect trace %s to link %s: trace undefined",
265                 trace_name, elm);
266
267     link->lmm_resource.state_event =
268         tmgr_history_add_trace(history, trace, 0.0, 0, link);
269   }
270
271   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
272     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
273     link_CM02_im_t link =
274                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
275
276     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
277                 trace_name, elm);
278     xbt_assert(trace,
279                 "Cannot connect trace %s to link %s: trace undefined",
280                 trace_name, elm);
281
282     link->lmm_resource.power.event =
283         tmgr_history_add_trace(history, trace, 0.0, 0, link);
284   }
285
286   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
287     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
288     link_CM02_im_t link =
289                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
290
291     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
292                 trace_name, elm);
293     xbt_assert(trace,
294                 "Cannot connect trace %s to link %s: trace undefined",
295                 trace_name, elm);
296
297     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
298   }
299 }
300
301 static void im_net_define_callbacks(void)
302 {
303   /* Figuring out the network links */
304   sg_platf_link_add_cb(im_net_parse_link_init);
305   sg_platf_postparse_add_cb(im_net_add_traces);
306 }
307
308 static int im_net_resource_used(void *resource_id)
309 {
310   return lmm_constraint_used(network_im_maxmin_system,
311                              ((surf_resource_lmm_t)
312                               resource_id)->constraint);
313 }
314
315 static int im_net_action_unref(surf_action_t action)
316 {
317   action->refcount--;
318   if (!action->refcount) {
319     xbt_swag_remove(action, action->state_set);
320     if (((surf_action_network_CM02_im_t) action)->variable){
321       lmm_variable_free(network_im_maxmin_system,
322                         ((surf_action_network_CM02_im_t) action)->variable);
323     }
324     if(network_update_mechanism == UM_LAZY){// remove action from the heap
325       heap_remove((surf_action_network_CM02_im_t) action);
326       xbt_swag_remove(action, im_net_modified_set);
327     }
328 #ifdef HAVE_TRACING
329     xbt_free(((surf_action_network_CM02_im_t) action)->src_name);
330     xbt_free(((surf_action_network_CM02_im_t) action)->dst_name);
331     xbt_free(action->category);
332 #endif
333     surf_action_free(&action);
334     return 1;
335   }
336   return 0;
337 }
338
339
340
341 static void im_net_action_cancel(surf_action_t action)
342 {
343   surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
344   if(network_update_mechanism == UM_LAZY){// remove action from the heap
345     xbt_swag_remove(action, im_net_modified_set);
346     heap_remove((surf_action_network_CM02_im_t) action);
347   }
348 }
349
350 void im_net_action_recycle(surf_action_t action)
351 {
352   return;
353 }
354
355 #ifdef HAVE_LATENCY_BOUND_TRACKING
356 static int im_net_get_link_latency_limited(surf_action_t action)
357 {
358   return action->latency_limited;
359 }
360 #endif
361
362 double im_net_action_get_remains(surf_action_t action)
363 {
364   if(network_update_mechanism == UM_LAZY)/* update remains before return it */
365     update_action_remaining(surf_get_clock());
366   return action->remains;
367 }
368
369 static void update_action_remaining(double now){
370   surf_action_network_CM02_im_t action = NULL;
371   double delta = 0.0;
372
373   xbt_swag_foreach(action, im_net_modified_set) {
374
375     if(action->suspended != 0){
376         continue;
377     }
378
379     delta = now - action->last_update;
380
381     double_update(&(action->generic_action.remains),
382                   lmm_variable_getvalue(action->variable) * delta);
383
384     if (action->generic_action.max_duration != NO_MAX_DURATION)
385       double_update(&(action->generic_action.max_duration), delta);
386
387     if ((action->generic_action.remains <= 0) &&
388         (lmm_get_variable_weight(action->variable) > 0)) {
389       action->generic_action.finish = surf_get_clock();
390       surf_network_model->action_state_set((surf_action_t) action,
391                                            SURF_ACTION_DONE);
392       heap_remove(action);
393     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
394                && (action->generic_action.max_duration <= 0)) {
395       action->generic_action.finish = surf_get_clock();
396       surf_network_model->action_state_set((surf_action_t) action,
397                                            SURF_ACTION_DONE);
398       heap_remove(action);
399     }
400
401     action->last_update = now;
402   }
403 }
404
405 static double net_share_resources_full(double now)
406 {
407   s_surf_action_network_CM02_im_t s_action;
408   surf_action_network_CM02_im_t action = NULL;
409   xbt_swag_t running_actions =
410       surf_network_model->states.running_action_set;
411   double min;
412
413   min = generic_maxmin_share_resources(running_actions,
414                                        xbt_swag_offset(s_action,
415                                                        variable),
416                                        network_im_maxmin_system,
417                                        network_im_solve);
418
419 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable)  )))
420
421   xbt_swag_foreach(action, running_actions) {
422 #ifdef HAVE_LATENCY_BOUND_TRACKING
423     if (lmm_is_variable_limited_by_latency(action->variable)) {
424       (action->generic_action).latency_limited = 1;
425     } else {
426       (action->generic_action).latency_limited = 0;
427     }
428 #endif
429     if (action->latency > 0) {
430       if (min < 0)
431         min = action->latency;
432       else if (action->latency < min)
433         min = action->latency;
434     }
435   }
436
437   XBT_DEBUG("Min of share resources %f", min);
438
439   return min;
440 }
441
442 static double net_share_resources_lazy(double now)
443 {
444   surf_action_network_CM02_im_t action = NULL;
445   double min=-1;
446   double value;
447
448   XBT_DEBUG("Before share resources, the size of modified actions set is %d", xbt_swag_size(im_net_modified_set));
449   update_action_remaining(now);
450
451   keep_track = im_net_modified_set;
452   lmm_solve(network_im_maxmin_system);
453   keep_track = NULL;
454
455   XBT_DEBUG("After share resources, The size of modified actions set is %d", xbt_swag_size(im_net_modified_set));
456
457    xbt_swag_foreach(action, im_net_modified_set) {
458      if (GENERIC_ACTION(action).state_set != surf_network_model->states.running_action_set){
459        continue;
460      }
461
462      /* bogus priority, skip it */
463      if (GENERIC_ACTION(action).priority <= 0){
464          continue;
465      }
466
467      min = -1;
468      value = lmm_variable_getvalue(action->variable);
469      if (value > 0) {
470          if (GENERIC_ACTION(action).remains > 0) {
471              value = GENERIC_ACTION(action).remains / value;
472              min = now + value;
473          } else {
474              value = 0.0;
475              min = now;
476          }
477      }
478
479      if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
480          && (min == -1
481              || GENERIC_ACTION(action).start +
482              GENERIC_ACTION(action).max_duration < min)){
483        min =   GENERIC_ACTION(action).start +
484                GENERIC_ACTION(action).max_duration;
485      }
486
487      XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
488                 GENERIC_ACTION(action).start, now + value,
489                 GENERIC_ACTION(action).max_duration);
490
491
492
493      if (action->index_heap >= 0) {
494          heap_remove((surf_action_network_CM02_im_t) action);
495      }
496
497      if (min != -1) {
498          heap_insert((surf_action_network_CM02_im_t) action, min, NORMAL);
499          XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min, now);
500      }
501    }
502
503    //hereafter must have already the min value for this resource model
504    if(xbt_heap_size(im_net_action_heap) > 0 ){
505        min = xbt_heap_maxkey(im_net_action_heap) - now ;
506    }else{
507        min = -1;
508    }
509
510    XBT_DEBUG("The minimum with the HEAP %lf", min);
511
512
513   return min;
514 }
515
516 static void net_update_actions_state_full(double now, double delta)
517 {
518   double deltap = 0.0;
519   surf_action_network_CM02_im_t action = NULL;
520   surf_action_network_CM02_im_t next_action = NULL;
521   xbt_swag_t running_actions =
522       surf_network_model->states.running_action_set;
523   /*
524      xbt_swag_t failed_actions =
525      surf_network_model->states.failed_action_set;
526    */
527
528   xbt_swag_foreach_safe(action, next_action, running_actions) {
529     deltap = delta;
530     if (action->latency > 0) {
531       if (action->latency > deltap) {
532         double_update(&(action->latency), deltap);
533         deltap = 0.0;
534       } else {
535         double_update(&(deltap), action->latency);
536         action->latency = 0.0;
537       }
538       if ((action->latency == 0.0) && !(action->suspended))
539         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
540                                    action->weight);
541     }
542 #ifdef HAVE_TRACING
543     if (TRACE_is_enabled()) {
544       xbt_dynar_t route=NULL;
545       routing_get_route_and_latency(action->src_name, action->dst_name,&route,NULL);
546       link_CM02_im_t link;
547       unsigned int i;
548       xbt_dynar_foreach(route, i, link) {
549         TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
550                                         action->generic_action.data,
551                                         (surf_action_t) action,
552                                         lmm_variable_getvalue
553                                         (action->variable), now - delta,
554                                         delta);
555       }
556     }
557 #endif
558     if(!lmm_get_number_of_cnst_from_var(network_im_maxmin_system, action->variable)) {
559         /* There is actually no link used, hence an infinite bandwidth.
560          * This happens often when using models like vivaldi.
561          * In such case, just make sure that the action completes immediately.
562          */
563       double_update(&(action->generic_action.remains),
564           action->generic_action.remains);
565     }
566     double_update(&(action->generic_action.remains),
567                   lmm_variable_getvalue(action->variable) * deltap);
568     if (action->generic_action.max_duration != NO_MAX_DURATION)
569       double_update(&(action->generic_action.max_duration), delta);
570
571     if ((action->generic_action.remains <= 0) &&
572         (lmm_get_variable_weight(action->variable) > 0)) {
573       action->generic_action.finish = surf_get_clock();
574       surf_network_model->action_state_set((surf_action_t) action,
575                                            SURF_ACTION_DONE);
576 #ifdef HAVE_SMPI
577       gap_remove(action);
578 #endif
579     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
580                && (action->generic_action.max_duration <= 0)) {
581       action->generic_action.finish = surf_get_clock();
582       surf_network_model->action_state_set((surf_action_t) action,
583                                            SURF_ACTION_DONE);
584 #ifdef HAVE_SMPI
585       gap_remove(action);
586 #endif
587     }
588   }
589
590   return;
591 }
592
593 static void net_update_actions_state_lazy(double now, double delta)
594 {
595   surf_action_network_CM02_im_t action = NULL;
596
597   while ((xbt_heap_size(im_net_action_heap) > 0)
598          && (double_equals(xbt_heap_maxkey(im_net_action_heap), now))) {
599     action = xbt_heap_pop(im_net_action_heap);
600     XBT_DEBUG("Action %p: finish", action);
601     GENERIC_ACTION(action).finish = surf_get_clock();
602
603     // if I am wearing a latency heat
604     if( action->hat ==  LATENCY){
605         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
606                                            action->weight);
607         heap_remove(action);
608         action->last_update = surf_get_clock();
609
610         XBT_DEBUG("Action (%p) is not limited by latency anymore", action);
611 #ifdef HAVE_LATENCY_BOUND_TRACKING
612           GENERIC_ACTION(action).latency_limited = 0;
613 #endif
614
615     // if I am wearing a max_duration or normal hat
616     }else if( action->hat == MAX_DURATION || action->hat == NORMAL ){
617         // no need to communicate anymore
618         // assume that flows that reached max_duration have remaining of 0
619         GENERIC_ACTION(action).remains = 0;
620         action->generic_action.finish = surf_get_clock();
621               surf_network_model->action_state_set((surf_action_t) action,
622                                                    SURF_ACTION_DONE);
623         heap_remove(action);
624     }
625   }
626   return;
627 }
628
629 static void net_update_resource_state(void *id,
630                                       tmgr_trace_event_t event_type,
631                                       double value, double date)
632 {
633   link_CM02_im_t nw_link = id;
634   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
635   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
636   /*     value, event_type); */
637
638   if (event_type == nw_link->lmm_resource.power.event) {
639     double delta =
640         sg_weight_S_parameter / value - sg_weight_S_parameter /
641         (nw_link->lmm_resource.power.peak *
642          nw_link->lmm_resource.power.scale);
643     lmm_variable_t var = NULL;
644     lmm_element_t elem = NULL;
645     surf_action_network_CM02_im_t action = NULL;
646
647     nw_link->lmm_resource.power.peak = value;
648     lmm_update_constraint_bound(network_im_maxmin_system,
649                                 nw_link->lmm_resource.constraint,
650                                 sg_bandwidth_factor *
651                                 (nw_link->lmm_resource.power.peak *
652                                  nw_link->lmm_resource.power.scale));
653 #ifdef HAVE_TRACING
654     TRACE_surf_link_set_bandwidth(date, (char *)(((nw_link->lmm_resource).generic_resource).name),
655                                   sg_bandwidth_factor *
656                                   (nw_link->lmm_resource.power.peak *
657                                    nw_link->lmm_resource.power.scale));
658 #endif
659     if (sg_weight_S_parameter > 0) {
660       while ((var = lmm_get_var_from_cnst
661               (network_im_maxmin_system, nw_link->lmm_resource.constraint,
662                &elem))) {
663         action = lmm_variable_id(var);
664         action->weight += delta;
665         if (!(action->suspended))
666           lmm_update_variable_weight(network_im_maxmin_system,
667                                      action->variable, action->weight);
668       }
669     }
670     if (tmgr_trace_event_free(event_type))
671       nw_link->lmm_resource.power.event = NULL;
672   } else if (event_type == nw_link->lat_event) {
673     double delta = value - nw_link->lat_current;
674     lmm_variable_t var = NULL;
675     lmm_element_t elem = NULL;
676     surf_action_network_CM02_im_t action = NULL;
677
678     nw_link->lat_current = value;
679     while ((var = lmm_get_var_from_cnst
680             (network_im_maxmin_system, nw_link->lmm_resource.constraint,
681              &elem))) {
682       action = lmm_variable_id(var);
683       action->lat_current += delta;
684       action->weight += delta;
685       if (action->rate < 0)
686         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
687                                   sg_tcp_gamma / (2.0 *
688                                                   action->lat_current));
689       else {
690         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
691                                   min(action->rate,
692                                       sg_tcp_gamma / (2.0 *
693                                                       action->lat_current)));
694
695         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
696           XBT_INFO("Flow is limited BYBANDWIDTH");
697         } else {
698           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
699                 action->lat_current);
700         }
701       }
702       if (!(action->suspended))
703         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
704                                    action->weight);
705
706     }
707     if (tmgr_trace_event_free(event_type))
708       nw_link->lat_event = NULL;
709   } else if (event_type == nw_link->lmm_resource.state_event) {
710     if (value > 0)
711       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
712     else {
713       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
714       lmm_variable_t var = NULL;
715       lmm_element_t elem = NULL;
716
717       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
718       while ((var = lmm_get_var_from_cnst
719               (network_im_maxmin_system, cnst, &elem))) {
720         surf_action_t action = lmm_variable_id(var);
721
722         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
723             surf_action_state_get(action) == SURF_ACTION_READY) {
724           action->finish = date;
725           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
726         }
727       }
728     }
729     if (tmgr_trace_event_free(event_type))
730       nw_link->lmm_resource.state_event = NULL;
731   } else {
732     XBT_CRITICAL("Unknown event ! \n");
733     xbt_abort();
734   }
735
736   XBT_DEBUG("There were a resource state event, need to update actions related to the constraint (%p)", nw_link->lmm_resource.constraint);
737   return;
738 }
739
740
741 static surf_action_t im_net_communicate(const char *src_name,
742                                      const char *dst_name, double size,
743                                      double rate)
744 {
745   unsigned int i;
746   link_CM02_im_t link;
747   int failed = 0;
748   surf_action_network_CM02_im_t action = NULL;
749   double bandwidth_bound;
750   double latency=0.0;
751   /* LARGE PLATFORMS HACK:
752      Add a link_CM02_im_t *link and a int link_nb to network_card_CM02_im_t. It will represent local links for this node
753      Use the cluster_id for ->id */
754
755   xbt_dynar_t back_route = NULL;
756   int constraints_per_variable = 0;
757   // I need to have the forward and backward routes at the same time, so allocate "route". That way, the routing wont clean it up
758   xbt_dynar_t route=xbt_dynar_new(global_routing->size_of_link,NULL);
759   routing_get_route_and_latency(src_name, dst_name, &route, &latency);
760
761   if (sg_network_crosstraffic == 1) {
762     // FIXME: fill route directly (unclear: check with blame who put the FIXME)
763     routing_get_route_and_latency(dst_name, src_name, &back_route,NULL);
764   }
765
766   /* LARGE PLATFORMS HACK:
767      total_route_size = route_size + src->link_nb + dst->nb */
768
769   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
770   /* LARGE PLATFORMS HACK:
771      assert on total_route_size */
772   xbt_assert(!xbt_dynar_is_empty(route) || latency,
773               "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
774               src_name, dst_name);
775
776   xbt_dynar_foreach(route, i, link) {
777     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
778       failed = 1;
779       break;
780     }
781   }
782   if (sg_network_crosstraffic == 1) {
783     xbt_dynar_foreach(back_route, i, link) {
784       if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
785         failed = 1;
786         break;
787       }
788     }
789   }
790
791   action =
792       surf_action_new(sizeof(s_surf_action_network_CM02_im_t), size,
793                       surf_network_model, failed);
794 #ifdef HAVE_LATENCY_BOUND_TRACKING
795   (action->generic_action).latency_limited = 0;
796 #endif
797   action->weight = action->latency = latency;
798
799   xbt_swag_insert(action, action->generic_action.state_set);
800   action->rate = rate;
801   if(network_update_mechanism == UM_LAZY){
802     action->index_heap = -1;
803     action->latency = 0.0;
804     action->weight = 0.0;
805     action->last_update = surf_get_clock();
806   }
807
808   bandwidth_bound = -1.0;
809   xbt_dynar_foreach(route, i, link) {
810     action->weight +=
811         sg_weight_S_parameter /
812         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
813     if (bandwidth_bound < 0.0)
814       bandwidth_bound =
815           im_bandwidth_factor_callback(size) *
816           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
817     else
818       bandwidth_bound =
819           min(bandwidth_bound,
820               im_bandwidth_factor_callback(size) *
821               (link->lmm_resource.power.peak *
822                link->lmm_resource.power.scale));
823   }
824   /* LARGE PLATFORMS HACK:
825      Add src->link and dst->link latencies */
826   action->lat_current = action->latency;
827   action->latency *= im_latency_factor_callback(size);
828   action->rate =
829       im_bandwidth_constraint_callback(action->rate, bandwidth_bound,
830                                         size);
831 #ifdef HAVE_SMPI
832   if(!xbt_dynar_is_empty(route)) {
833     link = *(link_CM02_im_t*)xbt_dynar_get_ptr(route, 0);
834     gap_append(size, link, action);
835     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
836            action, src_name, dst_name, action->sender.gap, action->latency);
837   } else {
838     gap_unknown(action);
839   }
840 #endif
841
842   /* LARGE PLATFORMS HACK:
843      lmm_variable_new(..., total_route_size) */
844   if (back_route != NULL) {
845     constraints_per_variable =
846         xbt_dynar_length(route) + xbt_dynar_length(back_route);
847   } else {
848     constraints_per_variable = xbt_dynar_length(route);
849   }
850
851   if (action->latency > 0){
852       action->variable =
853         lmm_variable_new(network_im_maxmin_system, action, 0.0, -1.0,
854                          constraints_per_variable);
855     if(network_update_mechanism == UM_LAZY){
856       // add to the heap the event when the latency is payed
857       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency + action->last_update);
858       heap_insert(action, action->latency + action->last_update, LATENCY);
859     }
860 #ifdef HAVE_LATENCY_BOUND_TRACKING
861         (action->generic_action).latency_limited = 1;
862 #endif
863   }
864   else
865     action->variable =
866         lmm_variable_new(network_im_maxmin_system, action, 1.0, -1.0,
867                          constraints_per_variable);
868
869   if (action->rate < 0) {
870     if (action->lat_current > 0)
871       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
872                                 sg_tcp_gamma / (2.0 *
873                                                 action->lat_current));
874     else
875       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
876                                 -1.0);
877   } else {
878     if (action->lat_current > 0)
879       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
880                                 min(action->rate,
881                                     sg_tcp_gamma / (2.0 *
882                                                     action->lat_current)));
883     else
884       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
885                                 action->rate);
886   }
887
888   xbt_dynar_foreach(route, i, link) {
889     lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
890                action->variable, 1.0);
891   }
892
893   if (sg_network_crosstraffic == 1) {
894     XBT_DEBUG("Fullduplex active adding backward flow using 5%c", '%');
895     xbt_dynar_foreach(back_route, i, link) {
896       lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
897                  action->variable, .05);
898     }
899   }
900
901   /* LARGE PLATFORMS HACK:
902      expand also with src->link and dst->link */
903 #ifdef HAVE_TRACING
904   if (TRACE_is_enabled()) {
905     action->src_name = xbt_strdup(src_name);
906     action->dst_name = xbt_strdup(dst_name);
907   } else {
908     action->src_name = action->dst_name = NULL;
909   }
910 #endif
911
912   xbt_dynar_free(&route);
913   XBT_OUT();
914
915   return (surf_action_t) action;
916 }
917
918 static xbt_dynar_t im_net_get_route(const char *src, const char *dst)
919 {
920   xbt_dynar_t route=NULL;
921   routing_get_route_and_latency(src, dst,&route,NULL);
922   return route;
923 }
924
925 static double im_net_get_link_bandwidth(const void *link)
926 {
927   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
928   return lmm->power.peak * lmm->power.scale;
929 }
930
931 static double im_net_get_link_latency(const void *link)
932 {
933   return ((link_CM02_im_t) link)->lat_current;
934 }
935
936 static int im_net_link_shared(const void *link)
937 {
938   return
939       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
940 }
941
942 static void im_net_action_suspend(surf_action_t action)
943 {
944   ((surf_action_network_CM02_im_t) action)->suspended = 1;
945   lmm_update_variable_weight(network_im_maxmin_system,
946                              ((surf_action_network_CM02_im_t)
947                               action)->variable, 0.0);
948
949   if(network_update_mechanism == UM_LAZY)// remove action from the heap
950     heap_remove((surf_action_network_CM02_im_t) action);
951 }
952
953 static void im_net_action_resume(surf_action_t action)
954 {
955   if (((surf_action_network_CM02_im_t) action)->suspended) {
956     lmm_update_variable_weight(network_im_maxmin_system,
957                                ((surf_action_network_CM02_im_t)
958                                 action)->variable,
959                                ((surf_action_network_CM02_im_t)
960                                 action)->weight);
961     ((surf_action_network_CM02_im_t) action)->suspended = 0;
962     if(network_update_mechanism == UM_LAZY)// remove action from the heap
963       heap_remove((surf_action_network_CM02_im_t) action);
964   }
965 }
966
967 static int im_net_action_is_suspended(surf_action_t action)
968 {
969   return ((surf_action_network_CM02_im_t) action)->suspended;
970 }
971
972 void im_net_action_set_max_duration(surf_action_t action, double duration)
973 {
974   action->max_duration = duration;
975   if(network_update_mechanism == UM_LAZY)// remove action from the heap
976     heap_remove((surf_action_network_CM02_im_t) action);
977 }
978
979 #ifdef HAVE_TRACING
980 static void net_action_set_category(surf_action_t action, const char *category)
981 {
982   action->category = xbt_strdup (category);
983 }
984 #endif
985
986 static void im_net_finalize(void)
987 {
988   surf_model_exit(surf_network_model);
989   surf_network_model = NULL;
990
991   lmm_system_free(network_im_maxmin_system);
992   network_im_maxmin_system = NULL;
993
994   if(network_update_mechanism == UM_LAZY){
995     xbt_heap_free(im_net_action_heap);
996     xbt_swag_free(im_net_modified_set);
997   }
998 }
999
1000 #ifdef HAVE_SMPI
1001 static void gap_append(double size, const link_CM02_im_t link, surf_action_network_CM02_im_t action) {
1002    const char* src = link->lmm_resource.generic_resource.name;
1003    xbt_fifo_t fifo;
1004    surf_action_network_CM02_im_t last_action;
1005    double bw;
1006
1007    if(sg_sender_gap > 0.0) {
1008       if(!gap_lookup) {
1009          gap_lookup = xbt_dict_new();
1010       }
1011       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, src);
1012       action->sender.gap = 0.0;
1013       if(fifo && xbt_fifo_size(fifo) > 0) {
1014          /* Compute gap from last send */
1015          last_action = (surf_action_network_CM02_im_t)xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
1016          bw = im_net_get_link_bandwidth(link);
1017          action->sender.gap = last_action->sender.gap + max(sg_sender_gap, last_action->sender.size / bw);
1018          action->latency += action->sender.gap;
1019       }
1020       /* Append action as last send */
1021       action->sender.link_name = link->lmm_resource.generic_resource.name;
1022       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
1023       if(!fifo) {
1024          fifo = xbt_fifo_new();
1025          xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1026       }
1027       action->sender.fifo_item = xbt_fifo_push(fifo, action);
1028       action->sender.size = size;
1029    }
1030 }
1031
1032 static void gap_unknown(surf_action_network_CM02_im_t action) {
1033    action->sender.gap = 0.0;
1034    action->sender.link_name = NULL;
1035    action->sender.fifo_item = NULL;
1036    action->sender.size = 0.0;
1037 }
1038
1039 static void gap_remove(surf_action_network_CM02_im_t action) {
1040    xbt_fifo_t fifo;
1041    size_t size;
1042
1043    if(sg_sender_gap > 0.0 && action->sender.link_name && action->sender.fifo_item) {
1044       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
1045       xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1046       size = xbt_fifo_size(fifo);
1047       if(size == 0) {
1048          xbt_fifo_free(fifo);
1049          xbt_dict_remove(gap_lookup, action->sender.link_name);
1050          size = xbt_dict_length(gap_lookup);
1051          if(size == 0) {
1052             xbt_dict_free(&gap_lookup);
1053          }
1054       }
1055    }
1056 }
1057 #endif
1058
1059 static void im_surf_network_model_init_internal(void)
1060 {
1061   s_surf_action_network_CM02_im_t comm;
1062
1063   surf_network_model = surf_model_init();
1064
1065   surf_network_model->name = "network";
1066   surf_network_model->action_unref = im_net_action_unref;
1067   surf_network_model->action_cancel = im_net_action_cancel;
1068   surf_network_model->action_recycle = im_net_action_recycle;
1069   surf_network_model->get_remains = im_net_action_get_remains;
1070 #ifdef HAVE_LATENCY_BOUND_TRACKING
1071   surf_network_model->get_latency_limited = im_net_get_link_latency_limited;
1072 #endif
1073
1074   surf_network_model->model_private->resource_used = im_net_resource_used;
1075   if(network_update_mechanism == UM_LAZY)
1076     surf_network_model->model_private->share_resources = net_share_resources_lazy;
1077   else if(network_update_mechanism == UM_FULL)
1078     surf_network_model->model_private->share_resources = net_share_resources_full;
1079
1080   if(network_update_mechanism == UM_LAZY)
1081     surf_network_model->model_private->update_actions_state =net_update_actions_state_lazy;
1082   else if(network_update_mechanism == UM_FULL)
1083     surf_network_model->model_private->update_actions_state =net_update_actions_state_full;
1084
1085   surf_network_model->model_private->update_resource_state =
1086                   net_update_resource_state;
1087   surf_network_model->model_private->finalize = im_net_finalize;
1088
1089   surf_network_model->suspend = im_net_action_suspend;
1090   surf_network_model->resume = im_net_action_resume;
1091   surf_network_model->is_suspended = im_net_action_is_suspended;
1092   surf_cpu_model->set_max_duration = im_net_action_set_max_duration;
1093
1094   surf_network_model->extension.network.communicate = im_net_communicate;
1095   surf_network_model->extension.network.get_route = im_net_get_route;
1096   surf_network_model->extension.network.get_link_bandwidth =
1097                   im_net_get_link_bandwidth;
1098   surf_network_model->extension.network.get_link_latency =
1099                   im_net_get_link_latency;
1100   surf_network_model->extension.network.link_shared = im_net_link_shared;
1101   surf_network_model->extension.network.add_traces = im_net_add_traces;
1102   surf_network_model->extension.network.create_resource =
1103                   im_net_create_resource;
1104
1105   if (!network_im_maxmin_system)
1106     network_im_maxmin_system = lmm_system_new();
1107
1108  routing_model_create(sizeof(link_CM02_im_t),
1109       im_net_create_resource("__loopback__",
1110           498000000, NULL, 0.000015, NULL,
1111           SURF_RESOURCE_ON, NULL,
1112           SURF_LINK_FATPIPE, NULL));
1113
1114   if(network_update_mechanism == UM_LAZY){
1115     sg_maxmin_selective_update = 1;
1116     im_net_action_heap = xbt_heap_new(8,NULL);
1117     xbt_heap_set_update_callback(im_net_action_heap, im_net_action_update_index_heap);
1118     im_net_modified_set =
1119         xbt_swag_new(xbt_swag_offset(comm, action_list_hookup));
1120   }
1121 }
1122
1123 static void set_update_mechanism(void) {
1124   char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1125
1126   if(!strcmp(optim,"Full")) {
1127     network_update_mechanism = UM_FULL;
1128   } else if (!strcmp(optim,"Lazy")) {
1129     network_update_mechanism = UM_LAZY;
1130   } else {
1131     xbt_die("Unsupported optimization (%s) for this model",optim);
1132   }
1133 }
1134
1135 /************************************************************************/
1136 /* New model based on LV08 and experimental results of MPI ping-pongs   */
1137 /************************************************************************/
1138 void surf_network_model_init_SMPI(void)
1139 {
1140
1141   if (surf_network_model)
1142     return;
1143   set_update_mechanism();
1144
1145   im_surf_network_model_init_internal();
1146   im_latency_factor_callback = &smpi_latency_factor;
1147   im_bandwidth_factor_callback = &smpi_bandwidth_factor;
1148   im_bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1149   im_net_define_callbacks();
1150   xbt_dynar_push(model_list, &surf_network_model);
1151   network_im_solve = lmm_solve;
1152
1153   xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1154   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1155 }
1156
1157 /************************************************************************/
1158 /* New model based on optimizations discussed during this thesis        */
1159 /************************************************************************/
1160 void im_surf_network_model_init_LegrandVelho(void)
1161 {
1162   char *model = xbt_cfg_get_string(_surf_cfg_set, "network/model");
1163
1164   if (surf_network_model)
1165     return;
1166
1167   if(!strcmp(model,"LV08_fullupdate")) {
1168     XBT_WARN("[*Deprecated*. Use --cfg=network/model:LV08 with option --cfg=network/optim:Full instead.]");
1169   }
1170   set_update_mechanism();
1171
1172   im_surf_network_model_init_internal();
1173   im_net_define_callbacks();
1174   xbt_dynar_push(model_list, &surf_network_model);
1175   network_im_solve = lmm_solve;
1176
1177   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4); // 13.01 when callibration is done without phase effects
1178   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",0.92);// 0.97 when callibration is done without phase effects
1179   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);       // 20537 when callibration is done without phase effects
1180 }
1181
1182 /***************************************************************************/
1183 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1184 /***************************************************************************/
1185 /* @TechReport{      rr-lip2002-40, */
1186 /*   author        = {Henri Casanova and Loris Marchal}, */
1187 /*   institution   = {LIP}, */
1188 /*   title         = {A Network Model for Simulation of Grid Application}, */
1189 /*   number        = {2002-40}, */
1190 /*   month         = {oct}, */
1191 /*   year          = {2002} */
1192 /* } */
1193 void surf_network_model_init_CM02(void)
1194 {
1195
1196   if (surf_network_model)
1197     return;
1198
1199   set_update_mechanism();
1200   im_surf_network_model_init_internal();
1201   im_net_define_callbacks();
1202   xbt_dynar_push(model_list, &surf_network_model);
1203   network_im_solve = lmm_solve;
1204
1205   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1206   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor", 1.0);
1207   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1208 }
1209
1210 void surf_network_model_init_Reno(void)
1211 {
1212   if (surf_network_model)
1213     return;
1214
1215   set_update_mechanism();
1216   im_surf_network_model_init_internal();
1217   im_net_define_callbacks();
1218
1219   xbt_dynar_push(model_list, &surf_network_model);
1220   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1221                                     func_reno_fpi);
1222   network_im_solve = lagrange_solve;
1223
1224   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1225   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1226                             0.92);
1227   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1228 }
1229
1230
1231 void surf_network_model_init_Reno2(void)
1232 {
1233   if (surf_network_model)
1234     return;
1235
1236   set_update_mechanism();
1237   im_surf_network_model_init_internal();
1238   im_net_define_callbacks();
1239
1240   xbt_dynar_push(model_list, &surf_network_model);
1241   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1242                                     func_reno2_fpi);
1243   network_im_solve = lagrange_solve;
1244
1245   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1246   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1247                             0.92);
1248   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1249                             8775);
1250 }
1251
1252 void surf_network_model_init_Vegas(void)
1253 {
1254   if (surf_network_model)
1255     return;
1256
1257   set_update_mechanism();
1258   im_surf_network_model_init_internal();
1259   im_net_define_callbacks();
1260
1261   xbt_dynar_push(model_list, &surf_network_model);
1262   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1263                                     func_vegas_fpi);
1264   network_im_solve = lagrange_solve;
1265
1266   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1267   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1268                             0.92);
1269   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1270 }