Logo AND Algorithmique Numérique Distribuée

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