Logo AND Algorithmique Numérique Distribuée

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