Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixup for commit 08ac8cf2900.
[simgrid.git] / src / surf / workstation_ptask_L07.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/ex.h"
8 #include "xbt/str.h"
9 #include "xbt/dict.h"
10 #include "surf_private.h"
11 #include "surf/surf_resource.h"
12 //#include "surf/surf_resource_lmm.h"
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   s_surf_resource_t generic_resource;   /* Do not move this field: must match surf_resource_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_resource_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   e_surf_workstation_model_type_t type; /* Do not move this field: must match cpu_L07_t */
41   lmm_constraint_t constraint;  /* Do not move this field: must match cpu_L07_t */
42   double lat_current;
43   tmgr_trace_event_t lat_event;
44   double bw_current;
45   tmgr_trace_event_t bw_event;
46   e_surf_resource_state_t state_current;
47   tmgr_trace_event_t state_event;
48 } s_link_L07_t, *link_L07_t;
49
50 /**************************************/
51 /*************** actions **************/
52 /**************************************/
53 typedef struct surf_action_workstation_L07 {
54   s_surf_action_t generic_action;
55   lmm_variable_t variable;
56   int workstation_nb;
57   cpu_L07_t *workstation_list;
58   double *computation_amount;
59   double *communication_amount;
60   double latency;
61   double rate;
62   int suspended;
63 } s_surf_action_workstation_L07_t, *surf_action_workstation_L07_t;
64
65
66 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_workstation);
67
68 static int ptask_host_count = 0;
69 static xbt_dict_t ptask_parallel_task_link_set = NULL;
70 lmm_system_t ptask_maxmin_system = NULL;
71
72
73 static void ptask_update_action_bound(surf_action_workstation_L07_t action)
74 {
75   int workstation_nb = action->workstation_nb;
76   double lat_current = 0.0;
77   double lat_bound = -1.0;
78   int i, j;
79   unsigned int cpt;
80   link_L07_t link;
81
82   for (i = 0; i < workstation_nb; i++) {
83     for (j = 0; j < workstation_nb; j++) {
84       xbt_dynar_t route=NULL;
85       routing_get_route_and_latency(surf_resource_name
86           (action->workstation_list[i]),
87           surf_resource_name(action->workstation_list[j]),
88           &route, NULL);
89
90       // FIXME do we really need to recompute the latency here?
91       double lat = 0.0;
92
93       if (action->communication_amount[i * workstation_nb + j] > 0) {
94         xbt_dynar_foreach(route, cpt, link) {
95           lat += link->lat_current;
96         }
97         lat_current =
98             MAX(lat_current,
99                 lat * action->communication_amount[i * workstation_nb +
100                                                    j]);
101       }
102     }
103   }
104   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
105   XBT_DEBUG("action (%p) : lat_bound = %g", action, lat_bound);
106   if ((action->latency == 0.0) && (action->suspended == 0)) {
107     if (action->rate < 0)
108       lmm_update_variable_bound(ptask_maxmin_system, action->variable,
109                                 lat_bound);
110     else
111       lmm_update_variable_bound(ptask_maxmin_system, action->variable,
112                                 min(action->rate, lat_bound));
113   }
114 }
115
116 /**************************************/
117 /******* Resource Public     **********/
118 /**************************************/
119
120 static int ptask_action_unref(surf_action_t action)
121 {
122   action->refcount--;
123
124   if (!action->refcount) {
125     xbt_swag_remove(action, action->state_set);
126     if (((surf_action_workstation_L07_t) action)->variable)
127       lmm_variable_free(ptask_maxmin_system,
128                         ((surf_action_workstation_L07_t)
129                          action)->variable);
130     free(((surf_action_workstation_L07_t) action)->workstation_list);
131     free(((surf_action_workstation_L07_t) action)->communication_amount);
132     free(((surf_action_workstation_L07_t) action)->computation_amount);
133     surf_action_free(&action);
134     return 1;
135   }
136   return 0;
137 }
138
139 static void ptask_action_cancel(surf_action_t action)
140 {
141   surf_action_state_set(action, SURF_ACTION_FAILED);
142   return;
143 }
144
145 /* action_change_state is inherited from the surf module */
146 /* action_set_data is inherited from the surf module */
147
148 static void ptask_action_suspend(surf_action_t action)
149 {
150   XBT_IN("(%p))", action);
151   if (((surf_action_workstation_L07_t) action)->suspended != 2) {
152     ((surf_action_workstation_L07_t) action)->suspended = 1;
153     lmm_update_variable_weight(ptask_maxmin_system,
154                                ((surf_action_workstation_L07_t)
155                                 action)->variable, 0.0);
156   }
157   XBT_OUT();
158 }
159
160 static void ptask_action_resume(surf_action_t action)
161 {
162   surf_action_workstation_L07_t act =
163       (surf_action_workstation_L07_t) action;
164
165   XBT_IN("(%p)", act);
166   if (act->suspended != 2) {
167     lmm_update_variable_weight(ptask_maxmin_system, act->variable, 1.0);
168     act->suspended = 0;
169   }
170   XBT_OUT();
171 }
172
173 static int ptask_action_is_suspended(surf_action_t action)
174 {
175   return (((surf_action_workstation_L07_t) action)->suspended == 1);
176 }
177
178 static void ptask_action_set_max_duration(surf_action_t action,
179                                           double duration)
180 {                               /* FIXME: should inherit */
181   XBT_IN("(%p,%g)", action, duration);
182   action->max_duration = duration;
183   XBT_OUT();
184 }
185
186
187 static void ptask_action_set_priority(surf_action_t action,
188                                       double priority)
189 {                               /* FIXME: should inherit */
190   XBT_IN("(%p,%g)", action, priority);
191   action->priority = priority;
192   XBT_OUT();
193 }
194
195 static double ptask_action_get_remains(surf_action_t action)
196 {
197   XBT_IN("(%p)", action);
198   return action->remains;
199   XBT_OUT();
200 }
201
202 /**************************************/
203 /******* Resource Private    **********/
204 /**************************************/
205
206 static int ptask_resource_used(void *resource_id)
207 {
208   /* We can freely cast as a link_L07_t because it has
209      the same prefix as cpu_L07_t */
210   return lmm_constraint_used(ptask_maxmin_system,
211                              ((link_L07_t) resource_id)->constraint);
212
213 }
214
215 static double ptask_share_resources(double now)
216 {
217   s_surf_action_workstation_L07_t s_action;
218   surf_action_workstation_L07_t action = NULL;
219
220   xbt_swag_t running_actions =
221       surf_workstation_model->states.running_action_set;
222   double min = generic_maxmin_share_resources(running_actions,
223                                               xbt_swag_offset(s_action,
224                                                               variable),
225                                               ptask_maxmin_system,
226                                               bottleneck_solve);
227
228   xbt_swag_foreach(action, running_actions) {
229     if (action->latency > 0) {
230       if (min < 0) {
231         min = action->latency;
232         XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
233                action->generic_action.start, min);
234       } else if (action->latency < min) {
235         min = action->latency;
236         XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
237                action->generic_action.start, min);
238       }
239     }
240   }
241
242   XBT_DEBUG("min value : %f", min);
243
244   return min;
245 }
246
247 static void ptask_update_actions_state(double now, double delta)
248 {
249   double deltap = 0.0;
250   surf_action_workstation_L07_t action = NULL;
251   surf_action_workstation_L07_t next_action = NULL;
252   xbt_swag_t running_actions =
253       surf_workstation_model->states.running_action_set;
254
255   xbt_swag_foreach_safe(action, next_action, running_actions) {
256     deltap = delta;
257     if (action->latency > 0) {
258       if (action->latency > deltap) {
259         double_update(&(action->latency), deltap);
260         deltap = 0.0;
261       } else {
262         double_update(&(deltap), action->latency);
263         action->latency = 0.0;
264       }
265       if ((action->latency == 0.0) && (action->suspended == 0)) {
266         ptask_update_action_bound(action);
267         lmm_update_variable_weight(ptask_maxmin_system, action->variable,
268                                    1.0);
269       }
270     }
271     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
272            action, action->generic_action.remains,
273            lmm_variable_getvalue(action->variable) * delta);
274     double_update(&(action->generic_action.remains),
275                   lmm_variable_getvalue(action->variable) * delta);
276
277     if (action->generic_action.max_duration != NO_MAX_DURATION)
278       double_update(&(action->generic_action.max_duration), delta);
279
280     XBT_DEBUG("Action (%p) : remains (%g).",
281            action, action->generic_action.remains);
282     if ((action->generic_action.remains <= 0) &&
283         (lmm_get_variable_weight(action->variable) > 0)) {
284       action->generic_action.finish = surf_get_clock();
285       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
286     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
287                (action->generic_action.max_duration <= 0)) {
288       action->generic_action.finish = surf_get_clock();
289       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
290     } else {
291       /* Need to check that none of the model has failed */
292       lmm_constraint_t cnst = NULL;
293       int i = 0;
294       void *constraint_id = NULL;
295
296       while ((cnst =
297               lmm_get_cnst_from_var(ptask_maxmin_system, action->variable,
298                                     i++))) {
299         constraint_id = lmm_constraint_id(cnst);
300
301 /*      if(((link_L07_t)constraint_id)->type== */
302 /*         SURF_WORKSTATION_RESOURCE_LINK) { */
303 /*        XBT_DEBUG("Checking for link %s (%p)", */
304 /*               ((link_L07_t)constraint_id)->name, */
305 /*               ((link_L07_t)constraint_id)); */
306 /*      } */
307 /*      if(((cpu_L07_t)constraint_id)->type== */
308 /*         SURF_WORKSTATION_RESOURCE_CPU) { */
309 /*        XBT_DEBUG("Checking for cpu %s (%p) : %s", */
310 /*               ((cpu_L07_t)constraint_id)->name, */
311 /*               ((cpu_L07_t)constraint_id), */
312 /*               ((cpu_L07_t)constraint_id)->state_current==SURF_CPU_OFF?"Off":"On"); */
313 /*      } */
314
315         if (((((link_L07_t) constraint_id)->type ==
316               SURF_WORKSTATION_RESOURCE_LINK) &&
317              (((link_L07_t) constraint_id)->state_current ==
318               SURF_RESOURCE_OFF)) ||
319             ((((cpu_L07_t) constraint_id)->type ==
320               SURF_WORKSTATION_RESOURCE_CPU) &&
321              (((cpu_L07_t) constraint_id)->state_current ==
322               SURF_RESOURCE_OFF))) {
323           XBT_DEBUG("Action (%p) Failed!!", action);
324           action->generic_action.finish = surf_get_clock();
325           surf_action_state_set((surf_action_t) action,
326                                 SURF_ACTION_FAILED);
327           break;
328         }
329       }
330     }
331   }
332   return;
333 }
334
335 static void ptask_update_resource_state(void *id,
336                                         tmgr_trace_event_t event_type,
337                                         double value, double date)
338 {
339   cpu_L07_t cpu = id;
340   link_L07_t nw_link = id;
341
342   if (nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
343     XBT_DEBUG("Updating link %s (%p)", surf_resource_name(nw_link), nw_link);
344     if (event_type == nw_link->bw_event) {
345       nw_link->bw_current = value;
346       lmm_update_constraint_bound(ptask_maxmin_system, nw_link->constraint,
347                                   nw_link->bw_current);
348       if (tmgr_trace_event_free(event_type))
349         nw_link->bw_event = NULL;
350     } else if (event_type == nw_link->lat_event) {
351       lmm_variable_t var = NULL;
352       surf_action_workstation_L07_t action = NULL;
353       lmm_element_t elem = NULL;
354
355       nw_link->lat_current = value;
356       while ((var = lmm_get_var_from_cnst
357               (ptask_maxmin_system, nw_link->constraint, &elem))) {
358
359
360         action = lmm_variable_id(var);
361         ptask_update_action_bound(action);
362       }
363       if (tmgr_trace_event_free(event_type))
364         nw_link->lat_event = NULL;
365
366     } else if (event_type == nw_link->state_event) {
367       if (value > 0)
368         nw_link->state_current = SURF_RESOURCE_ON;
369       else
370         nw_link->state_current = SURF_RESOURCE_OFF;
371       if (tmgr_trace_event_free(event_type))
372         nw_link->state_event = NULL;
373     } else {
374       XBT_CRITICAL("Unknown event ! \n");
375       xbt_abort();
376     }
377     return;
378   } else if (cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
379     XBT_DEBUG("Updating cpu %s (%p) with value %g", surf_resource_name(cpu),
380            cpu, value);
381     if (event_type == cpu->power_event) {
382       cpu->power_current = value;
383       lmm_update_constraint_bound(ptask_maxmin_system, cpu->constraint,
384                                   cpu->power_current * cpu->power_scale);
385       if (tmgr_trace_event_free(event_type))
386         cpu->power_event = NULL;
387     } else if (event_type == cpu->state_event) {
388       if (value > 0)
389         cpu->state_current = SURF_RESOURCE_ON;
390       else
391         cpu->state_current = SURF_RESOURCE_OFF;
392       if (tmgr_trace_event_free(event_type))
393         cpu->state_event = NULL;
394     } else {
395       XBT_CRITICAL("Unknown event ! \n");
396       xbt_abort();
397     }
398     return;
399   } else {
400     DIE_IMPOSSIBLE;
401   }
402   return;
403 }
404
405 static void ptask_finalize(void)
406 {
407   xbt_dict_free(&ptask_parallel_task_link_set);
408
409   surf_model_exit(surf_workstation_model);
410   surf_workstation_model = NULL;
411   surf_model_exit(surf_network_model);
412   surf_network_model = NULL;
413
414   ptask_host_count = 0;
415
416   if (ptask_maxmin_system) {
417     lmm_system_free(ptask_maxmin_system);
418     ptask_maxmin_system = NULL;
419   }
420 }
421
422 /**************************************/
423 /******* Resource Private    **********/
424 /**************************************/
425
426 static e_surf_resource_state_t ptask_resource_get_state(void *cpu)
427 {
428   return ((cpu_L07_t) cpu)->state_current;
429 }
430
431 static double ptask_get_speed(void *cpu, double load)
432 {
433   return load * (((cpu_L07_t) cpu)->power_scale);
434 }
435
436 static double ptask_get_available_speed(void *cpu)
437 {
438   return ((cpu_L07_t) cpu)->power_current;
439 }
440
441 static surf_action_t ptask_execute_parallel_task(int workstation_nb,
442                                                  void **workstation_list,
443                                                  double
444                                                  *computation_amount, double
445                                                  *communication_amount,
446                                                  double amount,
447                                                  double rate)
448 {
449   surf_action_workstation_L07_t action = NULL;
450   int i, j;
451   unsigned int cpt;
452   int nb_link = 0;
453   int nb_host = 0;
454   double latency = 0.0;
455
456   if (ptask_parallel_task_link_set == NULL)
457     ptask_parallel_task_link_set = xbt_dict_new();
458
459   xbt_dict_reset(ptask_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       link_L07_t link;
465       xbt_dynar_t route=NULL;
466       routing_get_route_and_latency(
467           surf_resource_name(workstation_list[i]),
468           surf_resource_name(workstation_list[j]),
469           &route,NULL); // FIXME: do we want to recompute the latency?
470       double lat = 0.0;
471
472       if (communication_amount[i * workstation_nb + j] > 0)
473         xbt_dynar_foreach(route, cpt, link) {
474         lat += link->lat_current;
475         xbt_dict_set(ptask_parallel_task_link_set,
476                      link->generic_resource.name, link, NULL);
477         }
478       latency = MAX(latency, lat);
479     }
480   }
481
482   nb_link = xbt_dict_length(ptask_parallel_task_link_set);
483   xbt_dict_reset(ptask_parallel_task_link_set);
484
485   for (i = 0; i < workstation_nb; i++)
486     if (computation_amount[i] > 0)
487       nb_host++;
488
489   action =
490       surf_action_new(sizeof(s_surf_action_workstation_L07_t), amount,
491                       surf_workstation_model, 0);
492   XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
493          action, workstation_nb, nb_link);
494   action->suspended = 0;        /* Should be useless because of the
495                                    calloc but it seems to help valgrind... */
496   action->workstation_nb = workstation_nb;
497   action->workstation_list = (cpu_L07_t *) workstation_list;
498   action->computation_amount = computation_amount;
499   action->communication_amount = communication_amount;
500   action->latency = latency;
501   action->rate = rate;
502
503   action->variable =
504       lmm_variable_new(ptask_maxmin_system, action, 1.0,
505                        (action->rate > 0) ? action->rate : -1.0,
506                        workstation_nb + nb_link);
507
508   if (action->latency > 0)
509     lmm_update_variable_weight(ptask_maxmin_system, action->variable, 0.0);
510
511   for (i = 0; i < workstation_nb; i++)
512     lmm_expand(ptask_maxmin_system,
513                ((cpu_L07_t) workstation_list[i])->constraint,
514                action->variable, computation_amount[i]);
515
516   for (i = 0; i < workstation_nb; i++) {
517     for (j = 0; j < workstation_nb; j++) {
518       link_L07_t link;
519       xbt_dynar_t route=NULL;
520       routing_get_route_and_latency(
521           surf_resource_name(workstation_list[i]),
522           surf_resource_name(workstation_list[j]),
523           &route,NULL);
524
525       if (communication_amount[i * workstation_nb + j] == 0.0)
526         continue;
527       xbt_dynar_foreach(route, cpt, link) {
528         lmm_expand_add(ptask_maxmin_system, link->constraint,
529                        action->variable,
530                        communication_amount[i * workstation_nb + j]);
531       }
532     }
533   }
534
535   if (nb_link + nb_host == 0) {
536     action->generic_action.cost = 1.0;
537     action->generic_action.remains = 0.0;
538   }
539
540   return (surf_action_t) action;
541 }
542
543 static surf_action_t ptask_execute(void *cpu, double size)
544 {
545   void **workstation_list = xbt_new0(void *, 1);
546   double *computation_amount = xbt_new0(double, 1);
547   double *communication_amount = xbt_new0(double, 1);
548
549   workstation_list[0] = cpu;
550   communication_amount[0] = 0.0;
551   computation_amount[0] = size;
552
553   return ptask_execute_parallel_task(1, workstation_list,
554                                      computation_amount,
555                                      communication_amount, 1, -1);
556 }
557
558 static surf_action_t ptask_communicate(void *src, void *dst, double size,
559                                        double rate)
560 {
561   void **workstation_list = xbt_new0(void *, 2);
562   double *computation_amount = xbt_new0(double, 2);
563   double *communication_amount = xbt_new0(double, 4);
564   surf_action_t res = NULL;
565
566   workstation_list[0] = src;
567   workstation_list[1] = dst;
568   communication_amount[1] = size;
569
570   res = ptask_execute_parallel_task(2, workstation_list,
571                                     computation_amount,
572                                     communication_amount, 1, rate);
573
574   return res;
575 }
576
577 static surf_action_t ptask_action_sleep(void *cpu, double duration)
578 {
579   surf_action_workstation_L07_t action = NULL;
580
581   XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
582
583   action = (surf_action_workstation_L07_t) ptask_execute(cpu, 1.0);
584   action->generic_action.max_duration = duration;
585   action->suspended = 2;
586   lmm_update_variable_weight(ptask_maxmin_system, action->variable, 0.0);
587
588   XBT_OUT();
589   return (surf_action_t) action;
590 }
591
592 static xbt_dynar_t ptask_get_route(void *src, void *dst) // FIXME: kill that callback kind?
593 {
594   xbt_dynar_t route=NULL;
595   routing_get_route_and_latency(
596       surf_resource_name(src), surf_resource_name(dst),
597       &route,NULL);
598   return route;
599 }
600
601 static double ptask_get_link_bandwidth(const void *link)
602 {
603   return ((link_L07_t) link)->bw_current;
604 }
605
606 static double ptask_get_link_latency(const void *link)
607 {
608   return ((link_L07_t) link)->lat_current;
609 }
610
611 static int ptask_link_shared(const void *link)
612 {
613   return lmm_constraint_is_shared(((link_L07_t) link)->constraint);
614 }
615
616 /**************************************/
617 /*** Resource Creation & Destruction **/
618 /**************************************/
619
620 static void* ptask_cpu_create_resource(const char *name, double power_scale,
621                                double power_initial,
622                                tmgr_trace_t power_trace,
623                                e_surf_resource_state_t state_initial,
624                                tmgr_trace_t state_trace,
625                                xbt_dict_t cpu_properties)
626 {
627   cpu_L07_t cpu = NULL;
628   xbt_assert(!surf_workstation_resource_by_name(name),
629               "Host '%s' declared several times in the platform file.",
630               name);
631
632   cpu = (cpu_L07_t) surf_resource_new(sizeof(s_cpu_L07_t),
633           surf_workstation_model, name,cpu_properties);
634
635   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
636   cpu->id = ptask_host_count++;
637
638   cpu->power_scale = power_scale;
639   xbt_assert(cpu->power_scale > 0, "Power has to be >0");
640
641   cpu->power_current = power_initial;
642   if (power_trace)
643     cpu->power_event =
644         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
645
646   cpu->state_current = state_initial;
647   if (state_trace)
648     cpu->state_event =
649         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
650
651   cpu->constraint =
652       lmm_constraint_new(ptask_maxmin_system, cpu,
653                          cpu->power_current * cpu->power_scale);
654
655   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, cpu);
656
657   return cpu;
658 }
659
660 static void ptask_parse_cpu_init(sg_platf_host_cbarg_t host)
661 {
662   ptask_cpu_create_resource(
663                   host->id,
664                   host->power_peak,
665                   host->power_scale,
666                   host->power_trace,
667                   host->initial_state,
668                   host->state_trace,
669                   host->properties);
670 }
671
672 static void* ptask_link_create_resource(const char *name,
673                                  double bw_initial,
674                                  tmgr_trace_t bw_trace,
675                                  double lat_initial,
676                                  tmgr_trace_t lat_trace,
677                                  e_surf_resource_state_t
678                                  state_initial,
679                                  tmgr_trace_t state_trace,
680                                  e_surf_link_sharing_policy_t
681                                  policy, xbt_dict_t properties)
682 {
683   link_L07_t nw_link = xbt_new0(s_link_L07_t, 1);
684   xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
685               "Link '%s' declared several times in the platform file.",
686               name);
687
688   nw_link->generic_resource.model = surf_workstation_model;
689   nw_link->generic_resource.properties = properties;
690   nw_link->generic_resource.name = xbt_strdup(name);
691   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
692   nw_link->bw_current = bw_initial;
693   if (bw_trace)
694     nw_link->bw_event =
695         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
696   nw_link->state_current = state_initial;
697   nw_link->lat_current = lat_initial;
698   if (lat_trace)
699     nw_link->lat_event =
700         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
701   if (state_trace)
702     nw_link->state_event =
703         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
704
705   nw_link->constraint =
706       lmm_constraint_new(ptask_maxmin_system, nw_link,
707                          nw_link->bw_current);
708
709   if (policy == SURF_LINK_FATPIPE)
710     lmm_constraint_shared(nw_link->constraint);
711
712   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
713   return nw_link;
714 }
715
716 static void ptask_parse_link_init(sg_platf_link_cbarg_t link)
717 {
718   if (link->policy == SURF_LINK_FULLDUPLEX) {
719     char *link_id;
720     link_id = bprintf("%s_UP", link->id);
721     ptask_link_create_resource(link_id,
722                                link->bandwidth,
723                                link->bandwidth_trace,
724                                link->latency,
725                                link->latency_trace,
726                                link->state,
727                                link->state_trace,
728                                link->policy,
729                                link->properties);
730     xbt_free(link_id);
731     link_id = bprintf("%s_DOWN", link->id);
732     ptask_link_create_resource(link_id,
733                                link->bandwidth,
734                                link->bandwidth_trace,
735                                link->latency,
736                                link->latency_trace,
737                                link->state,
738                                link->state_trace,
739                                link->policy,
740                                NULL); /* FIXME: We need to deep copy the
741                                        * properties or we won't be able to free
742                                        * it */
743     xbt_free(link_id);
744   } else {
745     ptask_link_create_resource(link->id,
746                                link->bandwidth,
747                                link->bandwidth_trace,
748                                link->latency,
749                                link->latency_trace,
750                                link->state,
751                                link->state_trace,
752                                link->policy,
753                                link->properties);
754   }
755
756   current_property_set = NULL;
757 }
758
759 static void ptask_add_traces(void)
760 {
761   xbt_dict_cursor_t cursor = NULL;
762   char *trace_name, *elm;
763
764   if (!trace_connect_list_host_avail)
765     return;
766
767   /* Connect traces relative to cpu */
768   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
769     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
770     cpu_L07_t host = surf_workstation_resource_by_name(elm);
771
772     xbt_assert(host, "Host %s undefined", elm);
773     xbt_assert(trace, "Trace %s undefined", trace_name);
774
775     host->state_event =
776         tmgr_history_add_trace(history, trace, 0.0, 0, host);
777   }
778
779   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
780     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
781     cpu_L07_t host = surf_workstation_resource_by_name(elm);
782
783     xbt_assert(host, "Host %s undefined", elm);
784     xbt_assert(trace, "Trace %s undefined", trace_name);
785
786     host->power_event =
787         tmgr_history_add_trace(history, trace, 0.0, 0, host);
788   }
789
790   /* Connect traces relative to network */
791   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
792     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
793     link_L07_t link =
794                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
795
796     xbt_assert(link, "Link %s undefined", elm);
797     xbt_assert(trace, "Trace %s undefined", trace_name);
798
799     link->state_event =
800         tmgr_history_add_trace(history, trace, 0.0, 0, link);
801   }
802
803   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
804     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
805     link_L07_t link =
806                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
807
808     xbt_assert(link, "Link %s undefined", elm);
809     xbt_assert(trace, "Trace %s undefined", trace_name);
810
811     link->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
812   }
813
814   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
815     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
816     link_L07_t link =
817                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
818
819     xbt_assert(link, "Link %s undefined", elm);
820     xbt_assert(trace, "Trace %s undefined", trace_name);
821
822     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
823   }
824 }
825
826 static void ptask_define_callbacks()
827 {
828   sg_platf_host_add_cb(ptask_parse_cpu_init);
829   sg_platf_link_add_cb(ptask_parse_link_init);
830   sg_platf_postparse_add_cb(ptask_add_traces);
831 }
832
833 /**************************************/
834 /********* Module  creation ***********/
835 /**************************************/
836
837 static void ptask_model_init_internal(void)
838 {
839   surf_workstation_model = surf_model_init();
840
841   surf_workstation_model->action_unref = ptask_action_unref;
842   surf_workstation_model->action_cancel = ptask_action_cancel;
843   surf_workstation_model->action_state_set = surf_action_state_set;
844   surf_workstation_model->suspend = ptask_action_suspend;
845   surf_workstation_model->resume = ptask_action_resume;
846   surf_workstation_model->is_suspended = ptask_action_is_suspended;
847   surf_workstation_model->set_max_duration = ptask_action_set_max_duration;
848   surf_workstation_model->set_priority = ptask_action_set_priority;
849   surf_workstation_model->get_remains = ptask_action_get_remains;
850   surf_workstation_model->name = "Workstation ptask_L07";
851
852   surf_workstation_model->model_private->resource_used =
853       ptask_resource_used;
854   surf_workstation_model->model_private->share_resources =
855       ptask_share_resources;
856   surf_workstation_model->model_private->update_actions_state =
857       ptask_update_actions_state;
858   surf_workstation_model->model_private->update_resource_state =
859       ptask_update_resource_state;
860   surf_workstation_model->model_private->finalize = ptask_finalize;
861
862
863   surf_workstation_model->extension.workstation.execute = ptask_execute;
864   surf_workstation_model->extension.workstation.sleep = ptask_action_sleep;
865   surf_workstation_model->extension.workstation.get_state =
866       ptask_resource_get_state;
867   surf_workstation_model->extension.workstation.get_speed =
868       ptask_get_speed;
869   surf_workstation_model->extension.workstation.get_available_speed =
870       ptask_get_available_speed;
871   surf_workstation_model->extension.workstation.communicate =
872       ptask_communicate;
873   surf_workstation_model->extension.workstation.get_route =
874       ptask_get_route;
875   surf_workstation_model->extension.workstation.execute_parallel_task =
876       ptask_execute_parallel_task;
877   surf_workstation_model->extension.workstation.get_link_bandwidth =
878       ptask_get_link_bandwidth;
879   surf_workstation_model->extension.workstation.get_link_latency =
880       ptask_get_link_latency;
881   surf_workstation_model->extension.workstation.link_shared =
882       ptask_link_shared;
883   surf_workstation_model->extension.workstation.get_properties =
884       surf_resource_properties;
885   surf_workstation_model->extension.workstation.link_create_resource =
886       ptask_link_create_resource;
887   surf_workstation_model->extension.workstation.cpu_create_resource =
888       ptask_cpu_create_resource;
889   surf_workstation_model->extension.workstation.add_traces =
890       ptask_add_traces;
891
892   if (!ptask_maxmin_system)
893     ptask_maxmin_system = lmm_system_new();
894
895   routing_model_create(sizeof(link_L07_t),
896                        ptask_link_create_resource("__loopback__",
897                                                   498000000, NULL,
898                                                   0.000015, NULL,
899                                                   SURF_RESOURCE_ON, NULL,
900                                                   SURF_LINK_FATPIPE, NULL));
901
902 }
903
904 /**************************************/
905 /*************** Generic **************/
906 /**************************************/
907 void surf_workstation_model_init_ptask_L07(void)
908 {
909   XBT_INFO("surf_workstation_model_init_ptask_L07");
910   xbt_assert(!surf_cpu_model, "CPU model type already defined");
911   xbt_assert(!surf_network_model, "network model type already defined");
912   surf_network_model = surf_model_init();
913   ptask_define_callbacks();
914   ptask_model_init_internal();
915   xbt_dynar_push(model_list, &surf_workstation_model);
916 }