Logo AND Algorithmique Numérique Distribuée

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