Logo AND Algorithmique Numérique Distribuée

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