Logo AND Algorithmique Numérique Distribuée

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