Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check for failed links on back_route when crosstraffic is activated
[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(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 im_net_share_resources(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 double generic_net_share_resources(double now)
517 {
518   if(network_update_mechanism == UM_LAZY)
519     return im_net_share_resources(now);
520   else if (network_update_mechanism == UM_FULL)
521   {
522     return net_share_resources(now);
523   } else {
524     xbt_die("Invalide update mechanism!");
525     return 0;
526   }
527 }
528
529 static void net_update_actions_state(double now, double delta)
530 {
531   double deltap = 0.0;
532   surf_action_network_CM02_im_t action = NULL;
533   surf_action_network_CM02_im_t next_action = NULL;
534   xbt_swag_t running_actions =
535       surf_network_model->states.running_action_set;
536   /*
537      xbt_swag_t failed_actions =
538      surf_network_model->states.failed_action_set;
539    */
540
541   xbt_swag_foreach_safe(action, next_action, running_actions) {
542     deltap = delta;
543     if (action->latency > 0) {
544       if (action->latency > deltap) {
545         double_update(&(action->latency), deltap);
546         deltap = 0.0;
547       } else {
548         double_update(&(deltap), action->latency);
549         action->latency = 0.0;
550       }
551       if ((action->latency == 0.0) && !(action->suspended))
552         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
553                                    action->weight);
554     }
555 #ifdef HAVE_TRACING
556     if (TRACE_is_enabled()) {
557       xbt_dynar_t route=NULL;
558       routing_get_route_and_latency(action->src_name, action->dst_name,&route,NULL);
559       link_CM02_im_t link;
560       unsigned int i;
561       xbt_dynar_foreach(route, i, link) {
562         TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
563                                         action->generic_action.data,
564                                         (surf_action_t) action,
565                                         lmm_variable_getvalue
566                                         (action->variable), now - delta,
567                                         delta);
568       }
569     }
570 #endif
571     if(!lmm_get_number_of_cnst_from_var(network_im_maxmin_system, action->variable)) {
572         /* There is actually no link used, hence an infinite bandwidth.
573          * This happens often when using models like vivaldi.
574          * In such case, just make sure that the action completes immediately.
575          */
576       double_update(&(action->generic_action.remains),
577           action->generic_action.remains);
578     }
579     double_update(&(action->generic_action.remains),
580                   lmm_variable_getvalue(action->variable) * deltap);
581     if (action->generic_action.max_duration != NO_MAX_DURATION)
582       double_update(&(action->generic_action.max_duration), delta);
583
584     if ((action->generic_action.remains <= 0) &&
585         (lmm_get_variable_weight(action->variable) > 0)) {
586       action->generic_action.finish = surf_get_clock();
587       surf_network_model->action_state_set((surf_action_t) action,
588                                            SURF_ACTION_DONE);
589 #ifdef HAVE_SMPI
590       gap_remove(action);
591 #endif
592     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
593                && (action->generic_action.max_duration <= 0)) {
594       action->generic_action.finish = surf_get_clock();
595       surf_network_model->action_state_set((surf_action_t) action,
596                                            SURF_ACTION_DONE);
597 #ifdef HAVE_SMPI
598       gap_remove(action);
599 #endif
600     }
601   }
602
603   return;
604 }
605
606 static void im_net_update_actions_state(double now, double delta)
607 {
608   surf_action_network_CM02_im_t action = NULL;
609
610   while ((xbt_heap_size(im_net_action_heap) > 0)
611          && (double_equals(xbt_heap_maxkey(im_net_action_heap), now))) {
612     action = xbt_heap_pop(im_net_action_heap);
613     XBT_DEBUG("Action %p: finish", action);
614     GENERIC_ACTION(action).finish = surf_get_clock();
615
616     // if I am wearing a latency heat
617     if( action->hat ==  LATENCY){
618         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
619                                            action->weight);
620         heap_remove(action);
621         action->last_update = surf_get_clock();
622
623         XBT_DEBUG("Action (%p) is not limited by latency anymore", action);
624 #ifdef HAVE_LATENCY_BOUND_TRACKING
625           GENERIC_ACTION(action).latency_limited = 0;
626 #endif
627
628     // if I am wearing a max_duration or normal hat
629     }else if( action->hat == MAX_DURATION || 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         action->generic_action.finish = surf_get_clock();
634               surf_network_model->action_state_set((surf_action_t) action,
635                                                    SURF_ACTION_DONE);
636         heap_remove(action);
637     }
638   }
639   return;
640 }
641
642 static void generic_net_update_actions_state(double now, double delta)
643 {
644   if(network_update_mechanism == UM_LAZY)
645     im_net_update_actions_state(now,delta);
646   else if (network_update_mechanism == UM_FULL)
647   {
648     net_update_actions_state(now,delta);
649   } else {
650     xbt_die("Invalide update mechanism!");
651   }
652 }
653
654 static void im_net_update_resource_state(void *id,
655                                       tmgr_trace_event_t event_type,
656                                       double value, double date)
657 {
658   link_CM02_im_t nw_link = id;
659   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
660   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
661   /*     value, event_type); */
662
663   if (event_type == nw_link->lmm_resource.power.event) {
664     double delta =
665         sg_weight_S_parameter / value - sg_weight_S_parameter /
666         (nw_link->lmm_resource.power.peak *
667          nw_link->lmm_resource.power.scale);
668     lmm_variable_t var = NULL;
669     lmm_element_t elem = NULL;
670     surf_action_network_CM02_im_t action = NULL;
671
672     nw_link->lmm_resource.power.peak = value;
673     lmm_update_constraint_bound(network_im_maxmin_system,
674                                 nw_link->lmm_resource.constraint,
675                                 sg_bandwidth_factor *
676                                 (nw_link->lmm_resource.power.peak *
677                                  nw_link->lmm_resource.power.scale));
678 #ifdef HAVE_TRACING
679     TRACE_surf_link_set_bandwidth(date, (char *)(((nw_link->lmm_resource).generic_resource).name),
680                                   sg_bandwidth_factor *
681                                   (nw_link->lmm_resource.power.peak *
682                                    nw_link->lmm_resource.power.scale));
683 #endif
684     if (sg_weight_S_parameter > 0) {
685       while ((var = lmm_get_var_from_cnst
686               (network_im_maxmin_system, nw_link->lmm_resource.constraint,
687                &elem))) {
688         action = lmm_variable_id(var);
689         action->weight += delta;
690         if (!(action->suspended))
691           lmm_update_variable_weight(network_im_maxmin_system,
692                                      action->variable, action->weight);
693       }
694     }
695     if (tmgr_trace_event_free(event_type))
696       nw_link->lmm_resource.power.event = NULL;
697   } else if (event_type == nw_link->lat_event) {
698     double delta = value - nw_link->lat_current;
699     lmm_variable_t var = NULL;
700     lmm_element_t elem = NULL;
701     surf_action_network_CM02_im_t action = NULL;
702
703     nw_link->lat_current = value;
704     while ((var = lmm_get_var_from_cnst
705             (network_im_maxmin_system, nw_link->lmm_resource.constraint,
706              &elem))) {
707       action = lmm_variable_id(var);
708       action->lat_current += delta;
709       action->weight += delta;
710       if (action->rate < 0)
711         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
712                                   sg_tcp_gamma / (2.0 *
713                                                   action->lat_current));
714       else {
715         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
716                                   min(action->rate,
717                                       sg_tcp_gamma / (2.0 *
718                                                       action->lat_current)));
719
720         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
721           XBT_INFO("Flow is limited BYBANDWIDTH");
722         } else {
723           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
724                 action->lat_current);
725         }
726       }
727       if (!(action->suspended))
728         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
729                                    action->weight);
730
731     }
732     if (tmgr_trace_event_free(event_type))
733       nw_link->lat_event = NULL;
734   } else if (event_type == nw_link->lmm_resource.state_event) {
735     if (value > 0)
736       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
737     else {
738       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
739       lmm_variable_t var = NULL;
740       lmm_element_t elem = NULL;
741
742       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
743       while ((var = lmm_get_var_from_cnst
744               (network_im_maxmin_system, cnst, &elem))) {
745         surf_action_t action = lmm_variable_id(var);
746
747         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
748             surf_action_state_get(action) == SURF_ACTION_READY) {
749           action->finish = date;
750           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
751         }
752       }
753     }
754     if (tmgr_trace_event_free(event_type))
755       nw_link->lmm_resource.state_event = NULL;
756   } else {
757     XBT_CRITICAL("Unknown event ! \n");
758     xbt_abort();
759   }
760
761   XBT_DEBUG("There were a resource state event, need to update actions related to the constraint (%p)", nw_link->lmm_resource.constraint);
762   return;
763 }
764
765
766 static surf_action_t im_net_communicate(const char *src_name,
767                                      const char *dst_name, double size,
768                                      double rate)
769 {
770   unsigned int i;
771   link_CM02_im_t link;
772   int failed = 0;
773   surf_action_network_CM02_im_t action = NULL;
774   double bandwidth_bound;
775   double latency=0.0;
776   /* LARGE PLATFORMS HACK:
777      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
778      Use the cluster_id for ->id */
779
780   xbt_dynar_t back_route = NULL;
781   int constraints_per_variable = 0;
782   // I need to have the forward and backward routes at the same time, so allocate "route". That way, the routing wont clean it up
783   xbt_dynar_t route=xbt_dynar_new(global_routing->size_of_link,NULL);
784   routing_get_route_and_latency(src_name, dst_name, &route, &latency);
785
786   if (sg_network_crosstraffic == 1) {
787     // FIXME: fill route directly (unclear: check with blame who put the FIXME)
788     routing_get_route_and_latency(dst_name, src_name, &back_route,NULL);
789   }
790
791   /* LARGE PLATFORMS HACK:
792      total_route_size = route_size + src->link_nb + dst->nb */
793
794   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
795   /* LARGE PLATFORMS HACK:
796      assert on total_route_size */
797   xbt_assert(!xbt_dynar_is_empty(route) || latency,
798               "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
799               src_name, dst_name);
800
801   xbt_dynar_foreach(route, i, link) {
802     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
803       failed = 1;
804       break;
805     }
806   }
807   if (sg_network_crosstraffic == 1) {
808     xbt_dynar_foreach(back_route, i, link) {
809       if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
810         failed = 1;
811         break;
812       }
813     }
814   }
815
816   action =
817       surf_action_new(sizeof(s_surf_action_network_CM02_im_t), size,
818                       surf_network_model, failed);
819 #ifdef HAVE_LATENCY_BOUND_TRACKING
820   (action->generic_action).latency_limited = 0;
821 #endif
822   action->weight = action->latency = latency;
823
824   xbt_swag_insert(action, action->generic_action.state_set);
825   action->rate = rate;
826   if(network_update_mechanism == UM_LAZY){
827     action->index_heap = -1;
828     action->latency = 0.0;
829     action->weight = 0.0;
830     action->last_update = surf_get_clock();
831   }
832
833   bandwidth_bound = -1.0;
834   xbt_dynar_foreach(route, i, link) {
835     action->weight +=
836         sg_weight_S_parameter /
837         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
838     if (bandwidth_bound < 0.0)
839       bandwidth_bound =
840           im_bandwidth_factor_callback(size) *
841           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
842     else
843       bandwidth_bound =
844           min(bandwidth_bound,
845               im_bandwidth_factor_callback(size) *
846               (link->lmm_resource.power.peak *
847                link->lmm_resource.power.scale));
848   }
849   /* LARGE PLATFORMS HACK:
850      Add src->link and dst->link latencies */
851   action->lat_current = action->latency;
852   action->latency *= im_latency_factor_callback(size);
853   action->rate =
854       im_bandwidth_constraint_callback(action->rate, bandwidth_bound,
855                                         size);
856 #ifdef HAVE_SMPI
857   if(!xbt_dynar_is_empty(route)) {
858     link = *(link_CM02_im_t*)xbt_dynar_get_ptr(route, 0);
859     gap_append(size, link, action);
860     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
861            action, src_name, dst_name, action->sender.gap, action->latency);
862   } else {
863     gap_unknown(action);
864   }
865 #endif
866
867   /* LARGE PLATFORMS HACK:
868      lmm_variable_new(..., total_route_size) */
869   if (back_route != NULL) {
870     constraints_per_variable =
871         xbt_dynar_length(route) + xbt_dynar_length(back_route);
872   } else {
873     constraints_per_variable = xbt_dynar_length(route);
874   }
875
876   if (action->latency > 0){
877       action->variable =
878         lmm_variable_new(network_im_maxmin_system, action, 0.0, -1.0,
879                          constraints_per_variable);
880     if(network_update_mechanism == UM_LAZY){
881       // add to the heap the event when the latency is payed
882       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency + action->last_update);
883       heap_insert(action, action->latency + action->last_update, LATENCY);
884     }
885 #ifdef HAVE_LATENCY_BOUND_TRACKING
886         (action->generic_action).latency_limited = 1;
887 #endif
888   }
889   else
890     action->variable =
891         lmm_variable_new(network_im_maxmin_system, action, 1.0, -1.0,
892                          constraints_per_variable);
893
894   if (action->rate < 0) {
895     if (action->lat_current > 0)
896       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
897                                 sg_tcp_gamma / (2.0 *
898                                                 action->lat_current));
899     else
900       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
901                                 -1.0);
902   } else {
903     if (action->lat_current > 0)
904       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
905                                 min(action->rate,
906                                     sg_tcp_gamma / (2.0 *
907                                                     action->lat_current)));
908     else
909       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
910                                 action->rate);
911   }
912
913   xbt_dynar_foreach(route, i, link) {
914     lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
915                action->variable, 1.0);
916   }
917
918   if (sg_network_crosstraffic == 1) {
919     XBT_DEBUG("Fullduplex active adding backward flow using 5%c", '%');
920     xbt_dynar_foreach(back_route, i, link) {
921       lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
922                  action->variable, .05);
923     }
924   }
925
926   /* LARGE PLATFORMS HACK:
927      expand also with src->link and dst->link */
928 #ifdef HAVE_TRACING
929   if (TRACE_is_enabled()) {
930     action->src_name = xbt_strdup(src_name);
931     action->dst_name = xbt_strdup(dst_name);
932   } else {
933     action->src_name = action->dst_name = NULL;
934   }
935 #endif
936
937   xbt_dynar_free(&route);
938   XBT_OUT();
939
940   return (surf_action_t) action;
941 }
942
943 static xbt_dynar_t im_net_get_route(const char *src, const char *dst)
944 {
945   xbt_dynar_t route=NULL;
946   routing_get_route_and_latency(src, dst,&route,NULL);
947   return route;
948 }
949
950 static double im_net_get_link_bandwidth(const void *link)
951 {
952   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
953   return lmm->power.peak * lmm->power.scale;
954 }
955
956 static double im_net_get_link_latency(const void *link)
957 {
958   return ((link_CM02_im_t) link)->lat_current;
959 }
960
961 static int im_net_link_shared(const void *link)
962 {
963   return
964       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
965 }
966
967 static void im_net_action_suspend(surf_action_t action)
968 {
969   ((surf_action_network_CM02_im_t) action)->suspended = 1;
970   lmm_update_variable_weight(network_im_maxmin_system,
971                              ((surf_action_network_CM02_im_t)
972                               action)->variable, 0.0);
973
974   if(network_update_mechanism == UM_LAZY)// remove action from the heap
975     heap_remove((surf_action_network_CM02_im_t) action);
976 }
977
978 static void im_net_action_resume(surf_action_t action)
979 {
980   if (((surf_action_network_CM02_im_t) action)->suspended) {
981     lmm_update_variable_weight(network_im_maxmin_system,
982                                ((surf_action_network_CM02_im_t)
983                                 action)->variable,
984                                ((surf_action_network_CM02_im_t)
985                                 action)->weight);
986     ((surf_action_network_CM02_im_t) action)->suspended = 0;
987     if(network_update_mechanism == UM_LAZY)// remove action from the heap
988       heap_remove((surf_action_network_CM02_im_t) action);
989   }
990 }
991
992 static int im_net_action_is_suspended(surf_action_t action)
993 {
994   return ((surf_action_network_CM02_im_t) action)->suspended;
995 }
996
997 void im_net_action_set_max_duration(surf_action_t action, double duration)
998 {
999   action->max_duration = duration;
1000   if(network_update_mechanism == UM_LAZY)// remove action from the heap
1001     heap_remove((surf_action_network_CM02_im_t) action);
1002 }
1003
1004 #ifdef HAVE_TRACING
1005 static void net_action_set_category(surf_action_t action, const char *category)
1006 {
1007   action->category = xbt_strdup (category);
1008 }
1009 #endif
1010
1011 static void im_net_finalize(void)
1012 {
1013   surf_model_exit(surf_network_model);
1014   surf_network_model = NULL;
1015
1016   lmm_system_free(network_im_maxmin_system);
1017   network_im_maxmin_system = NULL;
1018
1019   if(network_update_mechanism == UM_LAZY){
1020     xbt_heap_free(im_net_action_heap);
1021     xbt_swag_free(im_net_modified_set);
1022   }
1023 }
1024
1025 #ifdef HAVE_SMPI
1026 static void gap_append(double size, const link_CM02_im_t link, surf_action_network_CM02_im_t action) {
1027    const char* src = link->lmm_resource.generic_resource.name;
1028    xbt_fifo_t fifo;
1029    surf_action_network_CM02_im_t last_action;
1030    double bw;
1031
1032    if(sg_sender_gap > 0.0) {
1033       if(!gap_lookup) {
1034          gap_lookup = xbt_dict_new();
1035       }
1036       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, src);
1037       action->sender.gap = 0.0;
1038       if(fifo && xbt_fifo_size(fifo) > 0) {
1039          /* Compute gap from last send */
1040          last_action = (surf_action_network_CM02_im_t)xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
1041          bw = im_net_get_link_bandwidth(link);
1042          action->sender.gap = last_action->sender.gap + max(sg_sender_gap, last_action->sender.size / bw);
1043          action->latency += action->sender.gap;
1044       }
1045       /* Append action as last send */
1046       action->sender.link_name = link->lmm_resource.generic_resource.name;
1047       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
1048       if(!fifo) {
1049          fifo = xbt_fifo_new();
1050          xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1051       }
1052       action->sender.fifo_item = xbt_fifo_push(fifo, action);
1053       action->sender.size = size;
1054    }
1055 }
1056
1057 static void gap_unknown(surf_action_network_CM02_im_t action) {
1058    action->sender.gap = 0.0;
1059    action->sender.link_name = NULL;
1060    action->sender.fifo_item = NULL;
1061    action->sender.size = 0.0;
1062 }
1063
1064 static void gap_remove(surf_action_network_CM02_im_t action) {
1065    xbt_fifo_t fifo;
1066    size_t size;
1067
1068    if(sg_sender_gap > 0.0 && action->sender.link_name && action->sender.fifo_item) {
1069       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
1070       xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1071       size = xbt_fifo_size(fifo);
1072       if(size == 0) {
1073          xbt_fifo_free(fifo);
1074          xbt_dict_remove(gap_lookup, action->sender.link_name);
1075          size = xbt_dict_length(gap_lookup);
1076          if(size == 0) {
1077             xbt_dict_free(&gap_lookup);
1078          }
1079       }
1080    }
1081 }
1082 #endif
1083
1084 static void im_surf_network_model_init_internal(void)
1085 {
1086   s_surf_action_network_CM02_im_t comm;
1087
1088   surf_network_model = surf_model_init();
1089
1090   surf_network_model->name = "network";
1091   surf_network_model->action_unref = im_net_action_unref;
1092   surf_network_model->action_cancel = im_net_action_cancel;
1093   surf_network_model->action_recycle = im_net_action_recycle;
1094   surf_network_model->get_remains = im_net_action_get_remains;
1095 #ifdef HAVE_LATENCY_BOUND_TRACKING
1096   surf_network_model->get_latency_limited = im_net_get_link_latency_limited;
1097 #endif
1098
1099   surf_network_model->model_private->resource_used = im_net_resource_used;
1100   surf_network_model->model_private->share_resources = generic_net_share_resources;
1101   surf_network_model->model_private->update_actions_state =
1102       generic_net_update_actions_state;
1103   surf_network_model->model_private->update_resource_state =
1104                   im_net_update_resource_state;
1105   surf_network_model->model_private->finalize = im_net_finalize;
1106
1107   surf_network_model->suspend = im_net_action_suspend;
1108   surf_network_model->resume = im_net_action_resume;
1109   surf_network_model->is_suspended = im_net_action_is_suspended;
1110   surf_cpu_model->set_max_duration = im_net_action_set_max_duration;
1111
1112   surf_network_model->extension.network.communicate = im_net_communicate;
1113   surf_network_model->extension.network.get_route = im_net_get_route;
1114   surf_network_model->extension.network.get_link_bandwidth =
1115                   im_net_get_link_bandwidth;
1116   surf_network_model->extension.network.get_link_latency =
1117                   im_net_get_link_latency;
1118   surf_network_model->extension.network.link_shared = im_net_link_shared;
1119   surf_network_model->extension.network.add_traces = im_net_add_traces;
1120   surf_network_model->extension.network.create_resource =
1121                   im_net_create_resource;
1122
1123   if (!network_im_maxmin_system)
1124     network_im_maxmin_system = lmm_system_new();
1125
1126  routing_model_create(sizeof(link_CM02_im_t),
1127       im_net_create_resource("__loopback__",
1128           498000000, NULL, 0.000015, NULL,
1129           SURF_RESOURCE_ON, NULL,
1130           SURF_LINK_FATPIPE, NULL));
1131
1132   if(network_update_mechanism == UM_LAZY){
1133     sg_maxmin_selective_update = 1;
1134     im_net_action_heap = xbt_heap_new(8,NULL);
1135     xbt_heap_set_update_callback(im_net_action_heap, im_net_action_update_index_heap);
1136     im_net_modified_set =
1137         xbt_swag_new(xbt_swag_offset(comm, action_list_hookup));
1138   }
1139 }
1140
1141 /************************************************************************/
1142 /* New model based on LV08 and experimental results of MPI ping-pongs   */
1143 /************************************************************************/
1144 void surf_network_model_init_SMPI(void)
1145 {
1146
1147   if (surf_network_model)
1148     return;
1149   im_surf_network_model_init_internal();
1150   im_latency_factor_callback = &smpi_latency_factor;
1151   im_bandwidth_factor_callback = &smpi_bandwidth_factor;
1152   im_bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1153   im_net_define_callbacks();
1154   xbt_dynar_push(model_list, &surf_network_model);
1155   network_im_solve = lmm_solve;
1156
1157   xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1158   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1159
1160 }
1161
1162 /************************************************************************/
1163 /* New model based on optimizations discussed during this thesis        */
1164 /************************************************************************/
1165 void im_surf_network_model_init_LegrandVelho(void)
1166 {
1167   if( strcmp(xbt_cfg_get_string(_surf_cfg_set, "network/model"),"LV08"))
1168     network_update_mechanism = UM_LAZY;
1169   else
1170     network_update_mechanism = UM_FULL;
1171
1172   if (surf_network_model)
1173     return;
1174   im_surf_network_model_init_internal();
1175   im_net_define_callbacks();
1176   xbt_dynar_push(model_list, &surf_network_model);
1177   network_im_solve = lmm_solve;
1178
1179   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1180   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1181                             0.92);
1182   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1183 }
1184
1185 /***************************************************************************/
1186 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1187 /***************************************************************************/
1188 /* @TechReport{      rr-lip2002-40, */
1189 /*   author        = {Henri Casanova and Loris Marchal}, */
1190 /*   institution   = {LIP}, */
1191 /*   title         = {A Network Model for Simulation of Grid Application}, */
1192 /*   number        = {2002-40}, */
1193 /*   month         = {oct}, */
1194 /*   year          = {2002} */
1195 /* } */
1196 void surf_network_model_init_CM02(void)
1197 {
1198
1199   if (surf_network_model)
1200     return;
1201   im_surf_network_model_init_internal();
1202   im_net_define_callbacks();
1203   xbt_dynar_push(model_list, &surf_network_model);
1204   network_im_solve = lmm_solve;
1205
1206   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1207   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor", 1.0);
1208   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1209 }
1210
1211 void surf_network_model_init_Reno(void)
1212 {
1213   if (surf_network_model)
1214     return;
1215   im_surf_network_model_init_internal();
1216   im_net_define_callbacks();
1217
1218   xbt_dynar_push(model_list, &surf_network_model);
1219   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1220                                     func_reno_fpi);
1221   network_im_solve = lagrange_solve;
1222
1223   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1224   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1225                             0.92);
1226   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1227 }
1228
1229
1230 void surf_network_model_init_Reno2(void)
1231 {
1232   if (surf_network_model)
1233     return;
1234   im_surf_network_model_init_internal();
1235   im_net_define_callbacks();
1236
1237   xbt_dynar_push(model_list, &surf_network_model);
1238   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1239                                     func_reno2_fpi);
1240   network_im_solve = lagrange_solve;
1241
1242   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1243   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1244                             0.92);
1245   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1246                             8775);
1247 }
1248
1249 void surf_network_model_init_Vegas(void)
1250 {
1251   if (surf_network_model)
1252     return;
1253   im_surf_network_model_init_internal();
1254   im_net_define_callbacks();
1255
1256   xbt_dynar_push(model_list, &surf_network_model);
1257   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1258                                     func_vegas_fpi);
1259   network_im_solve = lagrange_solve;
1260
1261   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1262   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1263                             0.92);
1264   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1265 }