Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Corrected small bug in network model host_number is used instead of card_number.
[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 = host_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 *), &xbt_free_ref);
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     xbt_dynar_free(&keys);
221  
222     xbt_dynar_foreach (links, cpt, link) {
223       TRY {
224         link_list[nb_link++] = xbt_dict_get(link_set, link);
225       }
226       CATCH(e) {
227         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
228       }     
229     }
230     route_new(src_id, dst_id, link_list, nb_link);
231    }
232
233    xbt_dict_free(&route_table);
234
235 }
236
237 static void count_hosts(void)
238 {
239    host_number++;
240 }
241
242
243 static void add_traces(void) {
244    xbt_dict_cursor_t cursor=NULL;
245    char *trace_name,*elm;
246    
247    static int called = 0;
248    if (called) return;
249    called = 1;
250
251    /* connect all traces relative to network */
252    xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
253       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
254       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
255       
256       xbt_assert1(link, "Link %s undefined", elm);
257       xbt_assert1(trace, "Trace %s undefined", trace_name);
258       
259       link->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
260    }
261
262    xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
263       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
264       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
265       
266       xbt_assert1(link, "Link %s undefined", elm);
267       xbt_assert1(trace, "Trace %s undefined", trace_name);
268       
269       link->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
270    }
271    
272    xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
273       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
274       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
275       
276       xbt_assert1(link, "Link %s undefined", elm);
277       xbt_assert1(trace, "Trace %s undefined", trace_name);
278       
279       link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
280    }
281
282    xbt_dict_free(&trace_connect_list_host_avail);
283    xbt_dict_free(&trace_connect_list_power);
284    xbt_dict_free(&trace_connect_list_link_avail);
285    xbt_dict_free(&trace_connect_list_bandwidth);
286    xbt_dict_free(&trace_connect_list_latency);
287    
288    xbt_dict_free(&traces_set_list); 
289 }
290
291 static void define_callbacks(const char *file)
292 {
293   /* Figuring out the network links */
294   surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts);
295   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
296   surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
297   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
298   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
299   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
300   surfxml_add_callback(STag_surfxml_platform_cb_list, &init_data);
301   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces);
302   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
303   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_loopback);
304   surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
305   surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
306   surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
307   surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
308   surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
309   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
310   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
311   surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
312 }
313
314 static void *name_service(const char *name)
315 {
316   network_card_CM02_t card = xbt_dict_get_or_null(network_card_set, name);
317   return card;
318 }
319
320 static const char *get_resource_name(void *resource_id)
321 {
322   return ((network_card_CM02_t) resource_id)->name;
323 }
324
325 static int resource_used(void *resource_id)
326 {
327   return lmm_constraint_used(network_maxmin_system,
328                              ((link_CM02_t) resource_id)->
329                              constraint);
330 }
331
332 static int action_free(surf_action_t action)
333 {
334   action->using--;
335   if (!action->using) {
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     free(action);
341     return 1;
342   }
343   return 0;
344 }
345
346 static void action_use(surf_action_t action)
347 {
348   action->using++;
349 }
350
351 static void action_cancel(surf_action_t action)
352 {
353   return;
354 }
355
356 static void action_recycle(surf_action_t action)
357 {
358   return;
359 }
360
361 static void action_change_state(surf_action_t action,
362                                 e_surf_action_state_t state)
363 {
364 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
365 /*     if(((surf_action_network_CM02_t)action)->variable) { */
366 /*       lmm_variable_disable(network_maxmin_system, ((surf_action_network_CM02_t)action)->variable); */
367 /*       ((surf_action_network_CM02_t)action)->variable = NULL; */
368 /*     } */
369
370   surf_action_change_state(action, state);
371   return;
372 }
373
374 static double 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->common_public->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   xbt_swag_foreach(action, running_actions) {
389     if (action->latency > 0) {
390       if (min < 0)
391         min = action->latency;
392       else if (action->latency < min)
393         min = action->latency;
394     }
395   }
396
397   return min;
398 }
399
400 static void update_actions_state(double now, double delta)
401 {
402   double deltap = 0.0;
403   surf_action_network_CM02_t action = NULL;
404   surf_action_network_CM02_t next_action = NULL;
405   xbt_swag_t running_actions =
406       surf_network_model->common_public->states.running_action_set;
407   /*
408      xbt_swag_t failed_actions =
409      surf_network_model->common_public->states.failed_action_set;
410    */
411
412   xbt_swag_foreach_safe(action, next_action, running_actions) {
413     deltap = delta;
414     if (action->latency > 0) {
415       if (action->latency > deltap) {
416         double_update(&(action->latency), deltap);
417         deltap = 0.0;
418       } else {
419         double_update(&(deltap), action->latency);
420         action->latency = 0.0;
421       }
422       if ((action->latency == 0.0) && !(action->suspended))
423         lmm_update_variable_weight(network_maxmin_system, action->variable,
424                                    action->lat_current);
425     }
426     double_update(&(action->generic_action.remains),
427                   lmm_variable_getvalue(action->variable) * deltap);
428     if (action->generic_action.max_duration != NO_MAX_DURATION)
429       double_update(&(action->generic_action.max_duration), delta);
430
431     if ((action->generic_action.remains <= 0) &&
432         (lmm_get_variable_weight(action->variable) > 0)) {
433       action->generic_action.finish = surf_get_clock();
434       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
435     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
436                (action->generic_action.max_duration <= 0)) {
437       action->generic_action.finish = surf_get_clock();
438       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
439     }
440   }
441
442   return;
443 }
444
445 static void update_resource_state(void *id,
446                                   tmgr_trace_event_t event_type,
447                                   double value, double date)
448 {
449   link_CM02_t nw_link = id;
450   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
451   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
452   /*     value, event_type); */
453
454   if (event_type == nw_link->bw_event) {
455     nw_link->bw_current = value;
456     lmm_update_constraint_bound(network_maxmin_system, nw_link->constraint,
457                                 nw_link->bw_current);
458   } else if (event_type == nw_link->lat_event) {
459     double delta = value - nw_link->lat_current;
460     lmm_variable_t var = NULL;
461     lmm_element_t elem = NULL;
462     surf_action_network_CM02_t action = NULL;
463
464     nw_link->lat_current = value;
465     while ((var= lmm_get_var_from_cnst
466            (network_maxmin_system, nw_link->constraint, &elem))) {
467       action = lmm_variable_id(var);
468       action->lat_current += delta;
469       if (action->rate < 0)
470         lmm_update_variable_bound(network_maxmin_system, action->variable,
471                                   SG_TCP_CTE_GAMMA / (2.0 *
472                                                       action->
473                                                       lat_current));
474       else
475         lmm_update_variable_bound(network_maxmin_system, action->variable,
476                                   min(action->rate,
477                                       SG_TCP_CTE_GAMMA / (2.0 *
478                                                           action->
479                                                           lat_current)));
480       if (!(action->suspended))
481         lmm_update_variable_weight(network_maxmin_system, action->variable,
482                                    action->lat_current);
483       lmm_update_variable_latency(network_maxmin_system, action->variable,
484                                   delta);
485     }
486   } else if (event_type == nw_link->state_event) {
487     if (value > 0)
488       nw_link->state_current = SURF_LINK_ON;
489     else {
490       lmm_constraint_t cnst = nw_link->constraint;
491       lmm_variable_t var = NULL;
492       lmm_element_t elem = NULL;
493
494       nw_link->state_current = SURF_LINK_OFF;
495       while ((var= lmm_get_var_from_cnst
496               (network_maxmin_system, cnst, &elem))) {
497         surf_action_t action = lmm_variable_id(var) ;
498
499         if(surf_action_get_state(action)==SURF_ACTION_RUNNING ||
500            surf_action_get_state(action)==SURF_ACTION_READY) {
501           action->finish = date;
502           action_change_state( action, SURF_ACTION_FAILED);
503         }
504       }
505     }
506   } else {
507     CRITICAL0("Unknown event ! \n");
508     xbt_abort();
509   }
510
511   return;
512 }
513
514 static surf_action_t communicate(void *src, void *dst, double size,
515                                  double rate)
516 {
517   surf_action_network_CM02_t action = NULL;
518   /* LARGE PLATFORMS HACK:
519      Add a link_CM02_t *link and a int link_nb to network_card_CM02_t. It will represent local links for this node
520      Use the cluster_id for ->id */
521   network_card_CM02_t card_src = src;
522   network_card_CM02_t card_dst = dst;
523   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
524   link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
525   /* LARGE PLATFORMS HACK:
526      total_route_size = route_size + src->link_nb + dst->nb */
527   int i;
528
529   XBT_IN4("(%s,%s,%g,%g)", card_src->name, card_dst->name, size, rate);
530   /* LARGE PLATFORMS HACK:
531      assert on total_route_size */
532   xbt_assert2(route_size,
533               "You're trying to send data from %s to %s but there is no connexion between these two cards.",
534               card_src->name, card_dst->name);
535
536   action = xbt_new0(s_surf_action_network_CM02_t, 1);
537
538   action->generic_action.using = 1;
539   action->generic_action.cost = size;
540   action->generic_action.remains = size;
541   action->generic_action.max_duration = NO_MAX_DURATION;
542   action->generic_action.start = surf_get_clock();
543   action->generic_action.finish = -1.0;
544   action->generic_action.model_type =
545       (surf_model_t) surf_network_model;
546   action->suspended = 0;        /* Should be useless because of the 
547                                    calloc but it seems to help valgrind... */
548   action->generic_action.state_set =
549       surf_network_model->common_public->states.running_action_set;
550   for (i = 0; i < route_size; i++) 
551     if(route[i]->state_current == SURF_LINK_OFF) {
552       action->generic_action.state_set =
553         surf_network_model->common_public->states.failed_action_set;
554       break;
555     }
556   
557   xbt_swag_insert(action, action->generic_action.state_set);
558   action->rate = rate;
559
560   action->latency = 0.0;
561   for (i = 0; i < route_size; i++)
562     action->latency += route[i]->lat_current;
563   /* LARGE PLATFORMS HACK:
564      Add src->link and dst->link latencies */
565   action->lat_current = action->latency;
566
567   /* LARGE PLATFORMS HACK:
568      lmm_variable_new(..., total_route_size)*/
569   if (action->latency > 0)
570     action->variable =
571         lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
572                          route_size);
573   else
574     action->variable =
575         lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
576                          route_size);
577
578   if (action->rate < 0) {
579     if (action->lat_current > 0)
580       lmm_update_variable_bound(network_maxmin_system, action->variable,
581                                 SG_TCP_CTE_GAMMA / (2.0 *
582                                                     action->lat_current));
583     else
584       lmm_update_variable_bound(network_maxmin_system, action->variable,
585                                 -1.0);
586   } else {
587     if (action->lat_current > 0)
588       lmm_update_variable_bound(network_maxmin_system, action->variable,
589                                 min(action->rate,
590                                     SG_TCP_CTE_GAMMA / (2.0 *
591                                                         action->
592                                                         lat_current)));
593     else
594       lmm_update_variable_bound(network_maxmin_system, action->variable,
595                                 action->rate);
596   }
597   lmm_update_variable_latency(network_maxmin_system, action->variable,
598                               action->latency);
599
600   for (i = 0; i < route_size; i++)
601     lmm_expand(network_maxmin_system, route[i]->constraint,
602                action->variable, 1.0);
603   /* LARGE PLATFORMS HACK:
604      expand also with src->link and dst->link */
605
606   XBT_OUT;
607
608   return (surf_action_t) action;
609 }
610
611 /* returns an array of link_CM02_t */
612 static const void **get_route(void *src, void *dst)
613 {
614   network_card_CM02_t card_src = src;
615   network_card_CM02_t card_dst = dst;
616   return (const void **) ROUTE(card_src->id, card_dst->id);
617 }
618
619 static int get_route_size(void *src, void *dst)
620 {
621   network_card_CM02_t card_src = src;
622   network_card_CM02_t card_dst = dst;
623   return ROUTE_SIZE(card_src->id, card_dst->id);
624 }
625
626 static const char *get_link_name(const void *link)
627 {
628   return ((link_CM02_t) link)->name;
629 }
630
631 static double get_link_bandwidth(const void *link)
632 {
633   return ((link_CM02_t) link)->bw_current;
634 }
635
636 static double get_link_latency(const void *link)
637 {
638   return ((link_CM02_t) link)->lat_current;
639 }
640
641 static xbt_dict_t get_properties(void *link)
642 {
643  return ((link_CM02_t) link)->properties;
644 }
645
646 static void action_suspend(surf_action_t action)
647 {
648   ((surf_action_network_CM02_t) action)->suspended = 1;
649   lmm_update_variable_weight(network_maxmin_system,
650                              ((surf_action_network_CM02_t) action)->
651                              variable, 0.0);
652 }
653
654 static void action_resume(surf_action_t action)
655 {
656   if (((surf_action_network_CM02_t) action)->suspended) {
657     lmm_update_variable_weight(network_maxmin_system,
658                                ((surf_action_network_CM02_t) action)->
659                                variable,
660                                ((surf_action_network_CM02_t) action)->
661                                lat_current);
662     ((surf_action_network_CM02_t) action)->suspended = 0;
663   }
664 }
665
666 static int action_is_suspended(surf_action_t action)
667 {
668   return ((surf_action_network_CM02_t) action)->suspended;
669 }
670
671 static void action_set_max_duration(surf_action_t action, double duration)
672 {
673   action->max_duration = duration;
674 }
675
676 static void finalize(void)
677 {
678   int i, j;
679
680   xbt_dict_free(&network_card_set);
681   xbt_dict_free(&link_set);
682   xbt_swag_free(surf_network_model->common_public->states.
683                 ready_action_set);
684   xbt_swag_free(surf_network_model->common_public->states.
685                 running_action_set);
686   xbt_swag_free(surf_network_model->common_public->states.
687                 failed_action_set);
688   xbt_swag_free(surf_network_model->common_public->states.
689                 done_action_set);
690   free(surf_network_model->common_public);
691   free(surf_network_model->common_private);
692   free(surf_network_model->extension_public);
693
694   free(surf_network_model);
695   surf_network_model = NULL;
696
697   loopback = NULL;
698   for (i = 0; i < host_number; i++)
699     for (j = 0; j < host_number; j++)
700       free(ROUTE(i, j));
701   free(routing_table);
702   routing_table = NULL;
703   free(routing_table_size);
704   routing_table_size = NULL;
705   host_number = 0;
706 }
707
708 static void surf_network_model_init_internal(void)
709 {
710   s_surf_action_t action;
711
712   surf_network_model = xbt_new0(s_surf_network_model_t, 1);
713
714   surf_network_model->common_private =
715       xbt_new0(s_surf_model_private_t, 1);
716   surf_network_model->common_public =
717       xbt_new0(s_surf_model_public_t, 1);
718   surf_network_model->extension_public =
719       xbt_new0(s_surf_network_model_extension_public_t, 1);
720
721   surf_network_model->common_public->states.ready_action_set =
722       xbt_swag_new(xbt_swag_offset(action, state_hookup));
723   surf_network_model->common_public->states.running_action_set =
724       xbt_swag_new(xbt_swag_offset(action, state_hookup));
725   surf_network_model->common_public->states.failed_action_set =
726       xbt_swag_new(xbt_swag_offset(action, state_hookup));
727   surf_network_model->common_public->states.done_action_set =
728       xbt_swag_new(xbt_swag_offset(action, state_hookup));
729
730   surf_network_model->common_public->name_service = name_service;
731   surf_network_model->common_public->get_resource_name =
732       get_resource_name;
733   surf_network_model->common_public->action_get_state =
734       surf_action_get_state;
735   surf_network_model->common_public->action_get_start_time =
736       surf_action_get_start_time;
737   surf_network_model->common_public->action_get_finish_time =
738       surf_action_get_finish_time;
739   surf_network_model->common_public->action_free = action_free;
740   surf_network_model->common_public->action_use = action_use;
741   surf_network_model->common_public->action_cancel = action_cancel;
742   surf_network_model->common_public->action_recycle = action_recycle;
743   surf_network_model->common_public->action_change_state =
744       action_change_state;
745   surf_network_model->common_public->action_set_data =
746       surf_action_set_data;
747   surf_network_model->common_public->name = "network";
748
749   surf_network_model->common_private->resource_used = resource_used;
750   surf_network_model->common_private->share_resources = share_resources;
751   surf_network_model->common_private->update_actions_state =
752       update_actions_state;
753   surf_network_model->common_private->update_resource_state =
754       update_resource_state;
755   surf_network_model->common_private->finalize = finalize;
756
757   surf_network_model->common_public->suspend = action_suspend;
758   surf_network_model->common_public->resume = action_resume;
759   surf_network_model->common_public->is_suspended = action_is_suspended;
760   surf_cpu_model->common_public->set_max_duration =
761       action_set_max_duration;
762
763   surf_network_model->extension_public->communicate = communicate;
764   surf_network_model->extension_public->get_route = get_route;
765   surf_network_model->extension_public->get_route_size = get_route_size;
766   surf_network_model->extension_public->get_link_name = get_link_name;
767   surf_network_model->extension_public->get_link_bandwidth =
768       get_link_bandwidth;
769   surf_network_model->extension_public->get_link_latency =
770       get_link_latency;
771
772   surf_network_model->common_public->get_properties =  get_properties;
773
774   link_set = xbt_dict_new();
775   network_card_set = xbt_dict_new();
776
777   if (!network_maxmin_system)
778     network_maxmin_system = lmm_system_new();
779 }
780
781 /***************************************************************************/
782 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
783 /***************************************************************************/
784 /* @TechReport{      rr-lip2002-40, */
785 /*   author        = {Henri Casanova and Loris Marchal}, */
786 /*   institution   = {LIP}, */
787 /*   title         = {A Network Model for Simulation of Grid Application}, */
788 /*   number        = {2002-40}, */
789 /*   month         = {oct}, */
790 /*   year          = {2002} */
791 /* } */
792 void surf_network_model_init_CM02(const char *filename)
793 {
794
795   if (surf_network_model)
796     return;
797   surf_network_model_init_internal();
798   define_callbacks(filename);
799   xbt_dynar_push(model_list, &surf_network_model);
800   network_solve = lmm_solve;
801
802   update_model_description(surf_network_model_description,
803                               surf_network_model_description_size,
804                               "CM02",
805                               (surf_model_t) surf_network_model);
806 }
807
808 void surf_network_model_init_Reno(const char *filename)
809 {
810   if (surf_network_model)
811     return;
812   surf_network_model_init_internal();
813   define_callbacks(filename);
814
815   xbt_dynar_push(model_list, &surf_network_model);
816   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
817                                     func_reno_fpi);
818   network_solve = lagrange_solve;
819
820   update_model_description(surf_network_model_description,
821                               surf_network_model_description_size,
822                               "Reno",
823                               (surf_model_t) surf_network_model);
824 }
825
826 void surf_network_model_init_Vegas(const char *filename)
827 {
828   if (surf_network_model)
829     return;
830   surf_network_model_init_internal();
831   define_callbacks(filename);
832
833   xbt_dynar_push(model_list, &surf_network_model);
834   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
835                                     func_vegas_fpi);
836   network_solve = lagrange_solve;
837
838   update_model_description(surf_network_model_description,
839                               surf_network_model_description_size,
840                               "Vegas",
841                               (surf_model_t) surf_network_model);
842 }
843
844 #ifdef HAVE_SDP
845 void surf_network_model_init_SDP(const char *filename)
846 {
847   if (surf_network_model)
848     return;
849   surf_network_model_init_internal();
850   define_callbacks(filename);
851
852   xbt_dynar_push(model_list, &surf_network_model);
853   network_solve = sdp_solve;
854
855   update_model_description(surf_network_model_description,
856                               surf_network_model_description_size,
857                               "SDP",
858                               (surf_model_t) surf_network_model);
859 }
860 #endif