Logo AND Algorithmique Numérique Distribuée

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