Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
* Moving things around (private->public)
[simgrid.git] / src / surf / workstation.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 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 "gras_config.h"
11 #include "workstation_private.h"
12 #include "cpu_private.h"
13 #include "network_private.h"
14
15 surf_workstation_resource_t surf_workstation_resource = NULL;
16 xbt_dict_t workstation_set = NULL;
17 static xbt_dict_t parallel_task_network_link_set = NULL;
18
19 static workstation_CLM03_t workstation_new(const char *name,
20                                      void *cpu, void *card)
21 {
22   workstation_CLM03_t workstation = xbt_new0(s_workstation_CLM03_t, 1);
23
24   workstation->resource = (surf_resource_t) surf_workstation_resource;
25   workstation->name = xbt_strdup(name);
26   workstation->cpu = cpu;
27   workstation->network_card = card;
28
29   return workstation;
30 }
31
32 static void workstation_free(void *workstation)
33 {
34   free(((workstation_CLM03_t)workstation)->name);
35   free(workstation);
36 }
37
38 static void create_workstations(void)
39 {
40   xbt_dict_cursor_t cursor = NULL;
41   char *name = NULL;
42   void *cpu = NULL;
43   void *nw_card = NULL;
44
45   xbt_dict_foreach(cpu_set, cursor, name, cpu) {
46     nw_card = xbt_dict_get_or_null(network_card_set, name);
47     xbt_assert1(nw_card,
48                 "No corresponding card found for %s",name);
49
50     xbt_dict_set(workstation_set, name,
51                  workstation_new(name, cpu, nw_card), workstation_free);
52   }
53 }
54
55 static void *name_service(const char *name)
56 {
57   return xbt_dict_get_or_null(workstation_set, name);
58 }
59
60 static const char *get_resource_name(void *resource_id)
61 {
62   return ((workstation_CLM03_t) resource_id)->name;
63 }
64
65 static int resource_used(void *resource_id)
66 {
67   xbt_assert0(0,
68               "Workstation is a virtual resource. I should not be there!");
69   return 0;
70 }
71
72 static int parallel_action_free(surf_action_t action)
73 {
74   action->using--;
75   if(!action->using) {
76     xbt_swag_remove(action, action->state_set);
77     if(((surf_action_parallel_task_CSL05_t)action)->variable)
78       lmm_variable_free(maxmin_system, ((surf_action_parallel_task_CSL05_t)action)->variable);
79     free(action);
80     return 1;
81   }
82   return 0;
83 }
84
85 static void parallel_action_use(surf_action_t action)
86 {
87   action->using++;
88 }
89
90 static int action_free(surf_action_t action)
91 {
92   if(action->resource_type==(surf_resource_t)surf_network_resource) 
93     return surf_network_resource->common_public->action_free(action);
94   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
95     return surf_cpu_resource->common_public->action_free(action);
96   else if(action->resource_type==(surf_resource_t)surf_workstation_resource)
97     return parallel_action_free(action);
98   else DIE_IMPOSSIBLE;
99   return 0;
100 }
101
102 static void action_use(surf_action_t action)
103 {
104   if(action->resource_type==(surf_resource_t)surf_network_resource) 
105     surf_network_resource->common_public->action_use(action);
106   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
107     surf_cpu_resource->common_public->action_use(action);
108   else if(action->resource_type==(surf_resource_t)surf_workstation_resource)
109     parallel_action_use(action);
110   else DIE_IMPOSSIBLE;
111   return;
112 }
113
114 static void action_cancel(surf_action_t action)
115 {
116   if(action->resource_type==(surf_resource_t)surf_network_resource) 
117     surf_network_resource->common_public->action_cancel(action);
118   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
119     surf_cpu_resource->common_public->action_cancel(action);
120   else if(action->resource_type==(surf_resource_t)surf_workstation_resource)
121     parallel_action_use(action);
122   else DIE_IMPOSSIBLE;
123   return;
124 }
125
126 static void action_recycle(surf_action_t action)
127 {
128   DIE_IMPOSSIBLE;
129   return;
130 }
131
132 static void action_change_state(surf_action_t action,
133                                 e_surf_action_state_t state)
134 {
135   if(action->resource_type==(surf_resource_t)surf_network_resource) 
136     surf_network_resource->common_public->action_change_state(action,state);
137   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
138     surf_cpu_resource->common_public->action_change_state(action,state);
139   else if(action->resource_type==(surf_resource_t)surf_workstation_resource)
140     surf_action_change_state(action, state);
141   else DIE_IMPOSSIBLE;
142   return;
143 }
144
145 static double share_resources(double now)
146 {
147   s_surf_action_parallel_task_CSL05_t action;
148   return generic_maxmin_share_resources(surf_workstation_resource->common_public->
149                                         states.running_action_set,
150                                         xbt_swag_offset(action, variable));
151 }
152
153 static void update_actions_state(double now, double delta)
154 {
155   surf_action_parallel_task_CSL05_t action = NULL;
156   surf_action_parallel_task_CSL05_t next_action = NULL;
157   xbt_swag_t running_actions =
158       surf_workstation_resource->common_public->states.running_action_set;
159   /* FIXME: unused
160   xbt_swag_t failed_actions =
161       surf_workstation_resource->common_public->states.failed_action_set;
162   */
163
164   xbt_swag_foreach_safe(action, next_action, running_actions) {
165     double_update(&(action->generic_action.remains),
166         lmm_variable_getvalue(action->variable) * delta);
167     if (action->generic_action.max_duration != NO_MAX_DURATION)
168       double_update(&(action->generic_action.max_duration), delta);
169     if ((action->generic_action.remains <= 0) && 
170         (lmm_get_variable_weight(action->variable)>0)) {
171       action->generic_action.finish = surf_get_clock();
172       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
173     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
174                (action->generic_action.max_duration <= 0)) {
175       action->generic_action.finish = surf_get_clock();
176       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
177     } else {                    /* Need to check that none of the resource has failed */
178       lmm_constraint_t cnst = NULL;
179       int i = 0;
180       surf_resource_t resource = NULL;
181
182       while ((cnst =
183               lmm_get_cnst_from_var(maxmin_system, action->variable,
184                                     i++))) {
185         resource = (surf_resource_t) lmm_constraint_id(cnst);
186         if(resource== (surf_resource_t) surf_cpu_resource) {
187           cpu_Cas01_t cpu = lmm_constraint_id(cnst);
188           if (cpu->state_current == SURF_CPU_OFF) {
189             action->generic_action.finish = surf_get_clock();
190             action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
191             break;
192           }
193         } else if (resource== (surf_resource_t) surf_network_resource) {
194           network_link_CM02_t nw_link = lmm_constraint_id(cnst);
195
196           if (nw_link->state_current == SURF_NETWORK_LINK_OFF) {
197             action->generic_action.finish = surf_get_clock();
198             action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
199             break;
200           }
201         } 
202       }
203     }
204   }
205
206   return;
207 }
208
209 static void update_resource_state(void *id,
210                                   tmgr_trace_event_t event_type,
211                                   double value)
212 {
213   return;
214 }
215
216 static surf_action_t execute(void *workstation, double size)
217 {
218   return surf_cpu_resource->extension_public->
219       execute(((workstation_CLM03_t) workstation)->cpu, size);
220 }
221
222 static surf_action_t action_sleep(void *workstation, double duration)
223 {
224   return surf_cpu_resource->extension_public->
225       sleep(((workstation_CLM03_t) workstation)->cpu, duration);
226 }
227
228 static void action_suspend(surf_action_t action)
229 {
230   if(action->resource_type==(surf_resource_t)surf_network_resource) 
231     surf_network_resource->common_public->suspend(action);
232   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
233     surf_cpu_resource->common_public->suspend(action);
234   else DIE_IMPOSSIBLE;
235 }
236
237 static void action_resume(surf_action_t action)
238 {
239   if(action->resource_type==(surf_resource_t)surf_network_resource)
240     surf_network_resource->common_public->resume(action);
241   else if(action->resource_type==(surf_resource_t)surf_cpu_resource)
242     surf_cpu_resource->common_public->resume(action);
243   else DIE_IMPOSSIBLE;
244 }
245
246 static int action_is_suspended(surf_action_t action)
247 {
248   if(action->resource_type==(surf_resource_t)surf_network_resource) 
249     return surf_network_resource->common_public->is_suspended(action);
250   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
251     return surf_cpu_resource->common_public->is_suspended(action);
252   DIE_IMPOSSIBLE;
253 }
254
255 static void action_set_max_duration(surf_action_t action, double duration)
256 {
257   if(action->resource_type==(surf_resource_t)surf_network_resource)
258     surf_network_resource->common_public->set_max_duration(action,duration);
259   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
260     surf_cpu_resource->common_public->set_max_duration(action,duration);
261   else  DIE_IMPOSSIBLE;
262 }
263  
264 static void action_set_priority(surf_action_t action, double priority)
265 {
266   if(action->resource_type==(surf_resource_t)surf_network_resource)
267     surf_network_resource->common_public->set_priority(action,priority);
268   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
269     surf_cpu_resource->common_public->set_priority(action,priority);
270   else  DIE_IMPOSSIBLE;
271 }
272
273 static surf_action_t communicate(void *workstation_src,
274                                  void *workstation_dst, double size,
275                                  double rate)
276 {
277   return surf_network_resource->extension_public->
278       communicate(((workstation_CLM03_t) workstation_src)->network_card,
279                   ((workstation_CLM03_t) workstation_dst)->network_card, size, rate);
280 }
281
282 static e_surf_cpu_state_t get_state(void *workstation)
283 {
284   return surf_cpu_resource->extension_public->
285       get_state(((workstation_CLM03_t) workstation)->cpu);
286 }
287
288 static double get_speed(void *workstation, double load)
289 {
290   return surf_cpu_resource->extension_public->
291       get_speed(((workstation_CLM03_t) workstation)->cpu, load);
292 }
293
294 static double get_available_speed(void *workstation)
295 {
296   return surf_cpu_resource->extension_public->
297       get_available_speed(((workstation_CLM03_t) workstation)->cpu);
298 }
299
300 static surf_action_t execute_parallel_task_bogus (int workstation_nb,
301                                             void **workstation_list,
302                                             double *computation_amount,
303                                             double *communication_amount,
304                                             double amount,
305                                             double rate)
306 {
307   DIE_IMPOSSIBLE;
308 }
309
310 static surf_action_t execute_parallel_task (int workstation_nb,
311                                             void **workstation_list,
312                                             double *computation_amount,
313                                             double *communication_amount,
314                                             double amount,
315                                             double rate)
316 {
317   surf_action_parallel_task_CSL05_t action = NULL;
318   int i, j, k;
319   int nb_link = 0;
320   int nb_host = 0;
321
322   if (parallel_task_network_link_set == NULL) {
323     parallel_task_network_link_set = xbt_dict_new_ext(workstation_nb * workstation_nb * 10);
324   }
325
326   /* Compute the number of affected resources... */
327   for(i=0; i< workstation_nb; i++) {
328     for(j=0; j< workstation_nb; j++) {
329       network_card_CM02_t card_src = ((workstation_CLM03_t*)workstation_list)[i]->network_card;
330       network_card_CM02_t card_dst = ((workstation_CLM03_t*)workstation_list)[j]->network_card;
331       int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
332       network_link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
333       
334       if(communication_amount[i*workstation_nb+j]>0)
335         for(k=0; k< route_size; k++) {
336           xbt_dict_set(parallel_task_network_link_set, route[k]->name, route[k], NULL);
337         }
338     }
339   }
340
341   nb_link = xbt_dict_length(parallel_task_network_link_set);
342   xbt_dict_reset(parallel_task_network_link_set);
343
344   for (i = 0; i<workstation_nb; i++)
345     if(computation_amount[i]>0) nb_host++;
346  
347   if(nb_link + workstation_nb == 0)
348     return NULL;
349
350   action = xbt_new0(s_surf_action_parallel_task_CSL05_t, 1);
351   action->generic_action.using = 1;
352   action->generic_action.cost = amount;
353   action->generic_action.remains = amount;
354   action->generic_action.max_duration = NO_MAX_DURATION;
355   action->generic_action.start = surf_get_clock();
356   action->generic_action.finish = -1.0;
357   action->generic_action.resource_type =
358       (surf_resource_t) surf_workstation_resource;
359   action->suspended = 0;  /* Should be useless because of the
360                              calloc but it seems to help valgrind... */
361   action->generic_action.state_set =
362       surf_workstation_resource->common_public->states.running_action_set;
363
364   xbt_swag_insert(action, action->generic_action.state_set);
365   action->rate = rate;
366
367   if(action->rate>0)
368     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
369                                         nb_host + nb_link);
370   else   
371     action->variable = lmm_variable_new(maxmin_system, action, 1.0, action->rate,
372                                         nb_host + nb_link);
373
374   for (i = 0; i<workstation_nb; i++)
375     if(computation_amount[i]>0)
376       lmm_expand(maxmin_system, ((cpu_Cas01_t) ((workstation_CLM03_t) workstation_list[i])->cpu)->constraint, 
377                  action->variable, computation_amount[i]);
378
379   for (i=0; i<workstation_nb; i++) {
380     for(j=0; j< workstation_nb; j++) {
381       network_card_CM02_t card_src = ((workstation_CLM03_t*)workstation_list)[i]->network_card;
382       network_card_CM02_t card_dst = ((workstation_CLM03_t*)workstation_list)[j]->network_card;
383       int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
384       network_link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
385       
386       for(k=0; k< route_size; k++) {
387         if(communication_amount[i*workstation_nb+j]>0) {
388           lmm_expand_add(maxmin_system, route[k]->constraint, 
389                        action->variable, communication_amount[i*workstation_nb+j]);
390         }
391       }
392     }
393   }
394   
395   return (surf_action_t) action;
396 }
397
398 /* returns an array of network_link_CM02_t */
399 static const void** get_route(void *src, void *dst) {
400   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
401   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
402   return surf_network_resource->extension_public->get_route(workstation_src->network_card, workstation_dst->network_card);
403 }
404
405 static int get_route_size(void *src, void *dst) {
406   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
407   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
408   return surf_network_resource->extension_public->get_route_size(workstation_src->network_card, workstation_dst->network_card);
409 }
410
411 static const char *get_link_name(const void *link) {
412   return surf_network_resource->extension_public->get_link_name(link);
413 }
414
415 static double get_link_bandwidth(const void *link) {
416   return surf_network_resource->extension_public->get_link_bandwidth(link);
417 }
418
419 static double get_link_latency(const void *link) {
420   return surf_network_resource->extension_public->get_link_latency(link); 
421 }
422
423 static void finalize(void)
424 {
425   xbt_dict_free(&workstation_set);
426   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
427   xbt_swag_free(surf_workstation_resource->common_public->states.
428                 running_action_set);
429   xbt_swag_free(surf_workstation_resource->common_public->states.
430                 failed_action_set);
431   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
432
433   free(surf_workstation_resource->common_public);
434   free(surf_workstation_resource->common_private);
435   free(surf_workstation_resource->extension_public);
436
437   free(surf_workstation_resource);
438   surf_workstation_resource = NULL;
439 }
440
441 static void surf_workstation_resource_init_internal(void)
442 {
443   s_surf_action_t action;
444
445   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
446
447   surf_workstation_resource->common_private =
448       xbt_new0(s_surf_resource_private_t, 1);
449   surf_workstation_resource->common_public =
450       xbt_new0(s_surf_resource_public_t, 1);
451 /*   surf_workstation_resource->extension_private = xbt_new0(s_surf_workstation_resource_extension_private_t,1); */
452   surf_workstation_resource->extension_public =
453       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
454
455   surf_workstation_resource->common_public->states.ready_action_set =
456       xbt_swag_new(xbt_swag_offset(action, state_hookup));
457   surf_workstation_resource->common_public->states.running_action_set =
458       xbt_swag_new(xbt_swag_offset(action, state_hookup));
459   surf_workstation_resource->common_public->states.failed_action_set =
460       xbt_swag_new(xbt_swag_offset(action, state_hookup));
461   surf_workstation_resource->common_public->states.done_action_set =
462       xbt_swag_new(xbt_swag_offset(action, state_hookup));
463
464   surf_workstation_resource->common_public->name_service = name_service;
465   surf_workstation_resource->common_public->get_resource_name =
466       get_resource_name;
467   surf_workstation_resource->common_public->action_get_state =
468       surf_action_get_state;
469   surf_workstation_resource->common_public->action_get_start_time =
470       surf_action_get_start_time;
471   surf_workstation_resource->common_public->action_get_finish_time =
472       surf_action_get_finish_time;
473   surf_workstation_resource->common_public->action_free = action_free;
474   surf_workstation_resource->common_public->action_use = action_use;
475   surf_workstation_resource->common_public->action_cancel = action_cancel;
476   surf_workstation_resource->common_public->action_recycle =
477       action_recycle;
478   surf_workstation_resource->common_public->action_change_state =
479       action_change_state;
480   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
481   surf_workstation_resource->common_public->name = "Workstation";
482
483   surf_workstation_resource->common_private->resource_used = resource_used;
484   surf_workstation_resource->common_private->share_resources =
485       share_resources;
486   surf_workstation_resource->common_private->update_actions_state =
487       update_actions_state;
488   surf_workstation_resource->common_private->update_resource_state =
489       update_resource_state;
490   surf_workstation_resource->common_private->finalize = finalize;
491
492   surf_workstation_resource->common_public->suspend = action_suspend;
493   surf_workstation_resource->common_public->resume = action_resume;
494   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
495   surf_workstation_resource->common_public->set_max_duration = action_set_max_duration;
496   surf_workstation_resource->common_public->set_priority = action_set_priority;
497
498   surf_workstation_resource->extension_public->execute = execute;
499   surf_workstation_resource->extension_public->sleep = action_sleep;
500   surf_workstation_resource->extension_public->get_state = get_state;
501   surf_workstation_resource->extension_public->get_speed = get_speed;
502   surf_workstation_resource->extension_public->get_available_speed = get_available_speed;
503   surf_workstation_resource->extension_public->communicate = communicate;
504   surf_workstation_resource->extension_public->execute_parallel_task = 
505     execute_parallel_task_bogus;
506   surf_workstation_resource->extension_public->get_route = get_route;
507   surf_workstation_resource->extension_public->get_route_size = get_route_size;
508   surf_workstation_resource->extension_public->get_link_name = get_link_name;
509   surf_workstation_resource->extension_public->get_link_bandwidth = get_link_bandwidth;
510   surf_workstation_resource->extension_public->get_link_latency = get_link_latency;
511   workstation_set = xbt_dict_new();
512
513   xbt_assert0(maxmin_system, "surf_init has to be called first!");
514 }
515
516 /********************************************************************/
517 /* The model used in MSG and presented at CCGrid03                  */
518 /********************************************************************/
519 /* @InProceedings{Casanova.CLM_03, */
520 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
521 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
522 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
523 /*   publisher = {"IEEE Computer Society Press"}, */
524 /*   month = {may}, */
525 /*   year = {2003} */
526 /* } */
527 void surf_workstation_resource_init_CLM03(const char *filename)
528 {
529   surf_workstation_resource_init_internal();
530   surf_cpu_resource_init_Cas01(filename);
531   surf_network_resource_init_CM02(filename);
532   create_workstations();
533   xbt_dynar_push(resource_list, &surf_workstation_resource);
534 }
535
536 #ifdef USE_GTNETS
537 /* KF. Use GTNetS for the network. */
538 void surf_workstation_resource_init_GTNETS(const char *filename)
539 {
540   surf_workstation_resource_init_internal();
541   surf_cpu_resource_init_Cas01(filename);
542   surf_network_resource_init_GTNETS(filename);
543   create_workstations();
544   xbt_dynar_push(resource_list, &surf_workstation_resource);
545 }
546 #endif
547
548 void surf_workstation_resource_init_compound(const char *filename)
549 {
550
551   xbt_assert0(surf_cpu_resource,"No CPU resource defined yet!");
552   xbt_assert0(surf_network_resource,"No network resource defined yet!");
553   surf_workstation_resource_init_internal();
554   create_workstations();
555   xbt_dynar_push(resource_list, &surf_workstation_resource);
556 }