Logo AND Algorithmique Numérique Distribuée

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