Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Forget to remove this line from previous commit
[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   surf_action_network_CM02_t action = NULL;
432
433   while ((xbt_heap_size(surf_network_model->model_private->action_heap) > 0)
434          && (double_equals(xbt_heap_maxkey(surf_network_model->model_private->action_heap), now))) {
435     action = xbt_heap_pop(surf_network_model->model_private->action_heap);
436     XBT_DEBUG("Action %p: finish", action);
437     GENERIC_ACTION(action).finish = surf_get_clock();
438 #ifdef HAVE_TRACING
439     if (TRACE_is_enabled()) {
440       int n = lmm_get_number_of_cnst_from_var(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable);
441       unsigned int i;
442       for (i = 0; i < n; i++){
443         lmm_constraint_t constraint = lmm_get_cnst_from_var(surf_network_model->model_private->maxmin_system,
444                                                             GENERIC_LMM_ACTION(action).variable,
445                                                             i);
446         link_CM02_t link = lmm_constraint_id(constraint);
447         TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
448                                         ((surf_action_t)action)->category,
449                                         (lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable)*
450                                             lmm_get_cnst_weight_from_var(surf_network_model->model_private->maxmin_system,
451                                                 GENERIC_LMM_ACTION(action).variable,
452                                                 i)),
453                                         GENERIC_LMM_ACTION(action).last_update,
454                                         now - GENERIC_LMM_ACTION(action).last_update);
455       }
456     }
457 #endif
458
459     // if I am wearing a latency hat
460     if (GENERIC_LMM_ACTION(action).hat == LATENCY) {
461       lmm_update_variable_weight(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
462                                  action->weight);
463       surf_action_lmm_heap_remove(surf_network_model->model_private->action_heap,(surf_action_lmm_t)action);
464       GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
465
466       // if I am wearing a max_duration or normal hat
467     } else if (GENERIC_LMM_ACTION(action).hat == MAX_DURATION ||
468         GENERIC_LMM_ACTION(action).hat == NORMAL) {
469       // no need to communicate anymore
470       // assume that flows that reached max_duration have remaining of 0
471       GENERIC_ACTION(action).remains = 0;
472       ((surf_action_t)action)->finish = surf_get_clock();
473       surf_network_model->action_state_set((surf_action_t) action,
474                                            SURF_ACTION_DONE);
475       surf_action_lmm_heap_remove(surf_network_model->model_private->action_heap,(surf_action_lmm_t)action);
476     }
477   }
478   return;
479 }
480
481 static void net_update_resource_state(void *id,
482                                       tmgr_trace_event_t event_type,
483                                       double value, double date)
484 {
485   link_CM02_t nw_link = id;
486   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
487   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
488   /*     value, event_type); */
489
490   if (event_type == nw_link->lmm_resource.power.event) {
491     double delta =
492         sg_weight_S_parameter / value - sg_weight_S_parameter /
493         (nw_link->lmm_resource.power.peak *
494          nw_link->lmm_resource.power.scale);
495     lmm_variable_t var = NULL;
496     lmm_element_t elem = NULL;
497     surf_action_network_CM02_t action = NULL;
498
499     nw_link->lmm_resource.power.peak = value;
500     lmm_update_constraint_bound(surf_network_model->model_private->maxmin_system,
501                                 nw_link->lmm_resource.constraint,
502                                 sg_bandwidth_factor *
503                                 (nw_link->lmm_resource.power.peak *
504                                  nw_link->lmm_resource.power.scale));
505 #ifdef HAVE_TRACING
506     TRACE_surf_link_set_bandwidth(date,
507                                   (char
508                                    *) (((nw_link->lmm_resource).
509                                         generic_resource).name),
510                                   sg_bandwidth_factor *
511                                   (nw_link->lmm_resource.power.peak *
512                                    nw_link->lmm_resource.power.scale));
513 #endif
514     if (sg_weight_S_parameter > 0) {
515       while ((var = lmm_get_var_from_cnst
516               (surf_network_model->model_private->maxmin_system, nw_link->lmm_resource.constraint,
517                &elem))) {
518         action = lmm_variable_id(var);
519         action->weight += delta;
520         if (!(GENERIC_LMM_ACTION(action).suspended))
521           lmm_update_variable_weight(surf_network_model->model_private->maxmin_system,
522                                      GENERIC_LMM_ACTION(action).variable, action->weight);
523       }
524     }
525     if (tmgr_trace_event_free(event_type))
526       nw_link->lmm_resource.power.event = NULL;
527   } else if (event_type == nw_link->lat_event) {
528     double delta = value - nw_link->lat_current;
529     lmm_variable_t var = NULL;
530     lmm_element_t elem = NULL;
531     surf_action_network_CM02_t action = NULL;
532
533     nw_link->lat_current = value;
534     while ((var = lmm_get_var_from_cnst
535             (surf_network_model->model_private->maxmin_system, nw_link->lmm_resource.constraint,
536              &elem))) {
537       action = lmm_variable_id(var);
538       action->lat_current += delta;
539       action->weight += delta;
540       if (action->rate < 0)
541         lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
542                                   sg_tcp_gamma / (2.0 *
543                                                   action->lat_current));
544       else {
545         lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
546                                   min(action->rate,
547                                       sg_tcp_gamma / (2.0 *
548                                                       action->
549                                                       lat_current)));
550
551         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
552           XBT_INFO("Flow is limited BYBANDWIDTH");
553         } else {
554           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
555                    action->lat_current);
556         }
557       }
558       if (!(GENERIC_LMM_ACTION(action).suspended))
559         lmm_update_variable_weight(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
560                                    action->weight);
561
562     }
563     if (tmgr_trace_event_free(event_type))
564       nw_link->lat_event = NULL;
565   } else if (event_type == nw_link->lmm_resource.state_event) {
566     if (value > 0)
567       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
568     else {
569       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
570       lmm_variable_t var = NULL;
571       lmm_element_t elem = NULL;
572
573       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
574       while ((var = lmm_get_var_from_cnst
575               (surf_network_model->model_private->maxmin_system, cnst, &elem))) {
576         surf_action_t action = lmm_variable_id(var);
577
578         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
579             surf_action_state_get(action) == SURF_ACTION_READY) {
580           action->finish = date;
581           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
582         }
583       }
584     }
585     if (tmgr_trace_event_free(event_type))
586       nw_link->lmm_resource.state_event = NULL;
587   } else {
588     XBT_CRITICAL("Unknown event ! \n");
589     xbt_abort();
590   }
591
592   XBT_DEBUG
593       ("There were a resource state event, need to update actions related to the constraint (%p)",
594        nw_link->lmm_resource.constraint);
595   return;
596 }
597
598
599 static surf_action_t net_communicate(sg_routing_edge_t src,
600                                      sg_routing_edge_t dst,
601                                      double size, double rate)
602 {
603   unsigned int i;
604   link_CM02_t link;
605   int failed = 0;
606   surf_action_network_CM02_t action = NULL;
607   double bandwidth_bound;
608   double latency = 0.0;
609   xbt_dynar_t back_route = NULL;
610   int constraints_per_variable = 0;
611
612   xbt_dynar_t route = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
613
614   XBT_IN("(%s,%s,%g,%g)", src->name, dst->name, size, rate);
615
616   routing_get_route_and_latency(src, dst, &route, &latency);
617   xbt_assert(!xbt_dynar_is_empty(route) || latency,
618              "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
619              src->name, dst->name);
620
621   xbt_dynar_foreach(route, i, link) {
622     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
623       failed = 1;
624       break;
625     }
626   }
627   if (sg_network_crosstraffic == 1) {
628     routing_get_route_and_latency(dst, src, &back_route, NULL);
629     xbt_dynar_foreach(back_route, i, link) {
630       if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
631         failed = 1;
632         break;
633       }
634     }
635   }
636
637   action =
638       surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
639                       surf_network_model, failed);
640 #ifdef HAVE_LATENCY_BOUND_TRACKING
641   action->latency_limited = 0;
642 #endif
643   action->weight = action->latency = latency;
644
645   xbt_swag_insert(action, ((surf_action_t)action)->state_set);
646   action->rate = rate;
647   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
648     GENERIC_LMM_ACTION(action).index_heap = -1;
649     GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
650   }
651
652   bandwidth_bound = -1.0;
653   if (sg_weight_S_parameter > 0) {
654     xbt_dynar_foreach(route, i, link) {
655       action->weight +=
656           sg_weight_S_parameter /
657           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
658     }
659   }
660   xbt_dynar_foreach(route, i, link) {
661     double bb = bandwidth_factor_callback(size) *
662         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
663     bandwidth_bound =
664         (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
665   }
666
667   action->lat_current = action->latency;
668   action->latency *= latency_factor_callback(size);
669   action->rate =
670       bandwidth_constraint_callback(action->rate, bandwidth_bound, size);
671   if (gap_append) {
672     xbt_assert(!xbt_dynar_is_empty(route),
673                "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
674
675     link = *(link_CM02_t *) xbt_dynar_get_ptr(route, 0);
676     gap_append(size, link, action);
677     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
678               action, src->name, dst->name, action->sender.gap,
679               action->latency);
680   }
681
682   constraints_per_variable = xbt_dynar_length(route);
683   if (back_route != NULL)
684     constraints_per_variable += xbt_dynar_length(back_route);
685
686   if (action->latency > 0) {
687     GENERIC_LMM_ACTION(action).variable =
688         lmm_variable_new(surf_network_model->model_private->maxmin_system, action, 0.0, -1.0,
689                          constraints_per_variable);
690     if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
691       // add to the heap the event when the latency is payed
692       XBT_DEBUG("Added action (%p) one latency event at date %f", action,
693                 action->latency + GENERIC_LMM_ACTION(action).last_update);
694       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,
695                   xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
696     }
697   } else
698     GENERIC_LMM_ACTION(action).variable =
699         lmm_variable_new(surf_network_model->model_private->maxmin_system, action, 1.0, -1.0,
700                          constraints_per_variable);
701
702   if (action->rate < 0) {
703     lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
704                               (action->lat_current > 0) ?
705                               sg_tcp_gamma / (2.0 *
706                                               action->lat_current) : -1.0);
707   } else {
708     lmm_update_variable_bound(surf_network_model->model_private->maxmin_system, GENERIC_LMM_ACTION(action).variable,
709                               (action->lat_current > 0) ?
710                               min(action->rate,
711                                   sg_tcp_gamma / (2.0 *
712                                                   action->lat_current))
713                               : action->rate);
714   }
715
716   xbt_dynar_foreach(route, i, link) {
717     lmm_expand(surf_network_model->model_private->maxmin_system, link->lmm_resource.constraint,
718                GENERIC_LMM_ACTION(action).variable, 1.0);
719   }
720
721   if (sg_network_crosstraffic == 1) {
722     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
723     xbt_dynar_foreach(back_route, i, link) {
724       lmm_expand(surf_network_model->model_private->maxmin_system, link->lmm_resource.constraint,
725                  GENERIC_LMM_ACTION(action).variable, .05);
726     }
727   }
728
729   xbt_dynar_free(&route);
730   XBT_OUT();
731
732   return (surf_action_t) action;
733 }
734
735 static xbt_dynar_t net_get_route(void *src, void *dst)
736 {
737   xbt_dynar_t route = NULL;
738   routing_get_route_and_latency(src, dst, &route, NULL);
739   return route;
740 }
741
742 static double net_get_link_bandwidth(const void *link)
743 {
744   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
745   return lmm->power.peak * lmm->power.scale;
746 }
747
748 static double net_get_link_latency(const void *link)
749 {
750   return ((link_CM02_t) link)->lat_current;
751 }
752
753 static int net_link_shared(const void *link)
754 {
755   return
756       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
757 }
758
759 static void net_finalize(void)
760 {
761   lmm_system_free(surf_network_model->model_private->maxmin_system);
762   surf_network_model->model_private->maxmin_system = NULL;
763
764   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
765     xbt_heap_free(surf_network_model->model_private->action_heap);
766     xbt_swag_free(surf_network_model->model_private->modified_set);
767   }
768
769   surf_model_exit(surf_network_model);
770   surf_network_model = NULL;
771
772   if (smpi_bw_factor)
773     xbt_dynar_free(&smpi_bw_factor);
774   if (smpi_lat_factor)
775     xbt_dynar_free(&smpi_lat_factor);
776 }
777
778 static void smpi_gap_append(double size, const link_CM02_t link,
779                             surf_action_network_CM02_t action)
780 {
781   const char *src = link->lmm_resource.generic_resource.name;
782   xbt_fifo_t fifo;
783   surf_action_network_CM02_t last_action;
784   double bw;
785
786   if (sg_sender_gap > 0.0) {
787     if (!gap_lookup) {
788       gap_lookup = xbt_dict_new();
789     }
790     fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
791     action->sender.gap = 0.0;
792     if (fifo && xbt_fifo_size(fifo) > 0) {
793       /* Compute gap from last send */
794       last_action =
795           (surf_action_network_CM02_t)
796           xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
797       bw = net_get_link_bandwidth(link);
798       action->sender.gap =
799           last_action->sender.gap + max(sg_sender_gap,
800                                         last_action->sender.size / bw);
801       action->latency += action->sender.gap;
802     }
803     /* Append action as last send */
804     action->sender.link_name = link->lmm_resource.generic_resource.name;
805     fifo =
806         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
807                                           action->sender.link_name);
808     if (!fifo) {
809       fifo = xbt_fifo_new();
810       xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
811     }
812     action->sender.fifo_item = xbt_fifo_push(fifo, action);
813     action->sender.size = size;
814   }
815 }
816
817 static void smpi_gap_remove(surf_action_network_CM02_t action)
818 {
819   xbt_fifo_t fifo;
820   size_t size;
821
822   if (sg_sender_gap > 0.0 && action->sender.link_name
823       && action->sender.fifo_item) {
824     fifo =
825         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
826                                           action->sender.link_name);
827     xbt_fifo_remove_item(fifo, action->sender.fifo_item);
828     size = xbt_fifo_size(fifo);
829     if (size == 0) {
830       xbt_fifo_free(fifo);
831       xbt_dict_remove(gap_lookup, action->sender.link_name);
832       size = xbt_dict_length(gap_lookup);
833       if (size == 0) {
834         xbt_dict_free(&gap_lookup);
835       }
836     }
837   }
838 }
839
840 static void set_update_mechanism(void)
841 {
842   char *optim = xbt_cfg_get_string(_surf_cfg_set, "network/optim");
843   int select =
844       xbt_cfg_get_int(_surf_cfg_set, "network/maxmin_selective_update");
845
846   if (!strcmp(optim, "Full")) {
847     surf_network_model->model_private->update_mechanism = UM_FULL;
848     surf_network_model->model_private->selective_update = select;
849   } else if (!strcmp(optim, "Lazy")) {
850     surf_network_model->model_private->update_mechanism = UM_LAZY;
851     surf_network_model->model_private->selective_update = 1;
852     xbt_assert((select == 1)
853                ||
854                (xbt_cfg_is_default_value
855                 (_surf_cfg_set, "network/maxmin_selective_update")),
856                "Disabling selective update while using the lazy update mechanism is dumb!");
857   } else {
858     xbt_die("Unsupported optimization (%s) for this model", optim);
859   }
860 }
861
862 static void surf_network_model_init_internal(void)
863 {
864   s_surf_action_network_CM02_t comm;
865   surf_network_model = surf_model_init();
866
867   set_update_mechanism();
868
869   surf_network_model->name = "network";
870   surf_network_model->action_unref = surf_action_unref;
871   surf_network_model->action_cancel = surf_action_cancel;
872   surf_network_model->action_recycle = net_action_recycle;
873
874   surf_network_model->get_remains = surf_action_get_remains;
875
876 #ifdef HAVE_LATENCY_BOUND_TRACKING
877   surf_network_model->get_latency_limited = net_get_link_latency_limited;
878 #endif
879 #ifdef HAVE_TRACING
880   surf_network_model->set_category = surf_action_set_category;
881 #endif
882
883   surf_network_model->model_private->resource_used = net_resource_used;
884   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
885     surf_network_model->model_private->share_resources =
886         net_share_resources_lazy;
887     surf_network_model->model_private->update_actions_state =
888         net_update_actions_state_lazy;
889   } else if (surf_network_model->model_private->update_mechanism == UM_FULL) {
890     surf_network_model->model_private->share_resources =
891         net_share_resources_full;
892     surf_network_model->model_private->update_actions_state =
893         net_update_actions_state_full;
894   }
895
896   surf_network_model->model_private->update_resource_state =
897       net_update_resource_state;
898   surf_network_model->model_private->finalize = net_finalize;
899
900   surf_network_model->suspend = surf_action_suspend;
901   surf_network_model->resume = surf_action_resume;
902   surf_network_model->is_suspended = surf_action_is_suspended;
903   surf_cpu_model->set_max_duration = surf_action_set_max_duration;
904
905   surf_network_model->extension.network.communicate = net_communicate;
906   surf_network_model->extension.network.get_route = net_get_route;
907   surf_network_model->extension.network.get_link_bandwidth =
908       net_get_link_bandwidth;
909   surf_network_model->extension.network.get_link_latency =
910       net_get_link_latency;
911   surf_network_model->extension.network.link_shared = net_link_shared;
912   surf_network_model->extension.network.add_traces = net_add_traces;
913   surf_network_model->extension.network.create_resource =
914       net_create_resource;
915
916   if (!surf_network_model->model_private->maxmin_system)
917     surf_network_model->model_private->maxmin_system = lmm_system_new(surf_network_model->model_private->selective_update);
918
919   routing_model_create(net_create_resource("__loopback__",
920                                            498000000, NULL, 0.000015, NULL,
921                                            SURF_RESOURCE_ON, NULL,
922                                            SURF_LINK_FATPIPE, NULL));
923
924   if (surf_network_model->model_private->update_mechanism == UM_LAZY) {
925     surf_network_model->model_private->action_heap = xbt_heap_new(8, NULL);
926     xbt_heap_set_update_callback(surf_network_model->model_private->action_heap,
927                                  surf_action_lmm_update_index_heap);
928     surf_network_model->model_private->modified_set =
929         xbt_swag_new(xbt_swag_offset(comm, generic_lmm_action.action_list_hookup));
930     surf_network_model->model_private->maxmin_system->keep_track = surf_network_model->model_private->modified_set;
931   }
932 }
933
934 /************************************************************************/
935 /* New model based on LV08 and experimental results of MPI ping-pongs   */
936 /************************************************************************/
937 /* @Inproceedings{smpi_ipdps, */
938 /*  author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
939 /*  title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
940 /*  booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
941 /*  address={Anchorage (Alaska) USA}, */
942 /*  month=may, */
943 /*  year={2011} */
944 /*  } */
945 void surf_network_model_init_SMPI(void)
946 {
947
948   if (surf_network_model)
949     return;
950
951   surf_network_model_init_internal();
952   latency_factor_callback = &smpi_latency_factor;
953   bandwidth_factor_callback = &smpi_bandwidth_factor;
954   bandwidth_constraint_callback = &smpi_bandwidth_constraint;
955   gap_append = &smpi_gap_append;
956   gap_remove = &smpi_gap_remove;
957   net_define_callbacks();
958   xbt_dynar_push(model_list, &surf_network_model);
959   network_solve = lmm_solve;
960
961   xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
962   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
963 }
964
965 /************************************************************************/
966 /* New model based on optimizations discussed during Pedro Velho's thesis*/
967 /************************************************************************/
968 /* @techreport{VELHO:2011:HAL-00646896:1, */
969 /*      url = {http://hal.inria.fr/hal-00646896/en/}, */
970 /*      title = {{Flow-level network models: have we reached the limits?}}, */
971 /*      author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
972 /*      type = {Rapport de recherche}, */
973 /*      institution = {INRIA}, */
974 /*      number = {RR-7821}, */
975 /*      year = {2011}, */
976 /*      month = Nov, */
977 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
978 /*  } */
979 void surf_network_model_init_LegrandVelho(void)
980 {
981   if (surf_network_model)
982     return;
983
984   surf_network_model_init_internal();
985   net_define_callbacks();
986   xbt_dynar_push(model_list, &surf_network_model);
987   network_solve = lmm_solve;
988
989   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor",
990                             13.01);
991   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
992                             0.97);
993   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 20537);
994 }
995
996 /***************************************************************************/
997 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
998 /***************************************************************************/
999 /* @TechReport{      rr-lip2002-40, */
1000 /*   author        = {Henri Casanova and Loris Marchal}, */
1001 /*   institution   = {LIP}, */
1002 /*   title         = {A Network Model for Simulation of Grid Application}, */
1003 /*   number        = {2002-40}, */
1004 /*   month         = {oct}, */
1005 /*   year          = {2002} */
1006 /* } */
1007 void surf_network_model_init_CM02(void)
1008 {
1009
1010   if (surf_network_model)
1011     return;
1012
1013   surf_network_model_init_internal();
1014   net_define_callbacks();
1015   xbt_dynar_push(model_list, &surf_network_model);
1016   network_solve = lmm_solve;
1017
1018   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 1.0);
1019   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1020                             1.0);
1021   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 0.0);
1022 }
1023
1024 /***************************************************************************/
1025 /* The models from Steven H. Low                                           */
1026 /***************************************************************************/
1027 /* @article{Low03,                                                         */
1028 /*   author={Steven H. Low},                                               */
1029 /*   title={A Duality Model of {TCP} and Queue Management Algorithms},     */
1030 /*   year={2003},                                                          */
1031 /*   journal={{IEEE/ACM} Transactions on Networking},                      */
1032 /*    volume={11}, number={4},                                             */
1033 /*  }                                                                      */
1034 void surf_network_model_init_Reno(void)
1035 {
1036   if (surf_network_model)
1037     return;
1038
1039   surf_network_model_init_internal();
1040   net_define_callbacks();
1041
1042   xbt_dynar_push(model_list, &surf_network_model);
1043   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
1044                                     func_reno_fpi);
1045   network_solve = lagrange_solve;
1046
1047   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1048   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1049                             0.92);
1050   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1051 }
1052
1053
1054 void surf_network_model_init_Reno2(void)
1055 {
1056   if (surf_network_model)
1057     return;
1058
1059   surf_network_model_init_internal();
1060   net_define_callbacks();
1061
1062   xbt_dynar_push(model_list, &surf_network_model);
1063   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
1064                                     func_reno2_fpi);
1065   network_solve = lagrange_solve;
1066
1067   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1068   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1069                             0.92);
1070   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
1071                             8775);
1072 }
1073
1074 void surf_network_model_init_Vegas(void)
1075 {
1076   if (surf_network_model)
1077     return;
1078
1079   surf_network_model_init_internal();
1080   net_define_callbacks();
1081
1082   xbt_dynar_push(model_list, &surf_network_model);
1083   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
1084                                     func_vegas_fpi);
1085   network_solve = lagrange_solve;
1086
1087   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1088   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1089                             0.92);
1090   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1091 }