Logo AND Algorithmique Numérique Distribuée

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