Logo AND Algorithmique Numérique Distribuée

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