Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix cluster without backbone.
[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
12 typedef enum {
13   SURF_WORKSTATION_RESOURCE_CPU,
14   SURF_WORKSTATION_RESOURCE_LINK,
15 } e_surf_workstation_model_type_t;
16
17 /**************************************/
18 /********* cpu object *****************/
19 /**************************************/
20 typedef struct cpu_L07 {
21   s_surf_resource_t generic_resource;   /* Do not move this field: must match surf_resource_t */
22   e_surf_workstation_model_type_t type; /* Do not move this field: must match link_L07_t */
23   lmm_constraint_t constraint;  /* Do not move this field: must match link_L07_t */
24   double power_scale;
25   double power_current;
26   tmgr_trace_event_t power_event;
27   e_surf_resource_state_t state_current;
28   tmgr_trace_event_t state_event;
29   int id;                       /* cpu and network card are a single object... */
30 } s_cpu_L07_t, *cpu_L07_t;
31
32 /**************************************/
33 /*********** network object ***********/
34 /**************************************/
35
36 typedef struct link_L07 {
37   s_surf_resource_t generic_resource;   /* Do not move this field: must match surf_resource_t */
38   e_surf_workstation_model_type_t type; /* Do not move this field: must match cpu_L07_t */
39   lmm_constraint_t constraint;  /* Do not move this field: must match cpu_L07_t */
40   double lat_current;
41   tmgr_trace_event_t lat_event;
42   double bw_current;
43   tmgr_trace_event_t bw_event;
44   e_surf_resource_state_t state_current;
45   tmgr_trace_event_t state_event;
46 } s_link_L07_t, *link_L07_t;
47
48 /**************************************/
49 /*************** actions **************/
50 /**************************************/
51 typedef struct surf_action_workstation_L07 {
52   s_surf_action_t generic_action;
53   lmm_variable_t variable;
54   int workstation_nb;
55   cpu_L07_t *workstation_list;
56   double *computation_amount;
57   double *communication_amount;
58   double latency;
59   double rate;
60   int suspended;
61 } s_surf_action_workstation_L07_t, *surf_action_workstation_L07_t;
62
63
64 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_workstation);
65
66 static int ptask_host_count = 0;
67 static xbt_dict_t ptask_parallel_task_link_set = NULL;
68 lmm_system_t ptask_maxmin_system = NULL;
69
70
71 static void ptask_update_action_bound(surf_action_workstation_L07_t action)
72 {
73   int workstation_nb = action->workstation_nb;
74   double lat_current = 0.0;
75   double lat_bound = -1.0;
76   int i, j;
77   unsigned int cpt;
78   link_L07_t link;
79
80   for (i = 0; i < workstation_nb; i++) {
81     for (j = 0; j < workstation_nb; j++) {
82       xbt_dynar_t route =
83           global_routing->get_route(surf_resource_name
84                                     (action->workstation_list[i]),
85                                     surf_resource_name(action->
86                                                        workstation_list
87                                                        [j]));
88
89       double lat = 0.0;
90
91       if (action->communication_amount[i * workstation_nb + j] > 0) {
92         xbt_dynar_foreach(route, cpt, link) {
93           lat += link->lat_current;
94         }
95         lat_current =
96             MAX(lat_current,
97                 lat * action->communication_amount[i * workstation_nb +
98                                                    j]);
99       }
100     }
101   }
102   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
103   XBT_DEBUG("action (%p) : lat_bound = %g", action, lat_bound);
104   if ((action->latency == 0.0) && (action->suspended == 0)) {
105     if (action->rate < 0)
106       lmm_update_variable_bound(ptask_maxmin_system, action->variable,
107                                 lat_bound);
108     else
109       lmm_update_variable_bound(ptask_maxmin_system, action->variable,
110                                 min(action->rate, lat_bound));
111   }
112 }
113
114 /**************************************/
115 /******* Resource Public     **********/
116 /**************************************/
117
118 static int ptask_action_unref(surf_action_t action)
119 {
120   action->refcount--;
121
122   if (!action->refcount) {
123     xbt_swag_remove(action, action->state_set);
124     if (((surf_action_workstation_L07_t) action)->variable)
125       lmm_variable_free(ptask_maxmin_system,
126                         ((surf_action_workstation_L07_t)
127                          action)->variable);
128     free(((surf_action_workstation_L07_t) action)->workstation_list);
129     free(((surf_action_workstation_L07_t) action)->communication_amount);
130     free(((surf_action_workstation_L07_t) action)->computation_amount);
131     surf_action_free(&action);
132     return 1;
133   }
134   return 0;
135 }
136
137 static void ptask_action_cancel(surf_action_t action)
138 {
139   surf_action_state_set(action, SURF_ACTION_FAILED);
140   return;
141 }
142
143 /* action_change_state is inherited from the surf module */
144 /* action_set_data is inherited from the surf module */
145
146 static void ptask_action_suspend(surf_action_t action)
147 {
148   XBT_IN("(%p))", action);
149   if (((surf_action_workstation_L07_t) action)->suspended != 2) {
150     ((surf_action_workstation_L07_t) action)->suspended = 1;
151     lmm_update_variable_weight(ptask_maxmin_system,
152                                ((surf_action_workstation_L07_t)
153                                 action)->variable, 0.0);
154   }
155   XBT_OUT();
156 }
157
158 static void ptask_action_resume(surf_action_t action)
159 {
160   surf_action_workstation_L07_t act =
161       (surf_action_workstation_L07_t) action;
162
163   XBT_IN("(%p)", act);
164   if (act->suspended != 2) {
165     lmm_update_variable_weight(ptask_maxmin_system, act->variable, 1.0);
166     act->suspended = 0;
167   }
168   XBT_OUT();
169 }
170
171 static int ptask_action_is_suspended(surf_action_t action)
172 {
173   return (((surf_action_workstation_L07_t) action)->suspended == 1);
174 }
175
176 static void ptask_action_set_max_duration(surf_action_t action,
177                                           double duration)
178 {                               /* FIXME: should inherit */
179   XBT_IN("(%p,%g)", action, duration);
180   action->max_duration = duration;
181   XBT_OUT();
182 }
183
184
185 static void ptask_action_set_priority(surf_action_t action,
186                                       double priority)
187 {                               /* FIXME: should inherit */
188   XBT_IN("(%p,%g)", action, priority);
189   action->priority = priority;
190   XBT_OUT();
191 }
192
193 static double ptask_action_get_remains(surf_action_t action)
194 {
195   XBT_IN("(%p)", action);
196   return action->remains;
197   XBT_OUT();
198 }
199
200 /**************************************/
201 /******* Resource Private    **********/
202 /**************************************/
203
204 static int ptask_resource_used(void *resource_id)
205 {
206   /* We can freely cast as a link_L07_t because it has
207      the same prefix as cpu_L07_t */
208   return lmm_constraint_used(ptask_maxmin_system,
209                              ((link_L07_t) resource_id)->constraint);
210
211 }
212
213 static double ptask_share_resources(double now)
214 {
215   s_surf_action_workstation_L07_t s_action;
216   surf_action_workstation_L07_t action = NULL;
217
218   xbt_swag_t running_actions =
219       surf_workstation_model->states.running_action_set;
220   double min = generic_maxmin_share_resources(running_actions,
221                                               xbt_swag_offset(s_action,
222                                                               variable),
223                                               ptask_maxmin_system,
224                                               bottleneck_solve);
225
226   xbt_swag_foreach(action, running_actions) {
227     if (action->latency > 0) {
228       if (min < 0) {
229         min = action->latency;
230         XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
231                action->generic_action.start, min);
232       } else if (action->latency < min) {
233         min = action->latency;
234         XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
235                action->generic_action.start, min);
236       }
237     }
238   }
239
240   XBT_DEBUG("min value : %f", min);
241
242   return min;
243 }
244
245 static void ptask_update_actions_state(double now, double delta)
246 {
247   double deltap = 0.0;
248   surf_action_workstation_L07_t action = NULL;
249   surf_action_workstation_L07_t next_action = NULL;
250   xbt_swag_t running_actions =
251       surf_workstation_model->states.running_action_set;
252
253   xbt_swag_foreach_safe(action, next_action, running_actions) {
254     deltap = delta;
255     if (action->latency > 0) {
256       if (action->latency > deltap) {
257         double_update(&(action->latency), deltap);
258         deltap = 0.0;
259       } else {
260         double_update(&(deltap), action->latency);
261         action->latency = 0.0;
262       }
263       if ((action->latency == 0.0) && (action->suspended == 0)) {
264         ptask_update_action_bound(action);
265         lmm_update_variable_weight(ptask_maxmin_system, action->variable,
266                                    1.0);
267       }
268     }
269     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
270            action, action->generic_action.remains,
271            lmm_variable_getvalue(action->variable) * delta);
272     double_update(&(action->generic_action.remains),
273                   lmm_variable_getvalue(action->variable) * delta);
274
275     if (action->generic_action.max_duration != NO_MAX_DURATION)
276       double_update(&(action->generic_action.max_duration), delta);
277
278     XBT_DEBUG("Action (%p) : remains (%g).",
279            action, action->generic_action.remains);
280     if ((action->generic_action.remains <= 0) &&
281         (lmm_get_variable_weight(action->variable) > 0)) {
282       action->generic_action.finish = surf_get_clock();
283       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
284     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
285                (action->generic_action.max_duration <= 0)) {
286       action->generic_action.finish = surf_get_clock();
287       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
288     } else {
289       /* Need to check that none of the model has failed */
290       lmm_constraint_t cnst = NULL;
291       int i = 0;
292       void *constraint_id = NULL;
293
294       while ((cnst =
295               lmm_get_cnst_from_var(ptask_maxmin_system, action->variable,
296                                     i++))) {
297         constraint_id = lmm_constraint_id(cnst);
298
299 /*      if(((link_L07_t)constraint_id)->type== */
300 /*         SURF_WORKSTATION_RESOURCE_LINK) { */
301 /*        XBT_DEBUG("Checking for link %s (%p)", */
302 /*               ((link_L07_t)constraint_id)->name, */
303 /*               ((link_L07_t)constraint_id)); */
304 /*      } */
305 /*      if(((cpu_L07_t)constraint_id)->type== */
306 /*         SURF_WORKSTATION_RESOURCE_CPU) { */
307 /*        XBT_DEBUG("Checking for cpu %s (%p) : %s", */
308 /*               ((cpu_L07_t)constraint_id)->name, */
309 /*               ((cpu_L07_t)constraint_id), */
310 /*               ((cpu_L07_t)constraint_id)->state_current==SURF_CPU_OFF?"Off":"On"); */
311 /*      } */
312
313         if (((((link_L07_t) constraint_id)->type ==
314               SURF_WORKSTATION_RESOURCE_LINK) &&
315              (((link_L07_t) constraint_id)->state_current ==
316               SURF_RESOURCE_OFF)) ||
317             ((((cpu_L07_t) constraint_id)->type ==
318               SURF_WORKSTATION_RESOURCE_CPU) &&
319              (((cpu_L07_t) constraint_id)->state_current ==
320               SURF_RESOURCE_OFF))) {
321           XBT_DEBUG("Action (%p) Failed!!", action);
322           action->generic_action.finish = surf_get_clock();
323           surf_action_state_set((surf_action_t) action,
324                                 SURF_ACTION_FAILED);
325           break;
326         }
327       }
328     }
329   }
330   return;
331 }
332
333 static void ptask_update_resource_state(void *id,
334                                         tmgr_trace_event_t event_type,
335                                         double value, double date)
336 {
337   cpu_L07_t cpu = id;
338   link_L07_t nw_link = id;
339
340   if (nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
341     XBT_DEBUG("Updating link %s (%p)", surf_resource_name(nw_link), nw_link);
342     if (event_type == nw_link->bw_event) {
343       nw_link->bw_current = value;
344       lmm_update_constraint_bound(ptask_maxmin_system, nw_link->constraint,
345                                   nw_link->bw_current);
346       if (tmgr_trace_event_free(event_type))
347         nw_link->bw_event = NULL;
348     } else if (event_type == nw_link->lat_event) {
349       lmm_variable_t var = NULL;
350       surf_action_workstation_L07_t action = NULL;
351       lmm_element_t elem = NULL;
352
353       nw_link->lat_current = value;
354       while ((var = lmm_get_var_from_cnst
355               (ptask_maxmin_system, nw_link->constraint, &elem))) {
356
357
358         action = lmm_variable_id(var);
359         ptask_update_action_bound(action);
360       }
361       if (tmgr_trace_event_free(event_type))
362         nw_link->lat_event = NULL;
363
364     } else if (event_type == nw_link->state_event) {
365       if (value > 0)
366         nw_link->state_current = SURF_RESOURCE_ON;
367       else
368         nw_link->state_current = SURF_RESOURCE_OFF;
369       if (tmgr_trace_event_free(event_type))
370         nw_link->state_event = NULL;
371     } else {
372       XBT_CRITICAL("Unknown event ! \n");
373       xbt_abort();
374     }
375     return;
376   } else if (cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
377     XBT_DEBUG("Updating cpu %s (%p) with value %g", surf_resource_name(cpu),
378            cpu, value);
379     if (event_type == cpu->power_event) {
380       cpu->power_current = value;
381       lmm_update_constraint_bound(ptask_maxmin_system, cpu->constraint,
382                                   cpu->power_current * cpu->power_scale);
383       if (tmgr_trace_event_free(event_type))
384         cpu->power_event = NULL;
385     } else if (event_type == cpu->state_event) {
386       if (value > 0)
387         cpu->state_current = SURF_RESOURCE_ON;
388       else
389         cpu->state_current = SURF_RESOURCE_OFF;
390       if (tmgr_trace_event_free(event_type))
391         cpu->state_event = NULL;
392     } else {
393       XBT_CRITICAL("Unknown event ! \n");
394       xbt_abort();
395     }
396     return;
397   } else {
398     DIE_IMPOSSIBLE;
399   }
400   return;
401 }
402
403 static void ptask_finalize(void)
404 {
405   if (ptask_parallel_task_link_set != NULL)
406     xbt_dict_free(&ptask_parallel_task_link_set);
407
408   surf_model_exit(surf_workstation_model);
409   surf_workstation_model = NULL;
410   surf_model_exit(surf_network_model);
411   surf_network_model = NULL;
412   global_routing->finalize();
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 =
466           global_routing->get_route(surf_resource_name
467                                     (workstation_list[i]),
468                                     surf_resource_name(workstation_list
469                                                        [j]));
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 =
520           global_routing->get_route(surf_resource_name
521                                     (workstation_list[i]),
522                                     surf_resource_name(workstation_list
523                                                        [j]));
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)
593 {
594   return global_routing->get_route(surf_resource_name(src),
595                                    surf_resource_name(dst));
596 }
597
598 static double ptask_get_link_bandwidth(const void *link)
599 {
600   return ((link_L07_t) link)->bw_current;
601 }
602
603 static double ptask_get_link_latency(const void *link)
604 {
605   return ((link_L07_t) link)->lat_current;
606 }
607
608 static int ptask_link_shared(const void *link)
609 {
610   return lmm_constraint_is_shared(((link_L07_t) link)->constraint);
611 }
612
613 /**************************************/
614 /*** Resource Creation & Destruction **/
615 /**************************************/
616
617 static void* ptask_cpu_create_resource(char *name, double power_scale,
618                                double power_initial,
619                                tmgr_trace_t power_trace,
620                                e_surf_resource_state_t state_initial,
621                                tmgr_trace_t state_trace,
622                                xbt_dict_t cpu_properties)
623 {
624   cpu_L07_t cpu = xbt_new0(s_cpu_L07_t, 1);
625   xbt_assert(!surf_workstation_resource_by_name(name),
626               "Host '%s' declared several times in the platform file.",
627               name);
628
629   cpu->generic_resource.model = surf_workstation_model;
630   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
631   cpu->generic_resource.name = xbt_strdup(name);
632   cpu->generic_resource.properties = cpu_properties;
633   cpu->id = ptask_host_count++;
634
635   cpu->power_scale = power_scale;
636   xbt_assert(cpu->power_scale > 0, "Power has to be >0");
637
638   cpu->power_current = power_initial;
639   if (power_trace)
640     cpu->power_event =
641         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
642
643   cpu->state_current = state_initial;
644   if (state_trace)
645     cpu->state_event =
646         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
647
648   cpu->constraint =
649       lmm_constraint_new(ptask_maxmin_system, cpu,
650                          cpu->power_current * cpu->power_scale);
651
652   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, cpu);
653
654   return cpu;
655 }
656
657 static void ptask_parse_cpu_init(void)
658 {
659   ptask_cpu_create_resource(
660                   struct_host->V_host_id,
661                   struct_host->V_host_power_peak,
662                   struct_host->V_host_power_scale,
663                   struct_host->V_host_power_trace,
664                   struct_host->V_host_state_initial,
665                   struct_host->V_host_state_trace,
666           current_property_set);
667   current_property_set=NULL;
668 }
669
670 static void* ptask_link_create_resource(char *name,
671                                  double bw_initial,
672                                  tmgr_trace_t bw_trace,
673                                  double lat_initial,
674                                  tmgr_trace_t lat_trace,
675                                  e_surf_resource_state_t
676                                  state_initial,
677                                  tmgr_trace_t state_trace,
678                                  e_surf_link_sharing_policy_t
679                                  policy, xbt_dict_t properties)
680 {
681   link_L07_t nw_link = xbt_new0(s_link_L07_t, 1);
682   xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
683               "Link '%s' declared several times in the platform file.",
684               name);
685
686   nw_link->generic_resource.model = surf_workstation_model;
687   nw_link->generic_resource.properties = properties;
688   nw_link->generic_resource.name = name;
689   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
690   nw_link->bw_current = bw_initial;
691   if (bw_trace)
692     nw_link->bw_event =
693         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
694   nw_link->state_current = state_initial;
695   nw_link->lat_current = lat_initial;
696   if (lat_trace)
697     nw_link->lat_event =
698         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
699   if (state_trace)
700     nw_link->state_event =
701         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
702
703   nw_link->constraint =
704       lmm_constraint_new(ptask_maxmin_system, nw_link,
705                          nw_link->bw_current);
706
707   if (policy == SURF_LINK_FATPIPE)
708     lmm_constraint_shared(nw_link->constraint);
709
710   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
711   return nw_link;
712 }
713
714 static void ptask_parse_link_init(void)
715 {
716   if(struct_lnk->V_policy_initial_link == SURF_LINK_FULLDUPLEX)
717   {
718           ptask_link_create_resource(bprintf("%s_UP",struct_lnk->V_link_id), struct_lnk->V_link_bandwidth, struct_lnk->V_link_bandwidth_file,
719                        struct_lnk->V_link_latency, struct_lnk->V_link_latency_file, struct_lnk->V_link_state, struct_lnk->V_link_state_file,
720                        struct_lnk->V_policy_initial_link, current_property_set);
721     ptask_link_create_resource(bprintf("%s_DOWN",struct_lnk->V_link_id), struct_lnk->V_link_bandwidth, struct_lnk->V_link_bandwidth_file,
722             struct_lnk->V_link_latency, struct_lnk->V_link_latency_file, struct_lnk->V_link_state, struct_lnk->V_link_state_file,
723                        struct_lnk->V_policy_initial_link, current_property_set);
724   }
725   else
726   {
727           ptask_link_create_resource(xbt_strdup(struct_lnk->V_link_id), struct_lnk->V_link_bandwidth, struct_lnk->V_link_bandwidth_file,
728                 struct_lnk->V_link_latency, struct_lnk->V_link_latency_file, struct_lnk->V_link_state, struct_lnk->V_link_state_file,
729                        struct_lnk->V_policy_initial_link, current_property_set);
730   }
731
732   current_property_set = NULL;
733 }
734
735 static void ptask_add_traces(void)
736 {
737   xbt_dict_cursor_t cursor = NULL;
738   char *trace_name, *elm;
739
740   if (!trace_connect_list_host_avail)
741     return;
742
743   /* Connect traces relative to cpu */
744   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
745     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
746     cpu_L07_t host = surf_workstation_resource_by_name(elm);
747
748     xbt_assert(host, "Host %s undefined", elm);
749     xbt_assert(trace, "Trace %s undefined", trace_name);
750
751     host->state_event =
752         tmgr_history_add_trace(history, trace, 0.0, 0, host);
753   }
754
755   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
756     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
757     cpu_L07_t host = surf_workstation_resource_by_name(elm);
758
759     xbt_assert(host, "Host %s undefined", elm);
760     xbt_assert(trace, "Trace %s undefined", trace_name);
761
762     host->power_event =
763         tmgr_history_add_trace(history, trace, 0.0, 0, host);
764   }
765
766   /* Connect traces relative to network */
767   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
768     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
769     link_L07_t link =
770                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
771
772     xbt_assert(link, "Link %s undefined", elm);
773     xbt_assert(trace, "Trace %s undefined", trace_name);
774
775     link->state_event =
776         tmgr_history_add_trace(history, trace, 0.0, 0, link);
777   }
778
779   xbt_dict_foreach(trace_connect_list_bandwidth, 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->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
788   }
789
790   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
791     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
792     link_L07_t link =
793                 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
794
795     xbt_assert(link, "Link %s undefined", elm);
796     xbt_assert(trace, "Trace %s undefined", trace_name);
797
798     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
799   }
800 }
801
802 static void ptask_define_callbacks(const char *file)
803 {
804   /* Adding callback functions */
805   surfxml_add_callback(ETag_surfxml_host_cb_list, &ptask_parse_cpu_init);
806   surfxml_add_callback(ETag_surfxml_link_cb_list, &ptask_parse_link_init);
807   surfxml_add_callback(ETag_surfxml_platform_cb_list, &ptask_add_traces);
808 }
809
810 /**************************************/
811 /********* Module  creation ***********/
812 /**************************************/
813
814 static void ptask_model_init_internal(void)
815 {
816   surf_workstation_model = surf_model_init();
817
818   surf_workstation_model->action_unref = ptask_action_unref;
819   surf_workstation_model->action_cancel = ptask_action_cancel;
820   surf_workstation_model->action_state_set = surf_action_state_set;
821   surf_workstation_model->suspend = ptask_action_suspend;
822   surf_workstation_model->resume = ptask_action_resume;
823   surf_workstation_model->is_suspended = ptask_action_is_suspended;
824   surf_workstation_model->set_max_duration = ptask_action_set_max_duration;
825   surf_workstation_model->set_priority = ptask_action_set_priority;
826   surf_workstation_model->get_remains = ptask_action_get_remains;
827   surf_workstation_model->name = "Workstation ptask_L07";
828
829   surf_workstation_model->model_private->resource_used =
830       ptask_resource_used;
831   surf_workstation_model->model_private->share_resources =
832       ptask_share_resources;
833   surf_workstation_model->model_private->update_actions_state =
834       ptask_update_actions_state;
835   surf_workstation_model->model_private->update_resource_state =
836       ptask_update_resource_state;
837   surf_workstation_model->model_private->finalize = ptask_finalize;
838
839
840   surf_workstation_model->extension.workstation.execute = ptask_execute;
841   surf_workstation_model->extension.workstation.sleep = ptask_action_sleep;
842   surf_workstation_model->extension.workstation.get_state =
843       ptask_resource_get_state;
844   surf_workstation_model->extension.workstation.get_speed =
845       ptask_get_speed;
846   surf_workstation_model->extension.workstation.get_available_speed =
847       ptask_get_available_speed;
848   surf_workstation_model->extension.workstation.communicate =
849       ptask_communicate;
850   surf_workstation_model->extension.workstation.get_route =
851       ptask_get_route;
852   surf_workstation_model->extension.workstation.execute_parallel_task =
853       ptask_execute_parallel_task;
854   surf_workstation_model->extension.workstation.get_link_bandwidth =
855       ptask_get_link_bandwidth;
856   surf_workstation_model->extension.workstation.get_link_latency =
857       ptask_get_link_latency;
858   surf_workstation_model->extension.workstation.link_shared =
859       ptask_link_shared;
860   surf_workstation_model->extension.workstation.get_properties =
861       surf_resource_properties;
862   surf_workstation_model->extension.workstation.link_create_resource =
863       ptask_link_create_resource;
864   surf_workstation_model->extension.workstation.cpu_create_resource =
865       ptask_cpu_create_resource;
866   surf_workstation_model->extension.workstation.add_traces =
867       ptask_add_traces;
868
869   if (!ptask_maxmin_system)
870     ptask_maxmin_system = lmm_system_new();
871
872   routing_model_create(sizeof(link_L07_t),
873       ptask_link_create_resource(xbt_strdup("__loopback__"),
874                                       498000000, NULL, 0.000015, NULL,
875                                       SURF_RESOURCE_ON, NULL,
876                                       SURF_LINK_FATPIPE, NULL),
877                        ptask_get_link_latency);
878
879 }
880
881 /**************************************/
882 /*************** Generic **************/
883 /**************************************/
884 void surf_workstation_model_init_ptask_L07(const char *filename)
885 {
886   XBT_INFO("surf_workstation_model_init_ptask_L07");
887   xbt_assert(!surf_cpu_model, "CPU model type already defined");
888   xbt_assert(!surf_network_model, "network model type already defined");
889   surf_network_model = surf_model_init();
890   ptask_define_callbacks(filename);
891   ptask_model_init_internal();
892
893   update_model_description(surf_workstation_model_description,
894                            "ptask_L07", surf_workstation_model);
895   xbt_dynar_push(model_list, &surf_workstation_model);
896 }