Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding a nice description mechanism for models + cleaning (many?) useless functions
[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_or_null(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<.00001) action->generic_action.remains=0; */
306
307     if ((action->generic_action.remains <= 0) && 
308         (lmm_get_variable_weight(action->variable)>0)) {
309       action->generic_action.finish = surf_get_clock();
310       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
311     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
312                (action->generic_action.max_duration <= 0)) {
313       action->generic_action.finish = surf_get_clock();
314       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
315     } else {                    /* Need to check that none of the resource has failed */
316       lmm_constraint_t cnst = NULL;
317       int i = 0;
318       void *constraint_id = NULL;
319
320       while ((cnst =
321               lmm_get_cnst_from_var(maxmin_system, action->variable,
322                                     i++))) {
323         constraint_id = lmm_constraint_id(cnst);
324         if(((((network_link_KCCFLN05_t)constraint_id)->type==
325              SURF_WORKSTATION_RESOURCE_LINK) &&
326             (((network_link_KCCFLN05_t)constraint_id)->state_current==
327              SURF_NETWORK_LINK_OFF)) ||
328            ((((cpu_KCCFLN05_t)constraint_id)->type==
329              SURF_WORKSTATION_RESOURCE_CPU) &&
330             (((cpu_KCCFLN05_t)constraint_id)->state_current==
331              SURF_CPU_OFF))) {
332           action->generic_action.finish = surf_get_clock();
333           surf_action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
334           break;
335         }
336       }
337     }
338   }
339   return;
340 }
341
342 static void update_resource_state(void *id,
343                                   tmgr_trace_event_t event_type,
344                                   double value)
345 {
346   cpu_KCCFLN05_t cpu = id;
347   network_link_KCCFLN05_t nw_link = id ;
348
349   if(nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
350     if (event_type == nw_link->bw_event) {
351       nw_link->bw_current = value;
352       lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
353                                   nw_link->bw_current);
354     } else if (event_type == nw_link->lat_event) {
355       double delta = value - nw_link->lat_current;
356       lmm_variable_t var = NULL;
357       surf_action_workstation_KCCFLN05_t action = NULL;
358       
359       nw_link->lat_current = value;
360       while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
361         action = lmm_variable_id(var);
362         action->lat_current += delta;
363         if(action->rate<0)
364           lmm_update_variable_bound(maxmin_system, action->variable,
365                                     SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
366         else 
367           lmm_update_variable_bound(maxmin_system, action->variable,
368                                     min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
369         if(action->suspended==0)
370           lmm_update_variable_weight(maxmin_system, action->variable, 
371                                      action->lat_current);
372         lmm_update_variable_latency(maxmin_system, action->variable, delta);
373         
374
375       }
376     } else if (event_type == nw_link->state_event) {
377       if (value > 0)
378         nw_link->state_current = SURF_NETWORK_LINK_ON;
379       else
380         nw_link->state_current = SURF_NETWORK_LINK_OFF;
381     } else {
382       CRITICAL0("Unknown event ! \n");
383       xbt_abort();
384     }
385     return;
386   } else if(cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
387     if (event_type == cpu->power_event) {
388       cpu->power_current = value;
389       __update_cpu_usage(cpu);
390     } else if (event_type == cpu->state_event) {
391       if (value > 0)
392         cpu->state_current = SURF_CPU_ON;
393       else
394         cpu->state_current = SURF_CPU_OFF;
395     } else {
396       CRITICAL0("Unknown event ! \n");
397       xbt_abort();
398     }    
399     return;
400   } else {
401     DIE_IMPOSSIBLE;
402   }
403   return;
404 }
405
406 static void finalize(void)
407 {
408   int i,j;
409
410   xbt_dict_free(&network_link_set);
411   xbt_dict_free(&workstation_set);
412   xbt_dict_free(&router_set);
413   if (parallel_task_network_link_set != NULL) {
414     xbt_dict_free(&parallel_task_network_link_set);
415   }
416   xbt_swag_free(surf_workstation_resource->common_public->states.
417                 ready_action_set);
418   xbt_swag_free(surf_workstation_resource->common_public->states.
419                 running_action_set);
420   xbt_swag_free(surf_workstation_resource->common_public->states.
421                 failed_action_set);
422   xbt_swag_free(surf_workstation_resource->common_public->states.
423                 done_action_set);
424
425   free(surf_workstation_resource->common_public);
426   free(surf_workstation_resource->common_private);
427   free(surf_workstation_resource->extension_public);
428
429   free(surf_workstation_resource);
430   surf_workstation_resource = NULL;
431
432   for (i = 0; i < nb_workstation; i++)
433     for (j = 0; j < nb_workstation; j++)
434       free(ROUTE(i, j).links);
435   free(routing_table);
436   routing_table = NULL;
437   nb_workstation = 0;
438
439   if (maxmin_system) {
440     lmm_system_free(maxmin_system);
441     maxmin_system = NULL;
442   }
443 }
444
445 /**************************************/
446 /******* Resource Private    **********/
447 /**************************************/
448
449 static surf_action_t execute(void *cpu, double size)
450 {
451   surf_action_workstation_KCCFLN05_t action = NULL;
452   cpu_KCCFLN05_t CPU = cpu;
453
454   XBT_IN2("(%s,%g)",CPU->name,size);
455   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
456
457   action->generic_action.using = 1;
458   action->generic_action.cost = size;
459   action->generic_action.remains = size;
460   action->generic_action.priority = 1.0;
461   action->generic_action.max_duration = NO_MAX_DURATION;
462   action->generic_action.start = surf_get_clock();
463   action->generic_action.finish = -1.0;
464   action->generic_action.resource_type =
465       (surf_resource_t) surf_workstation_resource;
466   action->suspended = 0;
467
468   if (CPU->state_current == SURF_CPU_ON)
469     action->generic_action.state_set =
470         surf_workstation_resource->common_public->states.running_action_set;
471   else
472     action->generic_action.state_set =
473         surf_workstation_resource->common_public->states.failed_action_set;
474   xbt_swag_insert(action, action->generic_action.state_set);
475
476   action->variable = lmm_variable_new(maxmin_system, action, 
477                                       action->generic_action.priority, -1.0, 1);
478   lmm_expand(maxmin_system, CPU->constraint, action->variable,
479              1.0);
480   XBT_OUT;
481   return (surf_action_t) action;
482 }
483
484 static surf_action_t action_sleep(void *cpu, double duration)
485 {
486   surf_action_workstation_KCCFLN05_t action = NULL;
487
488   XBT_IN2("(%s,%g)",((cpu_KCCFLN05_t)cpu)->name,duration);
489
490   action = (surf_action_workstation_KCCFLN05_t) execute(cpu, 1.0);
491   action->generic_action.max_duration = duration;
492   action->suspended = 2;
493   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
494
495   XBT_OUT;
496   return (surf_action_t) action;
497 }
498
499 static e_surf_cpu_state_t resource_get_state(void *cpu)
500 {
501   return ((cpu_KCCFLN05_t) cpu)->state_current;
502 }
503
504 static double get_speed(void *cpu, double load)
505 {
506   return load*(((cpu_KCCFLN05_t) cpu)->power_scale);
507 }
508
509 static double get_available_speed(void *cpu)
510 {
511   return ((cpu_KCCFLN05_t) cpu)->power_current;
512 }
513
514
515 static surf_action_t communicate(void *src, void *dst, double size, double rate)
516 {
517   surf_action_workstation_KCCFLN05_t action = NULL;
518   cpu_KCCFLN05_t card_src = src;
519   cpu_KCCFLN05_t card_dst = dst;
520   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
521   int route_size = route->size;
522   int i;
523
524   XBT_IN4("(%s,%s,%g,%g)",card_src->name,card_dst->name,size,rate);
525   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);
526
527   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
528
529   action->generic_action.using = 1;
530   action->generic_action.cost = size;
531   action->generic_action.remains = size;
532   action->generic_action.max_duration = NO_MAX_DURATION;
533   action->generic_action.start = surf_get_clock();
534   action->generic_action.finish = -1.0;
535   action->src = src;
536   action->dst = dst;
537   action->generic_action.resource_type =
538     (surf_resource_t) surf_workstation_resource;
539   action->suspended = 0;  /* Should be useless because of the 
540                              calloc but it seems to help valgrind... */
541   action->generic_action.state_set =
542       surf_workstation_resource->common_public->states.running_action_set;
543
544   xbt_dynar_push(card_src->outgoing_communications, &action);
545   xbt_dynar_push(card_dst->incomming_communications, &action);
546
547   xbt_swag_insert(action, action->generic_action.state_set);
548   action->rate = rate;
549
550   action->latency = 0.0;
551   for (i = 0; i < route_size; i++)
552     action->latency += route->links[i]->lat_current;
553   action->lat_current = action->latency;
554
555   if(action->latency>0)
556     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0,
557                                         route_size+4); /* +1 for the src bus
558                                                           +1 for the dst bus
559                                                           +1 for the src cpu
560                                                           +1 for the dst cpu */
561   else
562     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
563                                         route_size+4);
564
565   if(action->rate<0) {
566     if(action->lat_current>0)
567       lmm_update_variable_bound(maxmin_system, action->variable,
568                                 SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
569     else
570       lmm_update_variable_bound(maxmin_system, action->variable, -1.0);
571   } else {
572     if(action->lat_current>0)
573       lmm_update_variable_bound(maxmin_system, action->variable,
574                                 min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
575     else
576       lmm_update_variable_bound(maxmin_system, action->variable, action->rate);
577   }
578
579   lmm_update_variable_latency(maxmin_system, action->variable, action->latency);
580   
581   for (i = 0; i < route_size; i++)
582     lmm_expand(maxmin_system, route->links[i]->constraint, action->variable, 1.0);
583   if (card_src->bus)
584     lmm_expand(maxmin_system, card_src->bus, action->variable, 1.0);
585   if (card_dst->bus)
586     lmm_expand(maxmin_system, card_dst->bus, action->variable, 1.0);
587   lmm_expand(maxmin_system, card_src->constraint, action->variable, 0.0);
588   lmm_expand(maxmin_system, card_dst->constraint, action->variable, 0.0);
589
590   XBT_OUT;
591   return (surf_action_t) action;
592 }
593
594 static surf_action_t execute_parallel_task(int workstation_nb,
595                                            void **workstation_list, 
596                                            double *computation_amount, 
597                                            double *communication_amount,
598                                            double amount,
599                                            double rate)
600 {
601   surf_action_workstation_KCCFLN05_t action = NULL;
602   int i, j, k;
603   int nb_link = 0;
604   int nb_host = 0;
605
606   if (parallel_task_network_link_set == NULL) {
607     parallel_task_network_link_set = xbt_dict_new_ext(workstation_nb * workstation_nb * 10);
608   }
609   
610   /* Compute the number of affected resources... */
611   for(i=0; i< workstation_nb; i++) {
612     for(j=0; j< workstation_nb; j++) {
613       cpu_KCCFLN05_t card_src = workstation_list[i];
614       cpu_KCCFLN05_t card_dst = workstation_list[j];
615       int route_size = ROUTE(card_src->id, card_dst->id).size;
616       network_link_KCCFLN05_t *route = ROUTE(card_src->id, card_dst->id).links;
617       
618       if(communication_amount[i*workstation_nb+j]>0)
619         for(k=0; k< route_size; k++) {
620           xbt_dict_set(parallel_task_network_link_set, route[k]->name, route[k], NULL);
621         }
622     }
623   }
624   nb_link = xbt_dict_length(parallel_task_network_link_set);
625   xbt_dict_reset(parallel_task_network_link_set);
626
627
628   for (i = 0; i<workstation_nb; i++)
629     if(computation_amount[i]>0) nb_host++;
630  
631
632   if(nb_link + nb_host == 0) /* was workstation_nb... */
633     return NULL;
634
635   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
636   DEBUG3("Creating a parallel task (%p) with %d cpus and %d links.",
637          action, nb_host,  nb_link);
638   action->generic_action.using = 1;
639   action->generic_action.cost = amount;
640   action->generic_action.remains = amount;
641   action->generic_action.max_duration = NO_MAX_DURATION;
642   action->generic_action.start = -1.0;
643   action->generic_action.finish = -1.0;
644   action->generic_action.resource_type =
645       (surf_resource_t) surf_workstation_resource;
646   action->suspended = 0;  /* Should be useless because of the
647                              calloc but it seems to help valgrind... */
648   action->generic_action.state_set =
649       surf_workstation_resource->common_public->states.running_action_set;
650
651   xbt_swag_insert(action, action->generic_action.state_set);
652   action->rate = rate;
653
654   if(action->rate>0)
655     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
656                                         nb_host + nb_link);
657   else   
658     action->variable = lmm_variable_new(maxmin_system, action, 1.0, action->rate,
659                                         nb_host + nb_link);
660
661   for (i = 0; i<workstation_nb; i++)
662     if(computation_amount[i]>0)
663       lmm_expand(maxmin_system, ((cpu_KCCFLN05_t) workstation_list[i])->constraint, 
664                  action->variable, computation_amount[i]);
665
666   for (i=0; i<workstation_nb; i++) {
667     for(j=0; j< workstation_nb; j++) {
668       cpu_KCCFLN05_t card_src = workstation_list[i];
669       cpu_KCCFLN05_t card_dst = workstation_list[j];
670       int route_size = ROUTE(card_src->id, card_dst->id).size;
671       network_link_KCCFLN05_t *route = ROUTE(card_src->id, card_dst->id).links;
672       
673       for(k=0; k< route_size; k++) {
674         if(communication_amount[i*workstation_nb+j]>0) {
675           lmm_expand_add(maxmin_system, route[k]->constraint, 
676                        action->variable, communication_amount[i*workstation_nb+j]);
677         }
678       }
679     }
680   }
681   
682   return (surf_action_t) action;
683 }
684
685 /* returns an array of network_link_KCCFLN05_t */
686 static const void** get_route(void *src, void *dst) {
687   cpu_KCCFLN05_t card_src = src;
688   cpu_KCCFLN05_t card_dst = dst;
689   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
690
691   return (const void**) route->links;
692 }
693
694 static int get_route_size(void *src, void *dst) {
695   cpu_KCCFLN05_t card_src = src;
696   cpu_KCCFLN05_t card_dst = dst;
697   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
698   return route->size;
699 }
700
701 static const char *get_link_name(const void *link) {
702   return ((network_link_KCCFLN05_t) link)->name;
703 }
704
705 static double get_link_bandwidth(const void *link) {
706   return ((network_link_KCCFLN05_t) link)->bw_current;
707 }
708
709 static double get_link_latency(const void *link) {
710   return ((network_link_KCCFLN05_t) link)->lat_current;
711 }
712
713 /**************************************/
714 /*** Resource Creation & Destruction **/
715 /**************************************/
716
717
718 static void router_free(void *router)
719 {
720   free( ((router_KCCFLN05_t) router)->name );
721 }
722
723 static void router_new(const char *name)
724 {
725   static unsigned int nb_routers = 0; 
726
727   INFO1("Creating a router %s", name);
728
729   router_KCCFLN05_t router;
730   router = xbt_new0(s_router_KCCFLN05_t, 1);
731
732   router->name = xbt_strdup(name);
733   router->id   = nb_routers++;
734   xbt_dict_set(router_set, name, router, router_free);
735 }
736
737 static void parse_routers(void)
738 {
739   //add a dumb router just to be GTNETS compatible
740   router_new(A_surfxml_router_name);
741 }
742
743 static void cpu_free(void *cpu)
744 {
745   free(((cpu_KCCFLN05_t) cpu)->name);
746   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->incomming_communications));
747   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->outgoing_communications));
748   free(cpu);
749 }
750
751 static cpu_KCCFLN05_t cpu_new(const char *name, double power_scale,
752                               double power_initial,
753                               tmgr_trace_t power_trace,
754                               e_surf_cpu_state_t state_initial,
755                               tmgr_trace_t state_trace,
756                               double interference_send,
757                               double interference_recv,
758                               double interference_send_recv,
759                               double max_outgoing_rate)
760 {
761   cpu_KCCFLN05_t cpu = xbt_new0(s_cpu_KCCFLN05_t, 1);
762
763   cpu->resource = (surf_resource_t) surf_workstation_resource;
764   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
765   cpu->name = xbt_strdup(name);
766   cpu->id = nb_workstation++;
767
768   cpu->power_scale = power_scale;
769   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
770
771   cpu->power_current = power_initial;
772   if (power_trace)
773     cpu->power_event =
774         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
775
776   cpu->state_current = state_initial;
777   if (state_trace)
778     cpu->state_event =
779         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
780
781   cpu->interference_send = interference_send;
782   cpu->interference_recv = interference_recv;
783   cpu->interference_send_recv = interference_send_recv;
784
785   cpu->constraint =
786       lmm_constraint_new(maxmin_system, cpu,
787                          cpu->power_current * cpu->power_scale);
788   if (max_outgoing_rate > 0)
789     cpu->bus = lmm_constraint_new(maxmin_system, cpu, max_outgoing_rate);
790
791   cpu->incomming_communications =
792       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
793   cpu->outgoing_communications =
794       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
795
796   xbt_dict_set(workstation_set, name, cpu, cpu_free);
797
798   return cpu;
799 }
800
801 static void parse_cpu(void)
802 {
803   double power_scale = 0.0;
804   double power_initial = 0.0;
805   tmgr_trace_t power_trace = NULL;
806   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
807   tmgr_trace_t state_trace = NULL;
808   double interference_send = 0.0;
809   double interference_recv = 0.0;
810   double interference_send_recv = 0.0;
811   double max_outgoing_rate = -1.0;
812
813   surf_parse_get_double(&power_scale, A_surfxml_cpu_power);
814   surf_parse_get_double(&power_initial, A_surfxml_cpu_availability);
815   surf_parse_get_trace(&power_trace, A_surfxml_cpu_availability_file);
816
817   xbt_assert0((A_surfxml_cpu_state == A_surfxml_cpu_state_ON) ||
818               (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF),
819               "Invalid state");
820   if (A_surfxml_cpu_state == A_surfxml_cpu_state_ON)
821     state_initial = SURF_CPU_ON;
822   if (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF)
823     state_initial = SURF_CPU_OFF;
824   surf_parse_get_trace(&state_trace, A_surfxml_cpu_state_file);
825
826   surf_parse_get_double(&interference_send,
827                         A_surfxml_cpu_interference_send);
828   surf_parse_get_double(&interference_recv,
829                         A_surfxml_cpu_interference_recv);
830   surf_parse_get_double(&interference_send_recv,
831                         A_surfxml_cpu_interference_send_recv);
832   surf_parse_get_double(&max_outgoing_rate,
833                         A_surfxml_cpu_max_outgoing_rate);
834
835   cpu_new(A_surfxml_cpu_name, power_scale, power_initial, power_trace,
836           state_initial, state_trace, interference_send, interference_recv,
837           interference_send_recv, max_outgoing_rate);
838 }
839
840 static void create_routing_table(void)
841 {
842   routing_table =
843       xbt_new0(s_route_KCCFLN05_t, nb_workstation * nb_workstation);
844 }
845
846 static void network_link_free(void *nw_link)
847 {
848   free(((network_link_KCCFLN05_t) nw_link)->name);
849   free(nw_link);
850 }
851
852 static network_link_KCCFLN05_t network_link_new(char *name,
853                                                 double bw_initial,
854                                                 tmgr_trace_t bw_trace,
855                                                 double lat_initial,
856                                                 tmgr_trace_t lat_trace,
857                                                 e_surf_network_link_state_t
858                                                 state_initial,
859                                                 tmgr_trace_t state_trace,
860                                                 e_surf_network_link_sharing_policy_t policy)
861 {
862   network_link_KCCFLN05_t nw_link = xbt_new0(s_network_link_KCCFLN05_t, 1);
863
864
865   nw_link->resource = (surf_resource_t) surf_workstation_resource;
866   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
867   nw_link->name = name;
868   nw_link->bw_current = bw_initial;
869   if (bw_trace)
870     nw_link->bw_event =
871         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
872   nw_link->state_current = state_initial;
873   nw_link->lat_current = lat_initial;
874   if (lat_trace)
875     nw_link->lat_event =
876         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
877   if (state_trace)
878     nw_link->state_event =
879         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
880
881   nw_link->constraint =
882       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
883
884   if(policy == SURF_NETWORK_LINK_FATPIPE)
885     lmm_constraint_shared(nw_link->constraint);
886
887   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
888
889   return nw_link;
890 }
891
892 static void parse_network_link(void)
893 {
894   char *name;
895   double bw_initial;
896   tmgr_trace_t bw_trace;
897   double lat_initial;
898   tmgr_trace_t lat_trace;
899   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
900   e_surf_network_link_sharing_policy_t policy_initial = SURF_NETWORK_LINK_SHARED;
901   tmgr_trace_t state_trace;
902
903   name = xbt_strdup(A_surfxml_network_link_name);
904   surf_parse_get_double(&bw_initial,A_surfxml_network_link_bandwidth);
905   surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
906   surf_parse_get_double(&lat_initial,A_surfxml_network_link_latency);
907   surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
908
909   xbt_assert0((A_surfxml_network_link_state==A_surfxml_network_link_state_ON)||
910               (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF),
911               "Invalid state");
912   if (A_surfxml_network_link_state==A_surfxml_network_link_state_ON) 
913     state_initial = SURF_NETWORK_LINK_ON;
914   else if (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF) 
915     state_initial = SURF_NETWORK_LINK_OFF;
916
917   if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_SHARED) 
918     policy_initial = SURF_NETWORK_LINK_SHARED;
919   else if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_FATPIPE) 
920     policy_initial = SURF_NETWORK_LINK_FATPIPE;
921
922   surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
923
924   network_link_new(name, bw_initial, bw_trace,
925                    lat_initial, lat_trace, state_initial, state_trace,
926                    policy_initial);
927 }
928
929 static void route_new(int src_id, int dst_id, network_link_KCCFLN05_t *link_list, int nb_link,
930                       double impact_on_src, double impact_on_dst,
931                       double impact_on_src_with_other_recv,
932                       double impact_on_dst_with_other_send)
933 {
934   route_KCCFLN05_t route = &(ROUTE(src_id, dst_id));
935
936   route->size = nb_link;
937   route->links = link_list = xbt_realloc(link_list, sizeof(network_link_KCCFLN05_t) * nb_link);
938   route->impact_on_src = impact_on_src;
939   route->impact_on_dst = impact_on_src;
940   route->impact_on_src_with_other_recv = impact_on_src_with_other_recv;
941   route->impact_on_dst_with_other_send = impact_on_dst_with_other_send;
942 }
943
944 static int nb_link;
945 static int link_list_capacity;
946 static network_link_KCCFLN05_t *link_list = NULL;
947 static int src_id = -1;
948 static int dst_id = -1;
949 static double impact_on_src;
950 static double impact_on_dst;
951 static double impact_on_src_with_other_recv;
952 static double impact_on_dst_with_other_send;
953
954 static void parse_route_set_endpoints(void)
955 {
956   cpu_KCCFLN05_t cpu_tmp = NULL;
957  
958   cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_src);
959   if(cpu_tmp != NULL) {
960     src_id = cpu_tmp->id;
961   }else {
962     xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_src),
963                "Invalid name '%s': neither a cpu nor a router!",
964                A_surfxml_route_src);
965     src_id=-1;
966     return;
967   }
968
969   cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_dst);
970   if(cpu_tmp != NULL) { 
971     dst_id = cpu_tmp->id;
972   }else {
973     xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_dst),
974                "Invalid name '%s': neither a cpu nor a router!",
975                A_surfxml_route_dst);
976     dst_id=-1;
977     return ;
978   }
979   
980   surf_parse_get_double(&impact_on_src, A_surfxml_route_impact_on_src);
981   surf_parse_get_double(&impact_on_dst, A_surfxml_route_impact_on_dst);
982   surf_parse_get_double(&impact_on_src_with_other_recv,
983                         A_surfxml_route_impact_on_src_with_other_recv);
984   surf_parse_get_double(&impact_on_dst_with_other_send,
985                         A_surfxml_route_impact_on_dst_with_other_send);
986   
987   nb_link = 0;
988   link_list_capacity = 1; 
989   link_list = xbt_new(network_link_KCCFLN05_t, link_list_capacity);
990
991 }
992
993 static void parse_route_elem(void)
994 {
995   xbt_ex_t e;
996   if (nb_link == link_list_capacity) {
997     link_list_capacity *= 2;
998     link_list = xbt_realloc(link_list, (link_list_capacity) * sizeof(network_link_KCCFLN05_t));
999   }
1000   TRY {
1001      link_list[nb_link++] = xbt_dict_get(network_link_set, A_surfxml_route_element_name);
1002   } CATCH(e) {
1003      RETHROW1("Link %s not found (dict raised this exception: %s)",A_surfxml_route_element_name);
1004   }
1005 }
1006
1007 static void parse_route_set_route(void)
1008 {
1009   if( src_id != -1 && dst_id != -1 )
1010   route_new(src_id, dst_id, link_list, nb_link, impact_on_src,
1011             impact_on_dst, impact_on_src_with_other_recv,
1012             impact_on_dst_with_other_send);
1013 }
1014
1015 static void parse_file(const char *file)
1016 {
1017   int i;
1018
1019   /* Figuring out the cpus */
1020   surf_parse_reset_parser();
1021   ETag_surfxml_cpu_fun = parse_cpu;
1022   surf_parse_open(file);
1023   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1024   surf_parse_close();
1025
1026   create_routing_table();
1027
1028   /* Figuring out the router (added after GTNETS) */
1029   surf_parse_reset_parser();
1030   STag_surfxml_router_fun=parse_routers;
1031   surf_parse_open(file);
1032   xbt_assert1((!surf_parse()),"Parse error in %s",file);
1033   surf_parse_close();
1034
1035   /* Figuring out the network links */
1036   surf_parse_reset_parser();
1037   ETag_surfxml_network_link_fun = parse_network_link;
1038   surf_parse_open(file);
1039   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1040   surf_parse_close();
1041
1042   /* Building the routes */
1043   surf_parse_reset_parser();
1044   STag_surfxml_route_fun = parse_route_set_endpoints;
1045   ETag_surfxml_route_element_fun = parse_route_elem;
1046   ETag_surfxml_route_fun = parse_route_set_route;
1047   surf_parse_open(file);
1048   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1049   surf_parse_close();
1050
1051   /* Adding loopback if needed */    
1052   for (i = 0; i < nb_workstation; i++) 
1053     if(!ROUTE(i,i).size) {
1054       if(!loopback)
1055         loopback = network_link_new(xbt_strdup("__MSG_loopback__"), 
1056                                    498000000, NULL, 0.000015, NULL, 
1057                                    SURF_NETWORK_LINK_ON, NULL,
1058                                    SURF_NETWORK_LINK_FATPIPE);
1059       ROUTE(i,i).size=1;
1060       ROUTE(i,i).links = xbt_new0(network_link_KCCFLN05_t, 1);
1061       ROUTE(i,i).links[0] = loopback;
1062     }
1063
1064 }
1065
1066 /**************************************/
1067 /********* Module  creation ***********/
1068 /**************************************/
1069
1070 static void resource_init_internal(void)
1071 {
1072   s_surf_action_t action;
1073
1074   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
1075
1076   surf_workstation_resource->common_private =
1077       xbt_new0(s_surf_resource_private_t, 1);
1078   surf_workstation_resource->common_public =
1079       xbt_new0(s_surf_resource_public_t, 1);
1080   surf_workstation_resource->extension_public =
1081       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
1082
1083   surf_workstation_resource->common_public->states.ready_action_set =
1084       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1085   surf_workstation_resource->common_public->states.running_action_set =
1086       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1087   surf_workstation_resource->common_public->states.failed_action_set =
1088       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1089   surf_workstation_resource->common_public->states.done_action_set =
1090       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1091
1092   surf_workstation_resource->common_public->name_service = name_service;
1093   surf_workstation_resource->common_public->get_resource_name = get_resource_name;
1094   surf_workstation_resource->common_public->action_get_state = surf_action_get_state;
1095   surf_workstation_resource->common_public->action_get_start_time =
1096       surf_action_get_start_time;
1097   surf_workstation_resource->common_public->action_get_finish_time =
1098       surf_action_get_finish_time;
1099   surf_workstation_resource->common_public->action_use = action_use;
1100   surf_workstation_resource->common_public->action_free = action_free;
1101   surf_workstation_resource->common_public->action_cancel = action_cancel;
1102   surf_workstation_resource->common_public->action_recycle = action_recycle;
1103   surf_workstation_resource->common_public->action_change_state = surf_action_change_state;
1104   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
1105   surf_workstation_resource->common_public->suspend = action_suspend;
1106   surf_workstation_resource->common_public->resume = action_resume;
1107   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
1108   surf_workstation_resource->common_public->set_max_duration = action_set_max_duration;
1109   surf_workstation_resource->common_public->set_priority = action_set_priority;
1110   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
1111
1112   surf_workstation_resource->common_private->resource_used = resource_used;
1113   surf_workstation_resource->common_private->share_resources = share_resources;
1114   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
1115   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
1116   surf_workstation_resource->common_private->finalize = finalize;
1117
1118   surf_workstation_resource->extension_public->execute = execute;
1119   surf_workstation_resource->extension_public->sleep = action_sleep;
1120   surf_workstation_resource->extension_public->get_state = resource_get_state;
1121   surf_workstation_resource->extension_public->get_speed = get_speed;
1122   surf_workstation_resource->extension_public->get_available_speed = get_available_speed;
1123   surf_workstation_resource->extension_public->communicate = communicate;
1124   surf_workstation_resource->extension_public->execute_parallel_task = execute_parallel_task;
1125   surf_workstation_resource->extension_public->get_route = get_route;
1126   surf_workstation_resource->extension_public->get_route_size = get_route_size;
1127   surf_workstation_resource->extension_public->get_link_name = get_link_name;
1128   surf_workstation_resource->extension_public->get_link_bandwidth = get_link_bandwidth;
1129   surf_workstation_resource->extension_public->get_link_latency = get_link_latency;
1130
1131   workstation_set  = xbt_dict_new();
1132   router_set       = xbt_dict_new();
1133   network_link_set = xbt_dict_new();
1134
1135   xbt_assert0(maxmin_system, "surf_init has to be called first!");
1136 }
1137
1138 /**************************************/
1139 /*************** Generic **************/
1140 /**************************************/
1141 void surf_workstation_resource_init_KCCFLN05(const char *filename)
1142 {
1143   xbt_assert0(!surf_cpu_resource, "CPU resource type already defined");
1144   xbt_assert0(!surf_network_resource, "network resource type already defined");
1145   resource_init_internal();
1146   parse_file(filename);
1147
1148   xbt_dynar_push(resource_list, &surf_workstation_resource);
1149 }