Logo AND Algorithmique Numérique Distribuée

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