Logo AND Algorithmique Numérique Distribuée

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