Logo AND Algorithmique Numérique Distribuée

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