Logo AND Algorithmique Numérique Distribuée

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