Logo AND Algorithmique Numérique Distribuée

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