Logo AND Algorithmique Numérique Distribuée

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