Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use the partial invalidation optimization by default for the network too
[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_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
177               "Link '%s' declared several times in the platform file.",
178               name);
179
180   nw_link->lat_current = lat_initial;
181   if (lat_trace)
182     nw_link->lat_event =
183         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
184
185   if (policy == SURF_LINK_FATPIPE)
186     lmm_constraint_shared(nw_link->lmm_resource.constraint);
187
188   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
189
190   return nw_link;
191 }
192
193 static void net_parse_link_init(void)
194 {
195   char *name_link;
196   double bw_initial;
197   tmgr_trace_t bw_trace;
198   double lat_initial;
199   tmgr_trace_t lat_trace;
200   e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
201   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
202   tmgr_trace_t state_trace;
203   XBT_DEBUG("link_CM02");
204   name_link = xbt_strdup(A_surfxml_link_id);
205   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
206   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
207   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
208   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
209
210   xbt_assert((A_surfxml_link_state == A_surfxml_link_state_ON)
211               || (A_surfxml_link_state ==
212                   A_surfxml_link_state_OFF), "Invalid state");
213   if (A_surfxml_link_state == A_surfxml_link_state_ON)
214     state_initial_link = SURF_RESOURCE_ON;
215   else if (A_surfxml_link_state == A_surfxml_link_state_OFF)
216     state_initial_link = SURF_RESOURCE_OFF;
217
218   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
219     policy_initial_link = SURF_LINK_SHARED;
220   else
221           {
222           if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
223                   policy_initial_link = SURF_LINK_FATPIPE;
224           else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
225                   policy_initial_link = SURF_LINK_FULLDUPLEX;
226           }
227
228   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
229
230   if(policy_initial_link == SURF_LINK_FULLDUPLEX)
231   {
232           net_link_new(bprintf("%s_UP",name_link), bw_initial, bw_trace,
233                        lat_initial, lat_trace, state_initial_link, state_trace,
234                        policy_initial_link, xbt_dict_new());
235           net_link_new(bprintf("%s_DOWN",name_link), bw_initial, bw_trace,
236                        lat_initial, lat_trace, state_initial_link, state_trace,
237                        policy_initial_link, xbt_dict_new());
238   }
239   else
240   {
241           net_link_new(name_link, bw_initial, bw_trace,
242                        lat_initial, lat_trace, state_initial_link, state_trace,
243                        policy_initial_link, xbt_dict_new());
244   }
245
246 }
247
248 static void net_create_resource(char *name,
249                                 double bw_initial,
250                                 tmgr_trace_t bw_trace,
251                                 double lat_initial,
252                                 tmgr_trace_t lat_trace,
253                                 e_surf_resource_state_t
254                                 state_initial,
255                                 tmgr_trace_t state_trace,
256                                 e_surf_link_sharing_policy_t policy,
257                                 xbt_dict_t properties)
258 {
259   net_link_new(name, bw_initial, bw_trace,
260                lat_initial, lat_trace, state_initial, state_trace,
261                policy, xbt_dict_new());
262 }
263
264 static void net_add_traces(void)
265 {
266   xbt_dict_cursor_t cursor = NULL;
267   char *trace_name, *elm;
268
269   static int called = 0;
270   if (called)
271     return;
272   called = 1;
273
274   /* connect all traces relative to network */
275   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
276     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
277     link_CM02_t link =
278         xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
279
280     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
281                 trace_name, elm);
282     xbt_assert(trace,
283                 "Cannot connect trace %s to link %s: trace undefined",
284                 trace_name, elm);
285
286     link->lmm_resource.state_event =
287         tmgr_history_add_trace(history, trace, 0.0, 0, link);
288   }
289
290   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
291     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
292     link_CM02_t link =
293                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
294
295     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
296                 trace_name, elm);
297     xbt_assert(trace,
298                 "Cannot connect trace %s to link %s: trace undefined",
299                 trace_name, elm);
300
301     link->lmm_resource.power.event =
302         tmgr_history_add_trace(history, trace, 0.0, 0, link);
303   }
304
305   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
306     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
307     link_CM02_t link =
308                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
309
310     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
311                 trace_name, elm);
312     xbt_assert(trace,
313                 "Cannot connect trace %s to link %s: trace undefined",
314                 trace_name, elm);
315
316     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
317   }
318 }
319
320 static void net_define_callbacks(const char *file)
321 {
322   /* Figuring out the network links */
323   surfxml_add_callback(STag_surfxml_link_cb_list, &net_parse_link_init);
324   surfxml_add_callback(ETag_surfxml_platform_cb_list, &net_add_traces);
325 }
326
327 static int net_resource_used(void *resource_id)
328 {
329   return lmm_constraint_used(network_maxmin_system,
330                              ((surf_resource_lmm_t)
331                               resource_id)->constraint);
332 }
333
334 static int net_action_unref(surf_action_t action)
335 {
336   action->refcount--;
337   if (!action->refcount) {
338     xbt_swag_remove(action, action->state_set);
339     if (((surf_action_network_CM02_t) action)->variable)
340       lmm_variable_free(network_maxmin_system,
341                         ((surf_action_network_CM02_t) action)->variable);
342 #ifdef HAVE_TRACING
343     xbt_free(((surf_action_network_CM02_t) action)->src_name);
344     xbt_free(((surf_action_network_CM02_t) action)->dst_name);
345     if (action->category)
346       xbt_free(action->category);
347 #endif
348     surf_action_free(&action);
349     return 1;
350   }
351   return 0;
352 }
353
354 static void net_action_cancel(surf_action_t action)
355 {
356   surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
357 }
358
359 void net_action_recycle(surf_action_t action)
360 {
361   return;
362 }
363
364 #ifdef HAVE_LATENCY_BOUND_TRACKING
365 int net_get_link_latency_limited(surf_action_t action)
366 {
367   return action->latency_limited;
368 }
369 #endif
370
371 double net_action_get_remains(surf_action_t action)
372 {
373   return action->remains;
374 }
375
376 static double net_share_resources(double now)
377 {
378   s_surf_action_network_CM02_t s_action;
379   surf_action_network_CM02_t action = NULL;
380   xbt_swag_t running_actions =
381       surf_network_model->states.running_action_set;
382   double min;
383
384   min = generic_maxmin_share_resources(running_actions,
385                                        xbt_swag_offset(s_action,
386                                                        variable),
387                                        network_maxmin_system,
388                                        network_solve);
389
390 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable)  )))
391
392   xbt_swag_foreach(action, running_actions) {
393 #ifdef HAVE_LATENCY_BOUND_TRACKING
394     if (lmm_is_variable_limited_by_latency(action->variable)) {
395       (action->generic_action).latency_limited = 1;
396     } else {
397       (action->generic_action).latency_limited = 0;
398     }
399 #endif
400     if (action->latency > 0) {
401       if (min < 0)
402         min = action->latency;
403       else if (action->latency < min)
404         min = action->latency;
405     }
406   }
407
408   XBT_DEBUG("Min of share resources %f", min);
409
410   return min;
411 }
412
413 static void net_update_actions_state(double now, double delta)
414 {
415   double deltap = 0.0;
416   surf_action_network_CM02_t action = NULL;
417   surf_action_network_CM02_t next_action = NULL;
418   xbt_swag_t running_actions =
419       surf_network_model->states.running_action_set;
420   /*
421      xbt_swag_t failed_actions =
422      surf_network_model->states.failed_action_set;
423    */
424
425   xbt_swag_foreach_safe(action, next_action, running_actions) {
426     deltap = delta;
427     if (action->latency > 0) {
428       if (action->latency > deltap) {
429         double_update(&(action->latency), deltap);
430         deltap = 0.0;
431       } else {
432         double_update(&(deltap), action->latency);
433         action->latency = 0.0;
434       }
435       if ((action->latency == 0.0) && !(action->suspended))
436         lmm_update_variable_weight(network_maxmin_system, action->variable,
437                                    action->weight);
438     }
439 #ifdef HAVE_TRACING
440     if (TRACE_is_enabled()) {
441       xbt_dynar_t route = global_routing->get_route(action->src_name,
442                                                     action->dst_name);
443       link_CM02_t link;
444       unsigned int i;
445       xbt_dynar_foreach(route, i, link) {
446         TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
447                                         action->generic_action.data,
448                                         (surf_action_t) action,
449                                         lmm_variable_getvalue
450                                         (action->variable), now - delta,
451                                         delta);
452       }
453     }
454 #endif
455     if(!lmm_get_number_of_cnst_from_var(network_maxmin_system, action->variable)) {
456                                 /* There is actually no link used, hence an infinite bandwidth.
457                                  * This happens often when using models like vivaldi.
458                                  * In such case, just make sure that the action completes immediately.
459                                  */
460         double_update(&(action->generic_action.remains),
461                         action->generic_action.remains);
462     }
463     double_update(&(action->generic_action.remains),
464                   lmm_variable_getvalue(action->variable) * deltap);
465     if (action->generic_action.max_duration != NO_MAX_DURATION)
466       double_update(&(action->generic_action.max_duration), delta);
467
468     if ((action->generic_action.remains <= 0) &&
469         (lmm_get_variable_weight(action->variable) > 0)) {
470       action->generic_action.finish = surf_get_clock();
471       surf_network_model->action_state_set((surf_action_t) action,
472                                            SURF_ACTION_DONE);
473       gap_remove(action);
474     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
475                && (action->generic_action.max_duration <= 0)) {
476       action->generic_action.finish = surf_get_clock();
477       surf_network_model->action_state_set((surf_action_t) action,
478                                            SURF_ACTION_DONE);
479       gap_remove(action);
480     }
481   }
482
483   return;
484 }
485
486 static void net_update_resource_state(void *id,
487                                       tmgr_trace_event_t event_type,
488                                       double value, double date)
489 {
490   link_CM02_t nw_link = id;
491   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
492   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
493   /*     value, event_type); */
494
495   if (event_type == nw_link->lmm_resource.power.event) {
496     double delta =
497         sg_weight_S_parameter / value - sg_weight_S_parameter /
498         (nw_link->lmm_resource.power.peak *
499          nw_link->lmm_resource.power.scale);
500     lmm_variable_t var = NULL;
501     lmm_element_t elem = NULL;
502     surf_action_network_CM02_t action = NULL;
503
504     nw_link->lmm_resource.power.peak = value;
505     lmm_update_constraint_bound(network_maxmin_system,
506                                 nw_link->lmm_resource.constraint,
507                                 sg_bandwidth_factor *
508                                 (nw_link->lmm_resource.power.peak *
509                                  nw_link->lmm_resource.power.scale));
510 #ifdef HAVE_TRACING
511     TRACE_surf_link_set_bandwidth(date, nw_link->lmm_resource.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               (network_maxmin_system, nw_link->lmm_resource.constraint,
519                &elem))) {
520         action = lmm_variable_id(var);
521         action->weight += delta;
522         if (!(action->suspended))
523           lmm_update_variable_weight(network_maxmin_system,
524                                      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             (network_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(network_maxmin_system, action->variable,
544                                   sg_tcp_gamma / (2.0 *
545                                                   action->lat_current));
546       else {
547         lmm_update_variable_bound(network_maxmin_system, action->variable,
548                                   min(action->rate,
549                                       sg_tcp_gamma / (2.0 *
550                                                       action->lat_current)));
551
552         if (action->rate < sg_tcp_gamma / (2.0 * action->lat_current)) {
553           XBT_INFO("Flow is limited BYBANDWIDTH");
554         } else {
555           XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
556                 action->lat_current);
557         }
558       }
559       if (!(action->suspended))
560         lmm_update_variable_weight(network_maxmin_system, action->variable,
561                                    action->weight);
562
563     }
564     if (tmgr_trace_event_free(event_type))
565       nw_link->lat_event = NULL;
566   } else if (event_type == nw_link->lmm_resource.state_event) {
567     if (value > 0)
568       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
569     else {
570       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
571       lmm_variable_t var = NULL;
572       lmm_element_t elem = NULL;
573
574       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
575       while ((var = lmm_get_var_from_cnst
576               (network_maxmin_system, cnst, &elem))) {
577         surf_action_t action = lmm_variable_id(var);
578
579         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
580             surf_action_state_get(action) == SURF_ACTION_READY) {
581           action->finish = date;
582           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
583         }
584       }
585     }
586     if (tmgr_trace_event_free(event_type))
587       nw_link->lmm_resource.state_event = NULL;
588   } else {
589     XBT_CRITICAL("Unknown event ! \n");
590     xbt_abort();
591   }
592
593   return;
594 }
595
596
597 static surf_action_t net_communicate(const char *src_name,
598                                      const char *dst_name, double size,
599                                      double rate)
600 {
601   unsigned int i;
602   link_CM02_t link;
603   int failed = 0;
604   surf_action_network_CM02_t action = NULL;
605   double bandwidth_bound;
606   double latency=0.0;
607   /* LARGE PLATFORMS HACK:
608      Add a link_CM02_t *link and a int link_nb to network_card_CM02_t. It will represent local links for this node
609      Use the cluster_id for ->id */
610
611   xbt_dynar_t back_route = NULL;
612   int constraints_per_variable = 0;
613   xbt_dynar_t route;
614   // I will need this route for some time so require for no cleanup
615   global_routing->get_route_latency(src_name, dst_name, &route, &latency, 0);
616
617   if (sg_network_fullduplex == 1) {
618     back_route = global_routing->get_route(dst_name, src_name);
619   }
620
621   /* LARGE PLATFORMS HACK:
622      total_route_size = route_size + src->link_nb + dst->nb */
623
624   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
625   /* LARGE PLATFORMS HACK:
626      assert on total_route_size */
627   xbt_assert(xbt_dynar_length(route) || latency,
628               "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
629               src_name, dst_name);
630
631   xbt_dynar_foreach(route, i, link) {
632     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
633       failed = 1;
634       break;
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->generic_action).latency_limited = 0;
642 #endif
643   action->weight = action->latency = latency;
644
645   xbt_swag_insert(action, action->generic_action.state_set);
646   action->rate = rate;
647
648   bandwidth_bound = -1.0;
649
650   xbt_dynar_foreach(route, i, link) {
651     action->weight +=
652         sg_weight_S_parameter /
653         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
654     if (bandwidth_bound < 0.0)
655       bandwidth_bound =
656           (*bandwidth_factor_callback) (size) *
657           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
658     else
659       bandwidth_bound =
660           min(bandwidth_bound,
661               (*bandwidth_factor_callback) (size) *
662               (link->lmm_resource.power.peak *
663                link->lmm_resource.power.scale));
664   }
665   /* LARGE PLATFORMS HACK:
666      Add src->link and dst->link latencies */
667   action->lat_current = action->latency;
668   action->latency *= (*latency_factor_callback) (size);
669   action->rate =
670       (*bandwidth_constraint_callback) (action->rate, bandwidth_bound,
671                                         size);
672
673   if(xbt_dynar_length(route) > 0) {
674     link = *(link_CM02_t*)xbt_dynar_get_ptr(route, 0);
675     gap_append(size, link, action);
676     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
677            action, src_name, dst_name, action->sender.gap, action->latency);
678   } else {
679     gap_unknown(action);
680   }
681
682
683   /* LARGE PLATFORMS HACK:
684      lmm_variable_new(..., total_route_size) */
685   if (back_route != NULL) {
686     constraints_per_variable =
687         xbt_dynar_length(route) + xbt_dynar_length(back_route);
688   } else {
689     constraints_per_variable = xbt_dynar_length(route);
690   }
691
692   if (action->latency > 0)
693     action->variable =
694         lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
695                          constraints_per_variable);
696   else
697     action->variable =
698         lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
699                          constraints_per_variable);
700
701   if (action->rate < 0) {
702     if (action->lat_current > 0)
703       lmm_update_variable_bound(network_maxmin_system, action->variable,
704                                 sg_tcp_gamma / (2.0 *
705                                                 action->lat_current));
706     else
707       lmm_update_variable_bound(network_maxmin_system, action->variable,
708                                 -1.0);
709   } else {
710     if (action->lat_current > 0)
711       lmm_update_variable_bound(network_maxmin_system, action->variable,
712                                 min(action->rate,
713                                     sg_tcp_gamma / (2.0 *
714                                                     action->lat_current)));
715     else
716       lmm_update_variable_bound(network_maxmin_system, action->variable,
717                                 action->rate);
718   }
719
720   xbt_dynar_foreach(route, i, link) {
721     lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
722                action->variable, 1.0);
723   }
724
725   if (sg_network_fullduplex == 1) {
726     XBT_DEBUG("Fullduplex active adding backward flow using 5%c", '%');
727     xbt_dynar_foreach(back_route, i, link) {
728       lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
729                  action->variable, .05);
730     }
731   }
732   /* LARGE PLATFORMS HACK:
733      expand also with src->link and dst->link */
734 #ifdef HAVE_TRACING
735   if (TRACE_is_enabled()) {
736     action->src_name = xbt_strdup(src_name);
737     action->dst_name = xbt_strdup(dst_name);
738   } else {
739     action->src_name = action->dst_name = NULL;
740   }
741 #endif
742
743   xbt_dynar_free(&route);
744   XBT_OUT();
745
746   return (surf_action_t) action;
747 }
748
749 static xbt_dynar_t net_get_route(const char *src, const char *dst)
750 {
751   return global_routing->get_route(src, dst);
752 }
753
754 static double net_get_link_bandwidth(const void *link)
755 {
756   surf_resource_lmm_t lmm = (surf_resource_lmm_t) link;
757   return lmm->power.peak * lmm->power.scale;
758 }
759
760 static double net_get_link_latency(const void *link)
761 {
762   return ((link_CM02_t) link)->lat_current;
763 }
764
765 static int net_link_shared(const void *link)
766 {
767   return
768       lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
769 }
770
771 static void net_action_suspend(surf_action_t action)
772 {
773   ((surf_action_network_CM02_t) action)->suspended = 1;
774   lmm_update_variable_weight(network_maxmin_system,
775                              ((surf_action_network_CM02_t)
776                               action)->variable, 0.0);
777 }
778
779 static void net_action_resume(surf_action_t action)
780 {
781   if (((surf_action_network_CM02_t) action)->suspended) {
782     lmm_update_variable_weight(network_maxmin_system,
783                                ((surf_action_network_CM02_t)
784                                 action)->variable,
785                                ((surf_action_network_CM02_t)
786                                 action)->weight);
787     ((surf_action_network_CM02_t) action)->suspended = 0;
788   }
789 }
790
791 static int net_action_is_suspended(surf_action_t action)
792 {
793   return ((surf_action_network_CM02_t) action)->suspended;
794 }
795
796 void net_action_set_max_duration(surf_action_t action, double duration)
797 {
798   action->max_duration = duration;
799 }
800
801 #ifdef HAVE_TRACING
802 static void net_action_set_category(surf_action_t action, const char *category)
803 {
804   action->category = xbt_strdup (category);
805 }
806 #endif
807
808 static void net_finalize(void)
809 {
810   surf_model_exit(surf_network_model);
811   surf_network_model = NULL;
812
813   global_routing->finalize();
814
815   lmm_system_free(network_maxmin_system);
816   network_maxmin_system = NULL;
817 }
818
819 static void surf_network_model_init_internal(void)
820 {
821   surf_network_model = surf_model_init();
822
823   surf_network_model->name = "network";
824   surf_network_model->action_unref = net_action_unref;
825   surf_network_model->action_cancel = net_action_cancel;
826   surf_network_model->action_recycle = net_action_recycle;
827   surf_network_model->get_remains = net_action_get_remains;
828 #ifdef HAVE_LATENCY_BOUND_TRACKING
829   surf_network_model->get_latency_limited = net_get_link_latency_limited;
830 #endif
831
832   surf_network_model->model_private->resource_used = net_resource_used;
833   surf_network_model->model_private->share_resources = net_share_resources;
834   surf_network_model->model_private->update_actions_state =
835       net_update_actions_state;
836   surf_network_model->model_private->update_resource_state =
837       net_update_resource_state;
838   surf_network_model->model_private->finalize = net_finalize;
839
840   surf_network_model->suspend = net_action_suspend;
841   surf_network_model->resume = net_action_resume;
842   surf_network_model->is_suspended = net_action_is_suspended;
843   surf_network_model->set_max_duration = net_action_set_max_duration;
844 #ifdef HAVE_TRACING
845   surf_network_model->set_category = net_action_set_category;
846 #endif
847
848   surf_network_model->extension.network.communicate = net_communicate;
849   surf_network_model->extension.network.get_route = net_get_route;
850   surf_network_model->extension.network.get_link_bandwidth =
851       net_get_link_bandwidth;
852   surf_network_model->extension.network.get_link_latency =
853       net_get_link_latency;
854   surf_network_model->extension.network.link_shared = net_link_shared;
855   surf_network_model->extension.network.add_traces = net_add_traces;
856   surf_network_model->extension.network.create_resource =
857       net_create_resource;
858
859   if (!network_maxmin_system)
860     network_maxmin_system = lmm_system_new();
861
862   routing_model_create(sizeof(link_CM02_t),
863                        net_link_new(xbt_strdup("__loopback__"),
864                                     498000000, NULL, 0.000015, NULL,
865                                     SURF_RESOURCE_ON, NULL,
866                                     SURF_LINK_FATPIPE, NULL),
867                        net_get_link_latency);
868 }
869
870
871
872 /************************************************************************/
873 /* New model based on LV08 and experimental results of MPI ping-pongs   */
874 /************************************************************************/
875 void surf_network_model_init_SMPI(const char *filename)
876 {
877
878   if (surf_network_model)
879     return;
880   surf_network_model_init_internal();
881   latency_factor_callback = &smpi_latency_factor;
882   bandwidth_factor_callback = &smpi_bandwidth_factor;
883   bandwidth_constraint_callback = &smpi_bandwidth_constraint;
884   net_define_callbacks(filename);
885   xbt_dynar_push(model_list, &surf_network_model);
886   network_solve = lmm_solve;
887
888   xbt_cfg_setdefault_double(_surf_cfg_set, "network/sender_gap", 10e-6);
889   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
890
891   update_model_description(surf_network_model_description,
892                            "SMPI", surf_network_model);
893 }
894
895 /************************************************************************/
896 /* New model based on optimizations discussed during this thesis        */
897 /************************************************************************/
898 void surf_network_model_init_LegrandVelho(const char *filename)
899 {
900
901   if (surf_network_model)
902     return;
903   surf_network_model_init_internal();
904   net_define_callbacks(filename);
905   xbt_dynar_push(model_list, &surf_network_model);
906   network_solve = lmm_solve;
907
908   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
909   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
910                             0.92);
911   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
912
913   update_model_description(surf_network_model_description,
914                            "LV08_fullupdate", surf_network_model);
915 }
916
917 /***************************************************************************/
918 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
919 /***************************************************************************/
920 /* @TechReport{      rr-lip2002-40, */
921 /*   author        = {Henri Casanova and Loris Marchal}, */
922 /*   institution   = {LIP}, */
923 /*   title         = {A Network Model for Simulation of Grid Application}, */
924 /*   number        = {2002-40}, */
925 /*   month         = {oct}, */
926 /*   year          = {2002} */
927 /* } */
928 void surf_network_model_init_CM02(const char *filename)
929 {
930
931   if (surf_network_model)
932     return;
933   surf_network_model_init_internal();
934   net_define_callbacks(filename);
935   xbt_dynar_push(model_list, &surf_network_model);
936   network_solve = lmm_solve;
937
938   update_model_description(surf_network_model_description,
939                            "CM02", surf_network_model);
940 }
941
942 void surf_network_model_init_Reno(const char *filename)
943 {
944   if (surf_network_model)
945     return;
946   surf_network_model_init_internal();
947   net_define_callbacks(filename);
948
949   xbt_dynar_push(model_list, &surf_network_model);
950   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
951                                     func_reno_fpi);
952   network_solve = lagrange_solve;
953
954   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
955   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
956                             0.92);
957   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
958
959   update_model_description(surf_network_model_description,
960                            "Reno", surf_network_model);
961 }
962
963
964 void surf_network_model_init_Reno2(const char *filename)
965 {
966   if (surf_network_model)
967     return;
968   surf_network_model_init_internal();
969   net_define_callbacks(filename);
970
971   xbt_dynar_push(model_list, &surf_network_model);
972   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
973                                     func_reno2_fpi);
974   network_solve = lagrange_solve;
975
976   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
977   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
978                             0.92);
979   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S_parameter",
980                             8775);
981
982   update_model_description(surf_network_model_description,
983                            "Reno2", surf_network_model);
984 }
985
986 void surf_network_model_init_Vegas(const char *filename)
987 {
988   if (surf_network_model)
989     return;
990   surf_network_model_init_internal();
991   net_define_callbacks(filename);
992
993   xbt_dynar_push(model_list, &surf_network_model);
994   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
995                                     func_vegas_fpi);
996   network_solve = lagrange_solve;
997
998   xbt_cfg_setdefault_double(_surf_cfg_set, "network/latency_factor", 10.4);
999   xbt_cfg_setdefault_double(_surf_cfg_set, "network/bandwidth_factor",
1000                             0.92);
1001   xbt_cfg_setdefault_double(_surf_cfg_set, "network/weight_S", 8775);
1002
1003   update_model_description(surf_network_model_description,
1004                            "Vegas", surf_network_model);
1005 }