Logo AND Algorithmique Numérique Distribuée

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