Logo AND Algorithmique Numérique Distribuée

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