Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9108c319b93cf54aa299b644ce09e61f1242b2d2
[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   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
679   DEBUG3("Creating a parallel task (%p) with %d cpus and %d links.",
680          action, nb_host, nb_link);
681   action->generic_action.using = 1;
682   action->generic_action.cost = amount;
683   action->generic_action.remains = amount;
684   action->generic_action.max_duration = NO_MAX_DURATION;
685   action->generic_action.start = -1.0;
686   action->generic_action.finish = -1.0;
687   action->generic_action.resource_type =
688       (surf_resource_t) surf_workstation_resource;
689   action->suspended = 0;        /* Should be useless because of the
690                                    calloc but it seems to help valgrind... */
691   action->generic_action.state_set =
692       surf_workstation_resource->common_public->states.running_action_set;
693
694   xbt_swag_insert(action, action->generic_action.state_set);
695   action->rate = rate;
696
697   if (action->rate > 0)
698     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
699                                         nb_host + nb_link);
700   else
701     action->variable =
702         lmm_variable_new(maxmin_system, action, 1.0, action->rate,
703                          nb_host + nb_link);
704
705   for (i = 0; i < workstation_nb; i++)
706     if (computation_amount[i] > 0)
707       lmm_expand(maxmin_system,
708                  ((cpu_KCCFLN05_t) workstation_list[i])->constraint,
709                  action->variable, computation_amount[i]);
710
711   for (i = 0; i < workstation_nb; i++) {
712     for (j = 0; j < workstation_nb; j++) {
713       cpu_KCCFLN05_t card_src = workstation_list[i];
714       cpu_KCCFLN05_t card_dst = workstation_list[j];
715       int route_size = ROUTE(card_src->id, card_dst->id).size;
716       network_link_KCCFLN05_t *route =
717           ROUTE(card_src->id, card_dst->id).links;
718
719       for (k = 0; k < route_size; k++) {
720         if (communication_amount[i * workstation_nb + j] > 0) {
721           lmm_expand_add(maxmin_system, route[k]->constraint,
722                          action->variable,
723                          communication_amount[i * workstation_nb + j]);
724         }
725       }
726     }
727   }
728
729   if (nb_link + nb_host == 0) {
730     action->generic_action.cost = 1.0;
731     action->generic_action.remains = 0.0;
732   }
733
734   return (surf_action_t) action;
735 }
736
737 /* returns an array of network_link_KCCFLN05_t */
738 static const void **get_route(void *src, void *dst)
739 {
740   cpu_KCCFLN05_t card_src = src;
741   cpu_KCCFLN05_t card_dst = dst;
742   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
743
744   return (const void **) route->links;
745 }
746
747 static int get_route_size(void *src, void *dst)
748 {
749   cpu_KCCFLN05_t card_src = src;
750   cpu_KCCFLN05_t card_dst = dst;
751   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
752   return route->size;
753 }
754
755 static const char *get_link_name(const void *link)
756 {
757   return ((network_link_KCCFLN05_t) link)->name;
758 }
759
760 static double get_link_bandwidth(const void *link)
761 {
762   return ((network_link_KCCFLN05_t) link)->bw_current;
763 }
764
765 static double get_link_latency(const void *link)
766 {
767   return ((network_link_KCCFLN05_t) link)->lat_current;
768 }
769
770 /**************************************/
771 /*** Resource Creation & Destruction **/
772 /**************************************/
773
774
775 static void router_free(void *router)
776 {
777   free(((router_KCCFLN05_t) router)->name);
778 }
779
780 static void router_new(const char *name)
781 {
782   static unsigned int nb_routers = 0;
783
784   INFO1("Creating a router %s", name);
785
786   router_KCCFLN05_t router;
787   router = xbt_new0(s_router_KCCFLN05_t, 1);
788
789   router->name = xbt_strdup(name);
790   router->id = nb_routers++;
791   xbt_dict_set(router_set, name, router, router_free);
792 }
793
794 static void parse_routers(void)
795 {
796   //add a dumb router just to be GTNETS compatible
797   router_new(A_surfxml_router_name);
798 }
799
800 static void cpu_free(void *cpu)
801 {
802   free(((cpu_KCCFLN05_t) cpu)->name);
803   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->incomming_communications));
804   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->outgoing_communications));
805   free(cpu);
806 }
807
808 static cpu_KCCFLN05_t cpu_new(const char *name, double power_scale,
809                               double power_initial,
810                               tmgr_trace_t power_trace,
811                               e_surf_cpu_state_t state_initial,
812                               tmgr_trace_t state_trace,
813                               double interference_send,
814                               double interference_recv,
815                               double interference_send_recv,
816                               double max_outgoing_rate)
817 {
818   cpu_KCCFLN05_t cpu = xbt_new0(s_cpu_KCCFLN05_t, 1);
819
820   cpu->resource = (surf_resource_t) surf_workstation_resource;
821   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
822   cpu->name = xbt_strdup(name);
823   cpu->id = nb_workstation++;
824
825   cpu->power_scale = power_scale;
826   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
827
828   cpu->power_current = power_initial;
829   if (power_trace)
830     cpu->power_event =
831         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
832
833   cpu->state_current = state_initial;
834   if (state_trace)
835     cpu->state_event =
836         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
837
838   cpu->interference_send = interference_send;
839   cpu->interference_recv = interference_recv;
840   cpu->interference_send_recv = interference_send_recv;
841
842   cpu->constraint =
843       lmm_constraint_new(maxmin_system, cpu,
844                          cpu->power_current * cpu->power_scale);
845   if (max_outgoing_rate > 0)
846     cpu->bus = lmm_constraint_new(maxmin_system, cpu, max_outgoing_rate);
847
848   cpu->incomming_communications =
849       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
850   cpu->outgoing_communications =
851       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
852
853   xbt_dict_set(workstation_set, name, cpu, cpu_free);
854
855   return cpu;
856 }
857
858 static void parse_cpu(void)
859 {
860   double power_scale = 0.0;
861   double power_initial = 0.0;
862   tmgr_trace_t power_trace = NULL;
863   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
864   tmgr_trace_t state_trace = NULL;
865   double interference_send = 0.0;
866   double interference_recv = 0.0;
867   double interference_send_recv = 0.0;
868   double max_outgoing_rate = -1.0;
869
870   surf_parse_get_double(&power_scale, A_surfxml_cpu_power);
871   surf_parse_get_double(&power_initial, A_surfxml_cpu_availability);
872   surf_parse_get_trace(&power_trace, A_surfxml_cpu_availability_file);
873
874   xbt_assert0((A_surfxml_cpu_state == A_surfxml_cpu_state_ON) ||
875               (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF),
876               "Invalid state");
877   if (A_surfxml_cpu_state == A_surfxml_cpu_state_ON)
878     state_initial = SURF_CPU_ON;
879   if (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF)
880     state_initial = SURF_CPU_OFF;
881   surf_parse_get_trace(&state_trace, A_surfxml_cpu_state_file);
882
883   surf_parse_get_double(&interference_send,
884                         A_surfxml_cpu_interference_send);
885   surf_parse_get_double(&interference_recv,
886                         A_surfxml_cpu_interference_recv);
887   surf_parse_get_double(&interference_send_recv,
888                         A_surfxml_cpu_interference_send_recv);
889   surf_parse_get_double(&max_outgoing_rate,
890                         A_surfxml_cpu_max_outgoing_rate);
891
892   cpu_new(A_surfxml_cpu_name, power_scale, power_initial, power_trace,
893           state_initial, state_trace, interference_send, interference_recv,
894           interference_send_recv, max_outgoing_rate);
895 }
896
897 static void create_routing_table(void)
898 {
899   routing_table =
900       xbt_new0(s_route_KCCFLN05_t, nb_workstation * nb_workstation);
901 }
902
903 static void network_link_free(void *nw_link)
904 {
905   free(((network_link_KCCFLN05_t) nw_link)->name);
906   free(nw_link);
907 }
908
909 static network_link_KCCFLN05_t network_link_new(char *name,
910                                                 double bw_initial,
911                                                 tmgr_trace_t bw_trace,
912                                                 double lat_initial,
913                                                 tmgr_trace_t lat_trace,
914                                                 e_surf_network_link_state_t
915                                                 state_initial,
916                                                 tmgr_trace_t state_trace,
917                                                 e_surf_network_link_sharing_policy_t
918                                                 policy)
919 {
920   network_link_KCCFLN05_t nw_link = xbt_new0(s_network_link_KCCFLN05_t, 1);
921
922
923   nw_link->resource = (surf_resource_t) surf_workstation_resource;
924   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
925   nw_link->name = name;
926   nw_link->bw_current = bw_initial;
927   if (bw_trace)
928     nw_link->bw_event =
929         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
930   nw_link->state_current = state_initial;
931   nw_link->lat_current = lat_initial;
932   if (lat_trace)
933     nw_link->lat_event =
934         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
935   if (state_trace)
936     nw_link->state_event =
937         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
938
939   nw_link->constraint =
940       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
941
942   if (policy == SURF_NETWORK_LINK_FATPIPE)
943     lmm_constraint_shared(nw_link->constraint);
944
945   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
946
947   return nw_link;
948 }
949
950 static void parse_network_link(void)
951 {
952   char *name;
953   double bw_initial;
954   tmgr_trace_t bw_trace;
955   double lat_initial;
956   tmgr_trace_t lat_trace;
957   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
958   e_surf_network_link_sharing_policy_t policy_initial =
959       SURF_NETWORK_LINK_SHARED;
960   tmgr_trace_t state_trace;
961
962   name = xbt_strdup(A_surfxml_network_link_name);
963   surf_parse_get_double(&bw_initial, A_surfxml_network_link_bandwidth);
964   surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
965   surf_parse_get_double(&lat_initial, A_surfxml_network_link_latency);
966   surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
967
968   xbt_assert0((A_surfxml_network_link_state ==
969                A_surfxml_network_link_state_ON)
970               || (A_surfxml_network_link_state ==
971                   A_surfxml_network_link_state_OFF), "Invalid state");
972   if (A_surfxml_network_link_state == A_surfxml_network_link_state_ON)
973     state_initial = SURF_NETWORK_LINK_ON;
974   else if (A_surfxml_network_link_state ==
975            A_surfxml_network_link_state_OFF)
976     state_initial = SURF_NETWORK_LINK_OFF;
977
978   if (A_surfxml_network_link_sharing_policy ==
979       A_surfxml_network_link_sharing_policy_SHARED)
980     policy_initial = SURF_NETWORK_LINK_SHARED;
981   else if (A_surfxml_network_link_sharing_policy ==
982            A_surfxml_network_link_sharing_policy_FATPIPE)
983     policy_initial = SURF_NETWORK_LINK_FATPIPE;
984
985   surf_parse_get_trace(&state_trace, A_surfxml_network_link_state_file);
986
987   network_link_new(name, bw_initial, bw_trace,
988                    lat_initial, lat_trace, state_initial, state_trace,
989                    policy_initial);
990 }
991
992 static void route_new(int src_id, int dst_id,
993                       network_link_KCCFLN05_t * link_list, int nb_link,
994                       double impact_on_src, double impact_on_dst,
995                       double impact_on_src_with_other_recv,
996                       double impact_on_dst_with_other_send)
997 {
998   route_KCCFLN05_t route = &(ROUTE(src_id, dst_id));
999
1000   route->size = nb_link;
1001   route->links = link_list =
1002       xbt_realloc(link_list, sizeof(network_link_KCCFLN05_t) * nb_link);
1003   route->impact_on_src = impact_on_src;
1004   route->impact_on_dst = impact_on_src;
1005   route->impact_on_src_with_other_recv = impact_on_src_with_other_recv;
1006   route->impact_on_dst_with_other_send = impact_on_dst_with_other_send;
1007 }
1008
1009 static int nb_link;
1010 static int link_list_capacity;
1011 static network_link_KCCFLN05_t *link_list = NULL;
1012 static int src_id = -1;
1013 static int dst_id = -1;
1014 static double impact_on_src;
1015 static double impact_on_dst;
1016 static double impact_on_src_with_other_recv;
1017 static double impact_on_dst_with_other_send;
1018
1019 static void parse_route_set_endpoints(void)
1020 {
1021   cpu_KCCFLN05_t cpu_tmp = NULL;
1022
1023   cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_src);
1024   if (cpu_tmp != NULL) {
1025     src_id = cpu_tmp->id;
1026   } else {
1027     xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_src),
1028                 "Invalid name '%s': neither a cpu nor a router!",
1029                 A_surfxml_route_src);
1030     src_id = -1;
1031     return;
1032   }
1033
1034   cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_dst);
1035   if (cpu_tmp != NULL) {
1036     dst_id = cpu_tmp->id;
1037   } else {
1038     xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_dst),
1039                 "Invalid name '%s': neither a cpu nor a router!",
1040                 A_surfxml_route_dst);
1041     dst_id = -1;
1042     return;
1043   }
1044
1045   surf_parse_get_double(&impact_on_src, A_surfxml_route_impact_on_src);
1046   surf_parse_get_double(&impact_on_dst, A_surfxml_route_impact_on_dst);
1047   surf_parse_get_double(&impact_on_src_with_other_recv,
1048                         A_surfxml_route_impact_on_src_with_other_recv);
1049   surf_parse_get_double(&impact_on_dst_with_other_send,
1050                         A_surfxml_route_impact_on_dst_with_other_send);
1051
1052   nb_link = 0;
1053   link_list_capacity = 1;
1054   link_list = xbt_new(network_link_KCCFLN05_t, link_list_capacity);
1055
1056 }
1057
1058 static void parse_route_elem(void)
1059 {
1060   xbt_ex_t e;
1061   if (nb_link == link_list_capacity) {
1062     link_list_capacity *= 2;
1063     link_list =
1064         xbt_realloc(link_list,
1065                     (link_list_capacity) *
1066                     sizeof(network_link_KCCFLN05_t));
1067   }
1068   TRY {
1069     link_list[nb_link++] =
1070         xbt_dict_get(network_link_set, A_surfxml_route_element_name);
1071   }
1072   CATCH(e) {
1073     RETHROW1("Link %s not found (dict raised this exception: %s)",
1074              A_surfxml_route_element_name);
1075   }
1076 }
1077
1078 static void parse_route_set_route(void)
1079 {
1080   if (src_id != -1 && dst_id != -1)
1081     route_new(src_id, dst_id, link_list, nb_link, impact_on_src,
1082               impact_on_dst, impact_on_src_with_other_recv,
1083               impact_on_dst_with_other_send);
1084 }
1085
1086 static void parse_file(const char *file)
1087 {
1088   int i;
1089
1090   /* Figuring out the cpus */
1091   surf_parse_reset_parser();
1092   ETag_surfxml_cpu_fun = parse_cpu;
1093   surf_parse_open(file);
1094   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1095   surf_parse_close();
1096
1097   create_routing_table();
1098
1099   /* Figuring out the router (added after GTNETS) */
1100   surf_parse_reset_parser();
1101   STag_surfxml_router_fun = parse_routers;
1102   surf_parse_open(file);
1103   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1104   surf_parse_close();
1105
1106   /* Figuring out the network links */
1107   surf_parse_reset_parser();
1108   ETag_surfxml_network_link_fun = parse_network_link;
1109   surf_parse_open(file);
1110   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1111   surf_parse_close();
1112
1113   /* Building the routes */
1114   surf_parse_reset_parser();
1115   STag_surfxml_route_fun = parse_route_set_endpoints;
1116   ETag_surfxml_route_element_fun = parse_route_elem;
1117   ETag_surfxml_route_fun = parse_route_set_route;
1118   surf_parse_open(file);
1119   xbt_assert1((!surf_parse()), "Parse error in %s", file);
1120   surf_parse_close();
1121
1122   /* Adding loopback if needed */
1123   for (i = 0; i < nb_workstation; i++)
1124     if (!ROUTE(i, i).size) {
1125       if (!loopback)
1126         loopback = network_link_new(xbt_strdup("__MSG_loopback__"),
1127                                     498000000, NULL, 0.000015, NULL,
1128                                     SURF_NETWORK_LINK_ON, NULL,
1129                                     SURF_NETWORK_LINK_FATPIPE);
1130       ROUTE(i, i).size = 1;
1131       ROUTE(i, i).links = xbt_new0(network_link_KCCFLN05_t, 1);
1132       ROUTE(i, i).links[0] = loopback;
1133     }
1134
1135 }
1136
1137 /**************************************/
1138 /********* Module  creation ***********/
1139 /**************************************/
1140
1141 static void resource_init_internal(void)
1142 {
1143   s_surf_action_t action;
1144
1145   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
1146
1147   surf_workstation_resource->common_private =
1148       xbt_new0(s_surf_resource_private_t, 1);
1149   surf_workstation_resource->common_public =
1150       xbt_new0(s_surf_resource_public_t, 1);
1151   surf_workstation_resource->extension_public =
1152       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
1153
1154   surf_workstation_resource->common_public->states.ready_action_set =
1155       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1156   surf_workstation_resource->common_public->states.running_action_set =
1157       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1158   surf_workstation_resource->common_public->states.failed_action_set =
1159       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1160   surf_workstation_resource->common_public->states.done_action_set =
1161       xbt_swag_new(xbt_swag_offset(action, state_hookup));
1162
1163   surf_workstation_resource->common_public->name_service = name_service;
1164   surf_workstation_resource->common_public->get_resource_name =
1165       get_resource_name;
1166   surf_workstation_resource->common_public->action_get_state =
1167       surf_action_get_state;
1168   surf_workstation_resource->common_public->action_get_start_time =
1169       surf_action_get_start_time;
1170   surf_workstation_resource->common_public->action_get_finish_time =
1171       surf_action_get_finish_time;
1172   surf_workstation_resource->common_public->action_use = action_use;
1173   surf_workstation_resource->common_public->action_free = action_free;
1174   surf_workstation_resource->common_public->action_cancel = action_cancel;
1175   surf_workstation_resource->common_public->action_recycle =
1176       action_recycle;
1177   surf_workstation_resource->common_public->action_change_state =
1178       surf_action_change_state;
1179   surf_workstation_resource->common_public->action_set_data =
1180       surf_action_set_data;
1181   surf_workstation_resource->common_public->suspend = action_suspend;
1182   surf_workstation_resource->common_public->resume = action_resume;
1183   surf_workstation_resource->common_public->is_suspended =
1184       action_is_suspended;
1185   surf_workstation_resource->common_public->set_max_duration =
1186       action_set_max_duration;
1187   surf_workstation_resource->common_public->set_priority =
1188       action_set_priority;
1189   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
1190
1191   surf_workstation_resource->common_private->resource_used = resource_used;
1192   surf_workstation_resource->common_private->share_resources =
1193       share_resources;
1194   surf_workstation_resource->common_private->update_actions_state =
1195       update_actions_state;
1196   surf_workstation_resource->common_private->update_resource_state =
1197       update_resource_state;
1198   surf_workstation_resource->common_private->finalize = finalize;
1199
1200   surf_workstation_resource->extension_public->execute = execute;
1201   surf_workstation_resource->extension_public->sleep = action_sleep;
1202   surf_workstation_resource->extension_public->get_state =
1203       resource_get_state;
1204   surf_workstation_resource->extension_public->get_speed = get_speed;
1205   surf_workstation_resource->extension_public->get_available_speed =
1206       get_available_speed;
1207   surf_workstation_resource->extension_public->communicate = communicate;
1208   surf_workstation_resource->extension_public->execute_parallel_task =
1209       execute_parallel_task;
1210   surf_workstation_resource->extension_public->get_route = get_route;
1211   surf_workstation_resource->extension_public->get_route_size =
1212       get_route_size;
1213   surf_workstation_resource->extension_public->get_link_name =
1214       get_link_name;
1215   surf_workstation_resource->extension_public->get_link_bandwidth =
1216       get_link_bandwidth;
1217   surf_workstation_resource->extension_public->get_link_latency =
1218       get_link_latency;
1219
1220   workstation_set = xbt_dict_new();
1221   router_set = xbt_dict_new();
1222   network_link_set = xbt_dict_new();
1223
1224   xbt_assert0(maxmin_system, "surf_init has to be called first!");
1225 }
1226
1227 /**************************************/
1228 /*************** Generic **************/
1229 /**************************************/
1230 void surf_workstation_resource_init_KCCFLN05(const char *filename)
1231 {
1232   xbt_assert0(!surf_cpu_resource, "CPU resource type already defined");
1233   xbt_assert0(!surf_network_resource,
1234               "network resource type already defined");
1235   resource_init_internal();
1236   parse_file(filename);
1237
1238   xbt_dynar_push(resource_list, &surf_workstation_resource);
1239 }