Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add in DTD direction for link_ctn and sharing_policy for link and cluster
[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   DEBUG2("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     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_IN1("(%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_IN1("(%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_IN2("(%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_IN2("(%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_IN1("(%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         DEBUG3("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         DEBUG3("Updating min (latency) with %p (start %f): %f", action,
235                action->generic_action.start, min);
236       }
237     }
238   }
239
240   DEBUG1("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     DEBUG3("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     DEBUG2("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 /*        DEBUG2("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 /*        DEBUG3("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           DEBUG1("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     DEBUG2("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       CRITICAL0("Unknown event ! \n");
373       xbt_abort();
374     }
375     return;
376   } else if (cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
377     DEBUG3("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       CRITICAL0("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   DEBUG3("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_IN2("(%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 cpu_L07_t ptask_cpu_new(const 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_assert1(!surf_model_resource_by_name(surf_workstation_model, 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 = current_property_set;
633   cpu->id = ptask_host_count++;
634
635   cpu->power_scale = power_scale;
636   xbt_assert0(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_dict_set(surf_model_resource_set(surf_workstation_model), name, cpu,
653                surf_resource_free);
654
655   return cpu;
656 }
657
658 static void ptask_parse_cpu_init(void)
659 {
660   double power_scale = 0.0;
661   double power_initial = 0.0;
662   tmgr_trace_t power_trace = NULL;
663   e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
664   tmgr_trace_t state_trace = NULL;
665
666   power_scale = get_cpu_power(A_surfxml_host_power);
667   surf_parse_get_double(&power_initial, A_surfxml_host_availability);
668   power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
669
670   xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
671               (A_surfxml_host_state == A_surfxml_host_state_OFF),
672               "Invalid state");
673   if (A_surfxml_host_state == A_surfxml_host_state_ON)
674     state_initial = SURF_RESOURCE_ON;
675   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
676     state_initial = SURF_RESOURCE_OFF;
677   state_trace = tmgr_trace_new(A_surfxml_host_state_file);
678
679   current_property_set = xbt_dict_new();
680   ptask_cpu_new(A_surfxml_host_id, power_scale, power_initial, power_trace,
681                 state_initial, state_trace, current_property_set);
682 }
683
684 static void ptask_cpu_create_resource(char *name, double power_peak,
685                                       double power_scale,
686                                       tmgr_trace_t power_trace,
687                                       e_surf_resource_state_t
688                                       state_initial,
689                                       tmgr_trace_t state_trace,
690                                       xbt_dict_t cpu_properties)
691 {
692   ptask_cpu_new(xbt_strdup(name), power_peak, power_scale, power_trace,
693                 state_initial, state_trace, cpu_properties);
694 }
695
696 static link_L07_t ptask_link_new(char *name,
697                                  double bw_initial,
698                                  tmgr_trace_t bw_trace,
699                                  double lat_initial,
700                                  tmgr_trace_t lat_trace,
701                                  e_surf_resource_state_t
702                                  state_initial,
703                                  tmgr_trace_t state_trace,
704                                  e_surf_link_sharing_policy_t
705                                  policy, xbt_dict_t properties)
706 {
707   link_L07_t nw_link = xbt_new0(s_link_L07_t, 1);
708   xbt_assert1(!xbt_dict_get_or_null
709               (surf_network_model->resource_set, name),
710               "Link '%s' declared several times in the platform file.",
711               name);
712
713   nw_link->generic_resource.model = surf_workstation_model;
714   nw_link->generic_resource.properties = properties;
715   nw_link->generic_resource.name = name;
716   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
717   nw_link->bw_current = bw_initial;
718   if (bw_trace)
719     nw_link->bw_event =
720         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
721   nw_link->state_current = state_initial;
722   nw_link->lat_current = lat_initial;
723   if (lat_trace)
724     nw_link->lat_event =
725         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
726   if (state_trace)
727     nw_link->state_event =
728         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
729
730   nw_link->constraint =
731       lmm_constraint_new(ptask_maxmin_system, nw_link,
732                          nw_link->bw_current);
733
734   if (policy == SURF_LINK_FATPIPE)
735     lmm_constraint_shared(nw_link->constraint);
736
737   xbt_dict_set(surf_network_model->resource_set, name, nw_link,
738                surf_resource_free);
739
740
741   return nw_link;
742 }
743
744 static void ptask_parse_link_init(void)
745 {
746   double bw_initial;
747   tmgr_trace_t bw_trace;
748   double lat_initial;
749   tmgr_trace_t lat_trace;
750   e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
751   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
752   tmgr_trace_t state_trace;
753   char *name_link_up = NULL;
754   char *name_link_down = NULL;
755   char *name_link = NULL;
756
757   if(A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
758   {
759   name_link_up = xbt_strdup(bprintf("%s_UP",A_surfxml_link_id));
760   name_link_down = xbt_strdup(bprintf("%s_DOWN",A_surfxml_link_id));
761   }
762   else
763   {
764   name_link = xbt_strdup(A_surfxml_link_id);
765   }
766   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
767   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
768   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
769   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
770
771   xbt_assert0((A_surfxml_link_state == A_surfxml_link_state_ON)
772               || (A_surfxml_link_state ==
773                   A_surfxml_link_state_OFF), "Invalid state");
774   if (A_surfxml_link_state == A_surfxml_link_state_ON)
775     state_initial_link = SURF_RESOURCE_ON;
776   else if (A_surfxml_link_state == A_surfxml_link_state_OFF)
777     state_initial_link = SURF_RESOURCE_OFF;
778
779   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
780     policy_initial_link = SURF_LINK_SHARED;
781   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
782         policy_initial_link = SURF_LINK_FATPIPE;
783   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
784         policy_initial_link = SURF_LINK_FULLDUPLEX;
785
786   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
787
788   current_property_set = xbt_dict_new();
789
790   if(policy_initial_link == SURF_LINK_FULLDUPLEX)
791   {
792           ptask_link_new(name_link_up, bw_initial, bw_trace, lat_initial, lat_trace,
793                          state_initial_link, state_trace, policy_initial_link,
794                          current_property_set);
795           ptask_link_new(name_link_down, bw_initial, bw_trace, lat_initial, lat_trace,
796                          state_initial_link, state_trace, policy_initial_link,
797                          xbt_dict_new());
798   }
799   else
800   {
801           ptask_link_new(name_link, bw_initial, bw_trace, lat_initial, lat_trace,
802                                          state_initial_link, state_trace, policy_initial_link,
803                                          current_property_set);
804   }
805 }
806
807 static void ptask_link_create_resource(char *name,
808                                        double bw_initial,
809                                        tmgr_trace_t bw_trace,
810                                        double lat_initial,
811                                        tmgr_trace_t lat_trace,
812                                        e_surf_resource_state_t
813                                        state_initial,
814                                        tmgr_trace_t state_trace,
815                                        e_surf_link_sharing_policy_t
816                                        policy, xbt_dict_t properties)
817 {
818
819   ptask_link_new(name, bw_initial, bw_trace,
820                  lat_initial, lat_trace, state_initial, state_trace,
821                  policy, xbt_dict_new());
822 }
823
824
825 static void ptask_add_traces(void)
826 {
827   xbt_dict_cursor_t cursor = NULL;
828   char *trace_name, *elm;
829
830   if (!trace_connect_list_host_avail)
831     return;
832
833   /* Connect traces relative to cpu */
834   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
835     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
836     cpu_L07_t host =
837         surf_model_resource_by_name(surf_workstation_model, elm);
838
839     xbt_assert1(host, "Host %s undefined", elm);
840     xbt_assert1(trace, "Trace %s undefined", trace_name);
841
842     host->state_event =
843         tmgr_history_add_trace(history, trace, 0.0, 0, host);
844   }
845
846   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
847     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
848     cpu_L07_t host =
849         surf_model_resource_by_name(surf_workstation_model, elm);
850
851     xbt_assert1(host, "Host %s undefined", elm);
852     xbt_assert1(trace, "Trace %s undefined", trace_name);
853
854     host->power_event =
855         tmgr_history_add_trace(history, trace, 0.0, 0, host);
856   }
857
858   /* Connect traces relative to network */
859   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
860     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
861     link_L07_t link =
862         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
863
864     xbt_assert1(link, "Link %s undefined", elm);
865     xbt_assert1(trace, "Trace %s undefined", trace_name);
866
867     link->state_event =
868         tmgr_history_add_trace(history, trace, 0.0, 0, link);
869   }
870
871   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
872     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
873     link_L07_t link =
874         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
875
876     xbt_assert1(link, "Link %s undefined", elm);
877     xbt_assert1(trace, "Trace %s undefined", trace_name);
878
879     link->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
880   }
881
882   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
883     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
884     link_L07_t link =
885         xbt_dict_get_or_null(surf_network_model->resource_set, elm);
886
887     xbt_assert1(link, "Link %s undefined", elm);
888     xbt_assert1(trace, "Trace %s undefined", trace_name);
889
890     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
891   }
892 }
893
894 static void ptask_define_callbacks(const char *file)
895 {
896   /* Adding callback functions */
897   surf_parse_reset_parser();
898   surfxml_add_callback(STag_surfxml_host_cb_list, &ptask_parse_cpu_init);
899   surfxml_add_callback(STag_surfxml_link_cb_list, &ptask_parse_link_init);
900   surfxml_add_callback(ETag_surfxml_platform_cb_list, &ptask_add_traces);
901 }
902
903 /**************************************/
904 /********* Module  creation ***********/
905 /**************************************/
906
907 static void ptask_model_init_internal(void)
908 {
909   surf_workstation_model = surf_model_init();
910
911   surf_workstation_model->action_unref = ptask_action_unref;
912   surf_workstation_model->action_cancel = ptask_action_cancel;
913   surf_workstation_model->action_state_set = surf_action_state_set;
914   surf_workstation_model->suspend = ptask_action_suspend;
915   surf_workstation_model->resume = ptask_action_resume;
916   surf_workstation_model->is_suspended = ptask_action_is_suspended;
917   surf_workstation_model->set_max_duration = ptask_action_set_max_duration;
918   surf_workstation_model->set_priority = ptask_action_set_priority;
919   surf_workstation_model->get_remains = ptask_action_get_remains;
920   surf_workstation_model->name = "Workstation ptask_L07";
921
922   surf_workstation_model->model_private->resource_used =
923       ptask_resource_used;
924   surf_workstation_model->model_private->share_resources =
925       ptask_share_resources;
926   surf_workstation_model->model_private->update_actions_state =
927       ptask_update_actions_state;
928   surf_workstation_model->model_private->update_resource_state =
929       ptask_update_resource_state;
930   surf_workstation_model->model_private->finalize = ptask_finalize;
931
932
933   surf_workstation_model->extension.workstation.execute = ptask_execute;
934   surf_workstation_model->extension.workstation.sleep = ptask_action_sleep;
935   surf_workstation_model->extension.workstation.get_state =
936       ptask_resource_get_state;
937   surf_workstation_model->extension.workstation.get_speed =
938       ptask_get_speed;
939   surf_workstation_model->extension.workstation.get_available_speed =
940       ptask_get_available_speed;
941   surf_workstation_model->extension.workstation.communicate =
942       ptask_communicate;
943   surf_workstation_model->extension.workstation.get_route =
944       ptask_get_route;
945   surf_workstation_model->extension.workstation.execute_parallel_task =
946       ptask_execute_parallel_task;
947   surf_workstation_model->extension.workstation.get_link_bandwidth =
948       ptask_get_link_bandwidth;
949   surf_workstation_model->extension.workstation.get_link_latency =
950       ptask_get_link_latency;
951   surf_workstation_model->extension.workstation.link_shared =
952       ptask_link_shared;
953   surf_workstation_model->extension.workstation.get_properties =
954       surf_resource_properties;
955   surf_workstation_model->extension.workstation.link_create_resource =
956       ptask_link_create_resource;
957   surf_workstation_model->extension.workstation.cpu_create_resource =
958       ptask_cpu_create_resource;
959   surf_workstation_model->extension.workstation.add_traces =
960       ptask_add_traces;
961
962   if (!ptask_maxmin_system)
963     ptask_maxmin_system = lmm_system_new();
964
965   routing_model_create(sizeof(link_L07_t),
966                        ptask_link_new(xbt_strdup("__loopback__"),
967                                       498000000, NULL, 0.000015, NULL,
968                                       SURF_RESOURCE_ON, NULL,
969                                       SURF_LINK_FATPIPE, NULL));
970
971 }
972
973 /**************************************/
974 /*************** Generic **************/
975 /**************************************/
976 void surf_workstation_model_init_ptask_L07(const char *filename)
977 {
978   INFO0("surf_workstation_model_init_ptask_L07");
979   xbt_assert0(!surf_cpu_model, "CPU model type already defined");
980   xbt_assert0(!surf_network_model, "network model type already defined");
981   surf_network_model = surf_model_init();
982   ptask_define_callbacks(filename);
983   ptask_model_init_internal();
984
985   update_model_description(surf_workstation_model_description,
986                            "ptask_L07", surf_workstation_model);
987   xbt_dynar_push(model_list, &surf_workstation_model);
988 }