Logo AND Algorithmique Numérique Distribuée

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