Logo AND Algorithmique Numérique Distribuée

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