Logo AND Algorithmique Numérique Distribuée

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