Logo AND Algorithmique Numérique Distribuée

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