Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace a combination of malloc+strcpy by xbt_strdup.
[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   XBT_DEBUG("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     surf_action_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   XBT_DEBUG("Min of share resources %f", min);
411
412   return min;
413 }
414
415 static void net_update_actions_state(double now, double delta)
416 {
417   double deltap = 0.0;
418   surf_action_network_CM02_t action = NULL;
419   surf_action_network_CM02_t next_action = NULL;
420   xbt_swag_t running_actions =
421       surf_network_model->states.running_action_set;
422   /*
423      xbt_swag_t failed_actions =
424      surf_network_model->states.failed_action_set;
425    */
426
427   xbt_swag_foreach_safe(action, next_action, running_actions) {
428     deltap = delta;
429     if (action->latency > 0) {
430       if (action->latency > deltap) {
431         double_update(&(action->latency), deltap);
432         deltap = 0.0;
433       } else {
434         double_update(&(deltap), action->latency);
435         action->latency = 0.0;
436       }
437       if ((action->latency == 0.0) && !(action->suspended))
438         lmm_update_variable_weight(network_maxmin_system, action->variable,
439                                    action->weight);
440     }
441 #ifdef HAVE_TRACING
442     xbt_dynar_t route =
443         global_routing->get_route(action->src_name, action->dst_name);
444     link_CM02_t link;
445     unsigned int i;
446     xbt_dynar_foreach(route, i, link) {
447       TRACE_surf_link_set_utilization(link->lmm_resource.generic_resource.name,
448                                       action->generic_action.data,
449                                       (surf_action_t) action,
450                                       lmm_variable_getvalue
451                                       (action->variable), now - delta,
452                                       delta);
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   // I will need this route for some time so let's call get_route_no_cleanup
614   xbt_dynar_t route = global_routing->get_route_no_cleanup(src_name, dst_name);
615
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   latency = global_routing->get_latency(src_name,dst_name);
628   xbt_assert2(xbt_dynar_length(route) || latency,
629               "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
630               src_name, dst_name);
631
632   xbt_dynar_foreach(route, i, link) {
633     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
634       failed = 1;
635       break;
636     }
637   }
638   action =
639       surf_action_new(sizeof(s_surf_action_network_CM02_t), size,
640                       surf_network_model, failed);
641 #ifdef HAVE_LATENCY_BOUND_TRACKING
642   (action->generic_action).latency_limited = 0;
643 #endif
644   action->weight = action->latency = latency;
645
646   xbt_swag_insert(action, action->generic_action.state_set);
647   action->rate = rate;
648
649   bandwidth_bound = -1.0;
650
651   xbt_dynar_foreach(route, i, link) {
652     action->weight +=
653         sg_weight_S_parameter /
654         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
655     if (bandwidth_bound < 0.0)
656       bandwidth_bound =
657           (*bandwidth_factor_callback) (size) *
658           (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
659     else
660       bandwidth_bound =
661           min(bandwidth_bound,
662               (*bandwidth_factor_callback) (size) *
663               (link->lmm_resource.power.peak *
664                link->lmm_resource.power.scale));
665   }
666   /* LARGE PLATFORMS HACK:
667      Add src->link and dst->link latencies */
668   action->lat_current = action->latency;
669   action->latency *= (*latency_factor_callback) (size);
670   action->rate =
671       (*bandwidth_constraint_callback) (action->rate, bandwidth_bound,
672                                         size);
673
674   if(xbt_dynar_length(route) > 0) {
675     link = *(link_CM02_t*)xbt_dynar_get_ptr(route, 0);
676     gap_append(size, link, action);
677     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
678            action, src_name, dst_name, action->sender.gap, action->latency);
679   } else {
680     gap_unknown(action);
681   }
682
683
684   /* LARGE PLATFORMS HACK:
685      lmm_variable_new(..., total_route_size) */
686   if (back_route != NULL) {
687     constraints_per_variable =
688         xbt_dynar_length(route) + xbt_dynar_length(back_route);
689   } else {
690     constraints_per_variable = xbt_dynar_length(route);
691   }
692
693   if (action->latency > 0)
694     action->variable =
695         lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
696                          constraints_per_variable);
697   else
698     action->variable =
699         lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
700                          constraints_per_variable);
701
702   if (action->rate < 0) {
703     if (action->lat_current > 0)
704       lmm_update_variable_bound(network_maxmin_system, action->variable,
705                                 sg_tcp_gamma / (2.0 *
706                                                 action->lat_current));
707     else
708       lmm_update_variable_bound(network_maxmin_system, action->variable,
709                                 -1.0);
710   } else {
711     if (action->lat_current > 0)
712       lmm_update_variable_bound(network_maxmin_system, action->variable,
713                                 min(action->rate,
714                                     sg_tcp_gamma / (2.0 *
715                                                     action->lat_current)));
716     else
717       lmm_update_variable_bound(network_maxmin_system, action->variable,
718                                 action->rate);
719   }
720
721   xbt_dynar_foreach(route, i, link) {
722     lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
723                action->variable, 1.0);
724   }
725
726   if (sg_network_fullduplex == 1) {
727     XBT_DEBUG("Fullduplex active adding backward flow using 5%c", '%');
728     xbt_dynar_foreach(back_route, i, link) {
729       lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
730                  action->variable, .05);
731     }
732   }
733   /* LARGE PLATFORMS HACK:
734      expand also with src->link and dst->link */
735 #ifdef HAVE_TRACING
736   action->src_name = xbt_strdup(src_name);
737
738   action->dst_name = xbt_strdup(dst_name);
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 }