Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Direct context switching: clean the semantics of parmap
[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 void* im_net_create_resource(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_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
174               "Link '%s' declared several times in the platform file.",
175               name);
176
177   nw_link->lat_current = lat_initial;
178   if (lat_trace)
179     nw_link->lat_event =
180         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
181
182   if (policy == SURF_LINK_FATPIPE)
183     lmm_constraint_shared(nw_link->lmm_resource.constraint);
184
185   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
186
187
188   return nw_link;
189 }
190
191 static void im_net_parse_link_init(void)
192 {
193   if(struct_lnk->V_policy_initial_link == SURF_LINK_FULLDUPLEX)
194   {
195           im_net_create_resource(bprintf("%s_UP",struct_lnk->V_link_id), struct_lnk->V_link_bandwidth, struct_lnk->V_link_bandwidth_file,
196                        struct_lnk->V_link_latency, struct_lnk->V_link_latency_file, struct_lnk->V_link_state, struct_lnk->V_link_state_file,
197                        struct_lnk->V_policy_initial_link, xbt_dict_new());
198           im_net_create_resource(bprintf("%s_DOWN",struct_lnk->V_link_id), struct_lnk->V_link_bandwidth, struct_lnk->V_link_bandwidth_file,
199             struct_lnk->V_link_latency, struct_lnk->V_link_latency_file, struct_lnk->V_link_state, struct_lnk->V_link_state_file,
200                        struct_lnk->V_policy_initial_link, xbt_dict_new());
201   }
202   else
203   {
204           im_net_create_resource(xbt_strdup(struct_lnk->V_link_id), struct_lnk->V_link_bandwidth, struct_lnk->V_link_bandwidth_file,
205                 struct_lnk->V_link_latency, struct_lnk->V_link_latency_file, struct_lnk->V_link_state, struct_lnk->V_link_state_file,
206                        struct_lnk->V_policy_initial_link, xbt_dict_new());
207   }
208 }
209
210 static void im_net_add_traces(void)
211 {
212   xbt_dict_cursor_t cursor = NULL;
213   char *trace_name, *elm;
214
215   static int called = 0;
216   if (called)
217     return;
218   called = 1;
219
220   /* connect all traces relative to network */
221   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
222     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
223     link_CM02_im_t link =
224                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
225
226     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
227                 trace_name, elm);
228     xbt_assert(trace,
229                 "Cannot connect trace %s to link %s: trace undefined",
230                 trace_name, elm);
231
232     link->lmm_resource.state_event =
233         tmgr_history_add_trace(history, trace, 0.0, 0, link);
234   }
235
236   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
237     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
238     link_CM02_im_t link =
239                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
240
241     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
242                 trace_name, elm);
243     xbt_assert(trace,
244                 "Cannot connect trace %s to link %s: trace undefined",
245                 trace_name, elm);
246
247     link->lmm_resource.power.event =
248         tmgr_history_add_trace(history, trace, 0.0, 0, link);
249   }
250
251   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
252     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
253     link_CM02_im_t link =
254                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
255
256     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
257                 trace_name, elm);
258     xbt_assert(trace,
259                 "Cannot connect trace %s to link %s: trace undefined",
260                 trace_name, elm);
261
262     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
263   }
264 }
265
266 static void im_net_define_callbacks(const char *file)
267 {
268   /* Figuring out the network links */
269   surfxml_add_callback(STag_surfxml_link_cb_list, &im_net_parse_link_init);
270   surfxml_add_callback(ETag_surfxml_platform_cb_list, &im_net_add_traces);
271 }
272
273 static int im_net_resource_used(void *resource_id)
274 {
275   return lmm_constraint_used(network_im_maxmin_system,
276                              ((surf_resource_lmm_t)
277                               resource_id)->constraint);
278 }
279
280 static int im_net_action_unref(surf_action_t action)
281 {
282   action->refcount--;
283   if (!action->refcount) {
284     xbt_swag_remove(action, action->state_set);
285     if (((surf_action_network_CM02_im_t) action)->variable){
286       lmm_variable_free(network_im_maxmin_system,
287                         ((surf_action_network_CM02_im_t) action)->variable);
288     }
289     // remove action from the heap
290     heap_remove((surf_action_network_CM02_im_t) action);
291
292     xbt_swag_remove(action, im_net_modified_set);
293 #ifdef HAVE_TRACING
294     xbt_free(((surf_action_network_CM02_im_t) action)->src_name);
295     xbt_free(((surf_action_network_CM02_im_t) action)->dst_name);
296     if (action->category)
297       xbt_free(action->category);
298 #endif
299     surf_action_free(&action);
300     return 1;
301   }
302   return 0;
303 }
304
305
306
307 static void im_net_action_cancel(surf_action_t action)
308 {
309   surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
310
311   xbt_swag_remove(action, im_net_modified_set);
312   // remove action from the heap
313   heap_remove((surf_action_network_CM02_im_t) action);
314 }
315
316 void im_net_action_recycle(surf_action_t action)
317 {
318   return;
319 }
320
321 #ifdef HAVE_LATENCY_BOUND_TRACKING
322 int im_net_get_link_latency_limited(surf_action_t action)
323 {
324   return action->latency_limited;
325 }
326 #endif
327
328 double im_net_action_get_remains(surf_action_t action)
329 {
330   /* update remains before return it */
331   update_action_remaining(surf_get_clock());
332   return action->remains;
333 }
334
335 static void update_action_remaining(double now){
336   surf_action_network_CM02_im_t action = NULL;
337   double delta = 0.0;
338
339   xbt_swag_foreach(action, im_net_modified_set) {
340
341     if(action->suspended != 0){
342         continue;
343     }
344
345     delta = now - action->last_update;
346
347     double_update(&(action->generic_action.remains),
348                   lmm_variable_getvalue(action->variable) * delta);
349
350     if (action->generic_action.max_duration != NO_MAX_DURATION)
351       double_update(&(action->generic_action.max_duration), delta);
352
353     if ((action->generic_action.remains <= 0) &&
354         (lmm_get_variable_weight(action->variable) > 0)) {
355       action->generic_action.finish = surf_get_clock();
356       surf_network_model->action_state_set((surf_action_t) action,
357                                            SURF_ACTION_DONE);
358       heap_remove(action);
359     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
360                && (action->generic_action.max_duration <= 0)) {
361       action->generic_action.finish = surf_get_clock();
362       surf_network_model->action_state_set((surf_action_t) action,
363                                            SURF_ACTION_DONE);
364       heap_remove(action);
365     }
366
367     action->last_update = now;
368   }
369 }
370
371 static double im_net_share_resources(double now)
372 {
373   surf_action_network_CM02_im_t action = NULL;
374   double min=-1;
375   double value;
376
377   XBT_DEBUG("Before share resources, the size of modified actions set is %d", xbt_swag_size(im_net_modified_set));
378   update_action_remaining(now);
379
380   lmm_solve(network_im_maxmin_system);
381
382   XBT_DEBUG("After share resources, The size of modified actions set is %d", xbt_swag_size(im_net_modified_set));
383
384    xbt_swag_foreach(action, im_net_modified_set) {
385      if (GENERIC_ACTION(action).state_set != surf_network_model->states.running_action_set){
386        continue;
387      }
388
389      /* bogus priority, skip it */
390      if (GENERIC_ACTION(action).priority <= 0){
391          continue;
392      }
393
394      min = -1;
395      value = lmm_variable_getvalue(action->variable);
396      if (value > 0) {
397          if (GENERIC_ACTION(action).remains > 0) {
398              value = GENERIC_ACTION(action).remains / value;
399              min = now + value;
400          } else {
401              value = 0.0;
402              min = now;
403          }
404      }
405
406      if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
407          && (min == -1
408              || GENERIC_ACTION(action).start +
409              GENERIC_ACTION(action).max_duration < min)){
410        min =   GENERIC_ACTION(action).start +
411                GENERIC_ACTION(action).max_duration;
412      }
413
414      XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
415                 GENERIC_ACTION(action).start, now + value,
416                 GENERIC_ACTION(action).max_duration);
417
418
419
420      if (action->index_heap >= 0) {
421          heap_remove((surf_action_network_CM02_im_t) action);
422      }
423
424      if (min != -1) {
425          heap_insert((surf_action_network_CM02_im_t) action, min, NORMAL);
426          XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min, now);
427      }
428    }
429
430    //hereafter must have already the min value for this resource model
431    if(xbt_heap_size(im_net_action_heap) > 0 ){
432        min = xbt_heap_maxkey(im_net_action_heap) - now ;
433    }else{
434        min = -1;
435    }
436
437    XBT_DEBUG("The minimum with the HEAP %lf", min);
438
439
440   return min;
441 }
442
443 static void im_net_update_actions_state(double now, double delta)
444 {
445   surf_action_network_CM02_im_t action = NULL;
446
447   while ((xbt_heap_size(im_net_action_heap) > 0)
448          && (double_equals(xbt_heap_maxkey(im_net_action_heap), now))) {
449     action = xbt_heap_pop(im_net_action_heap);
450     XBT_DEBUG("Action %p: finish", action);
451     GENERIC_ACTION(action).finish = surf_get_clock();
452
453     // if I am wearing a latency heat
454     if( action->hat ==  LATENCY){
455         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
456                                            action->weight);
457         heap_remove(action);
458         action->last_update = surf_get_clock();
459
460         XBT_DEBUG("Action (%p) is not limited by latency anymore", action);
461 #ifdef HAVE_LATENCY_BOUND_TRACKING
462           GENERIC_ACTION(action).latency_limited = 0;
463 #endif
464
465     // if I am wearing a max_duration or normal hat
466     }else if( action->hat == MAX_DURATION || action->hat == NORMAL ){
467         // no need to communicate anymore
468         // assume that flows that reached max_duration have remaining of 0
469         GENERIC_ACTION(action).remains = 0;
470         action->generic_action.finish = surf_get_clock();
471               surf_network_model->action_state_set((surf_action_t) action,
472                                                    SURF_ACTION_DONE);
473         heap_remove(action);
474     }
475   }
476   return;
477 }
478
479 static void im_net_update_resource_state(void *id,
480                                       tmgr_trace_event_t event_type,
481                                       double value, double date)
482 {
483   link_CM02_im_t nw_link = id;
484   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
485   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
486   /*     value, event_type); */
487
488   if (event_type == nw_link->lmm_resource.power.event) {
489     double delta =
490         sg_weight_S_parameter / value - sg_weight_S_parameter /
491         (nw_link->lmm_resource.power.peak *
492          nw_link->lmm_resource.power.scale);
493     lmm_variable_t var = NULL;
494     lmm_element_t elem = NULL;
495     surf_action_network_CM02_im_t action = NULL;
496
497     nw_link->lmm_resource.power.peak = value;
498     lmm_update_constraint_bound(network_im_maxmin_system,
499                                 nw_link->lmm_resource.constraint,
500                                 sg_bandwidth_factor *
501                                 (nw_link->lmm_resource.power.peak *
502                                  nw_link->lmm_resource.power.scale));
503 #ifdef HAVE_TRACING
504     TRACE_surf_link_set_bandwidth(date, (char *)(((nw_link->lmm_resource).generic_resource).name),
505                                   sg_bandwidth_factor *
506                                   (nw_link->lmm_resource.power.peak *
507                                    nw_link->lmm_resource.power.scale));
508 #endif
509     if (sg_weight_S_parameter > 0) {
510       while ((var = lmm_get_var_from_cnst
511               (network_im_maxmin_system, nw_link->lmm_resource.constraint,
512                &elem))) {
513         action = lmm_variable_id(var);
514         action->weight += delta;
515         if (!(action->suspended))
516           lmm_update_variable_weight(network_im_maxmin_system,
517                                      action->variable, action->weight);
518       }
519     }
520     if (tmgr_trace_event_free(event_type))
521       nw_link->lmm_resource.power.event = NULL;
522   } else if (event_type == nw_link->lat_event) {
523     double delta = value - nw_link->lat_current;
524     lmm_variable_t var = NULL;
525     lmm_element_t elem = NULL;
526     surf_action_network_CM02_im_t action = NULL;
527
528     nw_link->lat_current = value;
529     while ((var = lmm_get_var_from_cnst
530             (network_im_maxmin_system, nw_link->lmm_resource.constraint,
531              &elem))) {
532       action = lmm_variable_id(var);
533       action->lat_current += delta;
534       action->weight += delta;
535       if (action->rate < 0)
536         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
537                                   sg_tcp_gamma / (2.0 *
538                                                   action->lat_current));
539       else {
540         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
541                                   min(action->rate,
542                                       sg_tcp_gamma / (2.0 *
543                                                       action->lat_current)));
544
545         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
546           XBT_INFO("Flow is limited BYBANDWIDTH");
547         } else {
548           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
549                 action->lat_current);
550         }
551       }
552       if (!(action->suspended))
553         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
554                                    action->weight);
555
556     }
557     if (tmgr_trace_event_free(event_type))
558       nw_link->lat_event = NULL;
559   } else if (event_type == nw_link->lmm_resource.state_event) {
560     if (value > 0)
561       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
562     else {
563       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
564       lmm_variable_t var = NULL;
565       lmm_element_t elem = NULL;
566
567       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
568       while ((var = lmm_get_var_from_cnst
569               (network_im_maxmin_system, cnst, &elem))) {
570         surf_action_t action = lmm_variable_id(var);
571
572         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
573             surf_action_state_get(action) == SURF_ACTION_READY) {
574           action->finish = date;
575           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
576         }
577       }
578     }
579     if (tmgr_trace_event_free(event_type))
580       nw_link->lmm_resource.state_event = NULL;
581   } else {
582     XBT_CRITICAL("Unknown event ! \n");
583     xbt_abort();
584   }
585
586   XBT_DEBUG("There were a resource state event, need to update actions related to the constraint (%p)", nw_link->lmm_resource.constraint);
587   return;
588 }
589
590
591 static surf_action_t im_net_communicate(const char *src_name,
592                                      const char *dst_name, double size,
593                                      double rate)
594 {
595   unsigned int i;
596   link_CM02_im_t link;
597   int failed = 0;
598   surf_action_network_CM02_im_t action = NULL;
599   double bandwidth_bound;
600   /* LARGE PLATFORMS HACK:
601      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
602      Use the cluster_id for ->id */
603
604   xbt_dynar_t back_route = NULL;
605   int constraints_per_variable = 0;
606   // I will need this route for some time so let's call get_route_no_cleanup
607   xbt_dynar_t route = global_routing->get_route_no_cleanup(src_name, dst_name);
608
609
610   if (sg_network_fullduplex == 1) {
611     back_route = global_routing->get_route(dst_name, src_name);
612   }
613
614   /* LARGE PLATFORMS HACK:
615      total_route_size = route_size + src->link_nb + dst->nb */
616
617   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
618   /* LARGE PLATFORMS HACK:
619      assert on total_route_size */
620   xbt_assert(xbt_dynar_length(route),
621               "You're trying to send data from %s to %s but there is no connection between these two hosts.",
622               src_name, dst_name);
623
624   xbt_dynar_foreach(route, i, link) {
625     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
626       failed = 1;
627       break;
628     }
629   }
630   action =
631       surf_action_new(sizeof(s_surf_action_network_CM02_im_t), size,
632                       surf_network_model, failed);
633
634
635 #ifdef HAVE_LATENCY_BOUND_TRACKING
636   (action->generic_action).latency_limited = 0;
637 #endif
638
639   xbt_swag_insert(action, action->generic_action.state_set);
640   action->rate = rate;
641   action->index_heap = -1;
642   action->latency = 0.0;
643   action->weight = 0.0;
644   action->last_update = surf_get_clock();
645
646   bandwidth_bound = -1.0;
647   xbt_dynar_foreach(route, i, link) {
648     action->latency += link->lat_current;
649     action->weight +=
650         link->lat_current +
651         sg_weight_S_parameter /
652         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
653     if (bandwidth_bound < 0.0)
654       bandwidth_bound =
655           (*im_bandwidth_factor_callback) (size) *
656           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
657     else
658       bandwidth_bound =
659           min(bandwidth_bound,
660               (*im_bandwidth_factor_callback) (size) *
661               (link->lmm_resource.power.peak *
662                link->lmm_resource.power.scale));
663   }
664   /* LARGE PLATFORMS HACK:
665      Add src->link and dst->link latencies */
666   action->lat_current = action->latency;
667   action->latency *= (*im_latency_factor_callback) (size);
668   action->rate =
669       (*im_bandwidth_constraint_callback) (action->rate, bandwidth_bound,
670                                         size);
671
672   /* LARGE PLATFORMS HACK:
673      lmm_variable_new(..., total_route_size) */
674   if (back_route != NULL) {
675     constraints_per_variable =
676         xbt_dynar_length(route) + xbt_dynar_length(back_route);
677   } else {
678     constraints_per_variable = xbt_dynar_length(route);
679   }
680
681   if (action->latency > 0){
682       action->variable =
683         lmm_variable_new(network_im_maxmin_system, action, 0.0, -1.0,
684                          constraints_per_variable);
685     // add to the heap the event when the latency is payed
686     XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency + action->last_update);
687     heap_insert(action, action->latency + action->last_update, LATENCY);
688 #ifdef HAVE_LATENCY_BOUND_TRACKING
689         (action->generic_action).latency_limited = 1;
690 #endif
691   }
692   else
693     action->variable =
694         lmm_variable_new(network_im_maxmin_system, action, 1.0, -1.0,
695                          constraints_per_variable);
696
697   if (action->rate < 0) {
698     if (action->lat_current > 0)
699       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
700                                 sg_tcp_gamma / (2.0 *
701                                                 action->lat_current));
702     else
703       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
704                                 -1.0);
705   } else {
706     if (action->lat_current > 0)
707       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
708                                 min(action->rate,
709                                     sg_tcp_gamma / (2.0 *
710                                                     action->lat_current)));
711     else
712       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
713                                 action->rate);
714   }
715
716   xbt_dynar_foreach(route, i, link) {
717     lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
718                action->variable, 1.0);
719   }
720
721   if (sg_network_fullduplex == 1) {
722     XBT_DEBUG("Fullduplex active adding backward flow using 5%c", '%');
723     xbt_dynar_foreach(back_route, i, link) {
724       lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
725                  action->variable, .05);
726     }
727   }
728
729   /* LARGE PLATFORMS HACK:
730      expand also with src->link and dst->link */
731 #ifdef HAVE_TRACING
732   if (TRACE_is_enabled()) {
733     action->src_name = xbt_strdup(src_name);
734     action->dst_name = xbt_strdup(dst_name);
735   } else {
736     action->src_name = action->dst_name = NULL;
737   }
738 #endif
739
740   xbt_dynar_free(&route);
741   XBT_OUT();
742
743   return (surf_action_t) action;
744 }
745
746 static xbt_dynar_t im_net_get_route(const char *src, const char *dst)
747 {
748   return global_routing->get_route(src, dst);
749 }
750
751 static double im_net_get_link_bandwidth(const void *link)
752 {
753   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
754   return lmm->power.peak * lmm->power.scale;
755 }
756
757 static double im_net_get_link_latency(const void *link)
758 {
759   return ((link_CM02_im_t) link)->lat_current;
760 }
761
762 static int im_net_link_shared(const void *link)
763 {
764   return
765       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
766 }
767
768 static void im_net_action_suspend(surf_action_t action)
769 {
770   ((surf_action_network_CM02_im_t) action)->suspended = 1;
771   lmm_update_variable_weight(network_im_maxmin_system,
772                              ((surf_action_network_CM02_im_t)
773                               action)->variable, 0.0);
774
775   // remove action from the heap
776   heap_remove((surf_action_network_CM02_im_t) action);
777 }
778
779 static void im_net_action_resume(surf_action_t action)
780 {
781   if (((surf_action_network_CM02_im_t) action)->suspended) {
782     lmm_update_variable_weight(network_im_maxmin_system,
783                                ((surf_action_network_CM02_im_t)
784                                 action)->variable,
785                                ((surf_action_network_CM02_im_t)
786                                 action)->weight);
787     ((surf_action_network_CM02_im_t) action)->suspended = 0;
788     // remove action from the heap
789     heap_remove((surf_action_network_CM02_im_t) action);
790   }
791 }
792
793 static int im_net_action_is_suspended(surf_action_t action)
794 {
795   return ((surf_action_network_CM02_im_t) action)->suspended;
796 }
797
798 void im_net_action_set_max_duration(surf_action_t action, double duration)
799 {
800   action->max_duration = duration;
801   // remove action from the heap
802   heap_remove((surf_action_network_CM02_im_t) action);
803 }
804
805
806 static void im_net_finalize(void)
807 {
808   surf_model_exit(surf_network_model);
809   surf_network_model = NULL;
810
811   global_routing->finalize();
812
813   lmm_system_free(network_im_maxmin_system);
814   network_im_maxmin_system = NULL;
815
816   xbt_heap_free(im_net_action_heap);
817   xbt_swag_free(im_net_modified_set);
818
819 }
820
821 static void im_surf_network_model_init_internal(void)
822 {
823   s_surf_action_network_CM02_im_t comm;
824
825   surf_network_model = surf_model_init();
826
827   surf_network_model->name = "network";
828   surf_network_model->action_unref = im_net_action_unref;
829   surf_network_model->action_cancel = im_net_action_cancel;
830   surf_network_model->action_recycle = im_net_action_recycle;
831   surf_network_model->get_remains = im_net_action_get_remains;
832 #ifdef HAVE_LATENCY_BOUND_TRACKING
833   surf_network_model->get_latency_limited = im_net_get_link_latency_limited;
834 #endif
835
836   surf_network_model->model_private->resource_used = im_net_resource_used;
837   surf_network_model->model_private->share_resources = im_net_share_resources;
838   surf_network_model->model_private->update_actions_state =
839                   im_net_update_actions_state;
840   surf_network_model->model_private->update_resource_state =
841                   im_net_update_resource_state;
842   surf_network_model->model_private->finalize = im_net_finalize;
843
844   surf_network_model->suspend = im_net_action_suspend;
845   surf_network_model->resume = im_net_action_resume;
846   surf_network_model->is_suspended = im_net_action_is_suspended;
847   surf_cpu_model->set_max_duration = im_net_action_set_max_duration;
848
849   surf_network_model->extension.network.communicate = im_net_communicate;
850   surf_network_model->extension.network.get_route = im_net_get_route;
851   surf_network_model->extension.network.get_link_bandwidth =
852                   im_net_get_link_bandwidth;
853   surf_network_model->extension.network.get_link_latency =
854                   im_net_get_link_latency;
855   surf_network_model->extension.network.link_shared = im_net_link_shared;
856   surf_network_model->extension.network.add_traces = im_net_add_traces;
857   surf_network_model->extension.network.create_resource =
858                   im_net_create_resource;
859
860
861   if (!network_im_maxmin_system){
862         sg_maxmin_selective_update = 1;
863     network_im_maxmin_system = lmm_system_new();
864   }
865   im_net_action_heap = xbt_heap_new(8,NULL);
866
867   xbt_heap_set_update_callback(im_net_action_heap, im_net_action_update_index_heap);
868
869   routing_model_create(sizeof(link_CM02_im_t),
870       im_net_create_resource(xbt_strdup("__loopback__"),
871           498000000, NULL, 0.000015, NULL,
872           SURF_RESOURCE_ON, NULL,
873           SURF_LINK_FATPIPE, NULL),
874           im_net_get_link_latency);
875   im_net_modified_set =
876       xbt_swag_new(xbt_swag_offset(comm, action_list_hookup));
877   keep_track = im_net_modified_set;
878 }
879
880
881
882 /************************************************************************/
883 /* New model based on optimizations discussed during this thesis        */
884 /************************************************************************/
885 void im_surf_network_model_init_LegrandVelho(const char *filename)
886 {
887
888   if (surf_network_model)
889     return;
890   im_surf_network_model_init_internal();
891   im_net_define_callbacks(filename);
892   xbt_dynar_push(model_list, &surf_network_model);
893   network_im_solve = lmm_solve;
894
895   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
896   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
897                             0.92);
898   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
899
900   update_model_description(surf_network_model_description,
901                            "LV08", surf_network_model);
902 }
903
904
905