Logo AND Algorithmique Numérique Distribuée

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