Logo AND Algorithmique Numérique Distribuée

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