Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove dead files.
[simgrid.git] / src / surf / workstation_KCCFLN05.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2005 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/ex.h"
9 #include "xbt/dict.h"
10 #include "workstation_KCCFLN05_private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
13                                 "Logging specific to the SURF workstation module (KCCFLN05)");
14
15 static int nb_workstation = 0;
16 static s_route_KCCFLN05_t *routing_table = NULL;
17 #define ROUTE(i,j) routing_table[(i)+(j)*nb_workstation]
18 static network_link_KCCFLN05_t loopback = NULL;
19 static xbt_dict_t parallel_task_network_link_set = NULL;
20 //added to work with GTNETS
21 static xbt_dict_t router_set = NULL;
22
23 /*xbt_dict_t network_link_set = NULL;*/
24
25
26 /* convenient function */
27 static void __update_cpu_usage(cpu_KCCFLN05_t cpu)
28 {
29   int cpt;
30   surf_action_workstation_KCCFLN05_t action = NULL;
31   if ((!xbt_dynar_length(cpu->incomming_communications)) &&
32       (!xbt_dynar_length(cpu->outgoing_communications))) {
33     /* No communications */
34     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
35                                 cpu->power_current * cpu->power_scale);
36   } else if ((!xbt_dynar_length(cpu->incomming_communications))
37              && (xbt_dynar_length(cpu->outgoing_communications))) {
38     /* Emission */
39     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
40                                 cpu->power_current * cpu->power_scale *
41                                 cpu->interference_send);
42     xbt_dynar_foreach(cpu->outgoing_communications, cpt, action)
43       lmm_elem_set_value(maxmin_system,cpu->constraint,action->variable,
44                          cpu->power_current * cpu->power_scale * 
45                          ROUTE(action->src->id, action->dst->id).impact_on_src
46                          );
47   } else if ((xbt_dynar_length(cpu->incomming_communications)) &&
48              (!xbt_dynar_length(cpu->outgoing_communications))) {
49     /* Reception */
50     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
51                                 cpu->power_current * cpu->power_scale *
52                                 cpu->interference_recv);
53     xbt_dynar_foreach(cpu->incomming_communications, cpt, action)
54       lmm_elem_set_value(maxmin_system,cpu->constraint,action->variable,
55                          cpu->power_current * cpu->power_scale * 
56                          ROUTE(action->src->id, action->dst->id).impact_on_dst
57                          );
58   } else {
59     /* Emission & Reception */
60     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
61                                 cpu->power_current * cpu->power_scale *
62                                 cpu->interference_send_recv);
63     xbt_dynar_foreach(cpu->outgoing_communications, cpt, action)
64       lmm_elem_set_value(maxmin_system,cpu->constraint,action->variable,
65                          cpu->power_current * cpu->power_scale * 
66                          ROUTE(action->src->id, action->dst->id).impact_on_src_with_other_recv
67                          );
68     xbt_dynar_foreach(cpu->incomming_communications, cpt, action)
69       lmm_elem_set_value(maxmin_system,cpu->constraint,action->variable,
70                          cpu->power_current * cpu->power_scale * 
71                          ROUTE(action->src->id, action->dst->id).impact_on_dst_with_other_send
72                          );
73   }
74 }
75
76 /**************************************/
77 /******* Resource Public     **********/
78 /**************************************/
79
80 static void *name_service(const char *name)
81 {
82   xbt_ex_t e;
83   void *res=NULL;
84
85   TRY {
86     res = xbt_dict_get(workstation_set, name);
87   } CATCH(e) {
88     if (e.category != not_found_error) 
89       RETHROW;
90     WARN1("Host '%s' not found, verifing if it is a router", name);
91     res = NULL;
92     xbt_ex_free(e);
93   }
94
95   return res;
96 }
97
98 static const char *get_resource_name(void *resource_id)
99 {
100   /* We can freely cast as a cpu_KCCFLN05_t because it has the same
101      prefix as network_link_KCCFLN05_t. However, only cpu_KCCFLN05_t
102      will theoretically be given as an argument here. */
103   return ((cpu_KCCFLN05_t) resource_id)->name;
104 }
105
106 /* action_get_state is inherited from the surf module */
107
108 static void action_use(surf_action_t action)
109 {
110   action->using++;
111   return;
112 }
113
114 static int action_free(surf_action_t action)
115 {
116   int cpt;
117   surf_action_t act = NULL;
118   cpu_KCCFLN05_t src = ((surf_action_workstation_KCCFLN05_t) action)->src;
119   cpu_KCCFLN05_t dst = ((surf_action_workstation_KCCFLN05_t) action)->dst;
120
121   action->using--;
122   if (!action->using) {
123     xbt_swag_remove(action, action->state_set);
124     if (((surf_action_workstation_KCCFLN05_t) action)->variable)
125       lmm_variable_free(maxmin_system,
126                         ((surf_action_workstation_KCCFLN05_t) action)->
127                         variable);
128     if(src)
129       xbt_dynar_foreach(src->outgoing_communications, cpt, act)
130         if (act == action) {
131           xbt_dynar_remove_at(src->outgoing_communications, cpt, &act);
132           break;
133         }    
134
135     if(dst)
136       xbt_dynar_foreach(dst->incomming_communications, cpt, act)
137         if (act == action) {
138           xbt_dynar_remove_at(dst->incomming_communications, cpt, &act);
139           break;
140         }
141
142     if(src && (!xbt_dynar_length(src->outgoing_communications)))
143       __update_cpu_usage(src);
144     if(dst && (!xbt_dynar_length(dst->incomming_communications)))
145       __update_cpu_usage(dst);
146
147     free(action);
148     return 1;
149   }
150   return 0;
151 }
152
153 static void action_cancel(surf_action_t action)
154 {
155   surf_action_change_state(action, SURF_ACTION_FAILED);
156   return;
157 }
158
159 static void action_recycle(surf_action_t action)
160 {
161   DIE_IMPOSSIBLE;
162   return;
163 }
164
165 /* action_change_state is inherited from the surf module */
166 /* action_set_data is inherited from the surf module */
167
168 static void action_suspend(surf_action_t action)
169 {
170   XBT_IN1("(%p))",action);
171   if(((surf_action_workstation_KCCFLN05_t) action)->suspended != 2) {
172     ((surf_action_workstation_KCCFLN05_t) action)->suspended = 1;
173     lmm_update_variable_weight(maxmin_system,
174                                ((surf_action_workstation_KCCFLN05_t)
175                                 action)->variable, 0.0);
176   }
177   XBT_OUT;
178 }
179
180 static void action_resume(surf_action_t action)
181 {
182   XBT_IN1("(%p)",action);
183   if(((surf_action_workstation_KCCFLN05_t) action)->suspended !=2) {
184     if(((surf_action_workstation_KCCFLN05_t)action)->lat_current==0.0)
185       lmm_update_variable_weight(maxmin_system,
186                                  ((surf_action_workstation_KCCFLN05_t)
187                                   action)->variable, 1.0);
188     else
189       lmm_update_variable_weight(maxmin_system,
190                                  ((surf_action_workstation_KCCFLN05_t) action)->variable, 
191                                  ((surf_action_workstation_KCCFLN05_t) action)->lat_current);
192     
193     ((surf_action_workstation_KCCFLN05_t) action)->suspended = 0;
194   }
195   XBT_OUT;
196 }
197
198 static int action_is_suspended(surf_action_t action)
199 {
200   return (((surf_action_workstation_KCCFLN05_t) action)->suspended==1);
201 }
202
203 static void action_set_max_duration(surf_action_t action, double duration)
204 {                               /* FIXME: should inherit */
205   XBT_IN2("(%p,%g)",action,duration);
206   action->max_duration = duration;
207   XBT_OUT;
208 }
209
210
211 static void action_set_priority(surf_action_t action, double priority)
212 {                               /* FIXME: should inherit */
213   XBT_IN2("(%p,%g)",action,priority);
214   action->priority = priority;
215   XBT_OUT;
216 }
217
218 /**************************************/
219 /******* Resource Private    **********/
220 /**************************************/
221
222 static int resource_used(void *resource_id)
223 {
224   /* We can freely cast as a network_link_KCCFLN05_t because it has
225      the same prefix as cpu_KCCFLN05_t */
226   if(((cpu_KCCFLN05_t) resource_id)->type == SURF_WORKSTATION_RESOURCE_CPU)
227     return (lmm_constraint_used(maxmin_system,
228                                ((cpu_KCCFLN05_t) resource_id)->
229                                constraint) || 
230             ((((cpu_KCCFLN05_t) resource_id)->bus)?
231              lmm_constraint_used(maxmin_system,
232                                 ((cpu_KCCFLN05_t) resource_id)->
233                                 bus):0));
234   else 
235     return lmm_constraint_used(maxmin_system,
236                                ((network_link_KCCFLN05_t) resource_id)->
237                                constraint);
238
239 }
240
241 static double share_resources(double now)
242 {
243   s_surf_action_workstation_KCCFLN05_t s_action;
244   surf_action_workstation_KCCFLN05_t action = NULL;
245
246   xbt_swag_t running_actions = surf_workstation_resource->common_public->states.running_action_set;
247   double min = generic_maxmin_share_resources(running_actions,
248                                               xbt_swag_offset(s_action, variable));
249
250   xbt_swag_foreach(action, running_actions) {
251     if(action->latency>0) {
252       if(min<0) {
253         min = action->latency;
254         DEBUG3("Updating min (value) with %p (start %f): %f",action, 
255                action->generic_action.start, min);
256       }
257       else if (action->latency<min) {
258         min = action->latency;
259         DEBUG3("Updating min (latency) with %p (start %f): %f",action, 
260                action->generic_action.start, min);
261       }
262     }
263   }
264
265   DEBUG1("min value : %f",min);
266
267   return min;
268 }
269
270 static void update_actions_state(double now, double delta)
271 {
272   double deltap = 0.0;
273   surf_action_workstation_KCCFLN05_t action = NULL;
274   surf_action_workstation_KCCFLN05_t next_action = NULL;
275   xbt_swag_t running_actions =
276       surf_workstation_resource->common_public->states.running_action_set;
277
278   xbt_swag_foreach_safe(action, next_action, running_actions) {
279     deltap = delta;
280     if (action->latency > 0) {
281       if (action->latency > deltap) {
282         double_update(&(action->latency), deltap);
283         deltap = 0.0;
284       } else {
285         double_update(&(deltap), action->latency);
286         action->latency = 0.0;
287       }
288       if ((action->latency == 0.0) && (action->suspended==0)) {
289         if((action)->lat_current==0.0)
290           lmm_update_variable_weight(maxmin_system,action->variable, 1.0);
291         else
292           lmm_update_variable_weight(maxmin_system, action->variable, 
293                                      action->lat_current);
294       }
295     }
296     DEBUG3("Action (%p) : remains (%g) updated by %g.",
297            action, action->generic_action.remains,
298            lmm_variable_getvalue(action->variable) * deltap);
299     double_update(&(action->generic_action.remains),
300                        lmm_variable_getvalue(action->variable) * deltap);
301
302     if (action->generic_action.max_duration != NO_MAX_DURATION)
303       double_update(&(action->generic_action.max_duration), delta);
304
305     if ((action->generic_action.remains <= 0) && 
306         (lmm_get_variable_weight(action->variable)>0)) {
307       action->generic_action.finish = surf_get_clock();
308       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
309     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
310                (action->generic_action.max_duration <= 0)) {
311       action->generic_action.finish = surf_get_clock();
312       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
313     } else {
314       /* Need to check that none of the resource has failed */
315       lmm_constraint_t cnst = NULL;
316       int i = 0;
317       void *constraint_id = NULL;
318
319       while ((cnst =
320               lmm_get_cnst_from_var(maxmin_system, action->variable,
321                                     i++))) {
322         constraint_id = lmm_constraint_id(cnst);
323
324 /*      if(((network_link_KCCFLN05_t)constraint_id)->type== */
325 /*         SURF_WORKSTATION_RESOURCE_LINK) { */
326 /*        DEBUG2("Checking for link %s (%p)", */
327 /*               ((network_link_KCCFLN05_t)constraint_id)->name, */
328 /*               ((network_link_KCCFLN05_t)constraint_id)); */
329 /*      } */
330 /*      if(((cpu_KCCFLN05_t)constraint_id)->type== */
331 /*         SURF_WORKSTATION_RESOURCE_CPU) { */
332 /*        DEBUG3("Checking for cpu %s (%p) : %s", */
333 /*               ((cpu_KCCFLN05_t)constraint_id)->name, */
334 /*               ((cpu_KCCFLN05_t)constraint_id), */
335 /*               ((cpu_KCCFLN05_t)constraint_id)->state_current==SURF_CPU_OFF?"Off":"On"); */
336 /*      } */
337
338         if(((((network_link_KCCFLN05_t)constraint_id)->type==
339              SURF_WORKSTATION_RESOURCE_LINK) &&
340             (((network_link_KCCFLN05_t)constraint_id)->state_current==
341              SURF_NETWORK_LINK_OFF)) ||
342            ((((cpu_KCCFLN05_t)constraint_id)->type==
343              SURF_WORKSTATION_RESOURCE_CPU) &&
344             (((cpu_KCCFLN05_t)constraint_id)->state_current==
345              SURF_CPU_OFF))) {
346           DEBUG1("Action (%p) Failed!!",action);
347           action->generic_action.finish = surf_get_clock();
348           surf_action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
349           break;
350         }
351       }
352     }
353   }
354   return;
355 }
356
357 static void update_resource_state(void *id,
358                                   tmgr_trace_event_t event_type,
359                                   double value)
360 {
361   cpu_KCCFLN05_t cpu = id;
362   network_link_KCCFLN05_t nw_link = id ;
363
364   if(nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
365     DEBUG2("Updating link %s (%p)",nw_link->name,nw_link);
366     if (event_type == nw_link->bw_event) {
367       nw_link->bw_current = value;
368       lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
369                                   nw_link->bw_current);
370     } else if (event_type == nw_link->lat_event) {
371       double delta = value - nw_link->lat_current;
372       lmm_variable_t var = NULL;
373       surf_action_workstation_KCCFLN05_t action = NULL;
374       
375       nw_link->lat_current = value;
376       while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
377         action = lmm_variable_id(var);
378         action->lat_current += delta;
379         if(action->rate<0)
380           lmm_update_variable_bound(maxmin_system, action->variable,
381                                     SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
382         else 
383           lmm_update_variable_bound(maxmin_system, action->variable,
384                                     min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
385         if(action->suspended==0)
386           lmm_update_variable_weight(maxmin_system, action->variable, 
387                                      action->lat_current);
388         lmm_update_variable_latency(maxmin_system, action->variable, delta);
389         
390
391       }
392     } else if (event_type == nw_link->state_event) {
393       if (value > 0)
394         nw_link->state_current = SURF_NETWORK_LINK_ON;
395       else
396         nw_link->state_current = SURF_NETWORK_LINK_OFF;
397     } else {
398       CRITICAL0("Unknown event ! \n");
399       xbt_abort();
400     }
401     return;
402   } else if(cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
403     DEBUG3("Updating cpu %s (%p) with value %g",cpu->name,cpu,value);
404     if (event_type == cpu->power_event) {
405       cpu->power_current = value;
406       __update_cpu_usage(cpu);
407     } else if (event_type == cpu->state_event) {
408       if (value > 0)
409         cpu->state_current = SURF_CPU_ON;
410       else
411         cpu->state_current = SURF_CPU_OFF;
412     } else {
413       CRITICAL0("Unknown event ! \n");
414       xbt_abort();
415     }
416     return;
417   } else {
418     DIE_IMPOSSIBLE;
419   }
420   return;
421 }
422
423 static void finalize(void)
424 {
425   int i,j;
426
427   xbt_dict_free(&network_link_set);
428   xbt_dict_free(&workstation_set);
429   xbt_dict_free(&router_set);
430   if (parallel_task_network_link_set != NULL) {
431     xbt_dict_free(&parallel_task_network_link_set);
432   }
433   xbt_swag_free(surf_workstation_resource->common_public->states.
434                 ready_action_set);
435   xbt_swag_free(surf_workstation_resource->common_public->states.
436                 running_action_set);
437   xbt_swag_free(surf_workstation_resource->common_public->states.
438                 failed_action_set);
439   xbt_swag_free(surf_workstation_resource->common_public->states.
440                 done_action_set);
441
442   free(surf_workstation_resource->common_public);
443   free(surf_workstation_resource->common_private);
444   free(surf_workstation_resource->extension_public);
445
446   free(surf_workstation_resource);
447   surf_workstation_resource = NULL;
448
449   for (i = 0; i < nb_workstation; i++)
450     for (j = 0; j < nb_workstation; j++)
451       free(ROUTE(i, j).links);
452   free(routing_table);
453   routing_table = NULL;
454   nb_workstation = 0;
455
456   if (maxmin_system) {
457     lmm_system_free(maxmin_system);
458     maxmin_system = NULL;
459   }
460 }
461
462 /**************************************/
463 /******* Resource Private    **********/
464 /**************************************/
465
466 static surf_action_t execute(void *cpu, double size)
467 {
468   surf_action_workstation_KCCFLN05_t action = NULL;
469   cpu_KCCFLN05_t CPU = cpu;
470
471   XBT_IN2("(%s,%g)",CPU->name,size);
472   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
473
474   action->generic_action.using = 1;
475   action->generic_action.cost = size;
476   action->generic_action.remains = size;
477   action->generic_action.priority = 1.0;
478   action->generic_action.max_duration = NO_MAX_DURATION;
479   action->generic_action.start = surf_get_clock();
480   action->generic_action.finish = -1.0;
481   action->generic_action.resource_type =
482       (surf_resource_t) surf_workstation_resource;
483   action->suspended = 0;
484
485   if (CPU->state_current == SURF_CPU_ON)
486     action->generic_action.state_set =
487         surf_workstation_resource->common_public->states.running_action_set;
488   else
489     action->generic_action.state_set =
490         surf_workstation_resource->common_public->states.failed_action_set;
491   xbt_swag_insert(action, action->generic_action.state_set);
492
493   action->variable = lmm_variable_new(maxmin_system, action, 
494                                       action->generic_action.priority, -1.0, 1);
495   lmm_expand(maxmin_system, CPU->constraint, action->variable,
496              1.0);
497   XBT_OUT;
498   return (surf_action_t) action;
499 }
500
501 static surf_action_t action_sleep(void *cpu, double duration)
502 {
503   surf_action_workstation_KCCFLN05_t action = NULL;
504
505   XBT_IN2("(%s,%g)",((cpu_KCCFLN05_t)cpu)->name,duration);
506
507   action = (surf_action_workstation_KCCFLN05_t) execute(cpu, 1.0);
508   action->generic_action.max_duration = duration;
509   action->suspended = 2;
510   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
511
512   XBT_OUT;
513   return (surf_action_t) action;
514 }
515
516 static e_surf_cpu_state_t resource_get_state(void *cpu)
517 {
518   return ((cpu_KCCFLN05_t) cpu)->state_current;
519 }
520
521 static double get_speed(void *cpu, double load)
522 {
523   return load*(((cpu_KCCFLN05_t) cpu)->power_scale);
524 }
525
526 static double get_available_speed(void *cpu)
527 {
528   return ((cpu_KCCFLN05_t) cpu)->power_current;
529 }
530
531
532 static surf_action_t communicate(void *src, void *dst, double size, double rate)
533 {
534   surf_action_workstation_KCCFLN05_t action = NULL;
535   cpu_KCCFLN05_t card_src = src;
536   cpu_KCCFLN05_t card_dst = dst;
537   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
538   int route_size = route->size;
539   int i;
540
541   XBT_IN4("(%s,%s,%g,%g)",card_src->name,card_dst->name,size,rate);
542   xbt_assert2(route_size,"You're trying to send data from %s to %s but there is no connexion between these two cards.", card_src->name, card_dst->name);
543
544   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
545
546   action->generic_action.using = 1;
547   action->generic_action.cost = size;
548   action->generic_action.remains = size;
549   action->generic_action.max_duration = NO_MAX_DURATION;
550   action->generic_action.start = surf_get_clock();
551   action->generic_action.finish = -1.0;
552   action->src = src;
553   action->dst = dst;
554   action->generic_action.resource_type =
555     (surf_resource_t) surf_workstation_resource;
556   action->suspended = 0;  /* Should be useless because of the 
557                              calloc but it seems to help valgrind... */
558   action->generic_action.state_set =
559       surf_workstation_resource->common_public->states.running_action_set;
560
561   xbt_dynar_push(card_src->outgoing_communications, &action);
562   xbt_dynar_push(card_dst->incomming_communications, &action);
563
564   xbt_swag_insert(action, action->generic_action.state_set);
565   action->rate = rate;
566
567   action->latency = 0.0;
568   for (i = 0; i < route_size; i++)
569     action->latency += route->links[i]->lat_current;
570   action->lat_current = action->latency;
571
572   if(action->latency>0)
573     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0,
574                                         route_size+4); /* +1 for the src bus
575                                                           +1 for the dst bus
576                                                           +1 for the src cpu
577                                                           +1 for the dst cpu */
578   else
579     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
580                                         route_size+4);
581
582   if(action->rate<0) {
583     if(action->lat_current>0)
584       lmm_update_variable_bound(maxmin_system, action->variable,
585                                 SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
586     else
587       lmm_update_variable_bound(maxmin_system, action->variable, -1.0);
588   } else {
589     if(action->lat_current>0)
590       lmm_update_variable_bound(maxmin_system, action->variable,
591                                 min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
592     else
593       lmm_update_variable_bound(maxmin_system, action->variable, action->rate);
594   }
595
596   lmm_update_variable_latency(maxmin_system, action->variable, action->latency);
597   
598   for (i = 0; i < route_size; i++)
599     lmm_expand(maxmin_system, route->links[i]->constraint, action->variable, 1.0);
600   if (card_src->bus)
601     lmm_expand(maxmin_system, card_src->bus, action->variable, 1.0);
602   if (card_dst->bus)
603     lmm_expand(maxmin_system, card_dst->bus, action->variable, 1.0);
604   lmm_expand(maxmin_system, card_src->constraint, action->variable, 0.0);
605   lmm_expand(maxmin_system, card_dst->constraint, action->variable, 0.0);
606
607   XBT_OUT;
608   return (surf_action_t) action;
609 }
610
611 static surf_action_t execute_parallel_task(int workstation_nb,
612                                            void **workstation_list, 
613                                            double *computation_amount, 
614                                            double *communication_amount,
615                                            double amount,
616                                            double rate)
617 {
618   surf_action_workstation_KCCFLN05_t action = NULL;
619   int i, j, k;
620   int nb_link = 0;
621   int nb_host = 0;
622
623   if (parallel_task_network_link_set == NULL) {
624     parallel_task_network_link_set = xbt_dict_new_ext(workstation_nb * workstation_nb * 10);
625   }
626   
627   /* Compute the number of affected resources... */
628   for(i=0; i< workstation_nb; i++) {
629     for(j=0; j< workstation_nb; j++) {
630       cpu_KCCFLN05_t card_src = workstation_list[i];
631       cpu_KCCFLN05_t card_dst = workstation_list[j];
632       int route_size = ROUTE(card_src->id, card_dst->id).size;
633       network_link_KCCFLN05_t *route = ROUTE(card_src->id, card_dst->id).links;
634       
635       if(communication_amount[i*workstation_nb+j]>0)
636         for(k=0; k< route_size; k++) {
637           xbt_dict_set(parallel_task_network_link_set, route[k]->name, route[k], NULL);
638         }
639     }
640   }
641   nb_link = xbt_dict_length(parallel_task_network_link_set);
642   xbt_dict_reset(parallel_task_network_link_set);
643
644
645   for (i = 0; i<workstation_nb; i++)
646     if(computation_amount[i]>0) nb_host++;
647  
648
649   if(nb_link + nb_host == 0) /* was workstation_nb... */
650     return NULL;
651
652   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
653   DEBUG3("Creating a parallel task (%p) with %d cpus and %d links.",
654          action, nb_host,  nb_link);
655   action->generic_action.using = 1;
656   action->generic_action.cost = amount;
657   action->generic_action.remains = amount;
658   action->generic_action.max_duration = NO_MAX_DURATION;
659   action->generic_action.start = -1.0;
660   action->generic_action.finish = -1.0;
661   action->generic_action.resource_type =
662       (surf_resource_t) surf_workstation_resource;
663   action->suspended = 0;  /* Should be useless because of the
664                              calloc but it seems to help valgrind... */
665   action->generic_action.state_set =
666       surf_workstation_resource->common_public->states.running_action_set;
667
668   xbt_swag_insert(action, action->generic_action.state_set);
669   action->rate = rate;
670
671   if(action->rate>0)
672     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
673                                         nb_host + nb_link);
674   else   
675     action->variable = lmm_variable_new(maxmin_system, action, 1.0, action->rate,
676                                         nb_host + nb_link);
677
678   for (i = 0; i<workstation_nb; i++)
679     if(computation_amount[i]>0)
680       lmm_expand(maxmin_system, ((cpu_KCCFLN05_t) workstation_list[i])->constraint, 
681                  action->variable, computation_amount[i]);
682
683   for (i=0; i<workstation_nb; i++) {
684     for(j=0; j< workstation_nb; j++) {
685       cpu_KCCFLN05_t card_src = workstation_list[i];
686       cpu_KCCFLN05_t card_dst = workstation_list[j];
687       int route_size = ROUTE(card_src->id, card_dst->id).size;
688       network_link_KCCFLN05_t *route = ROUTE(card_src->id, card_dst->id).links;
689       
690       for(k=0; k< route_size; k++) {
691         if(communication_amount[i*workstation_nb+j]>0) {
692           lmm_expand_add(maxmin_system, route[k]->constraint, 
693                        action->variable, communication_amount[i*workstation_nb+j]);
694         }
695       }
696     }
697   }
698   
699   return (surf_action_t) action;
700 }
701
702 /* returns an array of network_link_KCCFLN05_t */
703 static const void** get_route(void *src, void *dst) {
704   cpu_KCCFLN05_t card_src = src;
705   cpu_KCCFLN05_t card_dst = dst;
706   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
707
708   return (const void**) route->links;
709 }
710
711 static int get_route_size(void *src, void *dst) {
712   cpu_KCCFLN05_t card_src = src;
713   cpu_KCCFLN05_t card_dst = dst;
714   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
715   return route->size;
716 }
717
718 static const char *get_link_name(const void *link) {
719   return ((network_link_KCCFLN05_t) link)->name;
720 }
721
722 static double get_link_bandwidth(const void *link) {
723   return ((network_link_KCCFLN05_t) link)->bw_current;
724 }
725
726 static double get_link_latency(const void *link) {
727   return ((network_link_KCCFLN05_t) link)->lat_current;
728 }
729
730 /**************************************/
731 /*** Resource Creation & Destruction **/
732 /**************************************/
733
734
735 static void router_free(void *router)
736 {
737   free( ((router_KCCFLN05_t) router)->name );
738 }
739
740 static void router_new(const char *name)
741 {
742   static unsigned int nb_routers = 0; 
743
744   INFO1("Creating a router %s", name);
745
746   router_KCCFLN05_t router;
747   router = xbt_new0(s_router_KCCFLN05_t, 1);
748
749   router->name = xbt_strdup(name);
750   router->id   = nb_routers++;
751   xbt_dict_set(router_set, name, router, router_free);
752 }
753
754 static void parse_routers(void)
755 {
756   //add a dumb router just to be GTNETS compatible
757   router_new(A_surfxml_router_name);
758 }
759
760 static void cpu_free(void *cpu)
761 {
762   free(((cpu_KCCFLN05_t) cpu)->name);
763   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->incomming_communications));
764   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->outgoing_communications));
765   free(cpu);
766 }
767
768 static cpu_KCCFLN05_t cpu_new(const char *name, double power_scale,
769                               double power_initial,
770                               tmgr_trace_t power_trace,
771                               e_surf_cpu_state_t state_initial,
772                               tmgr_trace_t state_trace,
773                               double interference_send,
774                               double interference_recv,
775                               double interference_send_recv,
776                               double max_outgoing_rate)
777 {
778   cpu_KCCFLN05_t cpu = xbt_new0(s_cpu_KCCFLN05_t, 1);
779
780   cpu->resource = (surf_resource_t) surf_workstation_resource;
781   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
782   cpu->name = xbt_strdup(name);
783   cpu->id = nb_workstation++;
784
785   cpu->power_scale = power_scale;
786   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
787
788   cpu->power_current = power_initial;
789   if (power_trace)
790     cpu->power_event =
791         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
792
793   cpu->state_current = state_initial;
794   if (state_trace)
795     cpu->state_event =
796         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
797
798   cpu->interference_send = interference_send;
799   cpu->interference_recv = interference_recv;
800   cpu->interference_send_recv = interference_send_recv;
801
802   cpu->constraint =
803       lmm_constraint_new(maxmin_system, cpu,
804                          cpu->power_current * cpu->power_scale);
805   if (max_outgoing_rate > 0)
806     cpu->bus = lmm_constraint_new(maxmin_system, cpu, max_outgoing_rate);
807
808   cpu->incomming_communications =
809       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
810   cpu->outgoing_communications =
811       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
812
813   xbt_dict_set(workstation_set, name, cpu, cpu_free);
814
815   return cpu;
816 }
817
818 static void parse_cpu(void)
819 {
820   double power_scale = 0.0;
821   double power_initial = 0.0;
822   tmgr_trace_t power_trace = NULL;
823   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
824   tmgr_trace_t state_trace = NULL;
825   double interference_send = 0.0;
826   double interference_recv = 0.0;
827   double interference_send_recv = 0.0;
828   double max_outgoing_rate = -1.0;
829
830   surf_parse_get_double(&power_scale, A_surfxml_cpu_power);
831   surf_parse_get_double(&power_initial, A_surfxml_cpu_availability);
832   surf_parse_get_trace(&power_trace, A_surfxml_cpu_availability_file);
833
834   xbt_assert0((A_surfxml_cpu_state == A_surfxml_cpu_state_ON) ||
835               (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF),
836               "Invalid state");
837   if (A_surfxml_cpu_state == A_surfxml_cpu_state_ON)
838     state_initial = SURF_CPU_ON;
839   if (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF)
840     state_initial = SURF_CPU_OFF;
841   surf_parse_get_trace(&state_trace, A_surfxml_cpu_state_file);
842   
843   surf_parse_get_double(&interference_send,
844                         A_surfxml_cpu_interference_send);
845   surf_parse_get_double(&interference_recv,
846                         A_surfxml_cpu_interference_recv);
847   surf_parse_get_double(&interference_send_recv,
848                         A_surfxml_cpu_interference_send_recv);
849   surf_parse_get_double(&max_outgoing_rate,
850                         A_surfxml_cpu_max_outgoing_rate);
851
852   cpu_new(A_surfxml_cpu_name, power_scale, power_initial, power_trace,
853           state_initial, state_trace, interference_send, interference_recv,
854           interference_send_recv, max_outgoing_rate);
855 }
856
857 static void create_routing_table(void)
858 {
859   routing_table =
860       xbt_new0(s_route_KCCFLN05_t, nb_workstation * nb_workstation);
861 }
862
863 static void network_link_free(void *nw_link)
864 {
865   free(((network_link_KCCFLN05_t) nw_link)->name);
866   free(nw_link);
867 }
868
869 static network_link_KCCFLN05_t network_link_new(char *name,
870                                                 double bw_initial,
871                                                 tmgr_trace_t bw_trace,
872                                                 double lat_initial,
873                                                 tmgr_trace_t lat_trace,
874                                                 e_surf_network_link_state_t
875                                                 state_initial,
876                                                 tmgr_trace_t state_trace,
877                                                 e_surf_network_link_sharing_policy_t policy)
878 {
879   network_link_KCCFLN05_t nw_link = xbt_new0(s_network_link_KCCFLN05_t, 1);
880
881
882   nw_link->resource = (surf_resource_t) surf_workstation_resource;
883   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
884   nw_link->name = name;
885   nw_link->bw_current = bw_initial;
886   if (bw_trace)
887     nw_link->bw_event =
888         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
889   nw_link->state_current = state_initial;
890   nw_link->lat_current = lat_initial;
891   if (lat_trace)
892     nw_link->lat_event =
893         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
894   if (state_trace)
895     nw_link->state_event =
896         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
897
898   nw_link->constraint =
899       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
900
901   if(policy == SURF_NETWORK_LINK_FATPIPE)
902     lmm_constraint_shared(nw_link->constraint);
903
904   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
905
906   return nw_link;
907 }
908
909 static void parse_network_link(void)
910 {
911   char *name;
912   double bw_initial;
913   tmgr_trace_t bw_trace;
914   double lat_initial;
915   tmgr_trace_t lat_trace;
916   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
917   e_surf_network_link_sharing_policy_t policy_initial = SURF_NETWORK_LINK_SHARED;
918   tmgr_trace_t state_trace;
919
920   name = xbt_strdup(A_surfxml_network_link_name);
921   surf_parse_get_double(&bw_initial,A_surfxml_network_link_bandwidth);
922   surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
923   surf_parse_get_double(&lat_initial,A_surfxml_network_link_latency);
924   surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
925
926   xbt_assert0((A_surfxml_network_link_state==A_surfxml_network_link_state_ON)||
927               (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF),
928               "Invalid state");
929   if (A_surfxml_network_link_state==A_surfxml_network_link_state_ON) 
930     state_initial = SURF_NETWORK_LINK_ON;
931   else if (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF) 
932     state_initial = SURF_NETWORK_LINK_OFF;
933
934   if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_SHARED) 
935     policy_initial = SURF_NETWORK_LINK_SHARED;
936   else if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_FATPIPE) 
937     policy_initial = SURF_NETWORK_LINK_FATPIPE;
938
939   surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
940
941   network_link_new(name, bw_initial, bw_trace,
942                    lat_initial, lat_trace, state_initial, state_trace,
943                    policy_initial);
944 }
945
946 static void route_new(int src_id, int dst_id, network_link_KCCFLN05_t *link_list, int nb_link,
947                       double impact_on_src, double impact_on_dst,
948                       double impact_on_src_with_other_recv,
949                       double impact_on_dst_with_other_send)
950 {
951   route_KCCFLN05_t route = &(ROUTE(src_id, dst_id));
952
953   route->size = nb_link;
954   route->links = link_list = xbt_realloc(link_list, sizeof(network_link_KCCFLN05_t) * nb_link);
955   route->impact_on_src = impact_on_src;
956   route->impact_on_dst = impact_on_src;
957   route->impact_on_src_with_other_recv = impact_on_src_with_other_recv;
958   route->impact_on_dst_with_other_send = impact_on_dst_with_other_send;
959 }
960
961 static int nb_link;
962 static int link_list_capacity;
963 static network_link_KCCFLN05_t *link_list = NULL;
964 static int src_id = -1;
965 static int dst_id = -1;
966 static double impact_on_src;
967 static double impact_on_dst;
968 static double impact_on_src_with_other_recv;
969 static double impact_on_dst_with_other_send;
970
971 static void parse_route_set_endpoints(void)
972 {
973   cpu_KCCFLN05_t cpu_tmp = NULL;
974  
975   cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_src);
976   if(cpu_tmp != NULL) {
977     src_id = cpu_tmp->id;
978   }else {
979     xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_src),
980                "Invalid name '%s': neither a cpu nor a router!",
981                A_surfxml_route_src);
982     src_id=-1;
983     return;
984   }
985
986   cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_dst);
987   if(cpu_tmp != NULL) { 
988     dst_id = cpu_tmp->id;
989   }else {
990     xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_dst),
991                "Invalid name '%s': neither a cpu nor a router!",
992                A_surfxml_route_dst);
993     dst_id=-1;
994     return ;
995   }
996   
997   surf_parse_get_double(&impact_on_src, A_surfxml_route_impact_on_src);
998   surf_parse_get_double(&impact_on_dst, A_surfxml_route_impact_on_dst);
999   surf_parse_get_double(&impact_on_src_with_other_recv,
1000                         A_surfxml_route_impact_on_src_with_other_recv);
1001   surf_parse_get_double(&impact_on_dst_with_other_send,
1002                         A_surfxml_route_impact_on_dst_with_other_send);
1003   
1004   nb_link = 0;
1005   link_list_capacity = 1; 
1006   link_list = xbt_new(network_link_KCCFLN05_t, link_list_capacity);
1007
1008 }
1009
1010 static void parse_route_elem(void)
1011 {
1012   xbt_ex_t e;
1013   if (nb_link == link_list_capacity) {
1014     link_list_capacity *= 2;
1015     link_list = xbt_realloc(link_list, (link_list_capacity) * sizeof(network_link_KCCFLN05_t));
1016   }
1017   TRY {
1018      link_list[nb_link++] = xbt_dict_get(network_link_set, A_surfxml_route_element_name);
1019   } CATCH(e) {
1020      RETHROW1("Link %s not found (dict raised this exception: %s)",A_surfxml_route_element_name);
1021   }
1022 }
1023
1024 static void parse_route_set_route(void)
1025 {
1026   if( src_id != -1 && dst_id != -1 )
1027   route_new(src_id, dst_id, link_list, nb_link, impact_on_src,
1028             impact_on_dst, impact_on_src_with_other_recv,
1029             impact_on_dst_with_other_send);
1030 }
1031
1032 static void parse_file(const char *file)
1033 {
1034   int i;
1035
1036   /* Figuring out the cpus */
1037   surf_parse_reset_parser();
1038   ETag_surfxml_cpu_fun = parse_cpu;
1039   surf_parse_open(file);
1040   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1041   surf_parse_close();
1042
1043   create_routing_table();
1044
1045   /* Figuring out the router (added after GTNETS) */
1046   surf_parse_reset_parser();
1047   STag_surfxml_router_fun=parse_routers;
1048   surf_parse_open(file);
1049   xbt_assert1((!surf_parse()),"Parse error in %s",file);
1050   surf_parse_close();
1051
1052   /* Figuring out the network links */
1053   surf_parse_reset_parser();
1054   ETag_surfxml_network_link_fun = parse_network_link;
1055   surf_parse_open(file);
1056   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1057   surf_parse_close();
1058
1059   /* Building the routes */
1060   surf_parse_reset_parser();
1061   STag_surfxml_route_fun = parse_route_set_endpoints;
1062   ETag_surfxml_route_element_fun = parse_route_elem;
1063   ETag_surfxml_route_fun = parse_route_set_route;
1064   surf_parse_open(file);
1065   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1066   surf_parse_close();
1067
1068   /* Adding loopback if needed */    
1069   for (i = 0; i < nb_workstation; i++) 
1070     if(!ROUTE(i,i).size) {
1071       if(!loopback)
1072         loopback = network_link_new(xbt_strdup("__MSG_loopback__"), 
1073                                    498000000, NULL, 0.000015, NULL, 
1074                                    SURF_NETWORK_LINK_ON, NULL,
1075                                    SURF_NETWORK_LINK_FATPIPE);
1076       ROUTE(i,i).size=1;
1077       ROUTE(i,i).links = xbt_new0(network_link_KCCFLN05_t, 1);
1078       ROUTE(i,i).links[0] = loopback;
1079     }
1080
1081 }
1082
1083 /**************************************/
1084 /********* Module  creation ***********/
1085 /**************************************/
1086
1087 static void resource_init_internal(void)
1088 {
1089   s_surf_action_t action;
1090
1091   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
1092
1093   surf_workstation_resource->common_private =
1094       xbt_new0(s_surf_resource_private_t, 1);
1095   surf_workstation_resource->common_public =
1096       xbt_new0(s_surf_resource_public_t, 1);
1097   surf_workstation_resource->extension_public =
1098       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
1099
1100   surf_workstation_resource->common_public->states.ready_action_set =
1101       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1102   surf_workstation_resource->common_public->states.running_action_set =
1103       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1104   surf_workstation_resource->common_public->states.failed_action_set =
1105       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1106   surf_workstation_resource->common_public->states.done_action_set =
1107       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1108
1109   surf_workstation_resource->common_public->name_service = name_service;
1110   surf_workstation_resource->common_public->get_resource_name = get_resource_name;
1111   surf_workstation_resource->common_public->action_get_state = surf_action_get_state;
1112   surf_workstation_resource->common_public->action_get_start_time =
1113       surf_action_get_start_time;
1114   surf_workstation_resource->common_public->action_get_finish_time =
1115       surf_action_get_finish_time;
1116   surf_workstation_resource->common_public->action_use = action_use;
1117   surf_workstation_resource->common_public->action_free = action_free;
1118   surf_workstation_resource->common_public->action_cancel = action_cancel;
1119   surf_workstation_resource->common_public->action_recycle = action_recycle;
1120   surf_workstation_resource->common_public->action_change_state = surf_action_change_state;
1121   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
1122   surf_workstation_resource->common_public->suspend = action_suspend;
1123   surf_workstation_resource->common_public->resume = action_resume;
1124   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
1125   surf_workstation_resource->common_public->set_max_duration = action_set_max_duration;
1126   surf_workstation_resource->common_public->set_priority = action_set_priority;
1127   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
1128
1129   surf_workstation_resource->common_private->resource_used = resource_used;
1130   surf_workstation_resource->common_private->share_resources = share_resources;
1131   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
1132   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
1133   surf_workstation_resource->common_private->finalize = finalize;
1134
1135   surf_workstation_resource->extension_public->execute = execute;
1136   surf_workstation_resource->extension_public->sleep = action_sleep;
1137   surf_workstation_resource->extension_public->get_state = resource_get_state;
1138   surf_workstation_resource->extension_public->get_speed = get_speed;
1139   surf_workstation_resource->extension_public->get_available_speed = get_available_speed;
1140   surf_workstation_resource->extension_public->communicate = communicate;
1141   surf_workstation_resource->extension_public->execute_parallel_task = execute_parallel_task;
1142   surf_workstation_resource->extension_public->get_route = get_route;
1143   surf_workstation_resource->extension_public->get_route_size = get_route_size;
1144   surf_workstation_resource->extension_public->get_link_name = get_link_name;
1145   surf_workstation_resource->extension_public->get_link_bandwidth = get_link_bandwidth;
1146   surf_workstation_resource->extension_public->get_link_latency = get_link_latency;
1147
1148   workstation_set  = xbt_dict_new();
1149   router_set       = xbt_dict_new();
1150   network_link_set = xbt_dict_new();
1151
1152   xbt_assert0(maxmin_system, "surf_init has to be called first!");
1153 }
1154
1155 /**************************************/
1156 /*************** Generic **************/
1157 /**************************************/
1158 void surf_workstation_resource_init_KCCFLN05(const char *filename)
1159 {
1160   xbt_assert0(!surf_cpu_resource, "CPU resource type already defined");
1161   xbt_assert0(!surf_network_resource, "network resource type already defined");
1162   resource_init_internal();
1163   parse_file(filename);
1164
1165   xbt_dynar_push(resource_list, &surf_workstation_resource);
1166 }