Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Handle FAT_PIPES.
[simgrid.git] / src / surf / workstation_ptask_L07.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 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 typedef enum {
13   SURF_WORKSTATION_RESOURCE_CPU,
14   SURF_WORKSTATION_RESOURCE_LINK,
15 } e_surf_workstation_resource_type_t;
16
17 /**************************************/
18 /********* cpu object *****************/
19 /**************************************/
20 typedef struct cpu_L07 {
21   surf_resource_t resource;                     /* Do not move this field */
22   e_surf_workstation_resource_type_t type;      /* Do not move this field */
23   char *name;                                   /* Do not move this field */
24   lmm_constraint_t constraint;                  /* Do not move this field */
25   double power_scale;
26   double power_current;
27   tmgr_trace_event_t power_event;
28   e_surf_cpu_state_t state_current;
29   tmgr_trace_event_t state_event;
30   int id;                       /* cpu and network card are a single object... */
31 } s_cpu_L07_t, *cpu_L07_t;
32
33 /**************************************/
34 /*********** network object ***********/
35 /**************************************/
36
37 typedef struct network_link_L07 {
38   surf_resource_t resource;                     /* Do not move this field */
39   e_surf_workstation_resource_type_t type;      /* Do not move this field */
40   char *name;                                   /* Do not move this field */
41   lmm_constraint_t constraint;                  /* Do not move this field */
42   double bw_current;
43   tmgr_trace_event_t bw_event;
44   e_surf_network_link_state_t state_current;
45   tmgr_trace_event_t state_event;
46 } s_network_link_L07_t, *network_link_L07_t;
47
48
49 typedef struct s_route_L07 {
50   network_link_L07_t *links;
51   int size;
52 } s_route_L07_t, *route_L07_t;
53
54 /**************************************/
55 /*************** actions **************/
56 /**************************************/
57 typedef struct surf_action_workstation_L07 {
58   s_surf_action_t generic_action;
59   lmm_variable_t variable;
60   double rate;
61   int suspended;
62 } s_surf_action_workstation_L07_t,
63   *surf_action_workstation_L07_t;
64
65
66 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_workstation);
67
68 static int nb_workstation = 0;
69 static s_route_L07_t *routing_table = NULL;
70 #define ROUTE(i,j) routing_table[(i)+(j)*nb_workstation]
71 static network_link_L07_t loopback = NULL;
72 static xbt_dict_t parallel_task_network_link_set = NULL;
73 lmm_system_t ptask_maxmin_system = NULL;
74
75 /**************************************/
76 /******* Resource Public     **********/
77 /**************************************/
78
79 static void *name_service(const char *name)
80 {
81   return xbt_dict_get_or_null(workstation_set, name);
82 }
83
84 static const char *get_resource_name(void *resource_id)
85 {
86   /* We can freely cast as a cpu_L07_t because it has the same
87      prefix as network_link_L07_t. However, only cpu_L07_t
88      will theoretically be given as an argument here. */
89   return ((cpu_L07_t) resource_id)->name;
90 }
91
92 /* action_get_state is inherited from the surf module */
93
94 static void action_use(surf_action_t action)
95 {
96   action->using++;
97   return;
98 }
99
100 static int action_free(surf_action_t action)
101 {
102   action->using--;
103
104   if (!action->using) {
105     xbt_swag_remove(action, action->state_set);
106     if (((surf_action_workstation_L07_t) action)->variable)
107       lmm_variable_free(ptask_maxmin_system,
108                         ((surf_action_workstation_L07_t) action)->
109                         variable);
110     free(action);
111     return 1;
112   }
113   return 0;
114 }
115
116 static void action_cancel(surf_action_t action)
117 {
118   surf_action_change_state(action, SURF_ACTION_FAILED);
119   return;
120 }
121
122 static void action_recycle(surf_action_t action)
123 {
124   DIE_IMPOSSIBLE;
125   return;
126 }
127
128 /* action_change_state is inherited from the surf module */
129 /* action_set_data is inherited from the surf module */
130
131 static void action_suspend(surf_action_t action)
132 {
133   XBT_IN1("(%p))",action);
134   if(((surf_action_workstation_L07_t) action)->suspended != 2) {
135     ((surf_action_workstation_L07_t) action)->suspended = 1;
136     lmm_update_variable_weight(ptask_maxmin_system,
137                                ((surf_action_workstation_L07_t)
138                                 action)->variable, 0.0);
139   }
140   XBT_OUT;
141 }
142
143 static void action_resume(surf_action_t action)
144 {
145   XBT_IN1("(%p)",action);
146   if(((surf_action_workstation_L07_t) action)->suspended !=2) {
147     lmm_update_variable_weight(ptask_maxmin_system,
148                                ((surf_action_workstation_L07_t)
149                                 action)->variable, 1.0);
150     ((surf_action_workstation_L07_t) action)->suspended = 0;
151   }
152   XBT_OUT;
153 }
154
155 static int action_is_suspended(surf_action_t action)
156 {
157   return (((surf_action_workstation_L07_t) action)->suspended==1);
158 }
159
160 static void action_set_max_duration(surf_action_t action, double duration)
161 {                               /* FIXME: should inherit */
162   XBT_IN2("(%p,%g)",action,duration);
163   action->max_duration = duration;
164   XBT_OUT;
165 }
166
167
168 static void action_set_priority(surf_action_t action, double priority)
169 {                               /* FIXME: should inherit */
170   XBT_IN2("(%p,%g)",action,priority);
171   action->priority = priority;
172   XBT_OUT;
173 }
174
175 /**************************************/
176 /******* Resource Private    **********/
177 /**************************************/
178
179 static int resource_used(void *resource_id)
180 {
181   /* We can freely cast as a network_link_L07_t because it has
182      the same prefix as cpu_L07_t */
183   return lmm_constraint_used(ptask_maxmin_system,
184                              ((network_link_L07_t) resource_id)->
185                              constraint);
186
187 }
188
189 static double share_resources(double now)
190 {
191   s_surf_action_workstation_L07_t s_action;
192
193   xbt_swag_t running_actions = 
194     surf_workstation_resource->common_public->states.running_action_set;
195   double min = 
196     generic_maxmin_share_resources2(running_actions,
197                                     xbt_swag_offset(s_action, variable),
198                                     ptask_maxmin_system, bottleneck_solve);
199   
200   DEBUG1("min value : %f",min);
201
202   return min;
203 }
204
205 static void update_actions_state(double now, double delta)
206 {
207   surf_action_workstation_L07_t action = NULL;
208   surf_action_workstation_L07_t next_action = NULL;
209   xbt_swag_t running_actions =
210       surf_workstation_resource->common_public->states.running_action_set;
211
212   xbt_swag_foreach_safe(action, next_action, running_actions) {
213     DEBUG3("Action (%p) : remains (%g) updated by %g.",
214            action, action->generic_action.remains,
215            lmm_variable_getvalue(action->variable) * delta);
216     double_update(&(action->generic_action.remains),
217                   lmm_variable_getvalue(action->variable) * delta);
218
219     if (action->generic_action.max_duration != NO_MAX_DURATION)
220       double_update(&(action->generic_action.max_duration), delta);
221
222     if ((action->generic_action.remains <= 0) && 
223         (lmm_get_variable_weight(action->variable)>0)) {
224       action->generic_action.finish = surf_get_clock();
225       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
226     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
227                (action->generic_action.max_duration <= 0)) {
228       action->generic_action.finish = surf_get_clock();
229       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
230     } else {
231       /* Need to check that none of the resource has failed */
232       lmm_constraint_t cnst = NULL;
233       int i = 0;
234       void *constraint_id = NULL;
235
236       while ((cnst =
237               lmm_get_cnst_from_var(ptask_maxmin_system, action->variable,
238                                     i++))) {
239         constraint_id = lmm_constraint_id(cnst);
240
241 /*      if(((network_link_L07_t)constraint_id)->type== */
242 /*         SURF_WORKSTATION_RESOURCE_LINK) { */
243 /*        DEBUG2("Checking for link %s (%p)", */
244 /*               ((network_link_L07_t)constraint_id)->name, */
245 /*               ((network_link_L07_t)constraint_id)); */
246 /*      } */
247 /*      if(((cpu_L07_t)constraint_id)->type== */
248 /*         SURF_WORKSTATION_RESOURCE_CPU) { */
249 /*        DEBUG3("Checking for cpu %s (%p) : %s", */
250 /*               ((cpu_L07_t)constraint_id)->name, */
251 /*               ((cpu_L07_t)constraint_id), */
252 /*               ((cpu_L07_t)constraint_id)->state_current==SURF_CPU_OFF?"Off":"On"); */
253 /*      } */
254
255         if(((((network_link_L07_t)constraint_id)->type==
256              SURF_WORKSTATION_RESOURCE_LINK) &&
257             (((network_link_L07_t)constraint_id)->state_current==
258              SURF_NETWORK_LINK_OFF)) ||
259            ((((cpu_L07_t)constraint_id)->type==
260              SURF_WORKSTATION_RESOURCE_CPU) &&
261             (((cpu_L07_t)constraint_id)->state_current==
262              SURF_CPU_OFF))) {
263           DEBUG1("Action (%p) Failed!!",action);
264           action->generic_action.finish = surf_get_clock();
265           surf_action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
266           break;
267         }
268       }
269     }
270   }
271   return;
272 }
273
274 static void update_resource_state(void *id,
275                                   tmgr_trace_event_t event_type,
276                                   double value)
277 {
278   cpu_L07_t cpu = id;
279   network_link_L07_t nw_link = id ;
280
281   if(nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
282     DEBUG2("Updating link %s (%p)",nw_link->name,nw_link);
283     if (event_type == nw_link->bw_event) {
284       nw_link->bw_current = value;
285       lmm_update_constraint_bound(ptask_maxmin_system, nw_link->constraint,
286                                   nw_link->bw_current);
287     } else if (event_type == nw_link->state_event) {
288       if (value > 0)
289         nw_link->state_current = SURF_NETWORK_LINK_ON;
290       else
291         nw_link->state_current = SURF_NETWORK_LINK_OFF;
292     } else {
293       CRITICAL0("Unknown event ! \n");
294       xbt_abort();
295     }
296     return;
297   } else if(cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
298     DEBUG3("Updating cpu %s (%p) with value %g",cpu->name,cpu,value);
299     if (event_type == cpu->power_event) {
300       cpu->power_current = value;
301       lmm_update_constraint_bound(ptask_maxmin_system, cpu->constraint,
302                                   cpu->power_current);
303     } else if (event_type == cpu->state_event) {
304       if (value > 0)
305         cpu->state_current = SURF_CPU_ON;
306       else
307         cpu->state_current = SURF_CPU_OFF;
308     } else {
309       CRITICAL0("Unknown event ! \n");
310       xbt_abort();
311     }
312     return;
313   } else {
314     DIE_IMPOSSIBLE;
315   }
316   return;
317 }
318
319 static void finalize(void)
320 {
321   int i,j;
322
323   xbt_dict_free(&network_link_set);
324   xbt_dict_free(&workstation_set);
325   if (parallel_task_network_link_set != NULL) {
326     xbt_dict_free(&parallel_task_network_link_set);
327   }
328   xbt_swag_free(surf_workstation_resource->common_public->states.
329                 ready_action_set);
330   xbt_swag_free(surf_workstation_resource->common_public->states.
331                 running_action_set);
332   xbt_swag_free(surf_workstation_resource->common_public->states.
333                 failed_action_set);
334   xbt_swag_free(surf_workstation_resource->common_public->states.
335                 done_action_set);
336
337   free(surf_workstation_resource->common_public);
338   free(surf_workstation_resource->common_private);
339   free(surf_workstation_resource->extension_public);
340
341   free(surf_workstation_resource);
342   surf_workstation_resource = NULL;
343
344   for (i = 0; i < nb_workstation; i++)
345     for (j = 0; j < nb_workstation; j++)
346       free(ROUTE(i, j).links);
347   free(routing_table);
348   routing_table = NULL;
349   nb_workstation = 0;
350
351   if (ptask_maxmin_system) {
352     lmm_system_free(ptask_maxmin_system);
353     ptask_maxmin_system = NULL;
354   }
355 }
356
357 /**************************************/
358 /******* Resource Private    **********/
359 /**************************************/
360
361 static e_surf_cpu_state_t resource_get_state(void *cpu)
362 {
363   return ((cpu_L07_t) cpu)->state_current;
364 }
365
366 static double get_speed(void *cpu, double load)
367 {
368   return load*(((cpu_L07_t) cpu)->power_scale);
369 }
370
371 static double get_available_speed(void *cpu)
372 {
373   return ((cpu_L07_t) cpu)->power_current;
374 }
375
376 static surf_action_t execute_parallel_task(int workstation_nb,
377                                            void **workstation_list, 
378                                            double *computation_amount, 
379                                            double *communication_amount,
380                                            double amount,
381                                            double rate)
382 {
383   surf_action_workstation_L07_t action = NULL;
384   int i, j, k;
385   int nb_link = 0;
386   int nb_host = 0;
387
388   if (parallel_task_network_link_set == NULL) {
389     parallel_task_network_link_set = xbt_dict_new_ext(workstation_nb * workstation_nb * 10);
390   }
391   
392   /* Compute the number of affected resources... */
393   for(i=0; i< workstation_nb; i++) {
394     for(j=0; j< workstation_nb; j++) {
395       cpu_L07_t card_src = workstation_list[i];
396       cpu_L07_t card_dst = workstation_list[j];
397       int route_size = ROUTE(card_src->id, card_dst->id).size;
398       network_link_L07_t *route = ROUTE(card_src->id, card_dst->id).links;
399       
400       if(communication_amount[i*workstation_nb+j]>0)
401         for(k=0; k< route_size; k++) {
402           xbt_dict_set(parallel_task_network_link_set, route[k]->name, route[k], NULL);
403         }
404     }
405   }
406   nb_link = xbt_dict_length(parallel_task_network_link_set);
407   xbt_dict_reset(parallel_task_network_link_set);
408
409
410   for (i = 0; i<workstation_nb; i++)
411     if(computation_amount[i]>0) nb_host++;
412  
413
414   if(nb_link + nb_host == 0) /* was workstation_nb... */
415     return NULL;
416
417   action = xbt_new0(s_surf_action_workstation_L07_t, 1);
418   DEBUG3("Creating a parallel task (%p) with %d cpus and %d links.",
419          action, nb_host,  nb_link);
420   action->generic_action.using = 1;
421   action->generic_action.cost = amount;
422   action->generic_action.remains = amount;
423   action->generic_action.max_duration = NO_MAX_DURATION;
424   action->generic_action.start = -1.0;
425   action->generic_action.finish = -1.0;
426   action->generic_action.resource_type =
427       (surf_resource_t) surf_workstation_resource;
428   action->suspended = 0;  /* Should be useless because of the
429                              calloc but it seems to help valgrind... */
430   action->generic_action.state_set =
431       surf_workstation_resource->common_public->states.running_action_set;
432
433   xbt_swag_insert(action, action->generic_action.state_set);
434   action->rate = rate;
435
436   if(action->rate>0)
437     action->variable = lmm_variable_new(ptask_maxmin_system, action, 1.0, -1.0,
438                                         nb_host + nb_link);
439   else   
440     action->variable = lmm_variable_new(ptask_maxmin_system, action, 1.0, action->rate,
441                                         nb_host + nb_link);
442
443   for (i = 0; i<workstation_nb; i++)
444     if(computation_amount[i]>0)
445       lmm_expand(ptask_maxmin_system, ((cpu_L07_t) workstation_list[i])->constraint, 
446                  action->variable, computation_amount[i]);
447
448   for (i=0; i<workstation_nb; i++) {
449     for(j=0; j< workstation_nb; j++) {
450       cpu_L07_t card_src = workstation_list[i];
451       cpu_L07_t card_dst = workstation_list[j];
452       int route_size = ROUTE(card_src->id, card_dst->id).size;
453       network_link_L07_t *route = ROUTE(card_src->id, card_dst->id).links;
454       
455       for(k=0; k< route_size; k++) {
456         if(communication_amount[i*workstation_nb+j]>0) {
457           lmm_expand_add(ptask_maxmin_system, route[k]->constraint, 
458                        action->variable, communication_amount[i*workstation_nb+j]);
459         }
460       }
461     }
462   }
463   
464   return (surf_action_t) action;
465 }
466
467 static surf_action_t execute(void *cpu, double size)
468 {
469   double val = 0.0;
470
471   return execute_parallel_task(1, &cpu, &size, &val, 1, -1);
472 }
473
474 static surf_action_t communicate(void *src, void *dst, double size, double rate)
475 {
476   void **workstation_list = xbt_new0(void*,2);
477   double *computation_amount = xbt_new0(double,2);
478   double *communication_amount = xbt_new0(double,4);
479   surf_action_t res = NULL;
480
481   workstation_list[0]=src;
482   workstation_list[1]=src;
483   communication_amount[1] = size;
484
485   res = execute_parallel_task(2, workstation_list, 
486                               computation_amount, communication_amount,
487                               1, rate);
488   
489   free(computation_amount);
490   free(communication_amount);
491   free(workstation_list);
492
493   return res;
494 }
495
496 static surf_action_t action_sleep(void *cpu, double duration)
497 {
498   surf_action_workstation_L07_t action = NULL;
499
500   XBT_IN2("(%s,%g)",((cpu_L07_t)cpu)->name,duration);
501
502   action = (surf_action_workstation_L07_t) execute(cpu, 1.0);
503   action->generic_action.max_duration = duration;
504   action->suspended = 2;
505   lmm_update_variable_weight(ptask_maxmin_system, action->variable, 0.0);
506
507   XBT_OUT;
508   return (surf_action_t) action;
509 }
510
511 /* returns an array of network_link_L07_t */
512 static const void** get_route(void *src, void *dst) {
513   cpu_L07_t card_src = src;
514   cpu_L07_t card_dst = dst;
515   route_L07_t route = &(ROUTE(card_src->id, card_dst->id));
516
517   return (const void**) route->links;
518 }
519
520 static int get_route_size(void *src, void *dst) {
521   cpu_L07_t card_src = src;
522   cpu_L07_t card_dst = dst;
523   route_L07_t route = &(ROUTE(card_src->id, card_dst->id));
524   return route->size;
525 }
526
527 static const char *get_link_name(const void *link) {
528   return ((network_link_L07_t) link)->name;
529 }
530
531 static double get_link_bandwidth(const void *link) {
532   return ((network_link_L07_t) link)->bw_current;
533 }
534
535 static double get_link_latency(const void *link) {
536   xbt_assert0(0,"This model does not implement latencies");  
537 }
538
539 /**************************************/
540 /*** Resource Creation & Destruction **/
541 /**************************************/
542
543 static void cpu_free(void *cpu)
544 {
545   free(((cpu_L07_t) cpu)->name);
546   free(cpu);
547 }
548
549 static cpu_L07_t cpu_new(const char *name, double power_scale,
550                               double power_initial,
551                               tmgr_trace_t power_trace,
552                               e_surf_cpu_state_t state_initial,
553                               tmgr_trace_t state_trace)
554 {
555   cpu_L07_t cpu = xbt_new0(s_cpu_L07_t, 1);
556
557   cpu->resource = (surf_resource_t) surf_workstation_resource;
558   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
559   cpu->name = xbt_strdup(name);
560   cpu->id = nb_workstation++;
561
562   cpu->power_scale = power_scale;
563   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
564
565   cpu->power_current = power_initial;
566   if (power_trace)
567     cpu->power_event =
568         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
569
570   cpu->state_current = state_initial;
571   if (state_trace)
572     cpu->state_event =
573         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
574
575   cpu->constraint =
576       lmm_constraint_new(ptask_maxmin_system, cpu,
577                          cpu->power_current * cpu->power_scale);
578
579   xbt_dict_set(workstation_set, name, cpu, cpu_free);
580
581   return cpu;
582 }
583
584 static void parse_cpu(void)
585 {
586   double power_scale = 0.0;
587   double power_initial = 0.0;
588   tmgr_trace_t power_trace = NULL;
589   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
590   tmgr_trace_t state_trace = NULL;
591
592   surf_parse_get_double(&power_scale, A_surfxml_cpu_power);
593   surf_parse_get_double(&power_initial, A_surfxml_cpu_availability);
594   surf_parse_get_trace(&power_trace, A_surfxml_cpu_availability_file);
595
596   xbt_assert0((A_surfxml_cpu_state == A_surfxml_cpu_state_ON) ||
597               (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF),
598               "Invalid state");
599   if (A_surfxml_cpu_state == A_surfxml_cpu_state_ON)
600     state_initial = SURF_CPU_ON;
601   if (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF)
602     state_initial = SURF_CPU_OFF;
603   surf_parse_get_trace(&state_trace, A_surfxml_cpu_state_file);
604   
605   cpu_new(A_surfxml_cpu_name, power_scale, power_initial, power_trace,
606           state_initial, state_trace);
607 }
608
609 static void create_routing_table(void)
610 {
611   routing_table =
612       xbt_new0(s_route_L07_t, nb_workstation * nb_workstation);
613 }
614
615 static void network_link_free(void *nw_link)
616 {
617   free(((network_link_L07_t) nw_link)->name);
618   free(nw_link);
619 }
620
621 static network_link_L07_t network_link_new(char *name,
622                                                 double bw_initial,
623                                                 tmgr_trace_t bw_trace,
624                                                 e_surf_network_link_state_t
625                                                 state_initial,
626                                                 tmgr_trace_t state_trace,
627                                                 e_surf_network_link_sharing_policy_t policy)
628 {
629   network_link_L07_t nw_link = xbt_new0(s_network_link_L07_t, 1);
630
631
632   nw_link->resource = (surf_resource_t) surf_workstation_resource;
633   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
634   nw_link->name = name;
635   nw_link->bw_current = bw_initial;
636   if (bw_trace)
637     nw_link->bw_event =
638         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
639   nw_link->state_current = state_initial;
640   if (state_trace)
641     nw_link->state_event =
642         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
643
644   nw_link->constraint =
645       lmm_constraint_new(ptask_maxmin_system, nw_link, nw_link->bw_current);
646
647   if(policy == SURF_NETWORK_LINK_FATPIPE)
648     lmm_constraint_shared(nw_link->constraint);
649
650   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
651
652   return nw_link;
653 }
654
655 static void parse_network_link(void)
656 {
657   char *name;
658   double bw_initial;
659   tmgr_trace_t bw_trace;
660   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
661   e_surf_network_link_sharing_policy_t policy_initial = SURF_NETWORK_LINK_SHARED;
662   tmgr_trace_t state_trace;
663
664   name = xbt_strdup(A_surfxml_network_link_name);
665   surf_parse_get_double(&bw_initial,A_surfxml_network_link_bandwidth);
666   surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
667
668   xbt_assert0((A_surfxml_network_link_state==A_surfxml_network_link_state_ON)||
669               (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF),
670               "Invalid state");
671   if (A_surfxml_network_link_state==A_surfxml_network_link_state_ON) 
672     state_initial = SURF_NETWORK_LINK_ON;
673   else if (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF) 
674     state_initial = SURF_NETWORK_LINK_OFF;
675
676   if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_SHARED) 
677     policy_initial = SURF_NETWORK_LINK_SHARED;
678   else if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_FATPIPE) 
679     policy_initial = SURF_NETWORK_LINK_FATPIPE;
680
681   surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
682
683   network_link_new(name, bw_initial, bw_trace, state_initial, state_trace,
684                    policy_initial);
685 }
686
687 static void route_new(int src_id, int dst_id, network_link_L07_t *link_list, int nb_link)
688 {
689   route_L07_t route = &(ROUTE(src_id, dst_id));
690
691   route->size = nb_link;
692   route->links = link_list = xbt_realloc(link_list, sizeof(network_link_L07_t) * nb_link);
693 }
694
695 static int nb_link;
696 static int link_list_capacity;
697 static network_link_L07_t *link_list = NULL;
698 static int src_id = -1;
699 static int dst_id = -1;
700
701 static void parse_route_set_endpoints(void)
702 {
703   cpu_L07_t cpu_tmp = NULL;
704  
705   cpu_tmp = (cpu_L07_t) name_service(A_surfxml_route_src);
706   xbt_assert1(cpu_tmp,"Invalid cpu %s",A_surfxml_route_src);
707   if(cpu_tmp != NULL) src_id = cpu_tmp->id;
708  
709   cpu_tmp = (cpu_L07_t) name_service(A_surfxml_route_dst);
710   xbt_assert1(cpu_tmp,"Invalid cpu %s",A_surfxml_route_dst);
711   if(cpu_tmp != NULL)  dst_id = cpu_tmp->id;
712
713   nb_link = 0;
714   link_list_capacity = 1; 
715   link_list = xbt_new(network_link_L07_t, link_list_capacity);
716 }
717
718 static void parse_route_elem(void)
719 {
720   xbt_ex_t e;
721   if (nb_link == link_list_capacity) {
722     link_list_capacity *= 2;
723     link_list = xbt_realloc(link_list, (link_list_capacity) * sizeof(network_link_L07_t));
724   }
725   TRY {
726      link_list[nb_link++] = xbt_dict_get(network_link_set, A_surfxml_route_element_name);
727   } CATCH(e) {
728      RETHROW1("Link %s not found (dict raised this exception: %s)",A_surfxml_route_element_name);
729   }
730 }
731
732 static void parse_route_set_route(void)
733 {
734   if( src_id != -1 && dst_id != -1 )
735   route_new(src_id, dst_id, link_list, nb_link);
736 }
737
738 static void parse_file(const char *file)
739 {
740   int i;
741
742   /* Figuring out the cpus */
743   surf_parse_reset_parser();
744   ETag_surfxml_cpu_fun = parse_cpu;
745   surf_parse_open(file);
746   xbt_assert1((!surf_parse()), "Parse error in %s", file);
747   surf_parse_close();
748
749   create_routing_table();
750
751   /* Figuring out the network links */
752   surf_parse_reset_parser();
753   ETag_surfxml_network_link_fun = parse_network_link;
754   surf_parse_open(file);
755   xbt_assert1((!surf_parse()), "Parse error in %s", file);
756   surf_parse_close();
757
758   /* Building the routes */
759   surf_parse_reset_parser();
760   STag_surfxml_route_fun = parse_route_set_endpoints;
761   ETag_surfxml_route_element_fun = parse_route_elem;
762   ETag_surfxml_route_fun = parse_route_set_route;
763   surf_parse_open(file);
764   xbt_assert1((!surf_parse()), "Parse error in %s", file);
765   surf_parse_close();
766
767   /* Adding loopback if needed */    
768   for (i = 0; i < nb_workstation; i++) 
769     if(!ROUTE(i,i).size) {
770       if(!loopback)
771         loopback = network_link_new(xbt_strdup("__MSG_loopback__"), 
772                                    498000000, NULL, 
773                                    SURF_NETWORK_LINK_ON, NULL,
774                                    SURF_NETWORK_LINK_FATPIPE);
775
776       ROUTE(i,i).size=1;
777       ROUTE(i,i).links = xbt_new0(network_link_L07_t, 1);
778       ROUTE(i,i).links[0] = loopback;
779     }
780 }
781
782 /**************************************/
783 /********* Module  creation ***********/
784 /**************************************/
785
786 static void resource_init_internal(void)
787 {
788   s_surf_action_t action;
789
790   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
791
792   surf_workstation_resource->common_private =
793       xbt_new0(s_surf_resource_private_t, 1);
794   surf_workstation_resource->common_public =
795       xbt_new0(s_surf_resource_public_t, 1);
796   surf_workstation_resource->extension_public =
797       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
798
799   surf_workstation_resource->common_public->states.ready_action_set =
800       xbt_swag_new(xbt_swag_offset(action, state_hookup));
801   surf_workstation_resource->common_public->states.running_action_set =
802       xbt_swag_new(xbt_swag_offset(action, state_hookup));
803   surf_workstation_resource->common_public->states.failed_action_set =
804       xbt_swag_new(xbt_swag_offset(action, state_hookup));
805   surf_workstation_resource->common_public->states.done_action_set =
806       xbt_swag_new(xbt_swag_offset(action, state_hookup));
807
808   surf_workstation_resource->common_public->name_service = name_service;
809   surf_workstation_resource->common_public->get_resource_name = get_resource_name;
810   surf_workstation_resource->common_public->action_get_state = surf_action_get_state;
811   surf_workstation_resource->common_public->action_get_start_time =
812       surf_action_get_start_time;
813   surf_workstation_resource->common_public->action_get_finish_time =
814       surf_action_get_finish_time;
815   surf_workstation_resource->common_public->action_use = action_use;
816   surf_workstation_resource->common_public->action_free = action_free;
817   surf_workstation_resource->common_public->action_cancel = action_cancel;
818   surf_workstation_resource->common_public->action_recycle = action_recycle;
819   surf_workstation_resource->common_public->action_change_state = surf_action_change_state;
820   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
821   surf_workstation_resource->common_public->suspend = action_suspend;
822   surf_workstation_resource->common_public->resume = action_resume;
823   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
824   surf_workstation_resource->common_public->set_max_duration = action_set_max_duration;
825   surf_workstation_resource->common_public->set_priority = action_set_priority;
826   surf_workstation_resource->common_public->name = "Workstation ptask_L07";
827
828   surf_workstation_resource->common_private->resource_used = resource_used;
829   surf_workstation_resource->common_private->share_resources = share_resources;
830   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
831   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
832   surf_workstation_resource->common_private->finalize = finalize;
833
834   surf_workstation_resource->extension_public->execute = execute;
835   surf_workstation_resource->extension_public->sleep = action_sleep;
836   surf_workstation_resource->extension_public->get_state = resource_get_state;
837   surf_workstation_resource->extension_public->get_speed = get_speed;
838   surf_workstation_resource->extension_public->get_available_speed = get_available_speed;
839   surf_workstation_resource->extension_public->communicate = communicate;
840   surf_workstation_resource->extension_public->execute_parallel_task = execute_parallel_task;
841   surf_workstation_resource->extension_public->get_route = get_route;
842   surf_workstation_resource->extension_public->get_route_size = get_route_size;
843   surf_workstation_resource->extension_public->get_link_name = get_link_name;
844   surf_workstation_resource->extension_public->get_link_bandwidth = get_link_bandwidth;
845   surf_workstation_resource->extension_public->get_link_latency = get_link_latency;
846
847   workstation_set  = xbt_dict_new();
848   network_link_set = xbt_dict_new();
849
850   if(!ptask_maxmin_system)  
851     ptask_maxmin_system = lmm_system_new();
852 }
853
854 /**************************************/
855 /*************** Generic **************/
856 /**************************************/
857 void surf_workstation_resource_init_ptask_L07(const char *filename)
858 {
859   xbt_assert0(!surf_cpu_resource, "CPU resource type already defined");
860   xbt_assert0(!surf_network_resource, "network resource type already defined");
861   resource_init_internal();
862   parse_file(filename);
863
864   update_resource_description(surf_workstation_resource_description,
865                               surf_workstation_resource_description_size,
866                               "ptask_L07",
867                               (surf_resource_t) surf_workstation_resource);
868   xbt_dynar_push(resource_list, &surf_workstation_resource);
869 }