Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not use a gap if route has length 0.
[simgrid.git] / src / surf / network.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "network_private.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
12                                 "Logging specific to the SURF network module");
13
14 surf_model_t surf_network_model = NULL;
15 static lmm_system_t network_maxmin_system = NULL;
16 static void (*network_solve) (lmm_system_t) = NULL;
17
18 double sg_sender_gap = 0.0;
19 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
20 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
21 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
22
23 double sg_tcp_gamma = 0.0;
24 int sg_network_fullduplex = 0;
25
26 xbt_dict_t gap_lookup = NULL;
27
28 static double net_get_link_bandwidth(const void *link);
29
30 static void gap_append(double size, const link_CM02_t link, surf_action_network_CM02_t action) {
31    const char* src = link->lmm_resource.generic_resource.name;
32    xbt_fifo_t fifo;
33    surf_action_network_CM02_t last_action;
34    double bw;
35
36    if(sg_sender_gap > 0.0) {
37       if(!gap_lookup) {
38          gap_lookup = xbt_dict_new();
39       }
40       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, src);
41       action->sender.gap = 0.0;
42       if(fifo && xbt_fifo_size(fifo) > 0) {
43          /* Compute gap from last send */
44          last_action = (surf_action_network_CM02_t)xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));
45          bw = net_get_link_bandwidth(link);
46          action->sender.gap = last_action->sender.gap + max(sg_sender_gap, last_action->sender.size / bw);
47          action->latency += action->sender.gap;
48       }
49       /* Append action as last send */
50       action->sender.link_name = link->lmm_resource.generic_resource.name;
51       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
52       if(!fifo) {
53          fifo = xbt_fifo_new();
54          xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
55       }
56       action->sender.fifo_item = xbt_fifo_push(fifo, action);
57       action->sender.size = size;
58    }
59 }
60
61 static void gap_unknown(surf_action_network_CM02_t action) {
62    action->sender.gap = 0.0;
63    action->sender.link_name = NULL;
64    action->sender.fifo_item = NULL;
65    action->sender.size = 0.0;
66 }
67
68 static void gap_remove(surf_action_network_CM02_t action) {
69    xbt_fifo_t fifo;
70    size_t size;
71
72    if(sg_sender_gap > 0.0 && action->sender.link_name && action->sender.fifo_item) {
73       fifo = (xbt_fifo_t)xbt_dict_get_or_null(gap_lookup, action->sender.link_name);
74       xbt_fifo_remove_item(fifo, action->sender.fifo_item);
75       size = xbt_fifo_size(fifo);
76       if(size == 0) {
77          xbt_fifo_free(fifo);
78          xbt_dict_remove(gap_lookup, action->sender.link_name);
79          size = xbt_dict_size(gap_lookup);
80          if(size == 0) {
81             xbt_dict_free(&gap_lookup);
82          }
83       }
84    }
85 }
86
87 /******************************************************************************/
88 /*                           Factors callbacks                                */
89 /******************************************************************************/
90 static double constant_latency_factor(double size)
91 {
92   return sg_latency_factor;
93 }
94
95 static double constant_bandwidth_factor(double size)
96 {
97   return sg_bandwidth_factor;
98 }
99
100 static double constant_bandwidth_constraint(double rate, double bound,
101                                             double size)
102 {
103   return rate;
104 }
105
106 /**********************/
107 /*   SMPI callbacks   */
108 /**********************/
109 static double smpi_latency_factor(double size)
110 {
111   /* 1 B <= size <= 1 KiB */
112   if (size <= 1024.0) {
113     return 1.0056;
114   }
115
116   /* 2 KiB <= size <= 32 KiB */
117   if (size <= 32768.0) {
118     return 1.8805;
119   }
120
121   /* 64 KiB <= size <= 4 MiB */
122   return 22.7111;
123 }
124
125 static double smpi_bandwidth_factor(double size)
126 {
127   /* 1 B <= size <= 1 KiB */
128   if (size <= 1024.0) {
129     return 0.2758;
130   }
131
132   /* 2 KiB <= size <= 32 KiB */
133   if (size <= 32768.0) {
134     return 0.5477;
135   }
136
137   /* 64 KiB <= size <= 4 MiB */
138   return 0.9359;
139 }
140
141 static double smpi_bandwidth_constraint(double rate, double bound,
142                                         double size)
143 {
144   return rate < 0 ? bound : min(bound, rate * smpi_bandwidth_factor(size));
145 }
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
156 static link_CM02_t net_link_new(char *name,
157                                 double bw_initial,
158                                 tmgr_trace_t bw_trace,
159                                 double lat_initial,
160                                 tmgr_trace_t lat_trace,
161                                 e_surf_resource_state_t
162                                 state_initial,
163                                 tmgr_trace_t state_trace,
164                                 e_surf_link_sharing_policy_t
165                                 policy, xbt_dict_t properties)
166 {
167   link_CM02_t nw_link = (link_CM02_t)
168       surf_resource_lmm_new(sizeof(s_link_CM02_t),
169                             surf_network_model, name, properties,
170                             network_maxmin_system,
171                             sg_bandwidth_factor * bw_initial,
172                             history,
173                             state_initial, state_trace,
174                             bw_initial, bw_trace);
175
176   xbt_assert1(!xbt_dict_get_or_null
177               (surf_network_model->resource_set, name),
178               "Link '%s' declared several times in the platform file.",
179               name);
180
181   nw_link->lat_current = lat_initial;
182   if (lat_trace)
183     nw_link->lat_event =
184         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
185
186   if (policy == SURF_LINK_FATPIPE)
187     lmm_constraint_shared(nw_link->lmm_resource.constraint);
188
189   xbt_dict_set(surf_network_model->resource_set, name, nw_link,
190                surf_resource_free);
191 #ifdef HAVE_TRACING
192   TRACE_surf_link_declaration(nw_link, name, bw_initial, lat_initial);
193 #endif
194
195   return nw_link;
196 }
197
198 static void net_parse_link_init(void)
199 {
200   char *name_link;
201   double bw_initial;
202   tmgr_trace_t bw_trace;
203   double lat_initial;
204   tmgr_trace_t lat_trace;
205   e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
206   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
207   tmgr_trace_t state_trace;
208   DEBUG0("link_CM02");
209   name_link = xbt_strdup(A_surfxml_link_id);
210   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
211   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
212   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
213   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
214
215   xbt_assert0((A_surfxml_link_state == A_surfxml_link_state_ON)
216               || (A_surfxml_link_state ==
217                   A_surfxml_link_state_OFF), "Invalid state");
218   if (A_surfxml_link_state == A_surfxml_link_state_ON)
219     state_initial_link = SURF_RESOURCE_ON;
220   else if (A_surfxml_link_state == A_surfxml_link_state_OFF)
221     state_initial_link = SURF_RESOURCE_OFF;
222
223   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
224     policy_initial_link = SURF_LINK_SHARED;
225   else
226           {
227           if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
228                   policy_initial_link = SURF_LINK_FATPIPE;
229           else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
230                   policy_initial_link = SURF_LINK_FULLDUPLEX;
231           }
232
233   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
234
235   if(policy_initial_link == SURF_LINK_FULLDUPLEX)
236   {
237           net_link_new(bprintf("%s_UP",name_link), bw_initial, bw_trace,
238                        lat_initial, lat_trace, state_initial_link, state_trace,
239                        policy_initial_link, xbt_dict_new());
240           net_link_new(bprintf("%s_DOWN",name_link), bw_initial, bw_trace,
241                        lat_initial, lat_trace, state_initial_link, state_trace,
242                        policy_initial_link, xbt_dict_new());
243   }
244   else
245   {
246           net_link_new(name_link, bw_initial, bw_trace,
247                        lat_initial, lat_trace, state_initial_link, state_trace,
248                        policy_initial_link, xbt_dict_new());
249   }
250
251 }
252
253 static void net_create_resource(char *name,
254                                 double bw_initial,
255                                 tmgr_trace_t bw_trace,
256                                 double lat_initial,
257                                 tmgr_trace_t lat_trace,
258                                 e_surf_resource_state_t
259                                 state_initial,
260                                 tmgr_trace_t state_trace,
261                                 e_surf_link_sharing_policy_t policy,
262                                 xbt_dict_t properties)
263 {
264   net_link_new(name, bw_initial, bw_trace,
265                lat_initial, lat_trace, state_initial, state_trace,
266                policy, xbt_dict_new());
267 }
268
269 static void net_add_traces(void)
270 {
271   xbt_dict_cursor_t cursor = NULL;
272   char *trace_name, *elm;
273
274   static int called = 0;
275   if (called)
276     return;
277   called = 1;
278
279   /* connect all traces relative to network */
280   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
281     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
282     link_CM02_t link =
283         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
284
285     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
286                 trace_name, elm);
287     xbt_assert2(trace,
288                 "Cannot connect trace %s to link %s: trace undefined",
289                 trace_name, elm);
290
291     link->lmm_resource.state_event =
292         tmgr_history_add_trace(history, trace, 0.0, 0, link);
293   }
294
295   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
296     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
297     link_CM02_t link =
298         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
299
300     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
301                 trace_name, elm);
302     xbt_assert2(trace,
303                 "Cannot connect trace %s to link %s: trace undefined",
304                 trace_name, elm);
305
306     link->lmm_resource.power.event =
307         tmgr_history_add_trace(history, trace, 0.0, 0, link);
308   }
309
310   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
311     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
312     link_CM02_t link =
313         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
314
315     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
316                 trace_name, elm);
317     xbt_assert2(trace,
318                 "Cannot connect trace %s to link %s: trace undefined",
319                 trace_name, elm);
320
321     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
322   }
323 }
324
325 static void net_define_callbacks(const char *file)
326 {
327   /* Figuring out the network links */
328   surfxml_add_callback(STag_surfxml_link_cb_list, &net_parse_link_init);
329   surfxml_add_callback(ETag_surfxml_platform_cb_list, &net_add_traces);
330 }
331
332 static int net_resource_used(void *resource_id)
333 {
334   return lmm_constraint_used(network_maxmin_system,
335                              ((surf_resource_lmm_t)
336                               resource_id)->constraint);
337 }
338
339 static int net_action_unref(surf_action_t action)
340 {
341   action->refcount--;
342   if (!action->refcount) {
343     xbt_swag_remove(action, action->state_set);
344     if (((surf_action_network_CM02_t) action)->variable)
345       lmm_variable_free(network_maxmin_system,
346                         ((surf_action_network_CM02_t) action)->variable);
347 #ifdef HAVE_TRACING
348     xbt_free(((surf_action_network_CM02_t) action)->src_name);
349     xbt_free(((surf_action_network_CM02_t) action)->dst_name);
350     if (action->category)
351       xbt_free(action->category);
352 #endif
353     free(action);
354     return 1;
355   }
356   return 0;
357 }
358
359 static void net_action_cancel(surf_action_t action)
360 {
361   surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
362 }
363
364 void net_action_recycle(surf_action_t action)
365 {
366   return;
367 }
368
369 #ifdef HAVE_LATENCY_BOUND_TRACKING
370 int net_get_link_latency_limited(surf_action_t action)
371 {
372   return action->latency_limited;
373 }
374 #endif
375
376 double net_action_get_remains(surf_action_t action)
377 {
378   return action->remains;
379 }
380
381 static double net_share_resources(double now)
382 {
383   s_surf_action_network_CM02_t s_action;
384   surf_action_network_CM02_t action = NULL;
385   xbt_swag_t running_actions =
386       surf_network_model->states.running_action_set;
387   double min;
388
389   min = generic_maxmin_share_resources(running_actions,
390                                        xbt_swag_offset(s_action,
391                                                        variable),
392                                        network_maxmin_system,
393                                        network_solve);
394
395 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable)  )))
396
397   xbt_swag_foreach(action, running_actions) {
398 #ifdef HAVE_LATENCY_BOUND_TRACKING
399     if (lmm_is_variable_limited_by_latency(action->variable)) {
400       (action->generic_action).latency_limited = 1;
401     } else {
402       (action->generic_action).latency_limited = 0;
403     }
404 #endif
405     if (action->latency > 0) {
406       if (min < 0)
407         min = action->latency;
408       else if (action->latency < min)
409         min = action->latency;
410     }
411   }
412
413   return min;
414 }
415
416 static void net_update_actions_state(double now, double delta)
417 {
418   double deltap = 0.0;
419   surf_action_network_CM02_t action = NULL;
420   surf_action_network_CM02_t next_action = NULL;
421   xbt_swag_t running_actions =
422       surf_network_model->states.running_action_set;
423   /*
424      xbt_swag_t failed_actions =
425      surf_network_model->states.failed_action_set;
426    */
427
428   xbt_swag_foreach_safe(action, next_action, running_actions) {
429     deltap = delta;
430     if (action->latency > 0) {
431       if (action->latency > deltap) {
432         double_update(&(action->latency), deltap);
433         deltap = 0.0;
434       } else {
435         double_update(&(deltap), action->latency);
436         action->latency = 0.0;
437       }
438       if ((action->latency == 0.0) && !(action->suspended))
439         lmm_update_variable_weight(network_maxmin_system, action->variable,
440                                    action->weight);
441     }
442 #ifdef HAVE_TRACING
443     xbt_dynar_t route =
444         global_routing->get_route(action->src_name, action->dst_name);
445     link_CM02_t link;
446     unsigned int i;
447     xbt_dynar_foreach(route, i, link) {
448       TRACE_surf_link_set_utilization(link,
449                                       action->generic_action.data,
450                                       (surf_action_t) action,
451                                       lmm_variable_getvalue
452                                       (action->variable), now - delta,
453                                       delta);
454     }
455 #endif
456     if(!lmm_get_number_of_cnst_from_var(network_maxmin_system, action->variable)) {
457                                 /* There is actually no link used, hence an infinite bandwidth.
458                                  * This happens often when using models like vivaldi.
459                                  * In such case, just make sure that the action completes immediately.
460                                  */
461         double_update(&(action->generic_action.remains),
462                         action->generic_action.remains);
463     }
464     double_update(&(action->generic_action.remains),
465                   lmm_variable_getvalue(action->variable) * deltap);
466     if (action->generic_action.max_duration != NO_MAX_DURATION)
467       double_update(&(action->generic_action.max_duration), delta);
468
469     if ((action->generic_action.remains <= 0) &&
470         (lmm_get_variable_weight(action->variable) > 0)) {
471       action->generic_action.finish = surf_get_clock();
472       surf_network_model->action_state_set((surf_action_t) action,
473                                            SURF_ACTION_DONE);
474       gap_remove(action);
475     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
476                && (action->generic_action.max_duration <= 0)) {
477       action->generic_action.finish = surf_get_clock();
478       surf_network_model->action_state_set((surf_action_t) action,
479                                            SURF_ACTION_DONE);
480       gap_remove(action);
481     }
482   }
483
484   return;
485 }
486
487 static void net_update_resource_state(void *id,
488                                       tmgr_trace_event_t event_type,
489                                       double value, double date)
490 {
491   link_CM02_t nw_link = id;
492   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
493   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
494   /*     value, event_type); */
495
496   if (event_type == nw_link->lmm_resource.power.event) {
497     double delta =
498         sg_weight_S_parameter / value - sg_weight_S_parameter /
499         (nw_link->lmm_resource.power.peak *
500          nw_link->lmm_resource.power.scale);
501     lmm_variable_t var = NULL;
502     lmm_element_t elem = NULL;
503     surf_action_network_CM02_t action = NULL;
504
505     nw_link->lmm_resource.power.peak = value;
506     lmm_update_constraint_bound(network_maxmin_system,
507                                 nw_link->lmm_resource.constraint,
508                                 sg_bandwidth_factor *
509                                 (nw_link->lmm_resource.power.peak *
510                                  nw_link->lmm_resource.power.scale));
511 #ifdef HAVE_TRACING
512     TRACE_surf_link_set_bandwidth(date, nw_link,
513                                   sg_bandwidth_factor *
514                                   (nw_link->lmm_resource.power.peak *
515                                    nw_link->lmm_resource.power.scale));
516 #endif
517     if (sg_weight_S_parameter > 0) {
518       while ((var = lmm_get_var_from_cnst
519               (network_maxmin_system, nw_link->lmm_resource.constraint,
520                &elem))) {
521         action = lmm_variable_id(var);
522         action->weight += delta;
523         if (!(action->suspended))
524           lmm_update_variable_weight(network_maxmin_system,
525                                      action->variable, action->weight);
526       }
527     }
528     if (tmgr_trace_event_free(event_type))
529       nw_link->lmm_resource.power.event = NULL;
530   } else if (event_type == nw_link->lat_event) {
531     double delta = value - nw_link->lat_current;
532     lmm_variable_t var = NULL;
533     lmm_element_t elem = NULL;
534     surf_action_network_CM02_t action = NULL;
535
536     nw_link->lat_current = value;
537     while ((var = lmm_get_var_from_cnst
538             (network_maxmin_system, nw_link->lmm_resource.constraint,
539              &elem))) {
540       action = lmm_variable_id(var);
541       action->lat_current += delta;
542       action->weight += delta;
543       if (action->rate < 0)
544         lmm_update_variable_bound(network_maxmin_system, action->variable,
545                                   sg_tcp_gamma / (2.0 *
546                                                   action->lat_current));
547       else {
548         lmm_update_variable_bound(network_maxmin_system, action->variable,
549                                   min(action->rate,
550                                       sg_tcp_gamma / (2.0 *
551                                                       action->lat_current)));
552
553         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
554           INFO0("Flow is limited BYBANDWIDTH");
555         } else {
556           INFO1("Flow is limited BYLATENCY, latency of flow is %f",
557                 action->lat_current);
558         }
559       }
560       if (!(action->suspended))
561         lmm_update_variable_weight(network_maxmin_system, 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               (network_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     CRITICAL0("Unknown event ! \n");
591     xbt_abort();
592   }
593
594   return;
595 }
596
597
598 static surf_action_t net_communicate(const char *src_name,
599                                      const char *dst_name, double size,
600                                      double rate)
601 {
602   unsigned int i;
603   link_CM02_t link;
604   int failed = 0;
605   surf_action_network_CM02_t action = NULL;
606   double bandwidth_bound;
607   double latency=0.0;
608   /* LARGE PLATFORMS HACK:
609      Add a link_CM02_t *link and a int link_nb to network_card_CM02_t. It will represent local links for this node
610      Use the cluster_id for ->id */
611
612   xbt_dynar_t back_route = NULL;
613   int constraints_per_variable = 0;
614   // I will need this route for some time so let's call get_route_no_cleanup
615   xbt_dynar_t route = global_routing->get_route_no_cleanup(src_name, dst_name);
616
617
618   if (sg_network_fullduplex == 1) {
619     back_route = global_routing->get_route(dst_name, src_name);
620   }
621
622   /* LARGE PLATFORMS HACK:
623      total_route_size = route_size + src->link_nb + dst->nb */
624
625   XBT_IN4("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
626   /* LARGE PLATFORMS HACK:
627      assert on total_route_size */
628   latency = global_routing->get_latency(src_name,dst_name);
629   xbt_assert2(xbt_dynar_length(route) || latency,
630               "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
631               src_name, dst_name);
632
633   xbt_dynar_foreach(route, i, link) {
634     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
635       failed = 1;
636       break;
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->generic_action).latency_limited = 0;
644 #endif
645   action->weight = action->latency = latency;
646
647   xbt_swag_insert(action, action->generic_action.state_set);
648   action->rate = rate;
649
650   bandwidth_bound = -1.0;
651
652   xbt_dynar_foreach(route, i, link) {
653     action->weight +=
654         sg_weight_S_parameter /
655         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
656     if (bandwidth_bound < 0.0)
657       bandwidth_bound =
658           (*bandwidth_factor_callback) (size) *
659           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
660     else
661       bandwidth_bound =
662           min(bandwidth_bound,
663               (*bandwidth_factor_callback) (size) *
664               (link->lmm_resource.power.peak *
665                link->lmm_resource.power.scale));
666   }
667   /* LARGE PLATFORMS HACK:
668      Add src->link and dst->link latencies */
669   action->lat_current = action->latency;
670   action->latency *= (*latency_factor_callback) (size);
671   action->rate =
672       (*bandwidth_constraint_callback) (action->rate, bandwidth_bound,
673                                         size);
674
675   if(xbt_dynar_length(route) > 0) {
676     link = *(link_CM02_t*)xbt_dynar_get_ptr(route, 0);
677     gap_append(size, link, action);
678     DEBUG5("Comm %p: %s -> %s gap=%f (lat=%f)",
679            action, src_name, dst_name, action->sender.gap, action->latency);
680   } else {
681     gap_unknown(action);
682   }
683
684
685   /* LARGE PLATFORMS HACK:
686      lmm_variable_new(..., total_route_size) */
687   if (back_route != NULL) {
688     constraints_per_variable =
689         xbt_dynar_length(route) + xbt_dynar_length(back_route);
690   } else {
691     constraints_per_variable = xbt_dynar_length(route);
692   }
693
694   if (action->latency > 0)
695     action->variable =
696         lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
697                          constraints_per_variable);
698   else
699     action->variable =
700         lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
701                          constraints_per_variable);
702
703   if (action->rate < 0) {
704     if (action->lat_current > 0)
705       lmm_update_variable_bound(network_maxmin_system, action->variable,
706                                 sg_tcp_gamma / (2.0 *
707                                                 action->lat_current));
708     else
709       lmm_update_variable_bound(network_maxmin_system, action->variable,
710                                 -1.0);
711   } else {
712     if (action->lat_current > 0)
713       lmm_update_variable_bound(network_maxmin_system, action->variable,
714                                 min(action->rate,
715                                     sg_tcp_gamma / (2.0 *
716                                                     action->lat_current)));
717     else
718       lmm_update_variable_bound(network_maxmin_system, action->variable,
719                                 action->rate);
720   }
721
722   xbt_dynar_foreach(route, i, link) {
723     lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
724                action->variable, 1.0);
725   }
726
727   if (sg_network_fullduplex == 1) {
728     DEBUG1("Fullduplex active adding backward flow using 5%c", '%');
729     xbt_dynar_foreach(back_route, i, link) {
730       lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
731                  action->variable, .05);
732     }
733   }
734   /* LARGE PLATFORMS HACK:
735      expand also with src->link and dst->link */
736 #ifdef HAVE_TRACING
737   action->src_name = xbt_new(char, strlen(src_name) + 1);
738   strncpy(action->src_name, src_name, strlen(src_name) + 1);
739
740   action->dst_name = xbt_new(char, strlen(dst_name) + 1);
741   strncpy(action->dst_name, dst_name, strlen(dst_name) + 1);
742 #endif
743
744   xbt_dynar_free(&route);
745   XBT_OUT;
746
747   return (surf_action_t) action;
748 }
749
750 static xbt_dynar_t net_get_route(const char *src, const char *dst)
751 {
752   return global_routing->get_route(src, dst);
753 }
754
755 static double net_get_link_bandwidth(const void *link)
756 {
757   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
758   return lmm->power.peak * lmm->power.scale;
759 }
760
761 static double net_get_link_latency(const void *link)
762 {
763   return ((link_CM02_t) link)->lat_current;
764 }
765
766 static int net_link_shared(const void *link)
767 {
768   return
769       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
770 }
771
772 static void net_action_suspend(surf_action_t action)
773 {
774   ((surf_action_network_CM02_t) action)->suspended = 1;
775   lmm_update_variable_weight(network_maxmin_system,
776                              ((surf_action_network_CM02_t)
777                               action)->variable, 0.0);
778 }
779
780 static void net_action_resume(surf_action_t action)
781 {
782   if (((surf_action_network_CM02_t) action)->suspended) {
783     lmm_update_variable_weight(network_maxmin_system,
784                                ((surf_action_network_CM02_t)
785                                 action)->variable,
786                                ((surf_action_network_CM02_t)
787                                 action)->weight);
788     ((surf_action_network_CM02_t) action)->suspended = 0;
789   }
790 }
791
792 static int net_action_is_suspended(surf_action_t action)
793 {
794   return ((surf_action_network_CM02_t) action)->suspended;
795 }
796
797 void net_action_set_max_duration(surf_action_t action, double duration)
798 {
799   action->max_duration = duration;
800 }
801
802 #ifdef HAVE_TRACING
803 static void net_action_set_category(surf_action_t action, const char *category)
804 {
805   action->category = xbt_strdup (category);
806 }
807 #endif
808
809 static void net_finalize(void)
810 {
811   surf_model_exit(surf_network_model);
812   surf_network_model = NULL;
813
814   global_routing->finalize();
815
816   lmm_system_free(network_maxmin_system);
817   network_maxmin_system = NULL;
818 }
819
820 static void surf_network_model_init_internal(void)
821 {
822   surf_network_model = surf_model_init();
823
824   surf_network_model->name = "network";
825   surf_network_model->action_unref = net_action_unref;
826   surf_network_model->action_cancel = net_action_cancel;
827   surf_network_model->action_recycle = net_action_recycle;
828   surf_network_model->get_remains = net_action_get_remains;
829 #ifdef HAVE_LATENCY_BOUND_TRACKING
830   surf_network_model->get_latency_limited = net_get_link_latency_limited;
831 #endif
832
833   surf_network_model->model_private->resource_used = net_resource_used;
834   surf_network_model->model_private->share_resources = net_share_resources;
835   surf_network_model->model_private->update_actions_state =
836       net_update_actions_state;
837   surf_network_model->model_private->update_resource_state =
838       net_update_resource_state;
839   surf_network_model->model_private->finalize = net_finalize;
840
841   surf_network_model->suspend = net_action_suspend;
842   surf_network_model->resume = net_action_resume;
843   surf_network_model->is_suspended = net_action_is_suspended;
844   surf_network_model->set_max_duration = net_action_set_max_duration;
845 #ifdef HAVE_TRACING
846   surf_network_model->set_category = net_action_set_category;
847 #endif
848
849   surf_network_model->extension.network.communicate = net_communicate;
850   surf_network_model->extension.network.get_route = net_get_route;
851   surf_network_model->extension.network.get_link_bandwidth =
852       net_get_link_bandwidth;
853   surf_network_model->extension.network.get_link_latency =
854       net_get_link_latency;
855   surf_network_model->extension.network.link_shared = net_link_shared;
856   surf_network_model->extension.network.add_traces = net_add_traces;
857   surf_network_model->extension.network.create_resource =
858       net_create_resource;
859
860   if (!network_maxmin_system)
861     network_maxmin_system = lmm_system_new();
862
863   routing_model_create(sizeof(link_CM02_t),
864                        net_link_new(xbt_strdup("__loopback__"),
865                                     498000000, NULL, 0.000015, NULL,
866                                     SURF_RESOURCE_ON, NULL,
867                                     SURF_LINK_FATPIPE, NULL),
868                        net_get_link_latency);
869 }
870
871
872
873 /************************************************************************/
874 /* New model based on LV08 and experimental results of MPI ping-pongs   */
875 /************************************************************************/
876 void surf_network_model_init_SMPI(const char *filename)
877 {
878
879   if (surf_network_model)
880     return;
881   surf_network_model_init_internal();
882   latency_factor_callback = &smpi_latency_factor;
883   bandwidth_factor_callback = &smpi_bandwidth_factor;
884   bandwidth_constraint_callback = &smpi_bandwidth_constraint;
885   net_define_callbacks(filename);
886   xbt_dynar_push(model_list, &surf_network_model);
887   network_solve = lmm_solve;
888
889   xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
890   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
891
892   update_model_description(surf_network_model_description,
893                            "SMPI", surf_network_model);
894 }
895
896 /************************************************************************/
897 /* New model based on optimizations discussed during this thesis        */
898 /************************************************************************/
899 void surf_network_model_init_LegrandVelho(const char *filename)
900 {
901
902   if (surf_network_model)
903     return;
904   surf_network_model_init_internal();
905   net_define_callbacks(filename);
906   xbt_dynar_push(model_list, &surf_network_model);
907   network_solve = lmm_solve;
908
909   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
910   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
911                             0.92);
912   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
913
914   update_model_description(surf_network_model_description,
915                            "LV08", surf_network_model);
916 }
917
918 /***************************************************************************/
919 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
920 /***************************************************************************/
921 /* @TechReport{      rr-lip2002-40, */
922 /*   author        = {Henri Casanova and Loris Marchal}, */
923 /*   institution   = {LIP}, */
924 /*   title         = {A Network Model for Simulation of Grid Application}, */
925 /*   number        = {2002-40}, */
926 /*   month         = {oct}, */
927 /*   year          = {2002} */
928 /* } */
929 void surf_network_model_init_CM02(const char *filename)
930 {
931
932   if (surf_network_model)
933     return;
934   surf_network_model_init_internal();
935   net_define_callbacks(filename);
936   xbt_dynar_push(model_list, &surf_network_model);
937   network_solve = lmm_solve;
938
939   update_model_description(surf_network_model_description,
940                            "CM02", surf_network_model);
941 }
942
943 void surf_network_model_init_Reno(const char *filename)
944 {
945   if (surf_network_model)
946     return;
947   surf_network_model_init_internal();
948   net_define_callbacks(filename);
949
950   xbt_dynar_push(model_list, &surf_network_model);
951   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
952                                     func_reno_fpi);
953   network_solve = lagrange_solve;
954
955   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
956   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
957                             0.92);
958   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
959
960   update_model_description(surf_network_model_description,
961                            "Reno", surf_network_model);
962 }
963
964
965 void surf_network_model_init_Reno2(const char *filename)
966 {
967   if (surf_network_model)
968     return;
969   surf_network_model_init_internal();
970   net_define_callbacks(filename);
971
972   xbt_dynar_push(model_list, &surf_network_model);
973   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
974                                     func_reno2_fpi);
975   network_solve = lagrange_solve;
976
977   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
978   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
979                             0.92);
980   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
981                             8775);
982
983   update_model_description(surf_network_model_description,
984                            "Reno2", surf_network_model);
985 }
986
987 void surf_network_model_init_Vegas(const char *filename)
988 {
989   if (surf_network_model)
990     return;
991   surf_network_model_init_internal();
992   net_define_callbacks(filename);
993
994   xbt_dynar_push(model_list, &surf_network_model);
995   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
996                                     func_vegas_fpi);
997   network_solve = lagrange_solve;
998
999   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
1000   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1001                             0.92);
1002   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1003
1004   update_model_description(surf_network_model_description,
1005                            "Vegas", surf_network_model);
1006 }