Logo AND Algorithmique Numérique Distribuée

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