Logo AND Algorithmique Numérique Distribuée

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