Logo AND Algorithmique Numérique Distribuée

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