Logo AND Algorithmique Numérique Distribuée

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