Logo AND Algorithmique Numérique Distribuée

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