Logo AND Algorithmique Numérique Distribuée

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