Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow selective update to be independently set for cpu and network model.
[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 "network_private.h"
16 #include "xbt/log.h"
17 #include "xbt/str.h"
18
19 #include "surf_private.h"
20 #include "xbt/dict.h"
21 #include "maxmin_private.h"
22 #include "surf/surfxml_parse_values.h"
23 #include "surf/surf_resource.h"
24 #include "surf/surf_resource_lmm.h"
25
26
27 #undef GENERIC_ACTION
28 #define GENERIC_ACTION(action) action->generic_action
29
30
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
32                                 "Logging specific to the SURF network module");
33
34 surf_model_t surf_network_model = NULL;
35 static lmm_system_t network_im_maxmin_system = NULL;
36 static void (*network_im_solve) (lmm_system_t) = NULL;
37
38 double sg_sender_gap = 0.0;
39 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
40 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
41 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
42
43 double sg_tcp_gamma = 0.0;
44 int sg_network_crosstraffic = 0;
45
46 xbt_dict_t gap_lookup = NULL;
47
48 e_UM_t network_update_mechanism = UM_UNDEFINED;
49 static int selective_update = 0;
50
51 typedef struct network_link_CM02_im {
52   s_surf_resource_lmm_t lmm_resource;   /* must remain first to be added to a trace */
53
54   /* Using this object with the public part of
55      model does not make sense */
56   double lat_current;
57   tmgr_trace_event_t lat_event;
58 } s_link_CM02_im_t, *link_CM02_im_t;
59
60 static int im_net_action_is_suspended(surf_action_t action);
61 static void im_net_update_actions_state(double now, double delta);
62 static void update_action_remaining(double now);
63
64 static xbt_swag_t im_net_modified_set = NULL;
65 static xbt_heap_t im_net_action_heap = NULL;
66 xbt_swag_t keep_track = NULL;
67 extern int sg_maxmin_selective_update;
68
69 #ifdef HAVE_SMPI
70 static void gap_append(double size, const link_CM02_im_t link, surf_action_network_CM02_im_t action);
71 static void gap_unknown(surf_action_network_CM02_im_t action);
72 static void gap_remove(surf_action_network_CM02_im_t action);
73 #endif
74
75 /* added to manage the communication action's heap */
76 static void im_net_action_update_index_heap(void *action, int i)
77 {
78   ((surf_action_network_CM02_im_t) action)->index_heap = i;
79 }
80
81 /* insert action on heap using a given key and a hat (heap_action_type)
82  * a hat can be of three types for communications:
83  *
84  * NORMAL = this is a normal heap entry stating the date to finish transmitting
85  * LATENCY = this is a heap entry to warn us when the latency is payed
86  * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
87  */
88 static void heap_insert(surf_action_network_CM02_im_t    action, double key, enum heap_action_type hat){
89   action->hat = hat;
90   xbt_heap_push(im_net_action_heap, action, key);
91 }
92
93 static void heap_remove(surf_action_network_CM02_im_t action){
94   action->hat = NONE;
95   if(((surf_action_network_CM02_im_t) action)->index_heap >= 0){
96       xbt_heap_remove(im_net_action_heap,action->index_heap);
97   }
98 }
99
100 /******************************************************************************/
101 /*                           Factors callbacks                                */
102 /******************************************************************************/
103 static double im_constant_latency_factor(double size)
104 {
105   return sg_latency_factor;
106 }
107
108 static double im_constant_bandwidth_factor(double size)
109 {
110   return sg_bandwidth_factor;
111 }
112
113 static double im_constant_bandwidth_constraint(double rate, double bound,
114                                             double size)
115 {
116   return rate;
117 }
118
119
120 /**********************/
121 /*   SMPI callbacks   */
122 /**********************/
123 static double smpi_latency_factor(double size)
124 {
125   /* 1 B <= size <= 1 KiB */
126   if (size <= 1024.0) {
127     return 1.0056;
128   }
129
130   /* 2 KiB <= size <= 32 KiB */
131   if (size <= 32768.0) {
132     return 1.8805;
133   }
134
135   /* 64 KiB <= size <= 4 MiB */
136   return 22.7111;
137 }
138
139 static double smpi_bandwidth_factor(double size)
140 {
141   /* 1 B <= size <= 1 KiB */
142   if (size <= 1024.0) {
143     return 0.2758;
144   }
145
146   /* 2 KiB <= size <= 32 KiB */
147   if (size <= 32768.0) {
148     return 0.5477;
149   }
150
151   /* 64 KiB <= size <= 4 MiB */
152   return 0.9359;
153 }
154
155 static double smpi_bandwidth_constraint(double rate, double bound,
156                                         double size)
157 {
158   return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
159 }
160
161 static double (*im_latency_factor_callback) (double) =
162     &im_constant_latency_factor;
163 static double (*im_bandwidth_factor_callback) (double) =
164     &im_constant_bandwidth_factor;
165 static double (*im_bandwidth_constraint_callback) (double, double, double) =
166     &im_constant_bandwidth_constraint;
167
168
169 static void* im_net_create_resource(const char *name,
170                                 double bw_initial,
171                                 tmgr_trace_t bw_trace,
172                                 double lat_initial,
173                                 tmgr_trace_t lat_trace,
174                                 e_surf_resource_state_t
175                                 state_initial,
176                                 tmgr_trace_t state_trace,
177                                 e_surf_link_sharing_policy_t
178                                 policy, xbt_dict_t properties)
179 {
180   link_CM02_im_t nw_link = (link_CM02_im_t)
181       surf_resource_lmm_new(sizeof(s_link_CM02_im_t),
182                             surf_network_model, name, properties,
183                             network_im_maxmin_system,
184                             sg_bandwidth_factor * bw_initial,
185                             history,
186                             state_initial, state_trace,
187                             bw_initial, bw_trace);
188
189   xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
190               "Link '%s' declared several times in the platform file.",
191               name);
192
193   nw_link->lat_current = lat_initial;
194   if (lat_trace)
195     nw_link->lat_event =
196         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
197
198   if (policy == SURF_LINK_FATPIPE)
199     lmm_constraint_shared(nw_link->lmm_resource.constraint);
200
201   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
202
203   return nw_link;
204 }
205
206 static void im_net_parse_link_init(sg_platf_link_cbarg_t link)
207 {
208   if(link->policy == SURF_LINK_FULLDUPLEX){
209     char *link_id;
210     link_id = bprintf("%s_UP", link->id);
211           im_net_create_resource(link_id,
212                            link->bandwidth,
213                            link->bandwidth_trace,
214                            link->latency,
215                            link->latency_trace,
216                            link->state,
217                            link->state_trace,
218                            link->policy,
219                            link->properties);
220           xbt_free(link_id);
221     link_id = bprintf("%s_DOWN", link->id);
222     im_net_create_resource(link_id,
223                         link->bandwidth,
224                         link->bandwidth_trace,
225                         link->latency,
226                         link->latency_trace,
227                         link->state,
228                         link->state_trace,
229                         link->policy,
230                         link->properties);
231     xbt_free(link_id);
232   }
233   else{
234     im_net_create_resource(link->id,
235                         link->bandwidth,
236                         link->bandwidth_trace,
237                         link->latency,
238                         link->latency_trace,
239                         link->state,
240                         link->state_trace,
241                         link->policy,
242                         link->properties);
243   }
244 }
245
246 static void im_net_add_traces(void)
247 {
248   xbt_dict_cursor_t cursor = NULL;
249   char *trace_name, *elm;
250
251   static int called = 0;
252   if (called)
253     return;
254   called = 1;
255
256   /* connect all traces relative to network */
257   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
258     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
259     link_CM02_im_t link =
260                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
261
262     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
263                 trace_name, elm);
264     xbt_assert(trace,
265                 "Cannot connect trace %s to link %s: trace undefined",
266                 trace_name, elm);
267
268     link->lmm_resource.state_event =
269         tmgr_history_add_trace(history, trace, 0.0, 0, link);
270   }
271
272   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
273     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
274     link_CM02_im_t link =
275                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
276
277     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
278                 trace_name, elm);
279     xbt_assert(trace,
280                 "Cannot connect trace %s to link %s: trace undefined",
281                 trace_name, elm);
282
283     link->lmm_resource.power.event =
284         tmgr_history_add_trace(history, trace, 0.0, 0, link);
285   }
286
287   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
288     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
289     link_CM02_im_t link =
290                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
291
292     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
293                 trace_name, elm);
294     xbt_assert(trace,
295                 "Cannot connect trace %s to link %s: trace undefined",
296                 trace_name, elm);
297
298     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
299   }
300 }
301
302 static void im_net_define_callbacks(void)
303 {
304   /* Figuring out the network links */
305   sg_platf_link_add_cb(im_net_parse_link_init);
306   sg_platf_postparse_add_cb(im_net_add_traces);
307 }
308
309 static int im_net_resource_used(void *resource_id)
310 {
311   return lmm_constraint_used(network_im_maxmin_system,
312                              ((surf_resource_lmm_t)
313                               resource_id)->constraint);
314 }
315
316 static int im_net_action_unref(surf_action_t action)
317 {
318   action->refcount--;
319   if (!action->refcount) {
320     xbt_swag_remove(action, action->state_set);
321     if (((surf_action_network_CM02_im_t) action)->variable){
322       lmm_variable_free(network_im_maxmin_system,
323                         ((surf_action_network_CM02_im_t) action)->variable);
324     }
325     if(network_update_mechanism == UM_LAZY){// remove action from the heap
326       heap_remove((surf_action_network_CM02_im_t) action);
327       xbt_swag_remove(action, im_net_modified_set);
328     }
329 #ifdef HAVE_TRACING
330     xbt_free(((surf_action_network_CM02_im_t) action)->src_name);
331     xbt_free(((surf_action_network_CM02_im_t) action)->dst_name);
332     xbt_free(action->category);
333 #endif
334     surf_action_free(&action);
335     return 1;
336   }
337   return 0;
338 }
339
340
341
342 static void im_net_action_cancel(surf_action_t action)
343 {
344   surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
345   if(network_update_mechanism == UM_LAZY){// remove action from the heap
346     xbt_swag_remove(action, im_net_modified_set);
347     heap_remove((surf_action_network_CM02_im_t) action);
348   }
349 }
350
351 void im_net_action_recycle(surf_action_t action)
352 {
353   return;
354 }
355
356 #ifdef HAVE_LATENCY_BOUND_TRACKING
357 static int im_net_get_link_latency_limited(surf_action_t action)
358 {
359   return action->latency_limited;
360 }
361 #endif
362
363 double im_net_action_get_remains(surf_action_t action)
364 {
365   if(network_update_mechanism == UM_LAZY)/* update remains before return it */
366     update_action_remaining(surf_get_clock());
367   return action->remains;
368 }
369
370 static void update_action_remaining(double now){
371   surf_action_network_CM02_im_t action = NULL;
372   double delta = 0.0;
373
374   xbt_swag_foreach(action, im_net_modified_set) {
375
376     if(action->suspended != 0){
377         continue;
378     }
379
380     delta = now - action->last_update;
381
382     double_update(&(action->generic_action.remains),
383                   lmm_variable_getvalue(action->variable) * delta);
384
385     if (action->generic_action.max_duration != NO_MAX_DURATION)
386       double_update(&(action->generic_action.max_duration), delta);
387
388     if ((action->generic_action.remains <= 0) &&
389         (lmm_get_variable_weight(action->variable) > 0)) {
390       action->generic_action.finish = surf_get_clock();
391       surf_network_model->action_state_set((surf_action_t) action,
392                                            SURF_ACTION_DONE);
393       heap_remove(action);
394     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
395                && (action->generic_action.max_duration <= 0)) {
396       action->generic_action.finish = surf_get_clock();
397       surf_network_model->action_state_set((surf_action_t) action,
398                                            SURF_ACTION_DONE);
399       heap_remove(action);
400     }
401
402     action->last_update = now;
403   }
404 }
405
406 static double net_share_resources_full(double now)
407 {
408   s_surf_action_network_CM02_im_t s_action;
409   surf_action_network_CM02_im_t action = NULL;
410   xbt_swag_t running_actions =
411       surf_network_model->states.running_action_set;
412   double min;
413
414   min = generic_maxmin_share_resources(running_actions,
415                                        xbt_swag_offset(s_action,
416                                                        variable),
417                                        network_im_maxmin_system,
418                                        network_im_solve);
419
420 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable)  )))
421
422   xbt_swag_foreach(action, running_actions) {
423 #ifdef HAVE_LATENCY_BOUND_TRACKING
424     if (lmm_is_variable_limited_by_latency(action->variable)) {
425       (action->generic_action).latency_limited = 1;
426     } else {
427       (action->generic_action).latency_limited = 0;
428     }
429 #endif
430     if (action->latency > 0) {
431       if (min < 0)
432         min = action->latency;
433       else if (action->latency < min)
434         min = action->latency;
435     }
436   }
437
438   XBT_DEBUG("Min of share resources %f", min);
439
440   return min;
441 }
442
443 static double net_share_resources_lazy(double now)
444 {
445   surf_action_network_CM02_im_t action = NULL;
446   double min=-1;
447   double value;
448
449   XBT_DEBUG("Before share resources, the size of modified actions set is %d", xbt_swag_size(im_net_modified_set));
450   update_action_remaining(now);
451
452   keep_track = im_net_modified_set;
453   lmm_solve(network_im_maxmin_system);
454   keep_track = NULL;
455
456   XBT_DEBUG("After share resources, The size of modified actions set is %d", xbt_swag_size(im_net_modified_set));
457
458    xbt_swag_foreach(action, im_net_modified_set) {
459      if (GENERIC_ACTION(action).state_set != surf_network_model->states.running_action_set){
460        continue;
461      }
462
463      /* bogus priority, skip it */
464      if (GENERIC_ACTION(action).priority <= 0){
465          continue;
466      }
467
468      min = -1;
469      value = lmm_variable_getvalue(action->variable);
470      if (value > 0) {
471          if (GENERIC_ACTION(action).remains > 0) {
472              value = GENERIC_ACTION(action).remains / value;
473              min = now + value;
474          } else {
475              value = 0.0;
476              min = now;
477          }
478      }
479
480      if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
481          && (min == -1
482              || GENERIC_ACTION(action).start +
483              GENERIC_ACTION(action).max_duration < min)){
484        min =   GENERIC_ACTION(action).start +
485                GENERIC_ACTION(action).max_duration;
486      }
487
488      XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
489                 GENERIC_ACTION(action).start, now + value,
490                 GENERIC_ACTION(action).max_duration);
491
492
493
494      if (action->index_heap >= 0) {
495          heap_remove((surf_action_network_CM02_im_t) action);
496      }
497
498      if (min != -1) {
499          heap_insert((surf_action_network_CM02_im_t) action, min, NORMAL);
500          XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min, now);
501      }
502    }
503
504    //hereafter must have already the min value for this resource model
505    if(xbt_heap_size(im_net_action_heap) > 0 ){
506        min = xbt_heap_maxkey(im_net_action_heap) - now ;
507    }else{
508        min = -1;
509    }
510
511    XBT_DEBUG("The minimum with the HEAP %lf", min);
512
513
514   return min;
515 }
516
517 static void net_update_actions_state_full(double now, double delta)
518 {
519   double deltap = 0.0;
520   surf_action_network_CM02_im_t action = NULL;
521   surf_action_network_CM02_im_t next_action = NULL;
522   xbt_swag_t running_actions =
523       surf_network_model->states.running_action_set;
524   /*
525      xbt_swag_t failed_actions =
526      surf_network_model->states.failed_action_set;
527    */
528
529   xbt_swag_foreach_safe(action, next_action, running_actions) {
530     deltap = delta;
531     if (action->latency > 0) {
532       if (action->latency > deltap) {
533         double_update(&(action->latency), deltap);
534         deltap = 0.0;
535       } else {
536         double_update(&(deltap), action->latency);
537         action->latency = 0.0;
538       }
539       if ((action->latency == 0.0) && !(action->suspended))
540         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
541                                    action->weight);
542     }
543 #ifdef HAVE_TRACING
544     if (TRACE_is_enabled()) {
545       xbt_dynar_t route=NULL;
546       routing_get_route_and_latency(action->src_name, action->dst_name,&route,NULL);
547       link_CM02_im_t link;
548       unsigned int i;
549       xbt_dynar_foreach(route, i, link) {
550         TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
551                                         action->generic_action.data,
552                                         (surf_action_t) action,
553                                         lmm_variable_getvalue
554                                         (action->variable), now - delta,
555                                         delta);
556       }
557     }
558 #endif
559     if(!lmm_get_number_of_cnst_from_var(network_im_maxmin_system, action->variable)) {
560         /* There is actually no link used, hence an infinite bandwidth.
561          * This happens often when using models like vivaldi.
562          * In such case, just make sure that the action completes immediately.
563          */
564       double_update(&(action->generic_action.remains),
565           action->generic_action.remains);
566     }
567     double_update(&(action->generic_action.remains),
568                   lmm_variable_getvalue(action->variable) * deltap);
569     if (action->generic_action.max_duration != NO_MAX_DURATION)
570       double_update(&(action->generic_action.max_duration), delta);
571
572     if ((action->generic_action.remains <= 0) &&
573         (lmm_get_variable_weight(action->variable) > 0)) {
574       action->generic_action.finish = surf_get_clock();
575       surf_network_model->action_state_set((surf_action_t) action,
576                                            SURF_ACTION_DONE);
577 #ifdef HAVE_SMPI
578       gap_remove(action);
579 #endif
580     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
581                && (action->generic_action.max_duration <= 0)) {
582       action->generic_action.finish = surf_get_clock();
583       surf_network_model->action_state_set((surf_action_t) action,
584                                            SURF_ACTION_DONE);
585 #ifdef HAVE_SMPI
586       gap_remove(action);
587 #endif
588     }
589   }
590
591   return;
592 }
593
594 static void net_update_actions_state_lazy(double now, double delta)
595 {
596   surf_action_network_CM02_im_t action = NULL;
597
598   while ((xbt_heap_size(im_net_action_heap) > 0)
599          && (double_equals(xbt_heap_maxkey(im_net_action_heap), now))) {
600     action = xbt_heap_pop(im_net_action_heap);
601     XBT_DEBUG("Action %p: finish", action);
602     GENERIC_ACTION(action).finish = surf_get_clock();
603
604     // if I am wearing a latency heat
605     if( action->hat ==  LATENCY){
606         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
607                                            action->weight);
608         heap_remove(action);
609         action->last_update = surf_get_clock();
610
611         XBT_DEBUG("Action (%p) is not limited by latency anymore", action);
612 #ifdef HAVE_LATENCY_BOUND_TRACKING
613           GENERIC_ACTION(action).latency_limited = 0;
614 #endif
615
616     // if I am wearing a max_duration or normal hat
617     }else if( action->hat == MAX_DURATION || action->hat == NORMAL ){
618         // no need to communicate anymore
619         // assume that flows that reached max_duration have remaining of 0
620         GENERIC_ACTION(action).remains = 0;
621         action->generic_action.finish = surf_get_clock();
622               surf_network_model->action_state_set((surf_action_t) action,
623                                                    SURF_ACTION_DONE);
624         heap_remove(action);
625     }
626   }
627   return;
628 }
629
630 static void net_update_resource_state(void *id,
631                                       tmgr_trace_event_t event_type,
632                                       double value, double date)
633 {
634   link_CM02_im_t nw_link = id;
635   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
636   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
637   /*     value, event_type); */
638
639   if (event_type == nw_link->lmm_resource.power.event) {
640     double delta =
641         sg_weight_S_parameter / value - sg_weight_S_parameter /
642         (nw_link->lmm_resource.power.peak *
643          nw_link->lmm_resource.power.scale);
644     lmm_variable_t var = NULL;
645     lmm_element_t elem = NULL;
646     surf_action_network_CM02_im_t action = NULL;
647
648     nw_link->lmm_resource.power.peak = value;
649     lmm_update_constraint_bound(network_im_maxmin_system,
650                                 nw_link->lmm_resource.constraint,
651                                 sg_bandwidth_factor *
652                                 (nw_link->lmm_resource.power.peak *
653                                  nw_link->lmm_resource.power.scale));
654 #ifdef HAVE_TRACING
655     TRACE_surf_link_set_bandwidth(date, (char *)(((nw_link->lmm_resource).generic_resource).name),
656                                   sg_bandwidth_factor *
657                                   (nw_link->lmm_resource.power.peak *
658                                    nw_link->lmm_resource.power.scale));
659 #endif
660     if (sg_weight_S_parameter > 0) {
661       while ((var = lmm_get_var_from_cnst
662               (network_im_maxmin_system, nw_link->lmm_resource.constraint,
663                &elem))) {
664         action = lmm_variable_id(var);
665         action->weight += delta;
666         if (!(action->suspended))
667           lmm_update_variable_weight(network_im_maxmin_system,
668                                      action->variable, action->weight);
669       }
670     }
671     if (tmgr_trace_event_free(event_type))
672       nw_link->lmm_resource.power.event = NULL;
673   } else if (event_type == nw_link->lat_event) {
674     double delta = value - nw_link->lat_current;
675     lmm_variable_t var = NULL;
676     lmm_element_t elem = NULL;
677     surf_action_network_CM02_im_t action = NULL;
678
679     nw_link->lat_current = value;
680     while ((var = lmm_get_var_from_cnst
681             (network_im_maxmin_system, nw_link->lmm_resource.constraint,
682              &elem))) {
683       action = lmm_variable_id(var);
684       action->lat_current += delta;
685       action->weight += delta;
686       if (action->rate < 0)
687         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
688                                   sg_tcp_gamma / (2.0 *
689                                                   action->lat_current));
690       else {
691         lmm_update_variable_bound(network_im_maxmin_system, action->variable,
692                                   min(action->rate,
693                                       sg_tcp_gamma / (2.0 *
694                                                       action->lat_current)));
695
696         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
697           XBT_INFO("Flow is limited BYBANDWIDTH");
698         } else {
699           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
700                 action->lat_current);
701         }
702       }
703       if (!(action->suspended))
704         lmm_update_variable_weight(network_im_maxmin_system, action->variable,
705                                    action->weight);
706
707     }
708     if (tmgr_trace_event_free(event_type))
709       nw_link->lat_event = NULL;
710   } else if (event_type == nw_link->lmm_resource.state_event) {
711     if (value > 0)
712       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
713     else {
714       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
715       lmm_variable_t var = NULL;
716       lmm_element_t elem = NULL;
717
718       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
719       while ((var = lmm_get_var_from_cnst
720               (network_im_maxmin_system, cnst, &elem))) {
721         surf_action_t action = lmm_variable_id(var);
722
723         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
724             surf_action_state_get(action) == SURF_ACTION_READY) {
725           action->finish = date;
726           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
727         }
728       }
729     }
730     if (tmgr_trace_event_free(event_type))
731       nw_link->lmm_resource.state_event = NULL;
732   } else {
733     XBT_CRITICAL("Unknown event ! \n");
734     xbt_abort();
735   }
736
737   XBT_DEBUG("There were a resource state event, need to update actions related to the constraint (%p)", nw_link->lmm_resource.constraint);
738   return;
739 }
740
741
742 static surf_action_t im_net_communicate(const char *src_name,
743                                      const char *dst_name, double size,
744                                      double rate)
745 {
746   unsigned int i;
747   link_CM02_im_t link;
748   int failed = 0;
749   surf_action_network_CM02_im_t action = NULL;
750   double bandwidth_bound;
751   double latency=0.0;
752   /* LARGE PLATFORMS HACK:
753      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
754      Use the cluster_id for ->id */
755
756   xbt_dynar_t back_route = NULL;
757   int constraints_per_variable = 0;
758   // I need to have the forward and backward routes at the same time, so allocate "route". That way, the routing wont clean it up
759   xbt_dynar_t route=xbt_dynar_new(global_routing->size_of_link,NULL);
760   routing_get_route_and_latency(src_name, dst_name, &route, &latency);
761
762   if (sg_network_crosstraffic == 1) {
763     // FIXME: fill route directly (unclear: check with blame who put the FIXME)
764     routing_get_route_and_latency(dst_name, src_name, &back_route,NULL);
765   }
766
767   /* LARGE PLATFORMS HACK:
768      total_route_size = route_size + src->link_nb + dst->nb */
769
770   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
771   /* LARGE PLATFORMS HACK:
772      assert on total_route_size */
773   xbt_assert(!xbt_dynar_is_empty(route) || latency,
774               "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
775               src_name, dst_name);
776
777   xbt_dynar_foreach(route, i, link) {
778     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
779       failed = 1;
780       break;
781     }
782   }
783   if (sg_network_crosstraffic == 1) {
784     xbt_dynar_foreach(back_route, i, link) {
785       if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
786         failed = 1;
787         break;
788       }
789     }
790   }
791
792   action =
793       surf_action_new(sizeof(s_surf_action_network_CM02_im_t), size,
794                       surf_network_model, failed);
795 #ifdef HAVE_LATENCY_BOUND_TRACKING
796   (action->generic_action).latency_limited = 0;
797 #endif
798   action->weight = action->latency = latency;
799
800   xbt_swag_insert(action, action->generic_action.state_set);
801   action->rate = rate;
802   if(network_update_mechanism == UM_LAZY){
803     action->index_heap = -1;
804     action->latency = 0.0;
805     action->weight = 0.0;
806     action->last_update = surf_get_clock();
807   }
808
809   bandwidth_bound = -1.0;
810   xbt_dynar_foreach(route, i, link) {
811     action->weight +=
812         sg_weight_S_parameter /
813         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
814     if (bandwidth_bound < 0.0)
815       bandwidth_bound =
816           im_bandwidth_factor_callback(size) *
817           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
818     else
819       bandwidth_bound =
820           min(bandwidth_bound,
821               im_bandwidth_factor_callback(size) *
822               (link->lmm_resource.power.peak *
823                link->lmm_resource.power.scale));
824   }
825   /* LARGE PLATFORMS HACK:
826      Add src->link and dst->link latencies */
827   action->lat_current = action->latency;
828   action->latency *= im_latency_factor_callback(size);
829   action->rate =
830       im_bandwidth_constraint_callback(action->rate, bandwidth_bound,
831                                         size);
832 #ifdef HAVE_SMPI
833   if(!xbt_dynar_is_empty(route)) {
834     link = *(link_CM02_im_t*)xbt_dynar_get_ptr(route, 0);
835     gap_append(size, link, action);
836     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
837            action, src_name, dst_name, action->sender.gap, action->latency);
838   } else {
839     gap_unknown(action);
840   }
841 #endif
842
843   /* LARGE PLATFORMS HACK:
844      lmm_variable_new(..., total_route_size) */
845   if (back_route != NULL) {
846     constraints_per_variable =
847         xbt_dynar_length(route) + xbt_dynar_length(back_route);
848   } else {
849     constraints_per_variable = xbt_dynar_length(route);
850   }
851
852   if (action->latency > 0){
853       action->variable =
854         lmm_variable_new(network_im_maxmin_system, action, 0.0, -1.0,
855                          constraints_per_variable);
856     if(network_update_mechanism == UM_LAZY){
857       // add to the heap the event when the latency is payed
858       XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency + action->last_update);
859       heap_insert(action, action->latency + action->last_update, LATENCY);
860     }
861 #ifdef HAVE_LATENCY_BOUND_TRACKING
862         (action->generic_action).latency_limited = 1;
863 #endif
864   }
865   else
866     action->variable =
867         lmm_variable_new(network_im_maxmin_system, action, 1.0, -1.0,
868                          constraints_per_variable);
869
870   if (action->rate < 0) {
871     if (action->lat_current > 0)
872       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
873                                 sg_tcp_gamma / (2.0 *
874                                                 action->lat_current));
875     else
876       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
877                                 -1.0);
878   } else {
879     if (action->lat_current > 0)
880       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
881                                 min(action->rate,
882                                     sg_tcp_gamma / (2.0 *
883                                                     action->lat_current)));
884     else
885       lmm_update_variable_bound(network_im_maxmin_system, action->variable,
886                                 action->rate);
887   }
888
889   xbt_dynar_foreach(route, i, link) {
890     lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
891                action->variable, 1.0);
892   }
893
894   if (sg_network_crosstraffic == 1) {
895     XBT_DEBUG("Fullduplex active adding backward flow using 5%c", '%');
896     xbt_dynar_foreach(back_route, i, link) {
897       lmm_expand(network_im_maxmin_system, link->lmm_resource.constraint,
898                  action->variable, .05);
899     }
900   }
901
902   /* LARGE PLATFORMS HACK:
903      expand also with src->link and dst->link */
904 #ifdef HAVE_TRACING
905   if (TRACE_is_enabled()) {
906     action->src_name = xbt_strdup(src_name);
907     action->dst_name = xbt_strdup(dst_name);
908   } else {
909     action->src_name = action->dst_name = NULL;
910   }
911 #endif
912
913   xbt_dynar_free(&route);
914   XBT_OUT();
915
916   return (surf_action_t) action;
917 }
918
919 static xbt_dynar_t im_net_get_route(const char *src, const char *dst)
920 {
921   xbt_dynar_t route=NULL;
922   routing_get_route_and_latency(src, dst,&route,NULL);
923   return route;
924 }
925
926 static double im_net_get_link_bandwidth(const void *link)
927 {
928   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
929   return lmm->power.peak * lmm->power.scale;
930 }
931
932 static double im_net_get_link_latency(const void *link)
933 {
934   return ((link_CM02_im_t) link)->lat_current;
935 }
936
937 static int im_net_link_shared(const void *link)
938 {
939   return
940       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
941 }
942
943 static void im_net_action_suspend(surf_action_t action)
944 {
945   ((surf_action_network_CM02_im_t) action)->suspended = 1;
946   lmm_update_variable_weight(network_im_maxmin_system,
947                              ((surf_action_network_CM02_im_t)
948                               action)->variable, 0.0);
949
950   if(network_update_mechanism == UM_LAZY)// remove action from the heap
951     heap_remove((surf_action_network_CM02_im_t) action);
952 }
953
954 static void im_net_action_resume(surf_action_t action)
955 {
956   if (((surf_action_network_CM02_im_t) action)->suspended) {
957     lmm_update_variable_weight(network_im_maxmin_system,
958                                ((surf_action_network_CM02_im_t)
959                                 action)->variable,
960                                ((surf_action_network_CM02_im_t)
961                                 action)->weight);
962     ((surf_action_network_CM02_im_t) action)->suspended = 0;
963     if(network_update_mechanism == UM_LAZY)// remove action from the heap
964       heap_remove((surf_action_network_CM02_im_t) action);
965   }
966 }
967
968 static int im_net_action_is_suspended(surf_action_t action)
969 {
970   return ((surf_action_network_CM02_im_t) action)->suspended;
971 }
972
973 void im_net_action_set_max_duration(surf_action_t action, double duration)
974 {
975   action->max_duration = duration;
976   if(network_update_mechanism == UM_LAZY)// remove action from the heap
977     heap_remove((surf_action_network_CM02_im_t) action);
978 }
979
980 #ifdef HAVE_TRACING
981 static void net_action_set_category(surf_action_t action, const char *category)
982 {
983   action->category = xbt_strdup (category);
984 }
985 #endif
986
987 static void im_net_finalize(void)
988 {
989   surf_model_exit(surf_network_model);
990   surf_network_model = NULL;
991
992   lmm_system_free(network_im_maxmin_system);
993   network_im_maxmin_system = NULL;
994
995   if(network_update_mechanism == UM_LAZY){
996     xbt_heap_free(im_net_action_heap);
997     xbt_swag_free(im_net_modified_set);
998   }
999 }
1000
1001 #ifdef HAVE_SMPI
1002 static void gap_append(double size, const link_CM02_im_t link, surf_action_network_CM02_im_t action) {
1003    const char* src = link->lmm_resource.generic_resource.name;
1004    xbt_fifo_t fifo;
1005    surf_action_network_CM02_im_t last_action;
1006    double bw;
1007
1008    if(sg_sender_gap > 0.0) {
1009       if(!gap_lookup) {
1010          gap_lookup = xbt_dict_new();
1011       }
1012       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, src);
1013       action->sender.gap = 0.0;
1014       if(fifo && xbt_fifo_size(fifo) > 0) {
1015          /* Compute gap from last send */
1016          last_action = (surf_action_network_CM02_im_t)xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
1017          bw = im_net_get_link_bandwidth(link);
1018          action->sender.gap = last_action->sender.gap + max(sg_sender_gap, last_action->sender.size / bw);
1019          action->latency += action->sender.gap;
1020       }
1021       /* Append action as last send */
1022       action->sender.link_name = link->lmm_resource.generic_resource.name;
1023       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
1024       if(!fifo) {
1025          fifo = xbt_fifo_new();
1026          xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
1027       }
1028       action->sender.fifo_item = xbt_fifo_push(fifo, action);
1029       action->sender.size = size;
1030    }
1031 }
1032
1033 static void gap_unknown(surf_action_network_CM02_im_t action) {
1034    action->sender.gap = 0.0;
1035    action->sender.link_name = NULL;
1036    action->sender.fifo_item = NULL;
1037    action->sender.size = 0.0;
1038 }
1039
1040 static void gap_remove(surf_action_network_CM02_im_t action) {
1041    xbt_fifo_t fifo;
1042    size_t size;
1043
1044    if(sg_sender_gap > 0.0 && action->sender.link_name && action->sender.fifo_item) {
1045       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
1046       xbt_fifo_remove_item(fifo, action->sender.fifo_item);
1047       size = xbt_fifo_size(fifo);
1048       if(size == 0) {
1049          xbt_fifo_free(fifo);
1050          xbt_dict_remove(gap_lookup, action->sender.link_name);
1051          size = xbt_dict_length(gap_lookup);
1052          if(size == 0) {
1053             xbt_dict_free(&gap_lookup);
1054          }
1055       }
1056    }
1057 }
1058 #endif
1059
1060 static void im_surf_network_model_init_internal(void)
1061 {
1062   s_surf_action_network_CM02_im_t comm;
1063   surf_network_model = surf_model_init();
1064
1065   surf_network_model->name = "network";
1066   surf_network_model->action_unref = im_net_action_unref;
1067   surf_network_model->action_cancel = im_net_action_cancel;
1068   surf_network_model->action_recycle = im_net_action_recycle;
1069   surf_network_model->get_remains = im_net_action_get_remains;
1070 #ifdef HAVE_LATENCY_BOUND_TRACKING
1071   surf_network_model->get_latency_limited = im_net_get_link_latency_limited;
1072 #endif
1073
1074   surf_network_model->model_private->resource_used = im_net_resource_used;
1075   if(network_update_mechanism == UM_LAZY) {
1076     surf_network_model->model_private->share_resources = net_share_resources_lazy;
1077     surf_network_model->model_private->update_actions_state = net_update_actions_state_lazy;
1078   } else if(network_update_mechanism == UM_FULL) {
1079     surf_network_model->model_private->share_resources = net_share_resources_full;
1080     surf_network_model->model_private->update_actions_state = net_update_actions_state_full;
1081   }
1082
1083   surf_network_model->model_private->update_resource_state =
1084                   net_update_resource_state;
1085   surf_network_model->model_private->finalize = im_net_finalize;
1086
1087   surf_network_model->suspend = im_net_action_suspend;
1088   surf_network_model->resume = im_net_action_resume;
1089   surf_network_model->is_suspended = im_net_action_is_suspended;
1090   surf_cpu_model->set_max_duration = im_net_action_set_max_duration;
1091
1092   surf_network_model->extension.network.communicate = im_net_communicate;
1093   surf_network_model->extension.network.get_route = im_net_get_route;
1094   surf_network_model->extension.network.get_link_bandwidth =
1095                   im_net_get_link_bandwidth;
1096   surf_network_model->extension.network.get_link_latency =
1097                   im_net_get_link_latency;
1098   surf_network_model->extension.network.link_shared = im_net_link_shared;
1099   surf_network_model->extension.network.add_traces = im_net_add_traces;
1100   surf_network_model->extension.network.create_resource =
1101                   im_net_create_resource;
1102
1103  if (!network_im_maxmin_system)
1104     network_im_maxmin_system = lmm_system_new(selective_update);
1105
1106  routing_model_create(sizeof(link_CM02_im_t),
1107       im_net_create_resource("__loopback__",
1108           498000000, NULL, 0.000015, NULL,
1109           SURF_RESOURCE_ON, NULL,
1110           SURF_LINK_FATPIPE, NULL));
1111
1112   if(network_update_mechanism == UM_LAZY){
1113     im_net_action_heap = xbt_heap_new(8,NULL);
1114     xbt_heap_set_update_callback(im_net_action_heap, im_net_action_update_index_heap);
1115     im_net_modified_set =
1116         xbt_swag_new(xbt_swag_offset(comm, action_list_hookup));
1117   }
1118 }
1119
1120 static void set_update_mechanism(void) {
1121   char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
1122   int select = xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
1123
1124   if(!strcmp(optim,"Full")) {
1125     network_update_mechanism = UM_FULL;
1126     selective_update = select;
1127   } else if (!strcmp(optim,"Lazy")) {
1128     network_update_mechanism = UM_LAZY;
1129     selective_update = 1;
1130     xbt_assert((select==1) || (xbt_cfg_is_default_value(_surf_cfg_set,"network/maxmin_selective_update")),
1131         "Disabling selective update while using the lazy update mechanism is dumb!");
1132   } else {
1133     xbt_die("Unsupported optimization (%s) for this model",optim);
1134   }
1135 }
1136
1137 /************************************************************************/
1138 /* New model based on LV08 and experimental results of MPI ping-pongs   */
1139 /************************************************************************/
1140 void surf_network_model_init_SMPI(void)
1141 {
1142
1143   if (surf_network_model)
1144     return;
1145   set_update_mechanism();
1146
1147   im_surf_network_model_init_internal();
1148   im_latency_factor_callback = &smpi_latency_factor;
1149   im_bandwidth_factor_callback = &smpi_bandwidth_factor;
1150   im_bandwidth_constraint_callback = &smpi_bandwidth_constraint;
1151   im_net_define_callbacks();
1152   xbt_dynar_push(model_list, &surf_network_model);
1153   network_im_solve = lmm_solve;
1154
1155   xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
1156   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1157 }
1158
1159 /************************************************************************/
1160 /* New model based on optimizations discussed during this thesis        */
1161 /************************************************************************/
1162 void im_surf_network_model_init_LegrandVelho(void)
1163 {
1164   char *model = xbt_cfg_get_string(_surf_cfg_set, "network/model");
1165
1166   if (surf_network_model)
1167     return;
1168
1169   if(!strcmp(model,"LV08_fullupdate")) {
1170     XBT_WARN("[*Deprecated*. Use --cfg=network/model:LV08 with option --cfg=network/optim:Full instead.]");
1171   }
1172   set_update_mechanism();
1173
1174   im_surf_network_model_init_internal();
1175   im_net_define_callbacks();
1176   xbt_dynar_push(model_list, &surf_network_model);
1177   network_im_solve = lmm_solve;
1178
1179   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4); // 13.01 when callibration is done without phase effects
1180   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",0.92);// 0.97 when callibration is done without phase effects
1181   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);       // 20537 when callibration is done without phase effects
1182 }
1183
1184 /***************************************************************************/
1185 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
1186 /***************************************************************************/
1187 /* @TechReport{      rr-lip2002-40, */
1188 /*   author        = {Henri Casanova and Loris Marchal}, */
1189 /*   institution   = {LIP}, */
1190 /*   title         = {A Network Model for Simulation of Grid Application}, */
1191 /*   number        = {2002-40}, */
1192 /*   month         = {oct}, */
1193 /*   year          = {2002} */
1194 /* } */
1195 void surf_network_model_init_CM02(void)
1196 {
1197
1198   if (surf_network_model)
1199     return;
1200
1201   set_update_mechanism();
1202   im_surf_network_model_init_internal();
1203   im_net_define_callbacks();
1204   xbt_dynar_push(model_list, &surf_network_model);
1205   network_im_solve = lmm_solve;
1206
1207   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1208   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor", 1.0);
1209   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1210 }
1211
1212 void surf_network_model_init_Reno(void)
1213 {
1214   if (surf_network_model)
1215     return;
1216
1217   set_update_mechanism();
1218   im_surf_network_model_init_internal();
1219   im_net_define_callbacks();
1220
1221   xbt_dynar_push(model_list, &surf_network_model);
1222   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1223                                     func_reno_fpi);
1224   network_im_solve = lagrange_solve;
1225
1226   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1227   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1228                             0.92);
1229   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1230 }
1231
1232
1233 void surf_network_model_init_Reno2(void)
1234 {
1235   if (surf_network_model)
1236     return;
1237
1238   set_update_mechanism();
1239   im_surf_network_model_init_internal();
1240   im_net_define_callbacks();
1241
1242   xbt_dynar_push(model_list, &surf_network_model);
1243   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1244                                     func_reno2_fpi);
1245   network_im_solve = lagrange_solve;
1246
1247   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1248   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1249                             0.92);
1250   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1251                             8775);
1252 }
1253
1254 void surf_network_model_init_Vegas(void)
1255 {
1256   if (surf_network_model)
1257     return;
1258
1259   set_update_mechanism();
1260   im_surf_network_model_init_internal();
1261   im_net_define_callbacks();
1262
1263   xbt_dynar_push(model_list, &surf_network_model);
1264   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1265                                     func_vegas_fpi);
1266   network_im_solve = lagrange_solve;
1267
1268   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1269   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1270                             0.92);
1271   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1272 }