Logo AND Algorithmique Numérique Distribuée

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