Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update workstation handling in SimDag. Change the prefix of SimDag functions with...
[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 surf_action_t execute_parallel_task (int workstation_nb,
294                                             void **workstation_list,
295                                             double *computation_amount,
296                                             double *communication_amount,
297                                             double amount,
298                                             double rate)
299 {
300   surf_action_parallel_task_CSL05_t action = NULL;
301   int i, j, k;
302   xbt_dict_t network_link_set = xbt_dict_new();
303   xbt_dict_cursor_t cursor = NULL;
304   char *name = NULL;
305   int nb_link = 0;
306   int nb_host = 0;
307   network_link_CM02_t link;
308
309   /* Compute the number of affected resources... */
310   for(i=0; i< workstation_nb; i++) {
311     for(j=0; j< workstation_nb; j++) {
312       network_card_CM02_t card_src = ((workstation_CLM03_t*)workstation_list)[i]->network_card;
313       network_card_CM02_t card_dst = ((workstation_CLM03_t*)workstation_list)[j]->network_card;
314       int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
315       network_link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
316       
317       if(communication_amount[i*workstation_nb+j]>0)
318         for(k=0; k< route_size; k++) {
319           xbt_dict_set(network_link_set, route[k]->name, route[k], NULL);
320         }
321     }
322   }
323
324   xbt_dict_foreach(network_link_set, cursor, name, link) {
325     nb_link++;
326   }
327
328   xbt_dict_free(&network_link_set);
329
330   for (i = 0; i<workstation_nb; i++)
331     if(computation_amount[i]>0) nb_host++;
332  
333   if(nb_link + workstation_nb == 0)
334     return NULL;
335
336   action = xbt_new0(s_surf_action_parallel_task_CSL05_t, 1);
337   action->generic_action.using = 1;
338   action->generic_action.cost = amount;
339   action->generic_action.remains = amount;
340   action->generic_action.max_duration = NO_MAX_DURATION;
341   action->generic_action.start = -1.0;
342   action->generic_action.finish = -1.0;
343   action->generic_action.resource_type =
344       (surf_resource_t) surf_workstation_resource;
345   action->suspended = 0;  /* Should be useless because of the
346                              calloc but it seems to help valgrind... */
347   action->generic_action.state_set =
348       surf_workstation_resource->common_public->states.running_action_set;
349
350   xbt_swag_insert(action, action->generic_action.state_set);
351   action->rate = rate;
352
353   if(action->rate>0)
354     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
355                                         nb_host + nb_link);
356   else   
357     action->variable = lmm_variable_new(maxmin_system, action, 1.0, action->rate,
358                                         nb_host + nb_link);
359
360   for (i = 0; i<workstation_nb; i++)
361     if(computation_amount[i]>0)
362       lmm_expand(maxmin_system, ((cpu_Cas01_t) ((workstation_CLM03_t) workstation_list[i])->cpu)->constraint, 
363                  action->variable, computation_amount[i]);
364
365   for (i=0; i<workstation_nb; i++) {
366     for(j=0; j< workstation_nb; j++) {
367       network_card_CM02_t card_src = ((workstation_CLM03_t*)workstation_list)[i]->network_card;
368       network_card_CM02_t card_dst = ((workstation_CLM03_t*)workstation_list)[j]->network_card;
369       int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
370       network_link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
371       
372       for(k=0; k< route_size; k++) {
373         if(communication_amount[i*workstation_nb+j]>0) {
374           lmm_expand_add(maxmin_system, route[k]->constraint, 
375                        action->variable, communication_amount[i*workstation_nb+j]);
376         }
377       }
378     }
379   }
380   
381   return (surf_action_t) action;
382 }
383
384 static void finalize(void)
385 {
386   xbt_dict_free(&workstation_set);
387   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
388   xbt_swag_free(surf_workstation_resource->common_public->states.
389                 running_action_set);
390   xbt_swag_free(surf_workstation_resource->common_public->states.
391                 failed_action_set);
392   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
393
394   free(surf_workstation_resource->common_public);
395   free(surf_workstation_resource->common_private);
396   free(surf_workstation_resource->extension_public);
397
398   free(surf_workstation_resource);
399   surf_workstation_resource = NULL;
400 }
401
402 static void surf_workstation_resource_init_internal(void)
403 {
404   s_surf_action_t action;
405
406   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
407
408   surf_workstation_resource->common_private =
409       xbt_new0(s_surf_resource_private_t, 1);
410   surf_workstation_resource->common_public =
411       xbt_new0(s_surf_resource_public_t, 1);
412 /*   surf_workstation_resource->extension_private = xbt_new0(s_surf_workstation_resource_extension_private_t,1); */
413   surf_workstation_resource->extension_public =
414       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
415
416   surf_workstation_resource->common_public->states.ready_action_set =
417       xbt_swag_new(xbt_swag_offset(action, state_hookup));
418   surf_workstation_resource->common_public->states.running_action_set =
419       xbt_swag_new(xbt_swag_offset(action, state_hookup));
420   surf_workstation_resource->common_public->states.failed_action_set =
421       xbt_swag_new(xbt_swag_offset(action, state_hookup));
422   surf_workstation_resource->common_public->states.done_action_set =
423       xbt_swag_new(xbt_swag_offset(action, state_hookup));
424
425   surf_workstation_resource->common_public->name_service = name_service;
426   surf_workstation_resource->common_public->get_resource_name =
427       get_resource_name;
428   surf_workstation_resource->common_public->action_get_state =
429       surf_action_get_state;
430   surf_workstation_resource->common_public->action_free = action_free;
431   surf_workstation_resource->common_public->action_use = action_use;
432   surf_workstation_resource->common_public->action_cancel = action_cancel;
433   surf_workstation_resource->common_public->action_recycle =
434       action_recycle;
435   surf_workstation_resource->common_public->action_change_state =
436       action_change_state;
437   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
438   surf_workstation_resource->common_public->name = "Workstation";
439
440   surf_workstation_resource->common_private->resource_used = resource_used;
441   surf_workstation_resource->common_private->share_resources =
442       share_resources;
443   surf_workstation_resource->common_private->update_actions_state =
444       update_actions_state;
445   surf_workstation_resource->common_private->update_resource_state =
446       update_resource_state;
447   surf_workstation_resource->common_private->finalize = finalize;
448
449   surf_workstation_resource->common_public->suspend = action_suspend;
450   surf_workstation_resource->common_public->resume = action_resume;
451   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
452   surf_workstation_resource->common_public->set_max_duration = action_set_max_duration;
453   surf_workstation_resource->common_public->set_priority = action_set_priority;
454
455   surf_workstation_resource->extension_public->execute = execute;
456   surf_workstation_resource->extension_public->sleep = action_sleep;
457   surf_workstation_resource->extension_public->get_state = get_state;
458   surf_workstation_resource->extension_public->get_speed = get_speed;
459   surf_workstation_resource->extension_public->communicate = communicate;
460   surf_workstation_resource->extension_public->execute_parallel_task = 
461     execute_parallel_task;
462
463   workstation_set = xbt_dict_new();
464
465   xbt_assert0(maxmin_system, "surf_init has to be called first!");
466 }
467
468 /********************************************************************/
469 /* The model used in MSG and presented at CCGrid03                  */
470 /********************************************************************/
471 /* @InProceedings{Casanova.CLM_03, */
472 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
473 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
474 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
475 /*   publisher = {"IEEE Computer Society Press"}, */
476 /*   month = {may}, */
477 /*   year = {2003} */
478 /* } */
479 void surf_workstation_resource_init_CLM03(const char *filename)
480 {
481 /*   int i ; */
482 /*   surf_resource_t resource =  NULL; */
483
484   surf_workstation_resource_init_internal();
485   surf_cpu_resource_init_Cas01(filename);
486   surf_network_resource_init_CM02(filename);
487   create_workstations();
488   xbt_dynar_push(resource_list, &surf_workstation_resource);
489 /*   xbt_dynar_foreach(resource_list, i, resource) { */
490 /*     if(resource==surf_cpu_resource) { */
491 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
492 /*       i--;  */
493 /*       continue; */
494 /*     } */
495 /*     if(resource==surf_network_resource) { */
496 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
497 /*       i--;  */
498 /*       continue; */
499 /*     } */
500 /*   } */
501 }