Logo AND Algorithmique Numérique Distribuée

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