Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adapt perl tesh to handle timeout from command line, and defaut to 50 for windows...
[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) 2004-2013. 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 #include "simgrid/sg_config.h"
26
27 #undef GENERIC_LMM_ACTION
28 #undef GENERIC_ACTION
29 #define GENERIC_LMM_ACTION(action) (action)->generic_lmm_action
30 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
31
32
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
34                                 "Logging specific to the SURF network module");
35
36 surf_model_t surf_network_model = NULL;
37 static void (*network_solve) (lmm_system_t) = NULL;
38
39 xbt_dynar_t smpi_bw_factor = NULL;
40 xbt_dynar_t smpi_lat_factor = NULL;
41
42 typedef struct s_smpi_factor *smpi_factor_t;
43 typedef struct s_smpi_factor {
44   long factor;
45   double value;
46 } s_smpi_factor_t;
47
48
49 double sg_sender_gap = 0.0;
50 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
51 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
52 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
53
54 double sg_tcp_gamma = 0.0;
55 int sg_network_crosstraffic = 0;
56
57 xbt_dict_t gap_lookup = NULL;
58
59 /******************************************************************************/
60 /*                           Factors callbacks                                */
61 /******************************************************************************/
62 static double constant_latency_factor(double size)
63 {
64   return sg_latency_factor;
65 }
66
67 static double constant_bandwidth_factor(double size)
68 {
69   return sg_bandwidth_factor;
70 }
71
72 static double constant_bandwidth_constraint(double rate, double bound,
73                                             double size)
74 {
75   return rate;
76 }
77
78 /**********************/
79 /*   SMPI callbacks   */
80 /**********************/
81
82 static int factor_cmp(const void *pa, const void *pb)
83 {
84   return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor);
85 }
86
87
88 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
89 {
90   char *value = NULL;
91   unsigned int iter = 0;
92   s_smpi_factor_t fact;
93   xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
94
95   smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
96   radical_elements = xbt_str_split(smpi_coef_string, ";");
97   xbt_dynar_foreach(radical_elements, iter, value) {
98
99     radical_elements2 = xbt_str_split(value, ":");
100     if (xbt_dynar_length(radical_elements2) != 2)
101       xbt_die("Malformed radical for smpi factor!");
102     fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
103     fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
104     xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
105     XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
106     xbt_dynar_free(&radical_elements2);
107   }
108   xbt_dynar_free(&radical_elements);
109   iter=0;
110   xbt_dynar_sort(smpi_factor, &factor_cmp);
111   xbt_dynar_foreach(smpi_factor, iter, fact) {
112     XBT_DEBUG("ordered smpi_factor:\t%ld : %f", fact.factor, fact.value);
113
114   }
115   return smpi_factor;
116 }
117
118 static double smpi_bandwidth_factor(double size)
119 {
120   if (!smpi_bw_factor)
121     smpi_bw_factor =
122         parse_factor(sg_cfg_get_string("smpi/bw_factor"));
123
124   unsigned int iter = 0;
125   s_smpi_factor_t fact;
126   double current=1.0;
127   xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
128     if (size <= fact.factor) {
129       XBT_DEBUG("%lf <= %ld return %f", size, fact.factor, current);
130       return current;
131     }else
132       current=fact.value;
133   }
134   XBT_DEBUG("%lf > %ld return %f", size, fact.factor, current);
135
136   return current;
137 }
138
139 static double smpi_latency_factor(double size)
140 {
141   if (!smpi_lat_factor)
142     smpi_lat_factor =
143         parse_factor(sg_cfg_get_string("smpi/lat_factor"));
144
145   unsigned int iter = 0;
146   s_smpi_factor_t fact;
147   double current=1.0;
148   xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
149     if (size <= fact.factor) {
150       XBT_DEBUG("%lf <= %ld return %f", size, fact.factor, current);
151       return current;
152     }else
153       current=fact.value;
154   }
155   XBT_DEBUG("%lf > %ld return %f", size, fact.factor, current);
156
157   return current;
158 }
159
160 /**--------- <copy/paste C code snippet in surf/network.c> -----------*/
161
162 static double smpi_bandwidth_constraint(double rate, double bound,
163                                         double size)
164 {
165   return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
166 }
167
168 static double (*latency_factor_callback) (double) =
169     &constant_latency_factor;
170 static double (*bandwidth_factor_callback) (double) =
171     &constant_bandwidth_factor;
172 static double (*bandwidth_constraint_callback) (double, double, double) =
173     &constant_bandwidth_constraint;
174
175 static void (*gap_append) (double, const link_CM02_t,
176                            surf_action_network_CM02_t) = NULL;
177
178 static void *net_create_resource(const char *name,
179                                  double bw_initial,
180                                  tmgr_trace_t bw_trace,
181                                  double lat_initial,
182                                  tmgr_trace_t lat_trace,
183                                  e_surf_resource_state_t
184                                  state_initial,
185                                  tmgr_trace_t state_trace,
186                                  e_surf_link_sharing_policy_t
187                                  policy, xbt_dict_t properties)
188 {
189   link_CM02_t nw_link = (link_CM02_t)
190       surf_resource_lmm_new(sizeof(s_link_CM02_t),
191                             surf_network_model, name, properties,
192                             surf_network_model->model_private->maxmin_system,
193                             sg_bandwidth_factor * bw_initial,
194                             history,
195                             state_initial, state_trace,
196                             bw_initial, bw_trace);
197
198   xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
199              "Link '%s' declared several times in the platform file.",
200              name);
201
202   nw_link->lat_current = lat_initial;
203   if (lat_trace)
204     nw_link->lat_event =
205         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
206
207   if (policy == SURF_LINK_FATPIPE)
208     lmm_constraint_shared(nw_link->lmm_resource.constraint);
209
210   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
211   XBT_DEBUG("Create link '%s'",name);
212
213   return nw_link;
214 }
215
216 static void net_parse_link_init(sg_platf_link_cbarg_t link)
217 {
218   if (link->policy == SURF_LINK_FULLDUPLEX) {
219     char *link_id;
220     link_id = bprintf("%s_UP", link->id);
221     net_create_resource(link_id,
222                         link->bandwidth,
223                         link->bandwidth_trace,
224                         link->latency,
225                         link->latency_trace,
226                         link->state,
227                         link->state_trace, link->policy, link->properties);
228     xbt_free(link_id);
229     link_id = bprintf("%s_DOWN", link->id);
230     net_create_resource(link_id,
231                         link->bandwidth,
232                         link->bandwidth_trace,
233                         link->latency,
234                         link->latency_trace,
235                         link->state,
236                         link->state_trace, link->policy, link->properties);
237     xbt_free(link_id);
238   } else {
239     net_create_resource(link->id,
240                         link->bandwidth,
241                         link->bandwidth_trace,
242                         link->latency,
243                         link->latency_trace,
244                         link->state,
245                         link->state_trace, link->policy, link->properties);
246   }
247 }
248
249 static void net_add_traces(void)
250 {
251   xbt_dict_cursor_t cursor = NULL;
252   char *trace_name, *elm;
253
254   static int called = 0;
255   if (called)
256     return;
257   called = 1;
258
259   /* connect all traces relative to network */
260   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
261     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
262     link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
263
264     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
265                trace_name, elm);
266     xbt_assert(trace,
267                "Cannot connect trace %s to link %s: trace undefined",
268                trace_name, elm);
269
270     link->lmm_resource.state_event =
271         tmgr_history_add_trace(history, trace, 0.0, 0, link);
272   }
273
274   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
275     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
276     link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
277
278     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
279                trace_name, elm);
280     xbt_assert(trace,
281                "Cannot connect trace %s to link %s: trace undefined",
282                trace_name, elm);
283
284     link->lmm_resource.power.event =
285         tmgr_history_add_trace(history, trace, 0.0, 0, link);
286   }
287
288   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
289     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
290     link_CM02_t link = xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
291
292     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
293                trace_name, elm);
294     xbt_assert(trace,
295                "Cannot connect trace %s to link %s: trace undefined",
296                trace_name, elm);
297
298     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
299   }
300 }
301
302 static void net_define_callbacks(void)
303 {
304   /* Figuring out the network links */
305   sg_platf_link_add_cb(net_parse_link_init);
306   sg_platf_postparse_add_cb(net_add_traces);
307 }
308
309 static int net_resource_used(void *resource_id)
310 {
311   return lmm_constraint_used(surf_network_model->model_private->maxmin_system, ((surf_resource_lmm_t)
312                                                      resource_id)->
313                              constraint);
314 }
315
316 void net_action_recycle(surf_action_t action)
317 {
318   return;
319 }
320
321 #ifdef HAVE_LATENCY_BOUND_TRACKING
322 int net_get_link_latency_limited(surf_action_t action)
323 {
324   return action->latency_limited;
325 }
326 #endif
327
328 static double net_share_resources_full(double now)
329 {
330   s_surf_action_lmm_t s_action;
331   surf_action_network_CM02_t action = NULL;
332   xbt_swag_t running_actions =
333       surf_network_model->states.running_action_set;
334   double min;
335
336   min = generic_maxmin_share_resources(running_actions,
337                                        xbt_swag_offset(s_action,
338                                                        variable),
339                                                        surf_network_model->model_private->maxmin_system,
340                                        network_solve);
341
342 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable)  )))
343
344   xbt_swag_foreach(action, running_actions) {
345 #ifdef HAVE_LATENCY_BOUND_TRACKING
346     if (lmm_is_variable_limited_by_latency(GENERIC_LMM_ACTION(action).variable)) {
347       action->latency_limited = 1;
348     } else {
349       action->latency_limited = 0;
350     }
351 #endif
352     if (action->latency > 0) {
353       min = (min < 0) ? action->latency : min(min, action->latency);
354     }
355   }
356
357   XBT_DEBUG("Min of share resources %f", min);
358
359   return min;
360 }
361
362 static double net_share_resources_lazy(double now)
363 {
364   return generic_share_resources_lazy(now, surf_network_model);
365 }
366
367 static void net_update_actions_state_full(double now, double delta)
368 {
369   generic_update_actions_state_full(now, delta, surf_network_model);
370 }
371
372 static void net_update_actions_state_lazy(double now, double delta)
373 {
374   generic_update_actions_state_lazy(now, delta, surf_network_model);
375 }
376
377 static void net_update_resource_state(void *id,
378                                       tmgr_trace_event_t event_type,
379                                       double value, double date)
380 {
381   link_CM02_t nw_link = id;
382   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
383   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
384   /*     value, event_type); */
385
386   if (event_type == nw_link->lmm_resource.power.event) {
387     double delta =
388         sg_weight_S_parameter / value - sg_weight_S_parameter /
389         (nw_link->lmm_resource.power.peak *
390          nw_link->lmm_resource.power.scale);
391     lmm_variable_t var = NULL;
392     lmm_element_t elem = NULL;
393     surf_action_network_CM02_t action = NULL;
394
395     nw_link->lmm_resource.power.peak = value;
396     lmm_update_constraint_bound(surf_network_model->model_private->maxmin_system,
397                                 nw_link->lmm_resource.constraint,
398                                 sg_bandwidth_factor *
399                                 (nw_link->lmm_resource.power.peak *
400                                  nw_link->lmm_resource.power.scale));
401 #ifdef HAVE_TRACING
402     TRACE_surf_link_set_bandwidth(date,
403                                   (char
404                                    *) (((nw_link->lmm_resource).
405                                         generic_resource).name),
406                                   sg_bandwidth_factor *
407                                   (nw_link->lmm_resource.power.peak *
408                                    nw_link->lmm_resource.power.scale));
409 #endif
410     if (sg_weight_S_parameter > 0) {
411       while ((var = lmm_get_var_from_cnst
412               (surf_network_model->model_private->maxmin_system, nw_link->lmm_resource.constraint,
413                &elem))) {
414         action = lmm_variable_id(var);
415         action->weight += delta;
416         if (!(GENERIC_LMM_ACTION(action).suspended))
417           lmm_update_variable_weight(surf_network_model->model_private->maxmin_system,
418                                      GENERIC_LMM_ACTION(action).variable, action->weight);
419       }
420     }
421     if (tmgr_trace_event_free(event_type))
422       nw_link->lmm_resource.power.event = NULL;
423   } else if (event_type == nw_link->lat_event) {
424     double delta = value - nw_link->lat_current;
425     lmm_variable_t var = NULL;
426     lmm_element_t elem = NULL;
427     surf_action_network_CM02_t action = NULL;
428
429     nw_link->lat_current = value;
430     while ((var = lmm_get_var_from_cnst
431             (surf_network_model->model_private->maxmin_system, nw_link->lmm_resource.constraint,
432              &elem))) {
433       action = lmm_variable_id(var);
434       action->lat_current += delta;
435       action->weight += delta;
436       if (action->rate < 0)
437         lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
438                                   sg_tcp_gamma / (2.0 *
439                                                   action->lat_current));
440       else {
441         lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
442                                   min(action->rate,
443                                       sg_tcp_gamma / (2.0 *
444                                                       action->
445                                                       lat_current)));
446
447         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
448           XBT_INFO("Flow is limited BYBANDWIDTH");
449         } else {
450           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
451                    action->lat_current);
452         }
453       }
454       if (!(GENERIC_LMM_ACTION(action).suspended))
455         lmm_update_variable_weight(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
456                                    action->weight);
457
458     }
459     if (tmgr_trace_event_free(event_type))
460       nw_link->lat_event = NULL;
461   } else if (event_type == nw_link->lmm_resource.state_event) {
462     if (value > 0)
463       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
464     else {
465       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
466       lmm_variable_t var = NULL;
467       lmm_element_t elem = NULL;
468
469       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
470       while ((var = lmm_get_var_from_cnst
471               (surf_network_model->model_private->maxmin_system, cnst, &elem))) {
472         surf_action_t action = lmm_variable_id(var);
473
474         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
475             surf_action_state_get(action) == SURF_ACTION_READY) {
476           action->finish = date;
477           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
478         }
479       }
480     }
481     if (tmgr_trace_event_free(event_type))
482       nw_link->lmm_resource.state_event = NULL;
483   } else {
484     XBT_CRITICAL("Unknown event ! \n");
485     xbt_abort();
486   }
487
488   XBT_DEBUG
489       ("There were a resource state event, need to update actions related to the constraint (%p)",
490        nw_link->lmm_resource.constraint);
491   return;
492 }
493
494
495 static surf_action_t net_communicate(sg_routing_edge_t src,
496                                      sg_routing_edge_t dst,
497                                      double size, double rate)
498 {
499   unsigned int i;
500   link_CM02_t link;
501   int failed = 0;
502   surf_action_network_CM02_t action = NULL;
503   double bandwidth_bound;
504   double latency = 0.0;
505   xbt_dynar_t back_route = NULL;
506   int constraints_per_variable = 0;
507
508   xbt_dynar_t route = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
509
510   XBT_IN("(%s,%s,%g,%g)", src->name, dst->name, size, rate);
511
512   routing_get_route_and_latency(src, dst, &route, &latency);
513   xbt_assert(!xbt_dynar_is_empty(route) || latency,
514              "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
515              src->name, dst->name);
516
517   xbt_dynar_foreach(route, i, link) {
518     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
519       failed = 1;
520       break;
521     }
522   }
523   if (sg_network_crosstraffic == 1) {
524     routing_get_route_and_latency(dst, src, &back_route, NULL);
525     xbt_dynar_foreach(back_route, i, link) {
526       if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
527         failed = 1;
528         break;
529       }
530     }
531   }
532
533   action =
534       surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
535                       surf_network_model, failed);
536 #ifdef HAVE_LATENCY_BOUND_TRACKING
537   action->latency_limited = 0;
538 #endif
539   action->weight = action->latency = latency;
540
541   xbt_swag_insert(action, ((surf_action_t)action)->state_set);
542   action->rate = rate;
543   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
544     GENERIC_LMM_ACTION(action).index_heap = -1;
545     GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
546   }
547
548   bandwidth_bound = -1.0;
549   if (sg_weight_S_parameter > 0) {
550     xbt_dynar_foreach(route, i, link) {
551       action->weight +=
552           sg_weight_S_parameter /
553           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
554     }
555   }
556   xbt_dynar_foreach(route, i, link) {
557     double bb = bandwidth_factor_callback(size) *
558         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
559     bandwidth_bound =
560         (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
561   }
562
563   action->lat_current = action->latency;
564   action->latency *= latency_factor_callback(size);
565   action->rate =
566       bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
567   if (gap_append) {
568     xbt_assert(!xbt_dynar_is_empty(route),
569                "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
570
571     link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
572     gap_append(size, link, action);
573     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
574               action, src->name, dst->name, action->sender.gap,
575               action->latency);
576   }
577
578   constraints_per_variable = xbt_dynar_length(route);
579   if (back_route != NULL)
580     constraints_per_variable += xbt_dynar_length(back_route);
581
582   if (action->latency > 0) {
583     GENERIC_LMM_ACTION(action).variable =
584         lmm_variable_new(surf_network_model->model_private->maxmin_system, action, 0.0, -1.0,
585                          constraints_per_variable);
586     if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
587       // add to the heap the event when the latency is payed
588       XBT_DEBUG("Added action (%p) one latency event at date %f", action,
589                 action->latency + GENERIC_LMM_ACTION(action).last_update);
590       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,
591                   xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
592     }
593   } else
594     GENERIC_LMM_ACTION(action).variable =
595         lmm_variable_new(surf_network_model->model_private->maxmin_system, action, 1.0, -1.0,
596                          constraints_per_variable);
597
598   if (action->rate < 0) {
599     lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
600                               (action->lat_current > 0) ?
601                               sg_tcp_gamma / (2.0 *
602                                               action->lat_current) : -1.0);
603   } else {
604     lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
605                               (action->lat_current > 0) ?
606                               min(action->rate,
607                                   sg_tcp_gamma / (2.0 *
608                                                   action->lat_current))
609                               : action->rate);
610   }
611
612   xbt_dynar_foreach(route, i, link) {
613     lmm_expand(surf_network_model->model_private->maxmin_system, link->lmm_resource.constraint,
614                GENERIC_LMM_ACTION(action).variable, 1.0);
615   }
616
617   if (sg_network_crosstraffic == 1) {
618     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
619     xbt_dynar_foreach(back_route, i, link) {
620       lmm_expand(surf_network_model->model_private->maxmin_system, link->lmm_resource.constraint,
621                  GENERIC_LMM_ACTION(action).variable, .05);
622     }
623   }
624
625   xbt_dynar_free(&route);
626   XBT_OUT();
627
628   return (surf_action_t) action;
629 }
630
631 static xbt_dynar_t net_get_route(void *src, void *dst)
632 {
633   xbt_dynar_t route = NULL;
634   routing_get_route_and_latency(src, dst, &route, NULL);
635   return route;
636 }
637
638 static double net_get_link_bandwidth(const void *link)
639 {
640   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
641   return lmm->power.peak * lmm->power.scale;
642 }
643
644 static double net_get_link_latency(const void *link)
645 {
646   return ((link_CM02_t) link)->lat_current;
647 }
648
649 static int net_link_shared(const void *link)
650 {
651   return
652       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
653 }
654
655 static void net_finalize(void)
656 {
657   lmm_system_free(surf_network_model->model_private->maxmin_system);
658   surf_network_model->model_private->maxmin_system = NULL;
659
660   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
661     xbt_heap_free(surf_network_model->model_private->action_heap);
662     xbt_swag_free(surf_network_model->model_private->modified_set);
663   }
664
665   surf_model_exit(surf_network_model);
666   surf_network_model = NULL;
667
668   xbt_dict_free(&gap_lookup);
669   xbt_dynar_free(&smpi_bw_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 = sg_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(_sg_cfg_set, "network/optim");
738   int select =
739       xbt_cfg_get_boolean(_sg_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                 (_sg_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
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(_sg_cfg_set, "network/sender_gap", 10e-6);
857   xbt_cfg_setdefault_double(_sg_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(_sg_cfg_set, "network/latency_factor",
885                             13.01);
886   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
887                             0.97);
888   xbt_cfg_setdefault_double(_sg_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(_sg_cfg_set, "network/latency_factor", 1.0);
914   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
915                             1.0);
916   xbt_cfg_setdefault_double(_sg_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(_sg_cfg_set, "network/latency_factor", 10.4);
943   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
944                             0.92);
945   xbt_cfg_setdefault_double(_sg_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(_sg_cfg_set, "network/latency_factor", 10.4);
963   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
964                             0.92);
965   xbt_cfg_setdefault_double(_sg_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(_sg_cfg_set, "network/latency_factor", 10.4);
983   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
984                             0.92);
985   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
986 }