Logo AND Algorithmique Numérique Distribuée

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