Logo AND Algorithmique Numérique Distribuée

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