Logo AND Algorithmique Numérique Distribuée

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