Logo AND Algorithmique Numérique Distribuée

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