Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
yet another correction for those factors, hope this is the last one ..
[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_LMM_ACTION
27 #undef GENERIC_ACTION
28 #define GENERIC_LMM_ACTION(action) (action)->generic_lmm_action
29 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
30
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
33                                 "Logging specific to the SURF network module");
34
35 surf_model_t surf_network_model = NULL;
36 static void (*network_solve) (lmm_system_t) = NULL;
37
38 xbt_dynar_t smpi_bw_factor = NULL;
39 xbt_dynar_t smpi_lat_factor = NULL;
40
41 typedef struct s_smpi_factor *smpi_factor_t;
42 typedef struct s_smpi_factor {
43   long factor;
44   double value;
45 } s_smpi_factor_t;
46
47
48 double sg_sender_gap = 0.0;
49 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
50 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
51 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
52
53 double sg_tcp_gamma = 0.0;
54 int sg_network_crosstraffic = 0;
55
56 xbt_dict_t gap_lookup = NULL;
57
58 /******************************************************************************/
59 /*                           Factors callbacks                                */
60 /******************************************************************************/
61 static double constant_latency_factor(double size)
62 {
63   return sg_latency_factor;
64 }
65
66 static double constant_bandwidth_factor(double size)
67 {
68   return sg_bandwidth_factor;
69 }
70
71 static double constant_bandwidth_constraint(double rate, double bound,
72                                             double size)
73 {
74   return rate;
75 }
76
77 /**********************/
78 /*   SMPI callbacks   */
79 /**********************/
80
81 static int factor_cmp(const void *pa, const void *pb)
82 {
83   return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor);
84 }
85
86
87 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
88 {
89   char *value = NULL;
90   unsigned int iter = 0;
91   s_smpi_factor_t fact;
92   xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
93
94   smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
95   radical_elements = xbt_str_split(smpi_coef_string, ";");
96   xbt_dynar_foreach(radical_elements, iter, value) {
97
98     radical_elements2 = xbt_str_split(value, ":");
99     if (xbt_dynar_length(radical_elements2) != 2)
100       xbt_die("Malformed radical for smpi factor!");
101     fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
102     fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
103     xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
104     XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
105     xbt_dynar_free(&radical_elements2);
106   }
107   xbt_dynar_free(&radical_elements);
108   iter=0;
109   xbt_dynar_sort(smpi_factor, &factor_cmp);
110   xbt_dynar_foreach(smpi_factor, iter, fact) {
111     XBT_DEBUG("ordered smpi_factor:\t%ld : %f", fact.factor, fact.value);
112
113   }
114   return smpi_factor;
115 }
116
117 static double smpi_bandwidth_factor(double size)
118 {
119   if (!smpi_bw_factor)
120     smpi_bw_factor =
121         parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/bw_factor"));
122
123   unsigned int iter = 0;
124   s_smpi_factor_t fact;
125   double current=1.0;
126   xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
127     if (size <= fact.factor) {
128       XBT_DEBUG("%lf <= %ld return %f", size, fact.factor, current);
129       return current;
130     }else
131       current=fact.value;
132   }
133
134   return 1.0;
135 }
136
137 static double smpi_latency_factor(double size)
138 {
139   if (!smpi_lat_factor)
140     smpi_lat_factor =
141         parse_factor(xbt_cfg_get_string(_surf_cfg_set, "smpi/lat_factor"));
142
143   unsigned int iter = 0;
144   s_smpi_factor_t fact;
145   double current=1.0;
146   xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
147     if (size <= fact.factor) {
148       XBT_DEBUG("%lf <= %ld return %f", size, fact.factor, current);
149       return current;
150     }else
151       current=fact.value;
152   }
153
154   return 1.0;
155 }
156
157 /**--------- <copy/paste C code snippet in surf/network.c> -----------*/
158
159 static double smpi_bandwidth_constraint(double rate, double bound,
160                                         double size)
161 {
162   return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
163 }
164
165 static double (*latency_factor_callback) (double) =
166     &constant_latency_factor;
167 static double (*bandwidth_factor_callback) (double) =
168     &constant_bandwidth_factor;
169 static double (*bandwidth_constraint_callback) (double, double, double) =
170     &constant_bandwidth_constraint;
171
172 static void (*gap_append) (double, const link_CM02_t,
173                            surf_action_network_CM02_t) = NULL;
174
175 static void *net_create_resource(const char *name,
176                                  double bw_initial,
177                                  tmgr_trace_t bw_trace,
178                                  double lat_initial,
179                                  tmgr_trace_t lat_trace,
180                                  e_surf_resource_state_t
181                                  state_initial,
182                                  tmgr_trace_t state_trace,
183                                  e_surf_link_sharing_policy_t
184                                  policy, xbt_dict_t properties)
185 {
186   link_CM02_t nw_link = (link_CM02_t)
187       surf_resource_lmm_new(sizeof(s_link_CM02_t),
188                             surf_network_model, name, properties,
189                             surf_network_model->model_private->maxmin_system,
190                             sg_bandwidth_factor * bw_initial,
191                             history,
192                             state_initial, state_trace,
193                             bw_initial, bw_trace);
194
195   xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
196              "Link '%s' declared several times in the platform file.",
197              name);
198
199   nw_link->lat_current = lat_initial;
200   if (lat_trace)
201     nw_link->lat_event =
202         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
203
204   if (policy == SURF_LINK_FATPIPE)
205     lmm_constraint_shared(nw_link->lmm_resource.constraint);
206
207   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
208   XBT_DEBUG("Create link '%s'",name);
209
210   return nw_link;
211 }
212
213 static void net_parse_link_init(sg_platf_link_cbarg_t link)
214 {
215   if (link->policy == SURF_LINK_FULLDUPLEX) {
216     char *link_id;
217     link_id = bprintf("%s_UP", link->id);
218     net_create_resource(link_id,
219                         link->bandwidth,
220                         link->bandwidth_trace,
221                         link->latency,
222                         link->latency_trace,
223                         link->state,
224                         link->state_trace, link->policy, link->properties);
225     xbt_free(link_id);
226     link_id = bprintf("%s_DOWN", link->id);
227     net_create_resource(link_id,
228                         link->bandwidth,
229                         link->bandwidth_trace,
230                         link->latency,
231                         link->latency_trace,
232                         link->state,
233                         link->state_trace, link->policy, link->properties);
234     xbt_free(link_id);
235   } else {
236     net_create_resource(link->id,
237                         link->bandwidth,
238                         link->bandwidth_trace,
239                         link->latency,
240                         link->latency_trace,
241                         link->state,
242                         link->state_trace, link->policy, link->properties);
243   }
244 }
245
246 static void 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_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
260
261     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
262                trace_name, elm);
263     xbt_assert(trace,
264                "Cannot connect trace %s to link %s: trace undefined",
265                trace_name, elm);
266
267     link->lmm_resource.state_event =
268         tmgr_history_add_trace(history, trace, 0.0, 0, link);
269   }
270
271   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
272     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
273     link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
274
275     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
276                trace_name, elm);
277     xbt_assert(trace,
278                "Cannot connect trace %s to link %s: trace undefined",
279                trace_name, elm);
280
281     link->lmm_resource.power.event =
282         tmgr_history_add_trace(history, trace, 0.0, 0, link);
283   }
284
285   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
286     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
287     link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
288
289     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
290                trace_name, elm);
291     xbt_assert(trace,
292                "Cannot connect trace %s to link %s: trace undefined",
293                trace_name, elm);
294
295     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
296   }
297 }
298
299 static void net_define_callbacks(void)
300 {
301   /* Figuring out the network links */
302   sg_platf_link_add_cb(net_parse_link_init);
303   sg_platf_postparse_add_cb(net_add_traces);
304 }
305
306 static int net_resource_used(void *resource_id)
307 {
308   return lmm_constraint_used(surf_network_model->model_private->maxmin_system, ((surf_resource_lmm_t)
309                                                      resource_id)->
310                              constraint);
311 }
312
313 void net_action_recycle(surf_action_t action)
314 {
315   return;
316 }
317
318 #ifdef HAVE_LATENCY_BOUND_TRACKING
319 int net_get_link_latency_limited(surf_action_t action)
320 {
321   return action->latency_limited;
322 }
323 #endif
324
325 static double net_share_resources_full(double now)
326 {
327   s_surf_action_lmm_t s_action;
328   surf_action_network_CM02_t action = NULL;
329   xbt_swag_t running_actions =
330       surf_network_model->states.running_action_set;
331   double min;
332
333   min = generic_maxmin_share_resources(running_actions,
334                                        xbt_swag_offset(s_action,
335                                                        variable),
336                                                        surf_network_model->model_private->maxmin_system,
337                                        network_solve);
338
339 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable)  )))
340
341   xbt_swag_foreach(action, running_actions) {
342 #ifdef HAVE_LATENCY_BOUND_TRACKING
343     if (lmm_is_variable_limited_by_latency(GENERIC_LMM_ACTION(action).variable)) {
344       action->latency_limited = 1;
345     } else {
346       action->latency_limited = 0;
347     }
348 #endif
349     if (action->latency > 0) {
350       min = (min < 0) ? action->latency : min(min, action->latency);
351     }
352   }
353
354   XBT_DEBUG("Min of share resources %f", min);
355
356   return min;
357 }
358
359 static double net_share_resources_lazy(double now)
360 {
361   return generic_share_resources_lazy(now, surf_network_model);
362 }
363
364 static void net_update_actions_state_full(double now, double delta)
365 {
366   generic_update_actions_state_full(now, delta, surf_network_model);
367 }
368
369 static void net_update_actions_state_lazy(double now, double delta)
370 {
371   generic_update_actions_state_lazy(now, delta, surf_network_model);
372 }
373
374 static void net_update_resource_state(void *id,
375                                       tmgr_trace_event_t event_type,
376                                       double value, double date)
377 {
378   link_CM02_t nw_link = id;
379   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
380   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
381   /*     value, event_type); */
382
383   if (event_type == nw_link->lmm_resource.power.event) {
384     double delta =
385         sg_weight_S_parameter / value - sg_weight_S_parameter /
386         (nw_link->lmm_resource.power.peak *
387          nw_link->lmm_resource.power.scale);
388     lmm_variable_t var = NULL;
389     lmm_element_t elem = NULL;
390     surf_action_network_CM02_t action = NULL;
391
392     nw_link->lmm_resource.power.peak = value;
393     lmm_update_constraint_bound(surf_network_model->model_private->maxmin_system,
394                                 nw_link->lmm_resource.constraint,
395                                 sg_bandwidth_factor *
396                                 (nw_link->lmm_resource.power.peak *
397                                  nw_link->lmm_resource.power.scale));
398 #ifdef HAVE_TRACING
399     TRACE_surf_link_set_bandwidth(date,
400                                   (char
401                                    *) (((nw_link->lmm_resource).
402                                         generic_resource).name),
403                                   sg_bandwidth_factor *
404                                   (nw_link->lmm_resource.power.peak *
405                                    nw_link->lmm_resource.power.scale));
406 #endif
407     if (sg_weight_S_parameter > 0) {
408       while ((var = lmm_get_var_from_cnst
409               (surf_network_model->model_private->maxmin_system, nw_link->lmm_resource.constraint,
410                &elem))) {
411         action = lmm_variable_id(var);
412         action->weight += delta;
413         if (!(GENERIC_LMM_ACTION(action).suspended))
414           lmm_update_variable_weight(surf_network_model->model_private->maxmin_system,
415                                      GENERIC_LMM_ACTION(action).variable, action->weight);
416       }
417     }
418     if (tmgr_trace_event_free(event_type))
419       nw_link->lmm_resource.power.event = NULL;
420   } else if (event_type == nw_link->lat_event) {
421     double delta = value - nw_link->lat_current;
422     lmm_variable_t var = NULL;
423     lmm_element_t elem = NULL;
424     surf_action_network_CM02_t action = NULL;
425
426     nw_link->lat_current = value;
427     while ((var = lmm_get_var_from_cnst
428             (surf_network_model->model_private->maxmin_system, nw_link->lmm_resource.constraint,
429              &elem))) {
430       action = lmm_variable_id(var);
431       action->lat_current += delta;
432       action->weight += delta;
433       if (action->rate < 0)
434         lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
435                                   sg_tcp_gamma / (2.0 *
436                                                   action->lat_current));
437       else {
438         lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
439                                   min(action->rate,
440                                       sg_tcp_gamma / (2.0 *
441                                                       action->
442                                                       lat_current)));
443
444         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
445           XBT_INFO("Flow is limited BYBANDWIDTH");
446         } else {
447           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
448                    action->lat_current);
449         }
450       }
451       if (!(GENERIC_LMM_ACTION(action).suspended))
452         lmm_update_variable_weight(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
453                                    action->weight);
454
455     }
456     if (tmgr_trace_event_free(event_type))
457       nw_link->lat_event = NULL;
458   } else if (event_type == nw_link->lmm_resource.state_event) {
459     if (value > 0)
460       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
461     else {
462       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
463       lmm_variable_t var = NULL;
464       lmm_element_t elem = NULL;
465
466       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
467       while ((var = lmm_get_var_from_cnst
468               (surf_network_model->model_private->maxmin_system, cnst, &elem))) {
469         surf_action_t action = lmm_variable_id(var);
470
471         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
472             surf_action_state_get(action) == SURF_ACTION_READY) {
473           action->finish = date;
474           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
475         }
476       }
477     }
478     if (tmgr_trace_event_free(event_type))
479       nw_link->lmm_resource.state_event = NULL;
480   } else {
481     XBT_CRITICAL("Unknown event ! \n");
482     xbt_abort();
483   }
484
485   XBT_DEBUG
486       ("There were a resource state event, need to update actions related to the constraint (%p)",
487        nw_link->lmm_resource.constraint);
488   return;
489 }
490
491
492 static surf_action_t net_communicate(sg_routing_edge_t src,
493                                      sg_routing_edge_t dst,
494                                      double size, double rate)
495 {
496   unsigned int i;
497   link_CM02_t link;
498   int failed = 0;
499   surf_action_network_CM02_t action = NULL;
500   double bandwidth_bound;
501   double latency = 0.0;
502   xbt_dynar_t back_route = NULL;
503   int constraints_per_variable = 0;
504
505   xbt_dynar_t route = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
506
507   XBT_IN("(%s,%s,%g,%g)", src->name, dst->name, size, rate);
508
509   routing_get_route_and_latency(src, dst, &route, &latency);
510   xbt_assert(!xbt_dynar_is_empty(route) || latency,
511              "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
512              src->name, dst->name);
513
514   xbt_dynar_foreach(route, i, link) {
515     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
516       failed = 1;
517       break;
518     }
519   }
520   if (sg_network_crosstraffic == 1) {
521     routing_get_route_and_latency(dst, src, &back_route, NULL);
522     xbt_dynar_foreach(back_route, i, link) {
523       if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
524         failed = 1;
525         break;
526       }
527     }
528   }
529
530   action =
531       surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
532                       surf_network_model, failed);
533 #ifdef HAVE_LATENCY_BOUND_TRACKING
534   action->latency_limited = 0;
535 #endif
536   action->weight = action->latency = latency;
537
538   xbt_swag_insert(action, ((surf_action_t)action)->state_set);
539   action->rate = rate;
540   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
541     GENERIC_LMM_ACTION(action).index_heap = -1;
542     GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
543   }
544
545   bandwidth_bound = -1.0;
546   if (sg_weight_S_parameter > 0) {
547     xbt_dynar_foreach(route, i, link) {
548       action->weight +=
549           sg_weight_S_parameter /
550           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
551     }
552   }
553   xbt_dynar_foreach(route, i, link) {
554     double bb = bandwidth_factor_callback(size) *
555         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
556     bandwidth_bound =
557         (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
558   }
559
560   action->lat_current = action->latency;
561   action->latency *= latency_factor_callback(size);
562   action->rate =
563       bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
564   if (gap_append) {
565     xbt_assert(!xbt_dynar_is_empty(route),
566                "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
567
568     link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
569     gap_append(size, link, action);
570     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
571               action, src->name, dst->name, action->sender.gap,
572               action->latency);
573   }
574
575   constraints_per_variable = xbt_dynar_length(route);
576   if (back_route != NULL)
577     constraints_per_variable += xbt_dynar_length(back_route);
578
579   if (action->latency > 0) {
580     GENERIC_LMM_ACTION(action).variable =
581         lmm_variable_new(surf_network_model->model_private->maxmin_system, action, 0.0, -1.0,
582                          constraints_per_variable);
583     if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
584       // add to the heap the event when the latency is payed
585       XBT_DEBUG("Added action (%p) one latency event at date %f", action,
586                 action->latency + GENERIC_LMM_ACTION(action).last_update);
587       surf_action_lmm_heap_insert(surf_network_model->model_private->action_heap,(surf_action_lmm_t)action, action->latency + GENERIC_LMM_ACTION(action).last_update,
588                   xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
589     }
590   } else
591     GENERIC_LMM_ACTION(action).variable =
592         lmm_variable_new(surf_network_model->model_private->maxmin_system, action, 1.0, -1.0,
593                          constraints_per_variable);
594
595   if (action->rate < 0) {
596     lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
597                               (action->lat_current > 0) ?
598                               sg_tcp_gamma / (2.0 *
599                                               action->lat_current) : -1.0);
600   } else {
601     lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
602                               (action->lat_current > 0) ?
603                               min(action->rate,
604                                   sg_tcp_gamma / (2.0 *
605                                                   action->lat_current))
606                               : action->rate);
607   }
608
609   xbt_dynar_foreach(route, i, link) {
610     lmm_expand(surf_network_model->model_private->maxmin_system, link->lmm_resource.constraint,
611                GENERIC_LMM_ACTION(action).variable, 1.0);
612   }
613
614   if (sg_network_crosstraffic == 1) {
615     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
616     xbt_dynar_foreach(back_route, i, link) {
617       lmm_expand(surf_network_model->model_private->maxmin_system, link->lmm_resource.constraint,
618                  GENERIC_LMM_ACTION(action).variable, .05);
619     }
620   }
621
622   xbt_dynar_free(&route);
623   XBT_OUT();
624
625   return (surf_action_t) action;
626 }
627
628 static xbt_dynar_t net_get_route(void *src, void *dst)
629 {
630   xbt_dynar_t route = NULL;
631   routing_get_route_and_latency(src, dst, &route, NULL);
632   return route;
633 }
634
635 static double net_get_link_bandwidth(const void *link)
636 {
637   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
638   return lmm->power.peak * lmm->power.scale;
639 }
640
641 static double net_get_link_latency(const void *link)
642 {
643   return ((link_CM02_t) link)->lat_current;
644 }
645
646 static int net_link_shared(const void *link)
647 {
648   return
649       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
650 }
651
652 static void net_finalize(void)
653 {
654   lmm_system_free(surf_network_model->model_private->maxmin_system);
655   surf_network_model->model_private->maxmin_system = NULL;
656
657   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
658     xbt_heap_free(surf_network_model->model_private->action_heap);
659     xbt_swag_free(surf_network_model->model_private->modified_set);
660   }
661
662   surf_model_exit(surf_network_model);
663   surf_network_model = NULL;
664
665   if (smpi_bw_factor)
666     xbt_dynar_free(&smpi_bw_factor);
667   if (smpi_lat_factor)
668     xbt_dynar_free(&smpi_lat_factor);
669 }
670
671 static void smpi_gap_append(double size, const link_CM02_t link,
672                             surf_action_network_CM02_t action)
673 {
674   const char *src = link->lmm_resource.generic_resource.name;
675   xbt_fifo_t fifo;
676   surf_action_network_CM02_t last_action;
677   double bw;
678
679   if (sg_sender_gap > 0.0) {
680     if (!gap_lookup) {
681       gap_lookup = xbt_dict_new();
682     }
683     fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
684     action->sender.gap = 0.0;
685     if (fifo && xbt_fifo_size(fifo) > 0) {
686       /* Compute gap from last send */
687       last_action =
688           (surf_action_network_CM02_t)
689           xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
690       bw = net_get_link_bandwidth(link);
691       action->sender.gap =
692           max(sg_sender_gap,last_action->sender.size / bw);
693       action->latency += action->sender.gap;
694     }
695     /* Append action as last send */
696     action->sender.link_name = link->lmm_resource.generic_resource.name;
697     fifo =
698         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
699                                           action->sender.link_name);
700     if (!fifo) {
701       fifo = xbt_fifo_new();
702       xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
703     }
704     action->sender.fifo_item = xbt_fifo_push(fifo, action);
705     action->sender.size = size;
706   }
707 }
708
709 static void smpi_gap_remove(surf_action_lmm_t lmm_action)
710 {
711   xbt_fifo_t fifo;
712   size_t size;
713   surf_action_network_CM02_t action = (surf_action_network_CM02_t)(lmm_action);
714
715   if (sg_sender_gap > 0.0 && action->sender.link_name
716       && action->sender.fifo_item) {
717     fifo =
718         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
719                                           action->sender.link_name);
720     xbt_fifo_remove_item(fifo, action->sender.fifo_item);
721     size = xbt_fifo_size(fifo);
722     if (size == 0) {
723       xbt_fifo_free(fifo);
724       xbt_dict_remove(gap_lookup, action->sender.link_name);
725       size = xbt_dict_length(gap_lookup);
726       if (size == 0) {
727         xbt_dict_free(&gap_lookup);
728       }
729     }
730   }
731 }
732
733 static void set_update_mechanism(void)
734 {
735   char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
736   int select =
737       xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
738
739   if (!strcmp(optim, "Full")) {
740     surf_network_model->model_private->update_mechanism = UM_FULL;
741     surf_network_model->model_private->selective_update = select;
742   } else if (!strcmp(optim, "Lazy")) {
743     surf_network_model->model_private->update_mechanism = UM_LAZY;
744     surf_network_model->model_private->selective_update = 1;
745     xbt_assert((select == 1)
746                ||
747                (xbt_cfg_is_default_value
748                 (_surf_cfg_set, "network/maxmin_selective_update")),
749                "Disabling selective update while using the lazy update mechanism is dumb!");
750   } else {
751     xbt_die("Unsupported optimization (%s) for this model", optim);
752   }
753 }
754
755 static void surf_network_model_init_internal(void)
756 {
757   s_surf_action_network_CM02_t comm;
758   surf_network_model = surf_model_init();
759
760   set_update_mechanism();
761
762   surf_network_model->name = "network";
763   surf_network_model->action_unref = surf_action_unref;
764   surf_network_model->action_cancel = surf_action_cancel;
765   surf_network_model->action_recycle = net_action_recycle;
766
767   surf_network_model->get_remains = surf_action_get_remains;
768
769 #ifdef HAVE_LATENCY_BOUND_TRACKING
770   surf_network_model->get_latency_limited = net_get_link_latency_limited;
771 #endif
772 #ifdef HAVE_TRACING
773   surf_network_model->set_category = surf_action_set_category;
774 #endif
775
776   surf_network_model->model_private->resource_used = net_resource_used;
777   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
778     surf_network_model->model_private->share_resources =
779         net_share_resources_lazy;
780     surf_network_model->model_private->update_actions_state =
781         net_update_actions_state_lazy;
782   } else if (surf_network_model->model_private->update_mechanism == UM_FULL) {
783     surf_network_model->model_private->share_resources =
784         net_share_resources_full;
785     surf_network_model->model_private->update_actions_state =
786         net_update_actions_state_full;
787   }
788
789   surf_network_model->model_private->update_resource_state =
790       net_update_resource_state;
791   surf_network_model->model_private->finalize = net_finalize;
792
793   surf_network_model->suspend = surf_action_suspend;
794   surf_network_model->resume = surf_action_resume;
795   surf_network_model->is_suspended = surf_action_is_suspended;
796   surf_cpu_model->set_max_duration = surf_action_set_max_duration;
797
798   surf_network_model->extension.network.communicate = net_communicate;
799   surf_network_model->extension.network.get_route = net_get_route;
800   surf_network_model->extension.network.get_link_bandwidth =
801       net_get_link_bandwidth;
802   surf_network_model->extension.network.get_link_latency =
803       net_get_link_latency;
804   surf_network_model->extension.network.link_shared = net_link_shared;
805   surf_network_model->extension.network.add_traces = net_add_traces;
806   surf_network_model->extension.network.create_resource =
807       net_create_resource;
808
809   if (!surf_network_model->model_private->maxmin_system)
810     surf_network_model->model_private->maxmin_system = lmm_system_new(surf_network_model->model_private->selective_update);
811
812   routing_model_create(net_create_resource("__loopback__",
813                                            498000000, NULL, 0.000015, NULL,
814                                            SURF_RESOURCE_ON, NULL,
815                                            SURF_LINK_FATPIPE, NULL));
816
817   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
818     surf_network_model->model_private->action_heap = xbt_heap_new(8, NULL);
819     xbt_heap_set_update_callback(surf_network_model->model_private->action_heap,
820                                  surf_action_lmm_update_index_heap);
821     surf_network_model->model_private->modified_set =
822         xbt_swag_new(xbt_swag_offset(comm, generic_lmm_action.action_list_hookup));
823     surf_network_model->model_private->maxmin_system->keep_track = surf_network_model->model_private->modified_set;
824   }
825
826   surf_network_model->gap_remove = NULL;
827 }
828
829 /************************************************************************/
830 /* New model based on LV08 and experimental results of MPI ping-pongs   */
831 /************************************************************************/
832 /* @Inproceedings{smpi_ipdps, */
833 /*  author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
834 /*  title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
835 /*  booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
836 /*  address={Anchorage (Alaska) USA}, */
837 /*  month=may, */
838 /*  year={2011} */
839 /*  } */
840 void surf_network_model_init_SMPI(void)
841 {
842
843   if (surf_network_model)
844     return;
845
846   surf_network_model_init_internal();
847   latency_factor_callback = &smpi_latency_factor;
848   bandwidth_factor_callback = &smpi_bandwidth_factor;
849   bandwidth_constraint_callback = &smpi_bandwidth_constraint;
850   gap_append = &smpi_gap_append;
851   surf_network_model->gap_remove = &smpi_gap_remove;
852   net_define_callbacks();
853   xbt_dynar_push(model_list, &surf_network_model);
854   network_solve = lmm_solve;
855
856   xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
857   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
858 }
859
860 /************************************************************************/
861 /* New model based on optimizations discussed during Pedro Velho's thesis*/
862 /************************************************************************/
863 /* @techreport{VELHO:2011:HAL-00646896:1, */
864 /*      url = {http://hal.inria.fr/hal-00646896/en/}, */
865 /*      title = {{Flow-level network models: have we reached the limits?}}, */
866 /*      author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
867 /*      type = {Rapport de recherche}, */
868 /*      institution = {INRIA}, */
869 /*      number = {RR-7821}, */
870 /*      year = {2011}, */
871 /*      month = Nov, */
872 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
873 /*  } */
874 void surf_network_model_init_LegrandVelho(void)
875 {
876   if (surf_network_model)
877     return;
878
879   surf_network_model_init_internal();
880   net_define_callbacks();
881   xbt_dynar_push(model_list, &surf_network_model);
882   network_solve = lmm_solve;
883
884   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor",
885                             13.01);
886   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
887                             0.97);
888   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 20537);
889 }
890
891 /***************************************************************************/
892 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
893 /***************************************************************************/
894 /* @TechReport{      rr-lip2002-40, */
895 /*   author        = {Henri Casanova and Loris Marchal}, */
896 /*   institution   = {LIP}, */
897 /*   title         = {A Network Model for Simulation of Grid Application}, */
898 /*   number        = {2002-40}, */
899 /*   month         = {oct}, */
900 /*   year          = {2002} */
901 /* } */
902 void surf_network_model_init_CM02(void)
903 {
904
905   if (surf_network_model)
906     return;
907
908   surf_network_model_init_internal();
909   net_define_callbacks();
910   xbt_dynar_push(model_list, &surf_network_model);
911   network_solve = lmm_solve;
912
913   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
914   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
915                             1.0);
916   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
917 }
918
919 /***************************************************************************/
920 /* The models from Steven H. Low                                           */
921 /***************************************************************************/
922 /* @article{Low03,                                                         */
923 /*   author={Steven H. Low},                                               */
924 /*   title={A Duality Model of {TCP} and Queue Management Algorithms},     */
925 /*   year={2003},                                                          */
926 /*   journal={{IEEE/ACM} Transactions on Networking},                      */
927 /*    volume={11}, number={4},                                             */
928 /*  }                                                                      */
929 void surf_network_model_init_Reno(void)
930 {
931   if (surf_network_model)
932     return;
933
934   surf_network_model_init_internal();
935   net_define_callbacks();
936
937   xbt_dynar_push(model_list, &surf_network_model);
938   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
939                                     func_reno_fpi);
940   network_solve = lagrange_solve;
941
942   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
943   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
944                             0.92);
945   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
946 }
947
948
949 void surf_network_model_init_Reno2(void)
950 {
951   if (surf_network_model)
952     return;
953
954   surf_network_model_init_internal();
955   net_define_callbacks();
956
957   xbt_dynar_push(model_list, &surf_network_model);
958   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
959                                     func_reno2_fpi);
960   network_solve = lagrange_solve;
961
962   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
963   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
964                             0.92);
965   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
966                             8775);
967 }
968
969 void surf_network_model_init_Vegas(void)
970 {
971   if (surf_network_model)
972     return;
973
974   surf_network_model_init_internal();
975   net_define_callbacks();
976
977   xbt_dynar_push(model_list, &surf_network_model);
978   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
979                                     func_vegas_fpi);
980   network_solve = lagrange_solve;
981
982   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
983   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
984                             0.92);
985   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
986 }