Logo AND Algorithmique Numérique Distribuée

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