Logo AND Algorithmique Numérique Distribuée

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