Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Get ride of a supurious compiler warning
[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     } else {                    /* Need to check that none of the model has failed */
439       lmm_constraint_t cnst = NULL;
440       int i = 0;
441       link_CM02_t nw_link = NULL;
442
443       while ((cnst =
444               lmm_get_cnst_from_var(network_maxmin_system,
445                                     action->variable, i++))) {
446         nw_link = lmm_constraint_id(cnst);
447         if (nw_link->state_current == SURF_LINK_OFF) {
448           action->generic_action.finish = surf_get_clock();
449           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
450           break;
451         }
452       }
453     }
454   }
455
456   return;
457 }
458
459 static void update_resource_state(void *id,
460                                   tmgr_trace_event_t event_type,
461                                   double value, double date)
462 {
463   link_CM02_t nw_link = id;
464   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
465   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
466   /*     value, event_type); */
467
468   if (event_type == nw_link->bw_event) {
469     nw_link->bw_current = value;
470     lmm_update_constraint_bound(network_maxmin_system, nw_link->constraint,
471                                 nw_link->bw_current);
472   } else if (event_type == nw_link->lat_event) {
473     double delta = value - nw_link->lat_current;
474     lmm_variable_t var = NULL;
475     lmm_element_t elem = NULL;
476     surf_action_network_CM02_t action = NULL;
477
478     nw_link->lat_current = value;
479     while ((var= lmm_get_var_from_cnst
480            (network_maxmin_system, nw_link->constraint, &elem))) {
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   /* 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
550   xbt_swag_insert(action, action->generic_action.state_set);
551   action->rate = rate;
552
553   action->latency = 0.0;
554   for (i = 0; i < route_size; i++)
555     action->latency += route[i]->lat_current;
556   /* LARGE PLATFORMS HACK:
557      Add src->link and dst->link latencies */
558   action->lat_current = action->latency;
559
560   /* LARGE PLATFORMS HACK:
561      lmm_variable_new(..., total_route_size)*/
562   if (action->latency > 0)
563     action->variable =
564         lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
565                          route_size);
566   else
567     action->variable =
568         lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
569                          route_size);
570
571   if (action->rate < 0) {
572     if (action->lat_current > 0)
573       lmm_update_variable_bound(network_maxmin_system, action->variable,
574                                 SG_TCP_CTE_GAMMA / (2.0 *
575                                                     action->lat_current));
576     else
577       lmm_update_variable_bound(network_maxmin_system, action->variable,
578                                 -1.0);
579   } else {
580     if (action->lat_current > 0)
581       lmm_update_variable_bound(network_maxmin_system, action->variable,
582                                 min(action->rate,
583                                     SG_TCP_CTE_GAMMA / (2.0 *
584                                                         action->
585                                                         lat_current)));
586     else
587       lmm_update_variable_bound(network_maxmin_system, action->variable,
588                                 action->rate);
589   }
590   lmm_update_variable_latency(network_maxmin_system, action->variable,
591                               action->latency);
592
593   for (i = 0; i < route_size; i++)
594     lmm_expand(network_maxmin_system, route[i]->constraint,
595                action->variable, 1.0);
596   /* LARGE PLATFORMS HACK:
597      expand also with src->link and dst->link */
598
599   XBT_OUT;
600
601   return (surf_action_t) action;
602 }
603
604 /* returns an array of link_CM02_t */
605 static const void **get_route(void *src, void *dst)
606 {
607   network_card_CM02_t card_src = src;
608   network_card_CM02_t card_dst = dst;
609   return (const void **) ROUTE(card_src->id, card_dst->id);
610 }
611
612 static int get_route_size(void *src, void *dst)
613 {
614   network_card_CM02_t card_src = src;
615   network_card_CM02_t card_dst = dst;
616   return ROUTE_SIZE(card_src->id, card_dst->id);
617 }
618
619 static const char *get_link_name(const void *link)
620 {
621   return ((link_CM02_t) link)->name;
622 }
623
624 static double get_link_bandwidth(const void *link)
625 {
626   return ((link_CM02_t) link)->bw_current;
627 }
628
629 static double get_link_latency(const void *link)
630 {
631   return ((link_CM02_t) link)->lat_current;
632 }
633
634 static xbt_dict_t get_properties(void *link)
635 {
636  return ((link_CM02_t) link)->properties;
637 }
638
639 static void action_suspend(surf_action_t action)
640 {
641   ((surf_action_network_CM02_t) action)->suspended = 1;
642   lmm_update_variable_weight(network_maxmin_system,
643                              ((surf_action_network_CM02_t) action)->
644                              variable, 0.0);
645 }
646
647 static void action_resume(surf_action_t action)
648 {
649   if (((surf_action_network_CM02_t) action)->suspended) {
650     lmm_update_variable_weight(network_maxmin_system,
651                                ((surf_action_network_CM02_t) action)->
652                                variable,
653                                ((surf_action_network_CM02_t) action)->
654                                lat_current);
655     ((surf_action_network_CM02_t) action)->suspended = 0;
656   }
657 }
658
659 static int action_is_suspended(surf_action_t action)
660 {
661   return ((surf_action_network_CM02_t) action)->suspended;
662 }
663
664 static void action_set_max_duration(surf_action_t action, double duration)
665 {
666   action->max_duration = duration;
667 }
668
669 static void finalize(void)
670 {
671   int i, j;
672
673   xbt_dict_free(&network_card_set);
674   xbt_dict_free(&link_set);
675   xbt_swag_free(surf_network_model->common_public->states.
676                 ready_action_set);
677   xbt_swag_free(surf_network_model->common_public->states.
678                 running_action_set);
679   xbt_swag_free(surf_network_model->common_public->states.
680                 failed_action_set);
681   xbt_swag_free(surf_network_model->common_public->states.
682                 done_action_set);
683   free(surf_network_model->common_public);
684   free(surf_network_model->common_private);
685   free(surf_network_model->extension_public);
686
687   free(surf_network_model);
688   surf_network_model = NULL;
689
690   loopback = NULL;
691   for (i = 0; i < card_number; i++)
692     for (j = 0; j < card_number; j++)
693       free(ROUTE(i, j));
694   free(routing_table);
695   routing_table = NULL;
696   free(routing_table_size);
697   routing_table_size = NULL;
698   card_number = 0;
699 }
700
701 static void surf_network_model_init_internal(void)
702 {
703   s_surf_action_t action;
704
705   surf_network_model = xbt_new0(s_surf_network_model_t, 1);
706
707   surf_network_model->common_private =
708       xbt_new0(s_surf_model_private_t, 1);
709   surf_network_model->common_public =
710       xbt_new0(s_surf_model_public_t, 1);
711   surf_network_model->extension_public =
712       xbt_new0(s_surf_network_model_extension_public_t, 1);
713
714   surf_network_model->common_public->states.ready_action_set =
715       xbt_swag_new(xbt_swag_offset(action, state_hookup));
716   surf_network_model->common_public->states.running_action_set =
717       xbt_swag_new(xbt_swag_offset(action, state_hookup));
718   surf_network_model->common_public->states.failed_action_set =
719       xbt_swag_new(xbt_swag_offset(action, state_hookup));
720   surf_network_model->common_public->states.done_action_set =
721       xbt_swag_new(xbt_swag_offset(action, state_hookup));
722
723   surf_network_model->common_public->name_service = name_service;
724   surf_network_model->common_public->get_resource_name =
725       get_resource_name;
726   surf_network_model->common_public->action_get_state =
727       surf_action_get_state;
728   surf_network_model->common_public->action_get_start_time =
729       surf_action_get_start_time;
730   surf_network_model->common_public->action_get_finish_time =
731       surf_action_get_finish_time;
732   surf_network_model->common_public->action_free = action_free;
733   surf_network_model->common_public->action_use = action_use;
734   surf_network_model->common_public->action_cancel = action_cancel;
735   surf_network_model->common_public->action_recycle = action_recycle;
736   surf_network_model->common_public->action_change_state =
737       action_change_state;
738   surf_network_model->common_public->action_set_data =
739       surf_action_set_data;
740   surf_network_model->common_public->name = "network";
741
742   surf_network_model->common_private->resource_used = resource_used;
743   surf_network_model->common_private->share_resources = share_resources;
744   surf_network_model->common_private->update_actions_state =
745       update_actions_state;
746   surf_network_model->common_private->update_resource_state =
747       update_resource_state;
748   surf_network_model->common_private->finalize = finalize;
749
750   surf_network_model->common_public->suspend = action_suspend;
751   surf_network_model->common_public->resume = action_resume;
752   surf_network_model->common_public->is_suspended = action_is_suspended;
753   surf_cpu_model->common_public->set_max_duration =
754       action_set_max_duration;
755
756   surf_network_model->extension_public->communicate = communicate;
757   surf_network_model->extension_public->get_route = get_route;
758   surf_network_model->extension_public->get_route_size = get_route_size;
759   surf_network_model->extension_public->get_link_name = get_link_name;
760   surf_network_model->extension_public->get_link_bandwidth =
761       get_link_bandwidth;
762   surf_network_model->extension_public->get_link_latency =
763       get_link_latency;
764
765   surf_network_model->common_public->get_properties =  get_properties;
766
767   link_set = xbt_dict_new();
768   network_card_set = xbt_dict_new();
769
770   if (!network_maxmin_system)
771     network_maxmin_system = lmm_system_new();
772 }
773
774 /***************************************************************************/
775 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
776 /***************************************************************************/
777 /* @TechReport{      rr-lip2002-40, */
778 /*   author        = {Henri Casanova and Loris Marchal}, */
779 /*   institution   = {LIP}, */
780 /*   title         = {A Network Model for Simulation of Grid Application}, */
781 /*   number        = {2002-40}, */
782 /*   month         = {oct}, */
783 /*   year          = {2002} */
784 /* } */
785 void surf_network_model_init_CM02(const char *filename)
786 {
787
788   if (surf_network_model)
789     return;
790   surf_network_model_init_internal();
791   define_callbacks(filename);
792   xbt_dynar_push(model_list, &surf_network_model);
793   network_solve = lmm_solve;
794
795   update_model_description(surf_network_model_description,
796                               surf_network_model_description_size,
797                               "CM02",
798                               (surf_model_t) surf_network_model);
799 }
800
801 void surf_network_model_init_Reno(const char *filename)
802 {
803   if (surf_network_model)
804     return;
805   surf_network_model_init_internal();
806   define_callbacks(filename);
807
808   xbt_dynar_push(model_list, &surf_network_model);
809   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
810                                     func_reno_fpi);
811   network_solve = lagrange_solve;
812
813   update_model_description(surf_network_model_description,
814                               surf_network_model_description_size,
815                               "Reno",
816                               (surf_model_t) surf_network_model);
817 }
818
819 void surf_network_model_init_Vegas(const char *filename)
820 {
821   if (surf_network_model)
822     return;
823   surf_network_model_init_internal();
824   define_callbacks(filename);
825
826   xbt_dynar_push(model_list, &surf_network_model);
827   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
828                                     func_vegas_fpi);
829   network_solve = lagrange_solve;
830
831   update_model_description(surf_network_model_description,
832                               surf_network_model_description_size,
833                               "Vegas",
834                               (surf_model_t) surf_network_model);
835 }
836
837 #ifdef HAVE_SDP
838 void surf_network_model_init_SDP(const char *filename)
839 {
840   if (surf_network_model)
841     return;
842   surf_network_model_init_internal();
843   define_callbacks(filename);
844
845   xbt_dynar_push(model_list, &surf_network_model);
846   network_solve = sdp_solve;
847
848   update_model_description(surf_network_model_description,
849                               surf_network_model_description_size,
850                               "SDP",
851                               (surf_model_t) surf_network_model);
852 }
853 #endif