Logo AND Algorithmique Numérique Distribuée

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