Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Handle failed tasks in update_resource_state function instead of the update_action_st...
[simgrid.git] / src / surf / network.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "network_private.h"
9 #include "xbt/log.h"
10 #include "xbt/str.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
13                                 "Logging specific to the SURF network module");
14
15 surf_network_model_t surf_network_model = NULL;
16 static lmm_system_t network_maxmin_system = NULL;
17 static void (*network_solve) (lmm_system_t) = NULL;
18
19 xbt_dict_t link_set = NULL;
20 xbt_dict_t network_card_set = NULL;
21
22 int card_number = 0;
23 int host_number = 0;
24 link_CM02_t **routing_table = NULL;
25 int *routing_table_size = NULL;
26 static link_CM02_t loopback = NULL;
27
28 static void create_routing_table(void)
29 {
30   routing_table =
31       xbt_new0(link_CM02_t *, /*card_number * card_number */ host_number * host_number);
32   routing_table_size = xbt_new0(int, /*card_number * card_number*/ host_number * host_number);
33 }
34
35 static void link_free(void *nw_link)
36 {
37   free(((link_CM02_t) nw_link)->name);
38   free(nw_link);
39 }
40
41 static link_CM02_t link_new(char *name,
42                             double bw_initial,
43                             tmgr_trace_t bw_trace,
44                             double lat_initial,
45                             tmgr_trace_t lat_trace,
46                             e_surf_link_state_t
47                             state_initial,
48                             tmgr_trace_t state_trace,
49                             e_surf_link_sharing_policy_t
50                             policy, xbt_dict_t properties)
51 {
52   link_CM02_t nw_link = xbt_new0(s_link_CM02_t, 1);
53   xbt_assert1(!xbt_dict_get_or_null(link_set, name), 
54               "Link '%s' declared several times in the platform file.", name);   
55
56   nw_link->model = (surf_model_t) surf_network_model;
57   nw_link->name = name;
58   nw_link->bw_current = bw_initial;
59   if (bw_trace)
60     nw_link->bw_event =
61         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
62   nw_link->lat_current = lat_initial;
63   if (lat_trace)
64     nw_link->lat_event =
65         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
66   nw_link->state_current = state_initial;
67   if (state_trace)
68     nw_link->state_event =
69         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
70
71   nw_link->constraint =
72       lmm_constraint_new(network_maxmin_system, nw_link,
73                          nw_link->bw_current);
74
75   if (policy == SURF_LINK_FATPIPE)
76     lmm_constraint_shared(nw_link->constraint);
77
78   nw_link->properties = properties;
79
80   current_property_set = properties;
81
82   xbt_dict_set(link_set, name, nw_link, link_free);
83
84   return nw_link;
85 }
86
87 static void network_card_free(void *nw_card)
88 {
89   free(((network_card_CM02_t) nw_card)->name);
90   free(nw_card);
91 }
92
93 static int network_card_new(const char *card_name)
94 {
95   network_card_CM02_t card =
96       xbt_dict_get_or_null(network_card_set, card_name);
97
98   if (!card) {
99     card = xbt_new0(s_network_card_CM02_t, 1);
100     card->name = xbt_strdup(card_name);
101     card->id = card_number++;
102     xbt_dict_set(network_card_set, card_name, card, network_card_free);
103   }
104   return card->id;
105 }
106
107 static void route_new(int src_id, int dst_id,
108                       link_CM02_t * link_list, int nb_link)
109 {
110   ROUTE_SIZE(src_id, dst_id) = nb_link;
111   ROUTE(src_id, dst_id) = link_list =
112       xbt_realloc(link_list, sizeof(link_CM02_t) * nb_link);
113 }
114
115 static void parse_link_init(void)
116 {
117   char *name_link;
118   double bw_initial;
119   tmgr_trace_t bw_trace;
120   double lat_initial;
121   tmgr_trace_t lat_trace;
122   e_surf_link_state_t state_initial_link = SURF_LINK_ON;
123   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
124   tmgr_trace_t state_trace;
125
126   name_link = xbt_strdup(A_surfxml_link_id);
127   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
128   surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
129   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
130   surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
131
132   xbt_assert0((A_surfxml_link_state ==
133                A_surfxml_link_state_ON)
134               || (A_surfxml_link_state ==
135                   A_surfxml_link_state_OFF), "Invalid state");
136   if (A_surfxml_link_state == A_surfxml_link_state_ON)
137     state_initial_link = SURF_LINK_ON;
138   else if (A_surfxml_link_state ==
139            A_surfxml_link_state_OFF)
140     state_initial_link = SURF_LINK_OFF;
141
142   if (A_surfxml_link_sharing_policy ==
143       A_surfxml_link_sharing_policy_SHARED)
144     policy_initial_link = SURF_LINK_SHARED;
145   else if (A_surfxml_link_sharing_policy ==
146            A_surfxml_link_sharing_policy_FATPIPE)
147     policy_initial_link = SURF_LINK_FATPIPE;
148
149   surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
150
151   link_new(name_link, bw_initial, bw_trace,
152                    lat_initial, lat_trace, state_initial_link, state_trace,
153                    policy_initial_link, xbt_dict_new());
154
155 }
156
157 static int src_id = -1;
158 static int dst_id = -1;
159
160 static void parse_route_set_endpoints(void)
161 {
162   src_id = network_card_new(A_surfxml_route_src);
163   dst_id = network_card_new(A_surfxml_route_dst);
164   route_action = A_surfxml_route_action;
165   route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
166 }
167
168 static void parse_route_set_route(void)
169 {
170   char *name;
171   if (src_id != -1 && dst_id != -1) {
172     name = bprintf("%x#%x",src_id, dst_id);
173     manage_route(route_table, name, route_action, 0);
174     free(name);    
175   }
176 }
177
178 static void add_loopback(void)
179 {
180   int i;
181   /* Adding loopback if needed */
182   for (i = 0; i < host_number; i++)
183     if (!ROUTE_SIZE(i, i)) {
184       if (!loopback)
185         loopback = link_new(xbt_strdup("__MSG_loopback__"),
186                                     498000000, NULL, 0.000015, NULL,
187                                     SURF_LINK_ON, NULL,
188                                     SURF_LINK_FATPIPE,NULL);
189       ROUTE_SIZE(i, i) = 1;
190       ROUTE(i, i) = xbt_new0(link_CM02_t, 1);
191       ROUTE(i, i)[0] = loopback;
192     }
193 }
194
195 static void add_route(void)
196 {
197   xbt_ex_t e;
198   int nb_link = 0;
199   unsigned int cpt = 0;    
200   int link_list_capacity = 0;
201   link_CM02_t *link_list = NULL;
202   xbt_dict_cursor_t cursor = NULL;
203   char *key,*data, *end;
204   const char *sep = "#";
205   xbt_dynar_t links, keys;
206
207   if (routing_table == NULL) create_routing_table();
208
209   xbt_dict_foreach(route_table, cursor, key, data) {
210           char* link = NULL;
211     nb_link = 0;
212     links = (xbt_dynar_t)data;
213     keys = xbt_str_split_str(key, sep);
214
215     link_list_capacity = xbt_dynar_length(links);
216     link_list = xbt_new(link_CM02_t, link_list_capacity);
217
218     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
219     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
220  
221     xbt_dynar_foreach (links, cpt, link) {
222       TRY {
223         link_list[nb_link++] = xbt_dict_get(link_set, link);
224       }
225       CATCH(e) {
226         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
227       }     
228     }
229     route_new(src_id, dst_id, link_list, nb_link);
230    }
231
232    xbt_dict_free(&route_table);
233
234 }
235
236 static void count_hosts(void)
237 {
238    host_number++;
239 }
240
241
242 static void add_traces(void) {
243    xbt_dict_cursor_t cursor=NULL;
244    char *trace_name,*elm;
245    
246    static int called = 0;
247    if (called) return;
248    called = 1;
249
250    /* connect all traces relative to network */
251    xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
252       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
253       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
254       
255       xbt_assert1(link, "Link %s undefined", elm);
256       xbt_assert1(trace, "Trace %s undefined", trace_name);
257       
258       link->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
259    }
260
261    xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
262       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
263       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
264       
265       xbt_assert1(link, "Link %s undefined", elm);
266       xbt_assert1(trace, "Trace %s undefined", trace_name);
267       
268       link->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
269    }
270    
271    xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
272       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
273       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
274       
275       xbt_assert1(link, "Link %s undefined", elm);
276       xbt_assert1(trace, "Trace %s undefined", trace_name);
277       
278       link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
279    }
280
281    xbt_dict_free(&trace_connect_list_host_avail);
282    xbt_dict_free(&trace_connect_list_power);
283    xbt_dict_free(&trace_connect_list_link_avail);
284    xbt_dict_free(&trace_connect_list_bandwidth);
285    xbt_dict_free(&trace_connect_list_latency);
286    
287    xbt_dict_free(&traces_set_list); 
288 }
289
290 static void define_callbacks(const char *file)
291 {
292   /* Figuring out the network links */
293   surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts);
294   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
295   surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
296   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
297   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
298   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
299   surfxml_add_callback(STag_surfxml_platform_cb_list, &init_data);
300   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces);
301   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
302   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_loopback);
303   surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
304   surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
305   surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
306   surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
307   surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
308   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
309   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
310   surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
311 }
312
313 static void *name_service(const char *name)
314 {
315   network_card_CM02_t card = xbt_dict_get_or_null(network_card_set, name);
316   return card;
317 }
318
319 static const char *get_resource_name(void *resource_id)
320 {
321   return ((network_card_CM02_t) resource_id)->name;
322 }
323
324 static int resource_used(void *resource_id)
325 {
326   return lmm_constraint_used(network_maxmin_system,
327                              ((link_CM02_t) resource_id)->
328                              constraint);
329 }
330
331 static int action_free(surf_action_t action)
332 {
333   action->using--;
334   if (!action->using) {
335     xbt_swag_remove(action, action->state_set);
336     if (((surf_action_network_CM02_t) action)->variable)
337       lmm_variable_free(network_maxmin_system,
338                         ((surf_action_network_CM02_t) action)->variable);
339     free(action);
340     return 1;
341   }
342   return 0;
343 }
344
345 static void action_use(surf_action_t action)
346 {
347   action->using++;
348 }
349
350 static void action_cancel(surf_action_t action)
351 {
352   return;
353 }
354
355 static void action_recycle(surf_action_t action)
356 {
357   return;
358 }
359
360 static void action_change_state(surf_action_t action,
361                                 e_surf_action_state_t state)
362 {
363 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
364 /*     if(((surf_action_network_CM02_t)action)->variable) { */
365 /*       lmm_variable_disable(network_maxmin_system, ((surf_action_network_CM02_t)action)->variable); */
366 /*       ((surf_action_network_CM02_t)action)->variable = NULL; */
367 /*     } */
368
369   surf_action_change_state(action, state);
370   return;
371 }
372
373 static double share_resources(double now)
374 {
375   s_surf_action_network_CM02_t s_action;
376   surf_action_network_CM02_t action = NULL;
377   xbt_swag_t running_actions =
378       surf_network_model->common_public->states.running_action_set;
379   double min;
380
381   min = generic_maxmin_share_resources(running_actions,
382                                        xbt_swag_offset(s_action,
383                                                        variable),
384                                        network_maxmin_system,
385                                        network_solve);
386
387   xbt_swag_foreach(action, running_actions) {
388     if (action->latency > 0) {
389       if (min < 0)
390         min = action->latency;
391       else if (action->latency < min)
392         min = action->latency;
393     }
394   }
395
396   return min;
397 }
398
399 static void update_actions_state(double now, double delta)
400 {
401   double deltap = 0.0;
402   surf_action_network_CM02_t action = NULL;
403   surf_action_network_CM02_t next_action = NULL;
404   xbt_swag_t running_actions =
405       surf_network_model->common_public->states.running_action_set;
406   /*
407      xbt_swag_t failed_actions =
408      surf_network_model->common_public->states.failed_action_set;
409    */
410
411   xbt_swag_foreach_safe(action, next_action, running_actions) {
412     deltap = delta;
413     if (action->latency > 0) {
414       if (action->latency > deltap) {
415         double_update(&(action->latency), deltap);
416         deltap = 0.0;
417       } else {
418         double_update(&(deltap), action->latency);
419         action->latency = 0.0;
420       }
421       if ((action->latency == 0.0) && !(action->suspended))
422         lmm_update_variable_weight(network_maxmin_system, action->variable,
423                                    action->lat_current);
424     }
425     double_update(&(action->generic_action.remains),
426                   lmm_variable_getvalue(action->variable) * deltap);
427     if (action->generic_action.max_duration != NO_MAX_DURATION)
428       double_update(&(action->generic_action.max_duration), delta);
429
430     if ((action->generic_action.remains <= 0) &&
431         (lmm_get_variable_weight(action->variable) > 0)) {
432       action->generic_action.finish = surf_get_clock();
433       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
434     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
435                (action->generic_action.max_duration <= 0)) {
436       action->generic_action.finish = surf_get_clock();
437       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
438     }
439   }
440
441   return;
442 }
443
444 static void update_resource_state(void *id,
445                                   tmgr_trace_event_t event_type,
446                                   double value, double date)
447 {
448   link_CM02_t nw_link = id;
449   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
450   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
451   /*     value, event_type); */
452
453   if (event_type == nw_link->bw_event) {
454     nw_link->bw_current = value;
455     lmm_update_constraint_bound(network_maxmin_system, nw_link->constraint,
456                                 nw_link->bw_current);
457   } else if (event_type == nw_link->lat_event) {
458     double delta = value - nw_link->lat_current;
459     lmm_variable_t var = NULL;
460     lmm_element_t elem = NULL;
461     surf_action_network_CM02_t action = NULL;
462
463     nw_link->lat_current = value;
464     while ((var= lmm_get_var_from_cnst
465            (network_maxmin_system, nw_link->constraint, &elem))) {
466       action = lmm_variable_id(var);
467       action->lat_current += delta;
468       if (action->rate < 0)
469         lmm_update_variable_bound(network_maxmin_system, action->variable,
470                                   SG_TCP_CTE_GAMMA / (2.0 *
471                                                       action->
472                                                       lat_current));
473       else
474         lmm_update_variable_bound(network_maxmin_system, action->variable,
475                                   min(action->rate,
476                                       SG_TCP_CTE_GAMMA / (2.0 *
477                                                           action->
478                                                           lat_current)));
479       if (!(action->suspended))
480         lmm_update_variable_weight(network_maxmin_system, action->variable,
481                                    action->lat_current);
482       lmm_update_variable_latency(network_maxmin_system, action->variable,
483                                   delta);
484     }
485   } else if (event_type == nw_link->state_event) {
486     if (value > 0)
487       nw_link->state_current = SURF_LINK_ON;
488     else {
489       lmm_constraint_t cnst = nw_link->constraint;
490       lmm_variable_t var = NULL;
491       lmm_element_t elem = NULL;
492
493       nw_link->state_current = SURF_LINK_OFF;
494       while ((var= lmm_get_var_from_cnst
495               (network_maxmin_system, cnst, &elem))) {
496         surf_action_t action = lmm_variable_id(var) ;
497
498         if(surf_action_get_state(action)==SURF_ACTION_RUNNING ||
499            surf_action_get_state(action)==SURF_ACTION_READY) {
500           action->finish = date;
501           action_change_state( action, SURF_ACTION_FAILED);
502         }
503       }
504     }
505   } else {
506     CRITICAL0("Unknown event ! \n");
507     xbt_abort();
508   }
509
510   return;
511 }
512
513 static surf_action_t communicate(void *src, void *dst, double size,
514                                  double rate)
515 {
516   surf_action_network_CM02_t action = NULL;
517   /* LARGE PLATFORMS HACK:
518      Add a link_CM02_t *link and a int link_nb to network_card_CM02_t. It will represent local links for this node
519      Use the cluster_id for ->id */
520   network_card_CM02_t card_src = src;
521   network_card_CM02_t card_dst = dst;
522   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
523   link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
524   /* LARGE PLATFORMS HACK:
525      total_route_size = route_size + src->link_nb + dst->nb */
526   int i;
527
528   XBT_IN4("(%s,%s,%g,%g)", card_src->name, card_dst->name, size, rate);
529   /* LARGE PLATFORMS HACK:
530      assert on total_route_size */
531   xbt_assert2(route_size,
532               "You're trying to send data from %s to %s but there is no connexion between these two cards.",
533               card_src->name, card_dst->name);
534
535   action = xbt_new0(s_surf_action_network_CM02_t, 1);
536
537   action->generic_action.using = 1;
538   action->generic_action.cost = size;
539   action->generic_action.remains = size;
540   action->generic_action.max_duration = NO_MAX_DURATION;
541   action->generic_action.start = surf_get_clock();
542   action->generic_action.finish = -1.0;
543   action->generic_action.model_type =
544       (surf_model_t) surf_network_model;
545   action->suspended = 0;        /* Should be useless because of the 
546                                    calloc but it seems to help valgrind... */
547   action->generic_action.state_set =
548       surf_network_model->common_public->states.running_action_set;
549   for (i = 0; i < route_size; i++) 
550     if(route[i]->state_current == SURF_LINK_OFF) {
551       action->generic_action.state_set =
552         surf_network_model->common_public->states.failed_action_set;
553       break;
554     }
555   
556   xbt_swag_insert(action, action->generic_action.state_set);
557   action->rate = rate;
558
559   action->latency = 0.0;
560   for (i = 0; i < route_size; i++)
561     action->latency += route[i]->lat_current;
562   /* LARGE PLATFORMS HACK:
563      Add src->link and dst->link latencies */
564   action->lat_current = action->latency;
565
566   /* LARGE PLATFORMS HACK:
567      lmm_variable_new(..., total_route_size)*/
568   if (action->latency > 0)
569     action->variable =
570         lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
571                          route_size);
572   else
573     action->variable =
574         lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
575                          route_size);
576
577   if (action->rate < 0) {
578     if (action->lat_current > 0)
579       lmm_update_variable_bound(network_maxmin_system, action->variable,
580                                 SG_TCP_CTE_GAMMA / (2.0 *
581                                                     action->lat_current));
582     else
583       lmm_update_variable_bound(network_maxmin_system, action->variable,
584                                 -1.0);
585   } else {
586     if (action->lat_current > 0)
587       lmm_update_variable_bound(network_maxmin_system, action->variable,
588                                 min(action->rate,
589                                     SG_TCP_CTE_GAMMA / (2.0 *
590                                                         action->
591                                                         lat_current)));
592     else
593       lmm_update_variable_bound(network_maxmin_system, action->variable,
594                                 action->rate);
595   }
596   lmm_update_variable_latency(network_maxmin_system, action->variable,
597                               action->latency);
598
599   for (i = 0; i < route_size; i++)
600     lmm_expand(network_maxmin_system, route[i]->constraint,
601                action->variable, 1.0);
602   /* LARGE PLATFORMS HACK:
603      expand also with src->link and dst->link */
604
605   XBT_OUT;
606
607   return (surf_action_t) action;
608 }
609
610 /* returns an array of link_CM02_t */
611 static const void **get_route(void *src, void *dst)
612 {
613   network_card_CM02_t card_src = src;
614   network_card_CM02_t card_dst = dst;
615   return (const void **) ROUTE(card_src->id, card_dst->id);
616 }
617
618 static int get_route_size(void *src, void *dst)
619 {
620   network_card_CM02_t card_src = src;
621   network_card_CM02_t card_dst = dst;
622   return ROUTE_SIZE(card_src->id, card_dst->id);
623 }
624
625 static const char *get_link_name(const void *link)
626 {
627   return ((link_CM02_t) link)->name;
628 }
629
630 static double get_link_bandwidth(const void *link)
631 {
632   return ((link_CM02_t) link)->bw_current;
633 }
634
635 static double get_link_latency(const void *link)
636 {
637   return ((link_CM02_t) link)->lat_current;
638 }
639
640 static xbt_dict_t get_properties(void *link)
641 {
642  return ((link_CM02_t) link)->properties;
643 }
644
645 static void action_suspend(surf_action_t action)
646 {
647   ((surf_action_network_CM02_t) action)->suspended = 1;
648   lmm_update_variable_weight(network_maxmin_system,
649                              ((surf_action_network_CM02_t) action)->
650                              variable, 0.0);
651 }
652
653 static void action_resume(surf_action_t action)
654 {
655   if (((surf_action_network_CM02_t) action)->suspended) {
656     lmm_update_variable_weight(network_maxmin_system,
657                                ((surf_action_network_CM02_t) action)->
658                                variable,
659                                ((surf_action_network_CM02_t) action)->
660                                lat_current);
661     ((surf_action_network_CM02_t) action)->suspended = 0;
662   }
663 }
664
665 static int action_is_suspended(surf_action_t action)
666 {
667   return ((surf_action_network_CM02_t) action)->suspended;
668 }
669
670 static void action_set_max_duration(surf_action_t action, double duration)
671 {
672   action->max_duration = duration;
673 }
674
675 static void finalize(void)
676 {
677   int i, j;
678
679   xbt_dict_free(&network_card_set);
680   xbt_dict_free(&link_set);
681   xbt_swag_free(surf_network_model->common_public->states.
682                 ready_action_set);
683   xbt_swag_free(surf_network_model->common_public->states.
684                 running_action_set);
685   xbt_swag_free(surf_network_model->common_public->states.
686                 failed_action_set);
687   xbt_swag_free(surf_network_model->common_public->states.
688                 done_action_set);
689   free(surf_network_model->common_public);
690   free(surf_network_model->common_private);
691   free(surf_network_model->extension_public);
692
693   free(surf_network_model);
694   surf_network_model = NULL;
695
696   loopback = NULL;
697   for (i = 0; i < card_number; i++)
698     for (j = 0; j < card_number; j++)
699       free(ROUTE(i, j));
700   free(routing_table);
701   routing_table = NULL;
702   free(routing_table_size);
703   routing_table_size = NULL;
704   card_number = 0;
705 }
706
707 static void surf_network_model_init_internal(void)
708 {
709   s_surf_action_t action;
710
711   surf_network_model = xbt_new0(s_surf_network_model_t, 1);
712
713   surf_network_model->common_private =
714       xbt_new0(s_surf_model_private_t, 1);
715   surf_network_model->common_public =
716       xbt_new0(s_surf_model_public_t, 1);
717   surf_network_model->extension_public =
718       xbt_new0(s_surf_network_model_extension_public_t, 1);
719
720   surf_network_model->common_public->states.ready_action_set =
721       xbt_swag_new(xbt_swag_offset(action, state_hookup));
722   surf_network_model->common_public->states.running_action_set =
723       xbt_swag_new(xbt_swag_offset(action, state_hookup));
724   surf_network_model->common_public->states.failed_action_set =
725       xbt_swag_new(xbt_swag_offset(action, state_hookup));
726   surf_network_model->common_public->states.done_action_set =
727       xbt_swag_new(xbt_swag_offset(action, state_hookup));
728
729   surf_network_model->common_public->name_service = name_service;
730   surf_network_model->common_public->get_resource_name =
731       get_resource_name;
732   surf_network_model->common_public->action_get_state =
733       surf_action_get_state;
734   surf_network_model->common_public->action_get_start_time =
735       surf_action_get_start_time;
736   surf_network_model->common_public->action_get_finish_time =
737       surf_action_get_finish_time;
738   surf_network_model->common_public->action_free = action_free;
739   surf_network_model->common_public->action_use = action_use;
740   surf_network_model->common_public->action_cancel = action_cancel;
741   surf_network_model->common_public->action_recycle = action_recycle;
742   surf_network_model->common_public->action_change_state =
743       action_change_state;
744   surf_network_model->common_public->action_set_data =
745       surf_action_set_data;
746   surf_network_model->common_public->name = "network";
747
748   surf_network_model->common_private->resource_used = resource_used;
749   surf_network_model->common_private->share_resources = share_resources;
750   surf_network_model->common_private->update_actions_state =
751       update_actions_state;
752   surf_network_model->common_private->update_resource_state =
753       update_resource_state;
754   surf_network_model->common_private->finalize = finalize;
755
756   surf_network_model->common_public->suspend = action_suspend;
757   surf_network_model->common_public->resume = action_resume;
758   surf_network_model->common_public->is_suspended = action_is_suspended;
759   surf_cpu_model->common_public->set_max_duration =
760       action_set_max_duration;
761
762   surf_network_model->extension_public->communicate = communicate;
763   surf_network_model->extension_public->get_route = get_route;
764   surf_network_model->extension_public->get_route_size = get_route_size;
765   surf_network_model->extension_public->get_link_name = get_link_name;
766   surf_network_model->extension_public->get_link_bandwidth =
767       get_link_bandwidth;
768   surf_network_model->extension_public->get_link_latency =
769       get_link_latency;
770
771   surf_network_model->common_public->get_properties =  get_properties;
772
773   link_set = xbt_dict_new();
774   network_card_set = xbt_dict_new();
775
776   if (!network_maxmin_system)
777     network_maxmin_system = lmm_system_new();
778 }
779
780 /***************************************************************************/
781 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
782 /***************************************************************************/
783 /* @TechReport{      rr-lip2002-40, */
784 /*   author        = {Henri Casanova and Loris Marchal}, */
785 /*   institution   = {LIP}, */
786 /*   title         = {A Network Model for Simulation of Grid Application}, */
787 /*   number        = {2002-40}, */
788 /*   month         = {oct}, */
789 /*   year          = {2002} */
790 /* } */
791 void surf_network_model_init_CM02(const char *filename)
792 {
793
794   if (surf_network_model)
795     return;
796   surf_network_model_init_internal();
797   define_callbacks(filename);
798   xbt_dynar_push(model_list, &surf_network_model);
799   network_solve = lmm_solve;
800
801   update_model_description(surf_network_model_description,
802                               surf_network_model_description_size,
803                               "CM02",
804                               (surf_model_t) surf_network_model);
805 }
806
807 void surf_network_model_init_Reno(const char *filename)
808 {
809   if (surf_network_model)
810     return;
811   surf_network_model_init_internal();
812   define_callbacks(filename);
813
814   xbt_dynar_push(model_list, &surf_network_model);
815   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
816                                     func_reno_fpi);
817   network_solve = lagrange_solve;
818
819   update_model_description(surf_network_model_description,
820                               surf_network_model_description_size,
821                               "Reno",
822                               (surf_model_t) surf_network_model);
823 }
824
825 void surf_network_model_init_Vegas(const char *filename)
826 {
827   if (surf_network_model)
828     return;
829   surf_network_model_init_internal();
830   define_callbacks(filename);
831
832   xbt_dynar_push(model_list, &surf_network_model);
833   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
834                                     func_vegas_fpi);
835   network_solve = lagrange_solve;
836
837   update_model_description(surf_network_model_description,
838                               surf_network_model_description_size,
839                               "Vegas",
840                               (surf_model_t) surf_network_model);
841 }
842
843 #ifdef HAVE_SDP
844 void surf_network_model_init_SDP(const char *filename)
845 {
846   if (surf_network_model)
847     return;
848   surf_network_model_init_internal();
849   define_callbacks(filename);
850
851   xbt_dynar_push(model_list, &surf_network_model);
852   network_solve = sdp_solve;
853
854   update_model_description(surf_network_model_description,
855                               surf_network_model_description_size,
856                               "SDP",
857                               (surf_model_t) surf_network_model);
858 }
859 #endif