Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
We may want to use this function in examples.
[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 "xbt/log.h"
16 #include "xbt/str.h"
17 #include "surf_private.h"
18 #include "xbt/dict.h"
19 #include "maxmin_private.h"
20
21 #undef GENERIC_ACTION
22 #define GENERIC_ACTION(action) action->generic_action
23
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_im, surf,
26                                 "Logging specific to the SURF network module");
27
28
29 enum heap_action_type{
30   LATENCY = 100,
31   MAX_DURATION,
32   NORMAL,
33   NOTSET
34 };
35
36 typedef struct surf_action_network_CM02_im {
37   s_surf_action_t generic_action;
38   s_xbt_swag_hookup_t action_list_hookup;
39   double latency;
40   double lat_current;
41   double weight;
42   lmm_variable_t variable;
43   double rate;
44 #ifdef HAVE_LATENCY_BOUND_TRACKING
45   int latency_limited;
46 #endif
47   int suspended;
48 #ifdef HAVE_TRACING
49   char *src_name;
50   char *dst_name;
51 #endif
52   int index_heap;
53   enum heap_action_type hat;
54   double last_update;
55 } s_surf_action_network_CM02_im_t, *surf_action_network_CM02_im_t;
56
57
58 typedef struct network_link_CM02_im {
59   s_surf_resource_lmm_t lmm_resource;   /* must remain first to be added to a trace */
60
61   /* Using this object with the public part of
62      model does not make sense */
63   double lat_current;
64   tmgr_trace_event_t lat_event;
65 } s_link_CM02_im_t, *link_CM02_im_t;
66
67
68
69
70
71
72
73
74 extern surf_model_t surf_network_model;
75 static lmm_system_t network_im_maxmin_system = NULL;
76 static void (*network_im_solve) (lmm_system_t) = NULL;
77
78 extern double sg_latency_factor;
79 extern double sg_bandwidth_factor;
80 extern double sg_weight_S_parameter;
81
82 extern double sg_tcp_gamma;
83 extern int sg_network_fullduplex;
84
85
86 static void im_net_action_recycle(surf_action_t action);
87 static int im_net_get_link_latency_limited(surf_action_t action);
88 static int im_net_action_is_suspended(surf_action_t action);
89 static double im_net_action_get_remains(surf_action_t action);
90 static void im_net_action_set_max_duration(surf_action_t action, double duration);
91 static void surf_network_model_init_CM02_im(const char *filename);
92 static void im_net_update_actions_state(double now, double delta);
93 static void update_action_remaining(double now);
94
95 static xbt_swag_t im_net_modified_set = NULL;
96 static xbt_heap_t im_net_action_heap = NULL;
97 xbt_swag_t keep_track = NULL;
98 extern int sg_maxmin_selective_update;
99
100 /* added to manage the communication action's heap */
101 static void im_net_action_update_index_heap(void *action, int i)
102 {
103   ((surf_action_network_CM02_im_t) action)->index_heap = i;
104 }
105
106 /* insert action on heap using a given key and a hat (heap_action_type)
107  * a hat can be of three types for communications:
108  *
109  * NORMAL = this is a normal heap entry stating the date to finish transmitting
110  * LATENCY = this is a heap entry to warn us when the latency is payed
111  * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
112  */
113 static void heap_insert(surf_action_network_CM02_im_t    action, double key, enum heap_action_type hat){
114   action->hat = hat;
115   xbt_heap_push(im_net_action_heap, action, key);
116 }
117
118 static void heap_remove(surf_action_network_CM02_im_t action){
119   action->hat = NONE;
120   if(((surf_action_network_CM02_im_t) action)->index_heap >= 0){
121       xbt_heap_remove(im_net_action_heap,action->index_heap);
122   }
123 }
124
125 /******************************************************************************/
126 /*                           Factors callbacks                                */
127 /******************************************************************************/
128 static double im_constant_latency_factor(double size)
129 {
130   return sg_latency_factor;
131 }
132
133 static double im_constant_bandwidth_factor(double size)
134 {
135   return sg_bandwidth_factor;
136 }
137
138 static double im_constant_bandwidth_constraint(double rate, double bound,
139                                             double size)
140 {
141   return rate;
142 }
143
144
145 static double (*im_latency_factor_callback) (double) =
146     &im_constant_latency_factor;
147 static double (*im_bandwidth_factor_callback) (double) =
148     &im_constant_bandwidth_factor;
149 static double (*im_bandwidth_constraint_callback) (double, double, double) =
150     &im_constant_bandwidth_constraint;
151
152
153 static link_CM02_im_t im_net_link_new(char *name,
154                                 double bw_initial,
155                                 tmgr_trace_t bw_trace,
156                                 double lat_initial,
157                                 tmgr_trace_t lat_trace,
158                                 e_surf_resource_state_t
159                                 state_initial,
160                                 tmgr_trace_t state_trace,
161                                 e_surf_link_sharing_policy_t
162                                 policy, xbt_dict_t properties)
163 {
164   link_CM02_im_t nw_link = (link_CM02_im_t)
165       surf_resource_lmm_new(sizeof(s_link_CM02_im_t),
166                             surf_network_model, name, properties,
167                             network_im_maxmin_system,
168                             sg_bandwidth_factor * bw_initial,
169                             history,
170                             state_initial, state_trace,
171                             bw_initial, bw_trace);
172
173   xbt_assert1(!xbt_dict_get_or_null
174               (surf_network_model->resource_set, name),
175               "Link '%s' declared several times in the platform file.",
176               name);
177
178   nw_link->lat_current = lat_initial;
179   if (lat_trace)
180     nw_link->lat_event =
181         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
182
183   if (policy == SURF_LINK_FATPIPE)
184     lmm_constraint_shared(nw_link->lmm_resource.constraint);
185
186   xbt_dict_set(surf_network_model->resource_set, name, nw_link,
187                surf_resource_free);
188
189
190   return nw_link;
191 }
192
193 static void im_net_parse_link_init(void)
194 {
195   char *name_link;
196   double bw_initial;
197   tmgr_trace_t bw_trace;
198   double lat_initial;
199   tmgr_trace_t lat_trace;
200   e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
201   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
202   tmgr_trace_t state_trace;
203   XBT_DEBUG("link_CM02_im");
204   name_link = xbt_strdup(A_surfxml_link_id);
205   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
206   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
207   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
208   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
209
210   xbt_assert0((A_surfxml_link_state == A_surfxml_link_state_ON)
211               || (A_surfxml_link_state ==
212                   A_surfxml_link_state_OFF), "Invalid state");
213   if (A_surfxml_link_state == A_surfxml_link_state_ON)
214     state_initial_link = SURF_RESOURCE_ON;
215   else if (A_surfxml_link_state == A_surfxml_link_state_OFF)
216     state_initial_link = SURF_RESOURCE_OFF;
217
218   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
219     policy_initial_link = SURF_LINK_SHARED;
220   else
221           {
222           if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
223                   policy_initial_link = SURF_LINK_FATPIPE;
224           else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
225                   policy_initial_link = SURF_LINK_FULLDUPLEX;
226           }
227
228   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
229
230   if(policy_initial_link == SURF_LINK_FULLDUPLEX)
231   {
232           im_net_link_new(bprintf("%s_UP",name_link), bw_initial, bw_trace,
233                        lat_initial, lat_trace, state_initial_link, state_trace,
234                        policy_initial_link, xbt_dict_new());
235           im_net_link_new(bprintf("%s_DOWN",name_link), bw_initial, bw_trace,
236                        lat_initial, lat_trace, state_initial_link, state_trace,
237                        policy_initial_link, xbt_dict_new());
238   }
239   else
240   {
241           im_net_link_new(name_link, bw_initial, bw_trace,
242                        lat_initial, lat_trace, state_initial_link, state_trace,
243                        policy_initial_link, xbt_dict_new());
244   }
245
246 }
247
248 static void im_net_create_resource(char *name,
249                                 double bw_initial,
250                                 tmgr_trace_t bw_trace,
251                                 double lat_initial,
252                                 tmgr_trace_t lat_trace,
253                                 e_surf_resource_state_t
254                                 state_initial,
255                                 tmgr_trace_t state_trace,
256                                 e_surf_link_sharing_policy_t policy,
257                                 xbt_dict_t properties)
258 {
259   im_net_link_new(name, bw_initial, bw_trace,
260                lat_initial, lat_trace, state_initial, state_trace,
261                policy, xbt_dict_new());
262 }
263
264 static void im_net_add_traces(void)
265 {
266   xbt_dict_cursor_t cursor = NULL;
267   char *trace_name, *elm;
268
269   static int called = 0;
270   if (called)
271     return;
272   called = 1;
273
274   /* connect all traces relative to network */
275   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
276     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
277     link_CM02_im_t link =
278         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
279
280     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
281                 trace_name, elm);
282     xbt_assert2(trace,
283                 "Cannot connect trace %s to link %s: trace undefined",
284                 trace_name, elm);
285
286     link->lmm_resource.state_event =
287         tmgr_history_add_trace(history, trace, 0.0, 0, link);
288   }
289
290   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
291     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
292     link_CM02_im_t link =
293         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
294
295     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
296                 trace_name, elm);
297     xbt_assert2(trace,
298                 "Cannot connect trace %s to link %s: trace undefined",
299                 trace_name, elm);
300
301     link->lmm_resource.power.event =
302         tmgr_history_add_trace(history, trace, 0.0, 0, link);
303   }
304
305   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
306     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
307     link_CM02_im_t link =
308         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
309
310     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
311                 trace_name, elm);
312     xbt_assert2(trace,
313                 "Cannot connect trace %s to link %s: trace undefined",
314                 trace_name, elm);
315
316     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
317   }
318 }
319
320 static void im_net_define_callbacks(const char *file)
321 {
322   /* Figuring out the network links */
323   surfxml_add_callback(STag_surfxml_link_cb_list, &im_net_parse_link_init);
324   surfxml_add_callback(ETag_surfxml_platform_cb_list, &im_net_add_traces);
325 }
326
327 static int im_net_resource_used(void *resource_id)
328 {
329   return lmm_constraint_used(network_im_maxmin_system,
330                              ((surf_resource_lmm_t)
331                               resource_id)->constraint);
332 }
333
334 static int im_net_action_unref(surf_action_t action)
335 {
336   action->refcount--;
337   if (!action->refcount) {
338     xbt_swag_remove(action, action->state_set);
339     if (((surf_action_network_CM02_im_t) action)->variable){
340       lmm_variable_free(network_im_maxmin_system,
341                         ((surf_action_network_CM02_im_t) action)->variable);
342     }
343     // remove action from the heap
344     heap_remove((surf_action_network_CM02_im_t) action);
345
346     xbt_swag_remove(action, im_net_modified_set);
347 #ifdef HAVE_TRACING
348     xbt_free(((surf_action_network_CM02_im_t) action)->src_name);
349     xbt_free(((surf_action_network_CM02_im_t) action)->dst_name);
350     if (action->category)
351       xbt_free(action->category);
352 #endif
353     surf_action_free(&action);
354     return 1;
355   }
356   return 0;
357 }
358
359
360
361 static void im_net_action_cancel(surf_action_t action)
362 {
363   surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
364
365   xbt_swag_remove(action, im_net_modified_set);
366   // remove action from the heap
367   heap_remove((surf_action_network_CM02_im_t) action);
368 }
369
370 void im_net_action_recycle(surf_action_t action)
371 {
372   return;
373 }
374
375 #ifdef HAVE_LATENCY_BOUND_TRACKING
376 int im_net_get_link_latency_limited(surf_action_t action)
377 {
378   return action->latency_limited;
379 }
380 #endif
381
382 double im_net_action_get_remains(surf_action_t action)
383 {
384   /* update remains before return it */
385   update_action_remaining(surf_get_clock());
386   return action->remains;
387 }
388
389 static void update_action_remaining(double now){
390   surf_action_network_CM02_im_t action = NULL;
391   double delta = 0.0;
392
393   xbt_swag_foreach(action, im_net_modified_set) {
394
395     if(action->suspended != 0){
396         continue;
397     }
398
399     delta = now - action->last_update;
400
401     double_update(&(action->generic_action.remains),
402                   lmm_variable_getvalue(action->variable) * delta);
403
404     if (action->generic_action.max_duration != NO_MAX_DURATION)
405       double_update(&(action->generic_action.max_duration), delta);
406
407     if ((action->generic_action.remains <= 0) &&
408         (lmm_get_variable_weight(action->variable) > 0)) {
409       action->generic_action.finish = surf_get_clock();
410       surf_network_model->action_state_set((surf_action_t) action,
411                                            SURF_ACTION_DONE);
412       heap_remove(action);
413     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
414                && (action->generic_action.max_duration <= 0)) {
415       action->generic_action.finish = surf_get_clock();
416       surf_network_model->action_state_set((surf_action_t) action,
417                                            SURF_ACTION_DONE);
418       heap_remove(action);
419     }
420
421     action->last_update = now;
422   }
423 }
424
425 static double im_net_share_resources(double now)
426 {
427   surf_action_network_CM02_im_t action = NULL;
428   double min=-1;
429   double value;
430
431   XBT_DEBUG("Before share resources, the size of modified actions set is %d", xbt_swag_size(im_net_modified_set));
432   update_action_remaining(now);
433
434   lmm_solve(network_im_maxmin_system);
435
436   XBT_DEBUG("After share resources, The size of modified actions set is %d", xbt_swag_size(im_net_modified_set));
437
438    xbt_swag_foreach(action, im_net_modified_set) {
439      if (GENERIC_ACTION(action).state_set != surf_network_model->states.running_action_set){
440        continue;
441      }
442
443      /* bogus priority, skip it */
444      if (GENERIC_ACTION(action).priority <= 0){
445          continue;
446      }
447
448      min = -1;
449      value = lmm_variable_getvalue(action->variable);
450      if (value > 0) {
451          if (GENERIC_ACTION(action).remains > 0) {
452              value = GENERIC_ACTION(action).remains / value;
453              min = now + value;
454          } else {
455              value = 0.0;
456              min = now;
457          }
458      }
459
460      if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
461          && (min == -1
462              || GENERIC_ACTION(action).start +
463              GENERIC_ACTION(action).max_duration < min)){
464        min =   GENERIC_ACTION(action).start +
465                GENERIC_ACTION(action).max_duration;
466      }
467
468      XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
469                 GENERIC_ACTION(action).start, now + value,
470                 GENERIC_ACTION(action).max_duration);
471
472
473
474      if (action->index_heap >= 0) {
475          heap_remove((surf_action_network_CM02_im_t) action);
476      }
477
478      if (min != -1) {
479          heap_insert((surf_action_network_CM02_im_t) action, min, NORMAL);
480          XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min, now);
481      }
482    }
483
484    //hereafter must have already the min value for this resource model
485    if(xbt_heap_size(im_net_action_heap) > 0 ){
486        min = xbt_heap_maxkey(im_net_action_heap) - now ;
487    }else{
488        min = -1;
489    }
490
491    XBT_DEBUG("The minimum with the HEAP %lf", min);
492
493
494   return min;
495 }
496
497 static void im_net_update_actions_state(double now, double delta)
498 {
499   surf_action_network_CM02_im_t action = NULL;
500
501   while ((xbt_heap_size(im_net_action_heap) > 0)
502          && (double_equals(xbt_heap_maxkey(im_net_action_heap), now))) {
503     action = xbt_heap_pop(im_net_action_heap);
504     XBT_DEBUG("Action %p: finish", action);
505     GENERIC_ACTION(action).finish = surf_get_clock();
506
507     // if I am wearing a latency heat
508     if( action->hat ==  LATENCY){
509         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
510                                            action->weight);
511         heap_remove(action);
512         action->last_update = surf_get_clock();
513
514         XBT_DEBUG("Action (%p) is not limited by latency anymore", action);
515 #ifdef HAVE_LATENCY_BOUND_TRACKING
516           GENERIC_ACTION(action).latency_limited = 0;
517 #endif
518
519     // if I am wearing a max_duration or normal hat
520     }else if( action->hat == MAX_DURATION || action->hat == NORMAL ){
521         // no need to communicate anymore
522         // assume that flows that reached max_duration have remaining of 0
523         GENERIC_ACTION(action).remains = 0;
524         action->generic_action.finish = surf_get_clock();
525               surf_network_model->action_state_set((surf_action_t) action,
526                                                    SURF_ACTION_DONE);
527         heap_remove(action);
528     }
529   }
530   return;
531 }
532
533 static void im_net_update_resource_state(void *id,
534                                       tmgr_trace_event_t event_type,
535                                       double value, double date)
536 {
537   link_CM02_im_t nw_link = id;
538   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
539   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
540   /*     value, event_type); */
541
542   if (event_type == nw_link->lmm_resource.power.event) {
543     double delta =
544         sg_weight_S_parameter / value - sg_weight_S_parameter /
545         (nw_link->lmm_resource.power.peak *
546          nw_link->lmm_resource.power.scale);
547     lmm_variable_t var = NULL;
548     lmm_element_t elem = NULL;
549     surf_action_network_CM02_im_t action = NULL;
550
551     nw_link->lmm_resource.power.peak = value;
552     lmm_update_constraint_bound(network_im_maxmin_system,
553                                 nw_link->lmm_resource.constraint,
554                                 sg_bandwidth_factor *
555                                 (nw_link->lmm_resource.power.peak *
556                                  nw_link->lmm_resource.power.scale));
557 #ifdef HAVE_TRACING
558     TRACE_surf_link_set_bandwidth(date, (char *)(((nw_link->lmm_resource).generic_resource).name),
559                                   sg_bandwidth_factor *
560                                   (nw_link->lmm_resource.power.peak *
561                                    nw_link->lmm_resource.power.scale));
562 #endif
563     if (sg_weight_S_parameter > 0) {
564       while ((var = lmm_get_var_from_cnst
565               (network_im_maxmin_system, nw_link->lmm_resource.constraint,
566                &elem))) {
567         action = lmm_variable_id(var);
568         action->weight += delta;
569         if (!(action->suspended))
570           lmm_update_variable_weight(network_im_maxmin_system,
571                                      action->variable, action->weight);
572       }
573     }
574     if (tmgr_trace_event_free(event_type))
575       nw_link->lmm_resource.power.event = NULL;
576   } else if (event_type == nw_link->lat_event) {
577     double delta = value - nw_link->lat_current;
578     lmm_variable_t var = NULL;
579     lmm_element_t elem = NULL;
580     surf_action_network_CM02_im_t action = NULL;
581
582     nw_link->lat_current = value;
583     while ((var = lmm_get_var_from_cnst
584             (network_im_maxmin_system, nw_link->lmm_resource.constraint,
585              &elem))) {
586       action = lmm_variable_id(var);
587       action->lat_current += delta;
588       action->weight += delta;
589       if (action->rate < 0)
590         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
591                                   sg_tcp_gamma / (2.0 *
592                                                   action->lat_current));
593       else {
594         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
595                                   min(action->rate,
596                                       sg_tcp_gamma / (2.0 *
597                                                       action->lat_current)));
598
599         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
600           XBT_INFO("Flow is limited BYBANDWIDTH");
601         } else {
602           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
603                 action->lat_current);
604         }
605       }
606       if (!(action->suspended))
607         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
608                                    action->weight);
609
610     }
611     if (tmgr_trace_event_free(event_type))
612       nw_link->lat_event = NULL;
613   } else if (event_type == nw_link->lmm_resource.state_event) {
614     if (value > 0)
615       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
616     else {
617       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
618       lmm_variable_t var = NULL;
619       lmm_element_t elem = NULL;
620
621       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
622       while ((var = lmm_get_var_from_cnst
623               (network_im_maxmin_system, cnst, &elem))) {
624         surf_action_t action = lmm_variable_id(var);
625
626         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
627             surf_action_state_get(action) == SURF_ACTION_READY) {
628           action->finish = date;
629           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
630         }
631       }
632     }
633     if (tmgr_trace_event_free(event_type))
634       nw_link->lmm_resource.state_event = NULL;
635   } else {
636     XBT_CRITICAL("Unknown event ! \n");
637     xbt_abort();
638   }
639
640   XBT_DEBUG("There were a resource state event, need to update actions related to the constraint (%p)", nw_link->lmm_resource.constraint);
641   return;
642 }
643
644
645 static surf_action_t im_net_communicate(const char *src_name,
646                                      const char *dst_name, double size,
647                                      double rate)
648 {
649   unsigned int i;
650   link_CM02_im_t link;
651   int failed = 0;
652   surf_action_network_CM02_im_t action = NULL;
653   double bandwidth_bound;
654   /* LARGE PLATFORMS HACK:
655      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
656      Use the cluster_id for ->id */
657
658   xbt_dynar_t back_route = NULL;
659   int constraints_per_variable = 0;
660   // I will need this route for some time so let's call get_route_no_cleanup
661   xbt_dynar_t route = global_routing->get_route_no_cleanup(src_name, dst_name);
662
663
664   if (sg_network_fullduplex == 1) {
665     back_route = global_routing->get_route(dst_name, src_name);
666   }
667
668   /* LARGE PLATFORMS HACK:
669      total_route_size = route_size + src->link_nb + dst->nb */
670
671   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
672   /* LARGE PLATFORMS HACK:
673      assert on total_route_size */
674   xbt_assert2(xbt_dynar_length(route),
675               "You're trying to send data from %s to %s but there is no connection between these two hosts.",
676               src_name, dst_name);
677
678   xbt_dynar_foreach(route, i, link) {
679     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
680       failed = 1;
681       break;
682     }
683   }
684   action =
685       surf_action_new(sizeof(s_surf_action_network_CM02_im_t), size,
686                       surf_network_model, failed);
687
688
689 #ifdef HAVE_LATENCY_BOUND_TRACKING
690   (action->generic_action).latency_limited = 0;
691 #endif
692
693   xbt_swag_insert(action, action->generic_action.state_set);
694   action->rate = rate;
695   action->index_heap = -1;
696   action->latency = 0.0;
697   action->weight = 0.0;
698   action->last_update = surf_get_clock();
699
700   bandwidth_bound = -1.0;
701   xbt_dynar_foreach(route, i, link) {
702     action->latency += link->lat_current;
703     action->weight +=
704         link->lat_current +
705         sg_weight_S_parameter /
706         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
707     if (bandwidth_bound < 0.0)
708       bandwidth_bound =
709           (*im_bandwidth_factor_callback) (size) *
710           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
711     else
712       bandwidth_bound =
713           min(bandwidth_bound,
714               (*im_bandwidth_factor_callback) (size) *
715               (link->lmm_resource.power.peak *
716                link->lmm_resource.power.scale));
717   }
718   /* LARGE PLATFORMS HACK:
719      Add src->link and dst->link latencies */
720   action->lat_current = action->latency;
721   action->latency *= (*im_latency_factor_callback) (size);
722   action->rate =
723       (*im_bandwidth_constraint_callback) (action->rate, bandwidth_bound,
724                                         size);
725
726   /* LARGE PLATFORMS HACK:
727      lmm_variable_new(..., total_route_size) */
728   if (back_route != NULL) {
729     constraints_per_variable =
730         xbt_dynar_length(route) + xbt_dynar_length(back_route);
731   } else {
732     constraints_per_variable = xbt_dynar_length(route);
733   }
734
735   if (action->latency > 0){
736       action->variable =
737         lmm_variable_new(network_im_maxmin_system, action, 0.0, -1.0,
738                          constraints_per_variable);
739     // add to the heap the event when the latency is payed
740     XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency + action->last_update);
741     heap_insert(action, action->latency + action->last_update, LATENCY);
742 #ifdef HAVE_LATENCY_BOUND_TRACKING
743         (action->generic_action).latency_limited = 1;
744 #endif
745   }
746   else
747     action->variable =
748         lmm_variable_new(network_im_maxmin_system, action, 1.0, -1.0,
749                          constraints_per_variable);
750
751   if (action->rate < 0) {
752     if (action->lat_current > 0)
753       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
754                                 sg_tcp_gamma / (2.0 *
755                                                 action->lat_current));
756     else
757       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
758                                 -1.0);
759   } else {
760     if (action->lat_current > 0)
761       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
762                                 min(action->rate,
763                                     sg_tcp_gamma / (2.0 *
764                                                     action->lat_current)));
765     else
766       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
767                                 action->rate);
768   }
769
770   xbt_dynar_foreach(route, i, link) {
771     lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
772                action->variable, 1.0);
773   }
774
775   if (sg_network_fullduplex == 1) {
776     XBT_DEBUG("Fullduplex active adding backward flow using 5%c", '%');
777     xbt_dynar_foreach(back_route, i, link) {
778       lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
779                  action->variable, .05);
780     }
781   }
782
783   /* LARGE PLATFORMS HACK:
784      expand also with src->link and dst->link */
785 #ifdef HAVE_TRACING
786   action->src_name = xbt_strdup(src_name);
787
788   action->dst_name = xbt_strdup(dst_name);
789 #endif
790
791   xbt_dynar_free(&route);
792   XBT_OUT();
793
794   return (surf_action_t) action;
795 }
796
797 static xbt_dynar_t im_net_get_route(const char *src, const char *dst)
798 {
799   return global_routing->get_route(src, dst);
800 }
801
802 static double im_net_get_link_bandwidth(const void *link)
803 {
804   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
805   return lmm->power.peak * lmm->power.scale;
806 }
807
808 static double im_net_get_link_latency(const void *link)
809 {
810   return ((link_CM02_im_t) link)->lat_current;
811 }
812
813 static int im_net_link_shared(const void *link)
814 {
815   return
816       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
817 }
818
819 static void im_net_action_suspend(surf_action_t action)
820 {
821   ((surf_action_network_CM02_im_t) action)->suspended = 1;
822   lmm_update_variable_weight(network_im_maxmin_system,
823                              ((surf_action_network_CM02_im_t)
824                               action)->variable, 0.0);
825
826   // remove action from the heap
827   heap_remove((surf_action_network_CM02_im_t) action);
828 }
829
830 static void im_net_action_resume(surf_action_t action)
831 {
832   if (((surf_action_network_CM02_im_t) action)->suspended) {
833     lmm_update_variable_weight(network_im_maxmin_system,
834                                ((surf_action_network_CM02_im_t)
835                                 action)->variable,
836                                ((surf_action_network_CM02_im_t)
837                                 action)->weight);
838     ((surf_action_network_CM02_im_t) action)->suspended = 0;
839     // remove action from the heap
840     heap_remove((surf_action_network_CM02_im_t) action);
841   }
842 }
843
844 static int im_net_action_is_suspended(surf_action_t action)
845 {
846   return ((surf_action_network_CM02_im_t) action)->suspended;
847 }
848
849 void im_net_action_set_max_duration(surf_action_t action, double duration)
850 {
851   action->max_duration = duration;
852   // remove action from the heap
853   heap_remove((surf_action_network_CM02_im_t) action);
854 }
855
856
857 static void im_net_finalize(void)
858 {
859   surf_model_exit(surf_network_model);
860   surf_network_model = NULL;
861
862   global_routing->finalize();
863
864   lmm_system_free(network_im_maxmin_system);
865   network_im_maxmin_system = NULL;
866
867   xbt_heap_free(im_net_action_heap);
868   xbt_swag_free(im_net_modified_set);
869
870 }
871
872 static void im_surf_network_model_init_internal(void)
873 {
874   s_surf_action_network_CM02_im_t comm;
875   XBT_INFO("You are using the UNSAFE lazy management optimization, I hope you know what you are doing.");
876   XBT_INFO("====> For now this optimization is only available for LV08_im network model.");
877
878   surf_network_model = surf_model_init();
879
880   surf_network_model->name = "network";
881   surf_network_model->action_unref = im_net_action_unref;
882   surf_network_model->action_cancel = im_net_action_cancel;
883   surf_network_model->action_recycle = im_net_action_recycle;
884   surf_network_model->get_remains = im_net_action_get_remains;
885 #ifdef HAVE_LATENCY_BOUND_TRACKING
886   surf_network_model->get_latency_limited = im_net_get_link_latency_limited;
887 #endif
888
889   surf_network_model->model_private->resource_used = im_net_resource_used;
890   surf_network_model->model_private->share_resources = im_net_share_resources;
891   surf_network_model->model_private->update_actions_state =
892                   im_net_update_actions_state;
893   surf_network_model->model_private->update_resource_state =
894                   im_net_update_resource_state;
895   surf_network_model->model_private->finalize = im_net_finalize;
896
897   surf_network_model->suspend = im_net_action_suspend;
898   surf_network_model->resume = im_net_action_resume;
899   surf_network_model->is_suspended = im_net_action_is_suspended;
900   surf_cpu_model->set_max_duration = im_net_action_set_max_duration;
901
902   surf_network_model->extension.network.communicate = im_net_communicate;
903   surf_network_model->extension.network.get_route = im_net_get_route;
904   surf_network_model->extension.network.get_link_bandwidth =
905                   im_net_get_link_bandwidth;
906   surf_network_model->extension.network.get_link_latency =
907                   im_net_get_link_latency;
908   surf_network_model->extension.network.link_shared = im_net_link_shared;
909   surf_network_model->extension.network.add_traces = im_net_add_traces;
910   surf_network_model->extension.network.create_resource =
911                   im_net_create_resource;
912
913
914   if (!network_im_maxmin_system){
915         sg_maxmin_selective_update = 1;
916     network_im_maxmin_system = lmm_system_new();
917   }
918   im_net_action_heap = xbt_heap_new(8,NULL);
919
920   xbt_heap_set_update_callback(im_net_action_heap, im_net_action_update_index_heap);
921
922   routing_model_create(sizeof(link_CM02_im_t),        im_net_link_new(xbt_strdup("__loopback__"),
923                                                                       498000000, NULL, 0.000015, NULL,
924                                                                       SURF_RESOURCE_ON, NULL,
925                                                                       SURF_LINK_FATPIPE, NULL),
926                               im_net_get_link_latency);
927   im_net_modified_set =
928       xbt_swag_new(xbt_swag_offset(comm, action_list_hookup));
929   keep_track = im_net_modified_set;
930 }
931
932
933
934 /************************************************************************/
935 /* New model based on optimizations discussed during this thesis        */
936 /************************************************************************/
937 void im_surf_network_model_init_LegrandVelho(const char *filename)
938 {
939
940   if (surf_network_model)
941     return;
942   im_surf_network_model_init_internal();
943   im_net_define_callbacks(filename);
944   xbt_dynar_push(model_list, &surf_network_model);
945   network_im_solve = lmm_solve;
946
947   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
948   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
949                             0.92);
950   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
951
952   update_model_description(surf_network_model_description,
953                            "LV08_im", surf_network_model);
954 }
955
956
957