Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
601f9b9ebf58449291a6d482161aa0bfc4b39351
[simgrid.git] / src / surf / workstation_KCCFLN05.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2005 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 "xbt/ex.h"
9 #include "xbt/dict.h"
10 #include "surf_private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
13                                 "Logging specific to the SURF workstation module (KCCFLN05)");
14 XBT_LOG_EXTERNAL_CATEGORY(surf_parse);
15
16 typedef enum {
17   SURF_WORKSTATION_RESOURCE_CPU,
18   SURF_WORKSTATION_RESOURCE_LINK,
19 } e_surf_workstation_model_type_t;
20
21
22 /**************************************/
23 /********* router object **************/
24 /**************************************/
25 typedef struct router_KCCFLN05 {
26   char *name;           
27   unsigned int id;
28 } s_router_KCCFLN05_t, *router_KCCFLN05_t;
29
30
31 /**************************************/
32 /********* cpu object *****************/
33 /**************************************/
34 typedef struct cpu_KCCFLN05 {
35   surf_model_t model;                   /* Do not move this field: must match model_obj_t */
36   xbt_dict_t properties;                /* Do not move this field: must match link_KCCFLN05_t */
37   e_surf_workstation_model_type_t type; /* Do not move this field: must match link_KCCFLN05_t */
38   char *name;                           /* Do not move this field: must match link_KCCFLN05_t */
39   lmm_constraint_t constraint;
40   lmm_constraint_t bus;
41   double power_scale;
42   double power_current;
43   double interference_send;
44   double interference_recv;
45   double interference_send_recv;
46   tmgr_trace_event_t power_event;
47   e_surf_cpu_state_t state_current;
48   tmgr_trace_event_t state_event;
49   int id;                       /* cpu and network card are a single object... */
50   xbt_dynar_t incomming_communications;
51   xbt_dynar_t outgoing_communications;
52 } s_cpu_KCCFLN05_t, *cpu_KCCFLN05_t;
53
54 /**************************************/
55 /*********** network object ***********/
56 /**************************************/
57
58 typedef struct link_KCCFLN05 {
59   surf_model_t model;                   /* Do not move this field: must match model_obj_t */
60   xbt_dict_t properties;                /* Do not move this field: must match cpu_KCCFLN05_t */
61   e_surf_workstation_model_type_t type; /* Do not move this field: must match cpu_KCCFLN05_t */
62   char *name;                           /* Do not move this field: must match cpu_KCCFLN05_t */
63   lmm_constraint_t constraint;
64   double lat_current;
65   tmgr_trace_event_t lat_event;
66   double bw_current;
67   tmgr_trace_event_t bw_event;
68   e_surf_link_state_t state_current;
69   tmgr_trace_event_t state_event;
70 } s_link_KCCFLN05_t, *link_KCCFLN05_t;
71
72
73 typedef struct s_route_KCCFLN05 {
74   double impact_on_src;
75   double impact_on_dst;
76   double impact_on_src_with_other_recv;
77   double impact_on_dst_with_other_send;
78   link_KCCFLN05_t *links;
79   int size;
80 } s_route_KCCFLN05_t, *route_KCCFLN05_t;
81
82 /**************************************/
83 /*************** actions **************/
84 /**************************************/
85 typedef struct surf_action_workstation_KCCFLN05 {
86   s_surf_action_t generic_action;
87   double latency;
88   double lat_current;
89   lmm_variable_t variable;
90   double rate;
91   int suspended;
92   cpu_KCCFLN05_t src;           /* could be avoided */
93   cpu_KCCFLN05_t dst;           /* could be avoided */
94 } s_surf_action_workstation_KCCFLN05_t,
95   *surf_action_workstation_KCCFLN05_t;
96
97
98
99 static int nb_workstation = 0;
100 static s_route_KCCFLN05_t *routing_table = NULL;
101 #define ROUTE(i,j) routing_table[(i)+(j)*nb_workstation]
102 static link_KCCFLN05_t loopback = NULL;
103 static xbt_dict_t parallel_task_link_set = NULL;
104 //added to work with GTNETS
105 static xbt_dict_t router_set = NULL;
106 static lmm_system_t maxmin_system = NULL;
107
108
109 /* convenient function */
110 static void __update_cpu_usage(cpu_KCCFLN05_t cpu)
111 {
112   unsigned int cpt;
113   surf_action_workstation_KCCFLN05_t action = NULL;
114   if ((!xbt_dynar_length(cpu->incomming_communications)) &&
115       (!xbt_dynar_length(cpu->outgoing_communications))) {
116     /* No communications */
117     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
118                                 cpu->power_current * cpu->power_scale);
119   } else if ((!xbt_dynar_length(cpu->incomming_communications))
120              && (xbt_dynar_length(cpu->outgoing_communications))) {
121     /* Emission */
122     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
123                                 cpu->power_current * cpu->power_scale *
124                                 cpu->interference_send);
125     xbt_dynar_foreach(cpu->outgoing_communications, cpt, action)
126         lmm_elem_set_value(maxmin_system, cpu->constraint,
127                            action->variable,
128                            cpu->power_current * cpu->power_scale *
129                            ROUTE(action->src->id,
130                                  action->dst->id).impact_on_src);
131   } else if ((xbt_dynar_length(cpu->incomming_communications))
132              && (!xbt_dynar_length(cpu->outgoing_communications))) {
133     /* Reception */
134     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
135                                 cpu->power_current * cpu->power_scale *
136                                 cpu->interference_recv);
137     xbt_dynar_foreach(cpu->incomming_communications, cpt, action)
138         lmm_elem_set_value(maxmin_system, cpu->constraint,
139                            action->variable,
140                            cpu->power_current * cpu->power_scale *
141                            ROUTE(action->src->id,
142                                  action->dst->id).impact_on_dst);
143   } else {
144     /* Emission & Reception */
145     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
146                                 cpu->power_current * cpu->power_scale *
147                                 cpu->interference_send_recv);
148     xbt_dynar_foreach(cpu->outgoing_communications, cpt, action)
149         lmm_elem_set_value(maxmin_system, cpu->constraint,
150                            action->variable,
151                            cpu->power_current * cpu->power_scale *
152                            ROUTE(action->src->id,
153                                  action->dst->id).
154                            impact_on_src_with_other_recv);
155     xbt_dynar_foreach(cpu->incomming_communications, cpt, action)
156         lmm_elem_set_value(maxmin_system, cpu->constraint,
157                            action->variable,
158                            cpu->power_current * cpu->power_scale *
159                            ROUTE(action->src->id,
160                                  action->dst->id).
161                            impact_on_dst_with_other_send);
162   }
163 }
164
165 /**************************************/
166 /******* Resource Public     **********/
167 /**************************************/
168
169 static void *name_service(const char *name)
170 {
171   xbt_ex_t e;
172   void *res = NULL;
173
174   TRY {
175     res = xbt_dict_get(workstation_set, name);
176   } CATCH(e) {
177     if (e.category != not_found_error)
178       RETHROW;
179     WARN1("Host '%s' not found, verifing if it is a router", name);
180     res = NULL;
181     xbt_ex_free(e);
182   }
183
184   return res;
185 }
186
187 static const char *get_resource_name(void *resource_id)
188 {
189   /* We can freely cast as a cpu_KCCFLN05_t because it has the same
190      prefix as link_KCCFLN05_t. However, only cpu_KCCFLN05_t
191      will theoretically be given as an argument here. */
192   return ((cpu_KCCFLN05_t) resource_id)->name;
193 }
194
195 static xbt_dict_t get_properties(void *resource)
196 {
197   /* We can freely cast as a cpu_KCCFLN05_t because it has the same
198      prefix as link_KCCFLN05_t. */
199   return ((cpu_KCCFLN05_t) resource)->properties;
200 }
201
202 /* action_get_state is inherited from the surf module */
203
204 static void action_use(surf_action_t action)
205 {
206   action->using++;
207   return;
208 }
209
210 static int action_free(surf_action_t action)
211 {
212   unsigned int cpt;
213   surf_action_t act = NULL;
214   cpu_KCCFLN05_t src = ((surf_action_workstation_KCCFLN05_t) action)->src;
215   cpu_KCCFLN05_t dst = ((surf_action_workstation_KCCFLN05_t) action)->dst;
216
217   action->using--;
218   if (!action->using) {
219     xbt_swag_remove(action, action->state_set);
220     if (((surf_action_workstation_KCCFLN05_t) action)->variable)
221       lmm_variable_free(maxmin_system,
222                         ((surf_action_workstation_KCCFLN05_t) action)->
223                         variable);
224     if (src)
225       xbt_dynar_foreach(src->outgoing_communications, cpt, act)
226           if (act == action) {
227         xbt_dynar_remove_at(src->outgoing_communications, cpt, &act);
228         break;
229       }
230
231     if (dst)
232       xbt_dynar_foreach(dst->incomming_communications, cpt, act)
233           if (act == action) {
234         xbt_dynar_remove_at(dst->incomming_communications, cpt, &act);
235         break;
236       }
237
238     if (src && (!xbt_dynar_length(src->outgoing_communications)))
239       __update_cpu_usage(src);
240     if (dst && (!xbt_dynar_length(dst->incomming_communications)))
241       __update_cpu_usage(dst);
242
243     free(action);
244     return 1;
245   }
246   return 0;
247 }
248
249 static void action_cancel(surf_action_t action)
250 {
251   surf_action_change_state(action, SURF_ACTION_FAILED);
252   return;
253 }
254
255 static void action_recycle(surf_action_t action)
256 {
257   DIE_IMPOSSIBLE;
258   return;
259 }
260
261 /* action_change_state is inherited from the surf module */
262 /* action_set_data is inherited from the surf module */
263
264 static void action_suspend(surf_action_t action)
265 {
266   XBT_IN1("(%p))", action);
267   if (((surf_action_workstation_KCCFLN05_t) action)->suspended != 2) {
268     ((surf_action_workstation_KCCFLN05_t) action)->suspended = 1;
269     lmm_update_variable_weight(maxmin_system,
270                                ((surf_action_workstation_KCCFLN05_t)
271                                 action)->variable, 0.0);
272   }
273   XBT_OUT;
274 }
275
276 static void action_resume(surf_action_t action)
277 {
278   XBT_IN1("(%p)", action);
279   if (((surf_action_workstation_KCCFLN05_t) action)->suspended != 2) {
280     if (((surf_action_workstation_KCCFLN05_t) action)->lat_current == 0.0)
281       lmm_update_variable_weight(maxmin_system,
282                                  ((surf_action_workstation_KCCFLN05_t)
283                                   action)->variable, action->priority);
284     else
285       lmm_update_variable_weight(maxmin_system,
286                                  ((surf_action_workstation_KCCFLN05_t)
287                                   action)->variable,
288                                  ((surf_action_workstation_KCCFLN05_t)
289                                   action)->lat_current);
290
291     ((surf_action_workstation_KCCFLN05_t) action)->suspended = 0;
292   }
293   XBT_OUT;
294 }
295
296 static int action_is_suspended(surf_action_t action)
297 {
298   return (((surf_action_workstation_KCCFLN05_t) action)->suspended == 1);
299 }
300
301 static void action_set_max_duration(surf_action_t action, double duration)
302 {                               /* FIXME: should inherit */
303   XBT_IN2("(%p,%g)", action, duration);
304   action->max_duration = duration;
305   XBT_OUT;
306 }
307
308
309 static void action_set_priority(surf_action_t action, double priority)
310 {
311   XBT_IN2("(%p,%g)", action, priority);
312   action->priority = priority;
313   lmm_update_variable_weight(maxmin_system, ((surf_action_workstation_KCCFLN05_t) action)->variable, priority);
314
315   XBT_OUT;
316 }
317
318 /**************************************/
319 /******* Resource Private    **********/
320 /**************************************/
321
322 static int resource_used(void *resource_id)
323 {
324   /* We can freely cast as a link_KCCFLN05_t because it has
325      the same prefix as cpu_KCCFLN05_t */
326   if (((cpu_KCCFLN05_t) resource_id)->type ==
327       SURF_WORKSTATION_RESOURCE_CPU)
328     return (lmm_constraint_used
329             (maxmin_system, ((cpu_KCCFLN05_t) resource_id)->constraint)
330             || ((((cpu_KCCFLN05_t) resource_id)->bus) ?
331                 lmm_constraint_used(maxmin_system,
332                                     ((cpu_KCCFLN05_t) resource_id)->
333                                     bus) : 0));
334   else
335     return lmm_constraint_used(maxmin_system,
336                                ((link_KCCFLN05_t) resource_id)->
337                                constraint);
338
339 }
340
341 static double share_resources(double now)
342 {
343   s_surf_action_workstation_KCCFLN05_t s_action;
344   surf_action_workstation_KCCFLN05_t action = NULL;
345
346   xbt_swag_t running_actions =
347       surf_workstation_model->common_public->states.running_action_set;
348   double min = generic_maxmin_share_resources(running_actions,
349                                               xbt_swag_offset(s_action,
350                                                               variable),
351                                               maxmin_system,
352                                               lmm_solve);
353
354   xbt_swag_foreach(action, running_actions) {
355     if (action->latency > 0) {
356       if (min < 0) {
357         min = action->latency;
358         DEBUG3("Updating min (value) with %p (start %f): %f", action,
359                action->generic_action.start, min);
360       } else if (action->latency < min) {
361         min = action->latency;
362         DEBUG3("Updating min (latency) with %p (start %f): %f", action,
363                action->generic_action.start, min);
364       }
365     }
366   }
367
368   DEBUG1("min value : %f", min);
369
370   return min;
371 }
372
373 static void update_actions_state(double now, double delta)
374 {
375   double deltap = 0.0;
376   surf_action_workstation_KCCFLN05_t action = NULL;
377   surf_action_workstation_KCCFLN05_t next_action = NULL;
378   xbt_swag_t running_actions =
379       surf_workstation_model->common_public->states.running_action_set;
380
381   xbt_swag_foreach_safe(action, next_action, running_actions) {
382     deltap = delta;
383     if (action->latency > 0) {
384       if (action->latency > deltap) {
385         double_update(&(action->latency), deltap);
386         deltap = 0.0;
387       } else {
388         double_update(&(deltap), action->latency);
389         action->latency = 0.0;
390       }
391       if ((action->latency == 0.0) && (action->suspended == 0)) {
392         if ((action)->lat_current == 0.0)
393           lmm_update_variable_weight(maxmin_system, action->variable, 1.0);
394         else
395           lmm_update_variable_weight(maxmin_system, action->variable,
396                                      action->lat_current);
397       }
398     }
399     DEBUG3("Action (%p) : remains (%g) updated by %g.",
400            action, action->generic_action.remains,
401            lmm_variable_getvalue(action->variable) * deltap);
402     double_update(&(action->generic_action.remains),
403                   lmm_variable_getvalue(action->variable) * deltap);
404
405     if (action->generic_action.max_duration != NO_MAX_DURATION)
406       double_update(&(action->generic_action.max_duration), delta);
407
408     if ((action->generic_action.remains <= 0) &&
409         (lmm_get_variable_weight(action->variable) > 0)) {
410       action->generic_action.finish = surf_get_clock();
411       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
412     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
413                (action->generic_action.max_duration <= 0)) {
414       action->generic_action.finish = surf_get_clock();
415       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
416     } else {
417       /* Need to check that none of the resource has failed */
418       lmm_constraint_t cnst = NULL;
419       int i = 0;
420       void *constraint_id = NULL;
421
422       while ((cnst =
423               lmm_get_cnst_from_var(maxmin_system, action->variable,
424                                     i++))) {
425         constraint_id = lmm_constraint_id(cnst);
426
427 /*      if(((link_KCCFLN05_t)constraint_id)->type== */
428 /*         SURF_WORKSTATION_RESOURCE_LINK) { */
429 /*        DEBUG2("Checking for link %s (%p)", */
430 /*               ((link_KCCFLN05_t)constraint_id)->name, */
431 /*               ((link_KCCFLN05_t)constraint_id)); */
432 /*      } */
433 /*      if(((cpu_KCCFLN05_t)constraint_id)->type== */
434 /*         SURF_WORKSTATION_RESOURCE_CPU) { */
435 /*        DEBUG3("Checking for cpu %s (%p) : %s", */
436 /*               ((cpu_KCCFLN05_t)constraint_id)->name, */
437 /*               ((cpu_KCCFLN05_t)constraint_id), */
438 /*               ((cpu_KCCFLN05_t)constraint_id)->state_current==SURF_CPU_OFF?"Off":"On"); */
439 /*      } */
440
441         if (((((link_KCCFLN05_t) constraint_id)->type ==
442               SURF_WORKSTATION_RESOURCE_LINK) &&
443              (((link_KCCFLN05_t) constraint_id)->state_current ==
444               SURF_LINK_OFF)) ||
445             ((((cpu_KCCFLN05_t) constraint_id)->type ==
446               SURF_WORKSTATION_RESOURCE_CPU) &&
447              (((cpu_KCCFLN05_t) constraint_id)->state_current ==
448               SURF_CPU_OFF))) {
449           DEBUG1("Action (%p) Failed!!", action);
450           action->generic_action.finish = surf_get_clock();
451           surf_action_change_state((surf_action_t) action,
452                                    SURF_ACTION_FAILED);
453           break;
454         }
455       }
456     }
457   }
458   return;
459 }
460
461 static void update_resource_state(void *id,
462                                   tmgr_trace_event_t event_type,
463                                   double value)
464 {
465   cpu_KCCFLN05_t cpu = id;
466   link_KCCFLN05_t nw_link = id;
467
468   if (nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
469     DEBUG2("Updating link %s (%p)", nw_link->name, nw_link);
470     if (event_type == nw_link->bw_event) {
471       nw_link->bw_current = value;
472       lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
473                                   nw_link->bw_current);
474     } else if (event_type == nw_link->lat_event) {
475       double delta = value - nw_link->lat_current;
476       lmm_variable_t var = NULL;
477       surf_action_workstation_KCCFLN05_t action = NULL;
478
479       nw_link->lat_current = value;
480       while (lmm_get_var_from_cnst
481              (maxmin_system, nw_link->constraint, &var)) {
482         action = lmm_variable_id(var);
483         action->lat_current += delta;
484         if (action->rate < 0)
485           lmm_update_variable_bound(maxmin_system, action->variable,
486                                     SG_TCP_CTE_GAMMA / (2.0 *
487                                                         action->
488                                                         lat_current));
489         else
490           lmm_update_variable_bound(maxmin_system, action->variable,
491                                     min(action->rate,
492                                         SG_TCP_CTE_GAMMA / (2.0 *
493                                                             action->
494                                                             lat_current)));
495         if (action->suspended == 0)
496           lmm_update_variable_weight(maxmin_system, action->variable,
497                                      action->lat_current);
498         lmm_update_variable_latency(maxmin_system, action->variable,
499                                     delta);
500
501
502       }
503     } else if (event_type == nw_link->state_event) {
504       if (value > 0)
505         nw_link->state_current = SURF_LINK_ON;
506       else
507         nw_link->state_current = SURF_LINK_OFF;
508     } else {
509       CRITICAL0("Unknown event ! \n");
510       xbt_abort();
511     }
512     return;
513   } else if (cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
514     DEBUG3("Updating cpu %s (%p) with value %g", cpu->name, cpu, value);
515     if (event_type == cpu->power_event) {
516       cpu->power_current = value;
517       __update_cpu_usage(cpu);
518     } else if (event_type == cpu->state_event) {
519       if (value > 0)
520         cpu->state_current = SURF_CPU_ON;
521       else
522         cpu->state_current = SURF_CPU_OFF;
523     } else {
524       CRITICAL0("Unknown event ! \n");
525       xbt_abort();
526     }
527     return;
528   } else {
529     DIE_IMPOSSIBLE;
530   }
531   return;
532 }
533
534 static void finalize(void)
535 {
536   int i, j;
537
538   xbt_dict_free(&link_set);
539   xbt_dict_free(&workstation_set);
540   xbt_dict_free(&router_set);
541   if (parallel_task_link_set != NULL) {
542     xbt_dict_free(&parallel_task_link_set);
543   }
544   xbt_swag_free(surf_workstation_model->common_public->states.
545                 ready_action_set);
546   xbt_swag_free(surf_workstation_model->common_public->states.
547                 running_action_set);
548   xbt_swag_free(surf_workstation_model->common_public->states.
549                 failed_action_set);
550   xbt_swag_free(surf_workstation_model->common_public->states.
551                 done_action_set);
552
553   free(surf_workstation_model->common_public);
554   free(surf_workstation_model->common_private);
555   free(surf_workstation_model->extension_public);
556
557   free(surf_workstation_model);
558   surf_workstation_model = NULL;
559
560   for (i = 0; i < nb_workstation; i++)
561     for (j = 0; j < nb_workstation; j++)
562       free(ROUTE(i, j).links);
563   free(routing_table);
564   routing_table = NULL;
565   nb_workstation = 0;
566
567   if (maxmin_system) {
568     lmm_system_free(maxmin_system);
569     maxmin_system = NULL;
570   }
571 }
572
573 /**************************************/
574 /******* Resource Private    **********/
575 /**************************************/
576
577 static surf_action_t execute(void *cpu, double size)
578 {
579   surf_action_workstation_KCCFLN05_t action = NULL;
580   cpu_KCCFLN05_t CPU = cpu;
581
582   XBT_IN2("(%s,%g)", CPU->name, size);
583   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
584
585   action->generic_action.using = 1;
586   action->generic_action.cost = size;
587   action->generic_action.remains = size;
588   action->generic_action.priority = 1.0;
589   action->generic_action.max_duration = NO_MAX_DURATION;
590   action->generic_action.start = surf_get_clock();
591   action->generic_action.finish = -1.0;
592   action->generic_action.model_type =
593       (surf_model_t) surf_workstation_model;
594   action->suspended = 0;
595
596   if (CPU->state_current == SURF_CPU_ON)
597     action->generic_action.state_set =
598         surf_workstation_model->common_public->states.
599         running_action_set;
600   else
601     action->generic_action.state_set =
602         surf_workstation_model->common_public->states.failed_action_set;
603   xbt_swag_insert(action, action->generic_action.state_set);
604
605   action->variable = lmm_variable_new(maxmin_system, action,
606                                       action->generic_action.priority,
607                                       -1.0, 1);
608   lmm_expand(maxmin_system, CPU->constraint, action->variable, 1.0);
609   XBT_OUT;
610   return (surf_action_t) action;
611 }
612
613 static surf_action_t action_sleep(void *cpu, double duration)
614 {
615   surf_action_workstation_KCCFLN05_t action = NULL;
616
617   XBT_IN2("(%s,%g)", ((cpu_KCCFLN05_t) cpu)->name, duration);
618
619   action = (surf_action_workstation_KCCFLN05_t) execute(cpu, 1.0);
620   action->generic_action.max_duration = duration;
621   action->suspended = 2;
622   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
623
624   XBT_OUT;
625   return (surf_action_t) action;
626 }
627
628 static e_surf_cpu_state_t resource_get_state(void *cpu)
629 {
630   return ((cpu_KCCFLN05_t) cpu)->state_current;
631 }
632
633 static double get_speed(void *cpu, double load)
634 {
635   return load * (((cpu_KCCFLN05_t) cpu)->power_scale);
636 }
637
638 static double get_available_speed(void *cpu)
639 {
640   return ((cpu_KCCFLN05_t) cpu)->power_current;
641 }
642
643 static surf_action_t communicate(void *src, void *dst, double size,
644                                  double rate)
645 {
646   surf_action_workstation_KCCFLN05_t action = NULL;
647   cpu_KCCFLN05_t card_src = src;
648   cpu_KCCFLN05_t card_dst = dst;
649   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
650   int route_size = route->size;
651   int i;
652
653   XBT_IN4("(%s,%s,%g,%g)", card_src->name, card_dst->name, size, rate);
654   xbt_assert2(route_size,
655               "You're trying to send data from %s to %s but there is no connexion between these two cards.",
656               card_src->name, card_dst->name);
657
658   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
659
660   action->generic_action.using = 1;
661   action->generic_action.cost = size;
662   action->generic_action.remains = size;
663   action->generic_action.max_duration = NO_MAX_DURATION;
664   action->generic_action.start = surf_get_clock();
665   action->generic_action.finish = -1.0;
666   action->src = src;
667   action->dst = dst;
668   action->generic_action.model_type =
669       (surf_model_t) surf_workstation_model;
670   action->suspended = 0;        /* Should be useless because of the 
671                                    calloc but it seems to help valgrind... */
672   action->generic_action.state_set =
673       surf_workstation_model->common_public->states.running_action_set;
674
675   xbt_dynar_push(card_src->outgoing_communications, &action);
676   xbt_dynar_push(card_dst->incomming_communications, &action);
677
678   xbt_swag_insert(action, action->generic_action.state_set);
679   action->rate = rate;
680
681   action->latency = 0.0;
682   for (i = 0; i < route_size; i++)
683     action->latency += route->links[i]->lat_current;
684   action->lat_current = action->latency;
685
686   if (action->latency > 0)
687     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0, route_size + 4);      /* +1 for the src bus
688                                                                                                    +1 for the dst bus
689                                                                                                    +1 for the src cpu
690                                                                                                    +1 for the dst cpu */
691   else
692     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
693                                         route_size + 4);
694
695   if (action->rate < 0) {
696     if (action->lat_current > 0)
697       lmm_update_variable_bound(maxmin_system, action->variable,
698                                 SG_TCP_CTE_GAMMA / (2.0 *
699                                                     action->lat_current));
700     else
701       lmm_update_variable_bound(maxmin_system, action->variable, -1.0);
702   } else {
703     if (action->lat_current > 0)
704       lmm_update_variable_bound(maxmin_system, action->variable,
705                                 min(action->rate,
706                                     SG_TCP_CTE_GAMMA / (2.0 *
707                                                         action->
708                                                         lat_current)));
709     else
710       lmm_update_variable_bound(maxmin_system, action->variable,
711                                 action->rate);
712   }
713
714   lmm_update_variable_latency(maxmin_system, action->variable,
715                               action->latency); /* Should be useless */
716
717   for (i = 0; i < route_size; i++)
718     lmm_expand(maxmin_system, route->links[i]->constraint,
719                action->variable, 1.0);
720   if (card_src->bus)
721     lmm_expand(maxmin_system, card_src->bus, action->variable, 1.0);
722   if (card_dst->bus)
723     lmm_expand(maxmin_system, card_dst->bus, action->variable, 1.0);
724   lmm_expand(maxmin_system, card_src->constraint, action->variable, 0.0);
725   lmm_expand(maxmin_system, card_dst->constraint, action->variable, 0.0);
726
727   XBT_OUT;
728   return (surf_action_t) action;
729 }
730
731 static surf_action_t execute_parallel_task(int workstation_nb,
732                                            void **workstation_list,
733                                            double *computation_amount,
734                                            double *communication_amount,
735                                            double amount, double rate)
736 {
737   surf_action_workstation_KCCFLN05_t action = NULL;
738   int i, j, k;
739   int nb_link = 0;
740   int nb_host = 0;
741
742   if (parallel_task_link_set == NULL) {
743     parallel_task_link_set = xbt_dict_new();
744   }
745
746   /* Compute the number of affected resources... */
747   for (i = 0; i < workstation_nb; i++) {
748     for (j = 0; j < workstation_nb; j++) {
749       cpu_KCCFLN05_t card_src = workstation_list[i];
750       cpu_KCCFLN05_t card_dst = workstation_list[j];
751       int route_size = ROUTE(card_src->id, card_dst->id).size;
752       link_KCCFLN05_t *route =
753           ROUTE(card_src->id, card_dst->id).links;
754
755       if (communication_amount[i * workstation_nb + j] > 0)
756         for (k = 0; k < route_size; k++) {
757           xbt_dict_set(parallel_task_link_set, route[k]->name,
758                        route[k], NULL);
759         }
760     }
761   }
762   nb_link = xbt_dict_length(parallel_task_link_set);
763   xbt_dict_reset(parallel_task_link_set);
764
765
766   for (i = 0; i < workstation_nb; i++)
767     if (computation_amount[i] > 0)
768       nb_host++;
769
770   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
771   DEBUG3("Creating a parallel task (%p) with %d cpus and %d links.",
772          action, nb_host, nb_link);
773   action->generic_action.using = 1;
774   action->generic_action.cost = amount;
775   action->generic_action.remains = amount;
776   action->generic_action.max_duration = NO_MAX_DURATION;
777   action->generic_action.start = surf_get_clock();
778   action->generic_action.finish = -1.0;
779   action->generic_action.model_type =
780       (surf_model_t) surf_workstation_model;
781   action->suspended = 0;        /* Should be useless because of the
782                                    calloc but it seems to help valgrind... */
783   action->generic_action.state_set =
784       surf_workstation_model->common_public->states.running_action_set;
785
786   xbt_swag_insert(action, action->generic_action.state_set);
787   action->rate = rate;
788
789   if (action->rate > 0)
790     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
791                                         nb_host + nb_link);
792   else
793     action->variable =
794         lmm_variable_new(maxmin_system, action, 1.0, action->rate,
795                          nb_host + nb_link);
796
797   for (i = 0; i < workstation_nb; i++)
798     if (computation_amount[i] > 0)
799       lmm_expand(maxmin_system,
800                  ((cpu_KCCFLN05_t) workstation_list[i])->constraint,
801                  action->variable, computation_amount[i]);
802
803   for (i = 0; i < workstation_nb; i++) {
804     for (j = 0; j < workstation_nb; j++) {
805       cpu_KCCFLN05_t card_src = workstation_list[i];
806       cpu_KCCFLN05_t card_dst = workstation_list[j];
807       int route_size = ROUTE(card_src->id, card_dst->id).size;
808       link_KCCFLN05_t *route =
809           ROUTE(card_src->id, card_dst->id).links;
810
811       for (k = 0; k < route_size; k++) {
812         if (communication_amount[i * workstation_nb + j] > 0) {
813           lmm_expand_add(maxmin_system, route[k]->constraint,
814                          action->variable,
815                          communication_amount[i * workstation_nb + j]);
816         }
817       }
818     }
819   }
820
821   if (nb_link + nb_host == 0) {
822     action->generic_action.cost = 1.0;
823     action->generic_action.remains = 0.0;
824   }
825
826   return (surf_action_t) action;
827 }
828
829 /* returns an array of link_KCCFLN05_t */
830 static const void **get_route(void *src, void *dst)
831 {
832   cpu_KCCFLN05_t card_src = src;
833   cpu_KCCFLN05_t card_dst = dst;
834   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
835
836   return (const void **) route->links;
837 }
838
839 static int get_route_size(void *src, void *dst)
840 {
841   cpu_KCCFLN05_t card_src = src;
842   cpu_KCCFLN05_t card_dst = dst;
843   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
844   return route->size;
845 }
846
847 static const char *get_link_name(const void *link)
848 {
849   return ((link_KCCFLN05_t) link)->name;
850 }
851
852 static double get_link_bandwidth(const void *link)
853 {
854   return ((link_KCCFLN05_t) link)->bw_current;
855 }
856
857 static double get_link_latency(const void *link)
858 {
859   return ((link_KCCFLN05_t) link)->lat_current;
860 }
861
862 /**************************************/
863 /*** Resource Creation & Destruction **/
864 /**************************************/
865
866
867 static void router_free(void *router)
868 {
869   free(((router_KCCFLN05_t) router)->name);
870 }
871
872 static void router_new(const char *name)
873 {
874   static unsigned int nb_routers = 0;
875   router_KCCFLN05_t router;
876
877   INFO1("Creating a router %s", name);
878
879   router = xbt_new0(s_router_KCCFLN05_t, 1);
880
881   router->name = xbt_strdup(name);
882   router->id = nb_routers++;
883   xbt_dict_set(router_set, name, router, router_free);
884 }
885
886 static void parse_routers(void)
887 {
888   //add a dumb router just to be GTNETS compatible
889   router_new(A_surfxml_router_id);
890 }
891
892 static void cpu_free(void *cpu)
893 {
894   free(((cpu_KCCFLN05_t) cpu)->name);
895   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->incomming_communications));
896   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->outgoing_communications));
897   free(cpu);
898 }
899
900 static cpu_KCCFLN05_t cpu_new(const char *name, double power_scale,
901                               double power_initial,
902                               tmgr_trace_t power_trace,
903                               e_surf_cpu_state_t state_initial,
904                               tmgr_trace_t state_trace,
905                               double interference_send,
906                               double interference_recv,
907                               double interference_send_recv,
908                               double max_outgoing_rate,
909                               xbt_dict_t cpu_properties_k)
910 {
911   cpu_KCCFLN05_t cpu = xbt_new0(s_cpu_KCCFLN05_t, 1);
912
913   xbt_assert1(! xbt_dict_get_or_null(workstation_set, name),
914               "Host '%s' declared several times in the platform file.",name);
915
916   CDEBUG8(surf_parse, "cpu_new(%s,power_scale=%.2f,power_initial=%.2f,state_init=%d,isend=%.2f,irecv=%.2f,isendrev=%.2f) -> %p",
917           name,power_scale,power_initial,state_initial,interference_send,interference_recv,interference_send_recv,cpu);
918    
919   cpu->model = (surf_model_t) surf_workstation_model;
920   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
921   cpu->name = xbt_strdup(name);
922   cpu->id = nb_workstation++;
923
924   cpu->power_scale = power_scale;
925   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
926
927   cpu->power_current = power_initial;
928   if (power_trace)
929     cpu->power_event =
930         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
931
932   cpu->state_current = state_initial;
933   if (state_trace)
934     cpu->state_event =
935         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
936
937   cpu->interference_send = interference_send;
938   cpu->interference_recv = interference_recv;
939   cpu->interference_send_recv = interference_send_recv;
940
941   cpu->constraint =
942       lmm_constraint_new(maxmin_system, cpu,
943                          cpu->power_current * cpu->power_scale);
944   if (max_outgoing_rate > 0)
945     cpu->bus = lmm_constraint_new(maxmin_system, cpu, max_outgoing_rate);
946
947   cpu->incomming_communications =
948       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
949   cpu->outgoing_communications =
950       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
951
952   /*add the property set*/
953   cpu->properties = current_property_set;
954  
955   xbt_dict_set(workstation_set, name, cpu, cpu_free);
956
957   return cpu;
958 }
959
960 static void create_routing_table(void)
961 {
962     routing_table = xbt_new0(s_route_KCCFLN05_t, nb_workstation * nb_workstation);
963 }
964
965 static void parse_cpu_init(void)
966 {
967   double power_scale = 0.0;
968   double power_initial = 0.0;
969   tmgr_trace_t power_trace = NULL;
970   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
971   tmgr_trace_t state_trace = NULL;
972   double interference_send = 0.0;
973   double interference_recv = 0.0;
974   double interference_send_recv = 0.0;
975   double max_outgoing_rate = -1.0;
976
977   power_scale = get_cpu_power(A_surfxml_host_power);
978   surf_parse_get_double(&power_initial, A_surfxml_host_availability);
979   surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
980
981   xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
982               (A_surfxml_host_state == A_surfxml_host_state_OFF),
983               "Invalid state");
984   if (A_surfxml_host_state == A_surfxml_host_state_ON)
985     state_initial = SURF_CPU_ON;
986   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
987     state_initial = SURF_CPU_OFF;
988   surf_parse_get_trace(&state_trace, A_surfxml_host_state_file);
989
990   surf_parse_get_double(&interference_send,
991                         A_surfxml_host_interference_send);
992   surf_parse_get_double(&interference_recv,
993                         A_surfxml_host_interference_recv);
994   surf_parse_get_double(&interference_send_recv,
995                         A_surfxml_host_interference_send_recv);
996   surf_parse_get_double(&max_outgoing_rate,
997                         A_surfxml_host_max_outgoing_rate);
998   current_property_set = xbt_dict_new();
999
1000   cpu_new(A_surfxml_host_id, power_scale, power_initial, power_trace,
1001           state_initial, state_trace, interference_send, interference_recv,
1002           interference_send_recv, max_outgoing_rate,current_property_set);
1003 }
1004
1005 static void link_free(void *nw_link)
1006 {
1007   free(((link_KCCFLN05_t) nw_link)->name);
1008   free(nw_link);
1009 }
1010
1011 static link_KCCFLN05_t link_new(char *name,
1012                                 double bw_initial,
1013                                 tmgr_trace_t bw_trace,
1014                                 double lat_initial,
1015                                 tmgr_trace_t lat_trace,
1016                                 e_surf_link_state_t
1017                                 state_initial,
1018                                 tmgr_trace_t state_trace,
1019                                 e_surf_link_sharing_policy_t
1020                                 policy, xbt_dict_t properties_args)
1021 {
1022   link_KCCFLN05_t nw_link = xbt_new0(s_link_KCCFLN05_t, 1);
1023
1024
1025   nw_link->model = (surf_model_t) surf_workstation_model;
1026   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
1027   nw_link->name = name;
1028   nw_link->bw_current = bw_initial;
1029   if (bw_trace)
1030     nw_link->bw_event =
1031         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
1032   nw_link->state_current = state_initial;
1033   nw_link->lat_current = lat_initial;
1034   if (lat_trace)
1035     nw_link->lat_event =
1036         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
1037   if (state_trace)
1038     nw_link->state_event =
1039         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
1040
1041   nw_link->constraint =
1042       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
1043
1044   if (policy == SURF_LINK_FATPIPE)
1045     lmm_constraint_shared(nw_link->constraint);
1046
1047   /*add the property set*/
1048   nw_link->properties = properties_args;
1049
1050   xbt_dict_set(link_set, name, nw_link, link_free);
1051
1052   return nw_link;
1053 }
1054
1055 static void parse_link_init(void)
1056 {
1057   char *name_link;
1058   double bw_initial;
1059   tmgr_trace_t bw_trace;
1060   double lat_initial;
1061   tmgr_trace_t lat_trace;
1062   e_surf_link_state_t state_initial_link = SURF_LINK_ON;
1063   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
1064   tmgr_trace_t state_trace;
1065
1066   name_link = xbt_strdup(A_surfxml_link_id);
1067   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
1068   surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
1069   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
1070   surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
1071
1072   xbt_assert0((A_surfxml_link_state ==
1073                A_surfxml_link_state_ON)
1074               || (A_surfxml_link_state ==
1075                   A_surfxml_link_state_OFF), "Invalid state");
1076   if (A_surfxml_link_state == A_surfxml_link_state_ON)
1077     state_initial_link = SURF_LINK_ON;
1078   else if (A_surfxml_link_state ==
1079            A_surfxml_link_state_OFF)
1080     state_initial_link = SURF_LINK_OFF;
1081
1082   if (A_surfxml_link_sharing_policy ==
1083       A_surfxml_link_sharing_policy_SHARED)
1084     policy_initial_link = SURF_LINK_SHARED;
1085   else if (A_surfxml_link_sharing_policy ==
1086            A_surfxml_link_sharing_policy_FATPIPE)
1087     policy_initial_link = SURF_LINK_FATPIPE;
1088
1089   surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
1090
1091  current_property_set = xbt_dict_new();
1092  link_new(name_link, bw_initial, bw_trace,
1093                    lat_initial, lat_trace, state_initial_link, state_trace,
1094                    policy_initial_link,/*add properties*/current_property_set);
1095 }
1096
1097 static void route_new(int src_id, int dst_id,
1098                       link_KCCFLN05_t * link_list, int nb_link,
1099                       double impact_on_src, double impact_on_dst,
1100                       double impact_on_src_with_other_recv,
1101                       double impact_on_dst_with_other_send)
1102 {
1103   route_KCCFLN05_t route = &(ROUTE(src_id, dst_id));
1104
1105   route->size = nb_link;
1106   route->links = link_list = xbt_realloc(link_list, sizeof(link_KCCFLN05_t) * nb_link);
1107   route->impact_on_src = impact_on_src;
1108   route->impact_on_dst = impact_on_dst;
1109   route->impact_on_src_with_other_recv = impact_on_src_with_other_recv;
1110   route->impact_on_dst_with_other_send = impact_on_dst_with_other_send;
1111 }
1112
1113 static int src_id = -1;
1114 static int dst_id = -1;
1115 static double impact_on_src;
1116 static double impact_on_dst;
1117 static double impact_on_src_with_other_recv;
1118 static double impact_on_dst_with_other_send;
1119
1120 static void parse_route_set_endpoints(void)
1121 {
1122   cpu_KCCFLN05_t cpu_tmp = NULL;
1123
1124   cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_src);
1125   if (cpu_tmp != NULL) {
1126     src_id = cpu_tmp->id;
1127   } else {
1128     xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_src),
1129                 "Invalid name '%s': neither a cpu nor a router!",
1130                 A_surfxml_route_src);
1131     src_id = -1;
1132     return;
1133   }
1134
1135   cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_dst);
1136   if (cpu_tmp != NULL) {
1137     dst_id = cpu_tmp->id;
1138   } else {
1139     xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_dst),
1140                 "Invalid name '%s': neither a cpu nor a router!",
1141                 A_surfxml_route_dst);
1142     dst_id = -1;
1143     return;
1144   }
1145
1146   surf_parse_get_double(&impact_on_src, A_surfxml_route_impact_on_src);
1147   surf_parse_get_double(&impact_on_dst, A_surfxml_route_impact_on_dst);
1148   surf_parse_get_double(&impact_on_src_with_other_recv,
1149                         A_surfxml_route_impact_on_src_with_other_recv);
1150   surf_parse_get_double(&impact_on_dst_with_other_send,
1151                         A_surfxml_route_impact_on_dst_with_other_send);
1152
1153   
1154   route_action = A_surfxml_route_action;
1155   route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
1156
1157 }
1158
1159 static void parse_route_set_route(void)
1160 {
1161   char* name;
1162   if (src_id != -1 && dst_id != -1) {
1163     name = bprintf("%x#%x",src_id, dst_id);
1164
1165     manage_route(route_table, name, route_action, 0);
1166     free(name);
1167   }
1168 }
1169
1170 static void add_loopback(void)
1171 {
1172   int i;
1173
1174   /* Adding loopback if needed */
1175   for (i = 0; i < nb_workstation; i++) {
1176     if (!ROUTE(i, i).size) {
1177       if (!loopback)
1178         loopback = link_new(xbt_strdup("__MSG_loopback__"),
1179                                     498000000, NULL, 0.000015, NULL,
1180                                     SURF_LINK_ON, NULL,
1181                                     SURF_LINK_FATPIPE, NULL);
1182       ROUTE(i, i).size = 1;
1183       ROUTE(i, i).links = xbt_new0(link_KCCFLN05_t, 1);
1184       ROUTE(i, i).links[0] = loopback;
1185     }
1186   }
1187 }
1188
1189 static void add_route(void)
1190 {  
1191   xbt_ex_t e;
1192   int nb_link = 0;
1193   unsigned int cpt = 0;    
1194   int link_list_capacity = 0;
1195   link_KCCFLN05_t *link_list = NULL;
1196   xbt_dict_cursor_t cursor = NULL;
1197   char *key,*data, *end;
1198   const char *sep = "#";
1199   xbt_dynar_t links, keys;
1200   char* link = NULL;
1201
1202   if (routing_table == NULL) create_routing_table();
1203
1204   xbt_dict_foreach(route_table, cursor, key, data) {
1205     nb_link = 0;
1206     links = (xbt_dynar_t)data;
1207     keys = xbt_str_split_str(key, sep);
1208
1209     link_list_capacity = xbt_dynar_length(links);
1210     link_list = xbt_new(link_KCCFLN05_t, link_list_capacity);
1211
1212     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
1213     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
1214     /*ATTRIBUTES NOT USED ANYMORE. WILL BE REMOVED FROM MODEL.*/
1215     impact_on_src = 0;//atof(xbt_dynar_get_as(keys, 2, char*));
1216     impact_on_dst = 0; //atof(xbt_dynar_get_as(keys, 3, char*));
1217     impact_on_src_with_other_recv = 0;//atof(xbt_dynar_get_as(keys, 4, char*));
1218     impact_on_dst_with_other_send = 0;//atof(xbt_dynar_get_as(keys, 5, char*));
1219
1220     
1221     xbt_dynar_foreach (links, cpt, link) {
1222       TRY {
1223         link_list[nb_link++] = xbt_dict_get(link_set, link);
1224       }
1225       CATCH(e) {
1226         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
1227       }     
1228     }
1229     route_new(src_id, dst_id, link_list, nb_link,impact_on_src,
1230               impact_on_dst, impact_on_src_with_other_recv,
1231               impact_on_dst_with_other_send);
1232    }
1233
1234    xbt_dict_free(&route_table);
1235
1236 }
1237
1238 static void add_traces(void)
1239 {
1240    xbt_dynar_t trace_connect = NULL;
1241    unsigned int cpt;
1242    int connect_element, connect_kind;
1243    char *value, *trace_id, *connector_id;
1244    link_KCCFLN05_t link;
1245    cpu_KCCFLN05_t host = NULL;
1246    tmgr_trace_t trace;
1247    
1248    if (!traces_connect_list) return;
1249  
1250    /*for all trace connects parse them and update traces for hosts or links */
1251    xbt_dynar_foreach (traces_connect_list, cpt, value) {
1252      trace_connect = xbt_str_split_str(value, "#");
1253      trace_id        = xbt_dynar_get_as(trace_connect, 0, char*);
1254      connect_element = atoi(xbt_dynar_get_as(trace_connect, 1, char*)); 
1255      connect_kind    = atoi(xbt_dynar_get_as(trace_connect, 2, char*));
1256      connector_id    = xbt_dynar_get_as(trace_connect, 3, char*);
1257
1258      xbt_assert1((trace = xbt_dict_get_or_null(traces_set_list, trace_id)), "Trace %s undefined", trace_id);
1259
1260      if (connect_element == A_surfxml_trace_c_connect_element_HOST) {
1261         xbt_assert1((host = xbt_dict_get_or_null(workstation_set, connector_id)), "Host %s undefined", connector_id);
1262         switch (connect_kind) {
1263            case A_surfxml_trace_c_connect_kind_AVAILABILITY: host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); break;
1264            case A_surfxml_trace_c_connect_kind_POWER: host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); break;
1265         }
1266      }
1267      else {
1268         xbt_assert1((link = xbt_dict_get_or_null(link_set, connector_id)), "Link %s undefined", connector_id);
1269         switch (connect_kind) {
1270            case A_surfxml_trace_c_connect_kind_AVAILABILITY: link->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, link); break;
1271            case A_surfxml_trace_c_connect_kind_BANDWIDTH: link->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link); break;
1272            case A_surfxml_trace_c_connect_kind_LATENCY: link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link); break;
1273         }
1274      }
1275    }
1276
1277    xbt_dynar_free(&trace_connect);
1278    xbt_dynar_free(&traces_connect_list);
1279    xbt_dict_free(&traces_set_list); 
1280 }
1281
1282 static void define_callbacks(const char *file)
1283 {
1284   CDEBUG0(surf_parse, "Use the KCCFKN05 model");
1285                                    
1286   /* Adding callback functions */
1287   surf_parse_reset_parser();
1288   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_cpu_init);
1289   surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
1290   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_routers);
1291   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
1292   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
1293   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
1294   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
1295   surfxml_add_callback(STag_surfxml_platform_cb_list, &init_data);
1296   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
1297   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_loopback);
1298   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces);
1299   surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
1300   surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
1301   surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
1302   surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
1303   surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
1304   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
1305   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
1306   surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
1307   surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
1308   surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
1309 }
1310
1311 /**************************************/
1312 /********* Module  creation ***********/
1313 /**************************************/
1314
1315 static void model_init_internal(void)
1316 {
1317   s_surf_action_t action;
1318
1319   surf_workstation_model = xbt_new0(s_surf_workstation_model_t, 1);
1320
1321   surf_workstation_model->common_private =
1322       xbt_new0(s_surf_model_private_t, 1);
1323   surf_workstation_model->common_public =
1324       xbt_new0(s_surf_model_public_t, 1);
1325   surf_workstation_model->extension_public =
1326       xbt_new0(s_surf_workstation_model_extension_public_t, 1);
1327
1328   surf_workstation_model->common_public->states.ready_action_set =
1329       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1330   surf_workstation_model->common_public->states.running_action_set =
1331       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1332   surf_workstation_model->common_public->states.failed_action_set =
1333       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1334   surf_workstation_model->common_public->states.done_action_set =
1335       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1336
1337   surf_workstation_model->common_public->name_service = name_service;
1338   surf_workstation_model->common_public->get_resource_name =
1339       get_resource_name;
1340   surf_workstation_model->common_public->action_get_state =
1341       surf_action_get_state;
1342   surf_workstation_model->common_public->action_get_start_time =
1343       surf_action_get_start_time;
1344   surf_workstation_model->common_public->action_get_finish_time =
1345       surf_action_get_finish_time;
1346   surf_workstation_model->common_public->action_use = action_use;
1347   surf_workstation_model->common_public->action_free = action_free;
1348   surf_workstation_model->common_public->action_cancel = action_cancel;
1349   surf_workstation_model->common_public->action_recycle =
1350       action_recycle;
1351   surf_workstation_model->common_public->action_change_state =
1352       surf_action_change_state;
1353   surf_workstation_model->common_public->action_set_data =
1354       surf_action_set_data;
1355   surf_workstation_model->common_public->suspend = action_suspend;
1356   surf_workstation_model->common_public->resume = action_resume;
1357   surf_workstation_model->common_public->is_suspended =
1358       action_is_suspended;
1359   surf_workstation_model->common_public->set_max_duration =
1360       action_set_max_duration;
1361   surf_workstation_model->common_public->set_priority =
1362       action_set_priority;
1363   surf_workstation_model->common_public->name = "Workstation KCCFLN05";
1364
1365   surf_workstation_model->common_private->resource_used = resource_used;
1366   surf_workstation_model->common_private->share_resources =
1367       share_resources;
1368   surf_workstation_model->common_private->update_actions_state =
1369       update_actions_state;
1370   surf_workstation_model->common_private->update_resource_state =
1371       update_resource_state;
1372   surf_workstation_model->common_private->finalize = finalize;
1373
1374   surf_workstation_model->extension_public->execute = execute;
1375   surf_workstation_model->extension_public->sleep = action_sleep;
1376   surf_workstation_model->extension_public->get_state =
1377       resource_get_state;
1378   surf_workstation_model->extension_public->get_speed = get_speed;
1379   surf_workstation_model->extension_public->get_available_speed =
1380       get_available_speed;
1381
1382   surf_workstation_model->common_public->get_properties = get_properties;
1383
1384   surf_workstation_model->extension_public->communicate = communicate;
1385   surf_workstation_model->extension_public->execute_parallel_task =
1386       execute_parallel_task;
1387   surf_workstation_model->extension_public->get_route = get_route;
1388   surf_workstation_model->extension_public->get_route_size =
1389       get_route_size;
1390   surf_workstation_model->extension_public->get_link_name =
1391       get_link_name;
1392   surf_workstation_model->extension_public->get_link_bandwidth =
1393       get_link_bandwidth;
1394   surf_workstation_model->extension_public->get_link_latency =
1395       get_link_latency;
1396
1397   workstation_set = xbt_dict_new();
1398   router_set = xbt_dict_new();
1399   link_set = xbt_dict_new(); 
1400   if (!maxmin_system)
1401     maxmin_system = lmm_system_new();
1402 }
1403
1404 /**************************************/
1405 /*************** Generic **************/
1406 /**************************************/
1407 void surf_workstation_model_init_KCCFLN05(const char *filename)
1408 {
1409   xbt_assert0(!surf_cpu_model, "CPU model type already defined");
1410   xbt_assert0(!surf_network_model,
1411               "network model type already defined");
1412   model_init_internal();
1413   define_callbacks(filename);
1414
1415   xbt_dynar_push(model_list, &surf_workstation_model);
1416 }