Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f9a3ac325a1011592017cb36625a058a8c6343c1
[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 xbt_dict_t workstation_set = NULL;
16 static xbt_dict_t parallel_task_network_link_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   int nb_link = 0;
309   int nb_host = 0;
310
311   if (parallel_task_network_link_set == NULL) {
312     parallel_task_network_link_set = xbt_dict_new_ext(workstation_nb * workstation_nb * 10);
313   }
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(parallel_task_network_link_set, route[k]->name, route[k], NULL);
326         }
327     }
328   }
329
330   nb_link = xbt_dict_length(parallel_task_network_link_set);
331   xbt_dict_reset(parallel_task_network_link_set);
332
333   for (i = 0; i<workstation_nb; i++)
334     if(computation_amount[i]>0) nb_host++;
335  
336   if(nb_link + workstation_nb == 0)
337     return NULL;
338
339   action = xbt_new0(s_surf_action_parallel_task_CSL05_t, 1);
340   action->generic_action.using = 1;
341   action->generic_action.cost = amount;
342   action->generic_action.remains = amount;
343   action->generic_action.max_duration = NO_MAX_DURATION;
344   action->generic_action.start = surf_get_clock();
345   action->generic_action.finish = -1.0;
346   action->generic_action.resource_type =
347       (surf_resource_t) surf_workstation_resource;
348   action->suspended = 0;  /* Should be useless because of the
349                              calloc but it seems to help valgrind... */
350   action->generic_action.state_set =
351       surf_workstation_resource->common_public->states.running_action_set;
352
353   xbt_swag_insert(action, action->generic_action.state_set);
354   action->rate = rate;
355
356   if(action->rate>0)
357     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
358                                         nb_host + nb_link);
359   else   
360     action->variable = lmm_variable_new(maxmin_system, action, 1.0, action->rate,
361                                         nb_host + nb_link);
362
363   for (i = 0; i<workstation_nb; i++)
364     if(computation_amount[i]>0)
365       lmm_expand(maxmin_system, ((cpu_Cas01_t) ((workstation_CLM03_t) workstation_list[i])->cpu)->constraint, 
366                  action->variable, computation_amount[i]);
367
368   for (i=0; i<workstation_nb; i++) {
369     for(j=0; j< workstation_nb; j++) {
370       network_card_CM02_t card_src = ((workstation_CLM03_t*)workstation_list)[i]->network_card;
371       network_card_CM02_t card_dst = ((workstation_CLM03_t*)workstation_list)[j]->network_card;
372       int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
373       network_link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
374       
375       for(k=0; k< route_size; k++) {
376         if(communication_amount[i*workstation_nb+j]>0) {
377           lmm_expand_add(maxmin_system, route[k]->constraint, 
378                        action->variable, communication_amount[i*workstation_nb+j]);
379         }
380       }
381     }
382   }
383   
384   return (surf_action_t) action;
385 }
386
387 /* returns an array of network_link_CM02_t */
388 static const void** get_route(void *src, void *dst) {
389   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
390   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
391   return surf_network_resource->extension_public->get_route(workstation_src->network_card, workstation_dst->network_card);
392 }
393
394 static int get_route_size(void *src, void *dst) {
395   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
396   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
397   return surf_network_resource->extension_public->get_route_size(workstation_src->network_card, workstation_dst->network_card);
398 }
399
400 static const char *get_link_name(const void *link) {
401   return surf_network_resource->extension_public->get_link_name(link);
402 }
403
404 static double get_link_bandwidth(const void *link) {
405   return surf_network_resource->extension_public->get_link_bandwidth(link);
406 }
407
408 static double get_link_latency(const void *link) {
409   return surf_network_resource->extension_public->get_link_latency(link); 
410 }
411
412 static void finalize(void)
413 {
414   xbt_dict_free(&workstation_set);
415   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
416   xbt_swag_free(surf_workstation_resource->common_public->states.
417                 running_action_set);
418   xbt_swag_free(surf_workstation_resource->common_public->states.
419                 failed_action_set);
420   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
421
422   free(surf_workstation_resource->common_public);
423   free(surf_workstation_resource->common_private);
424   free(surf_workstation_resource->extension_public);
425
426   free(surf_workstation_resource);
427   surf_workstation_resource = NULL;
428 }
429
430 static void surf_workstation_resource_init_internal(void)
431 {
432   s_surf_action_t action;
433
434   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
435
436   surf_workstation_resource->common_private =
437       xbt_new0(s_surf_resource_private_t, 1);
438   surf_workstation_resource->common_public =
439       xbt_new0(s_surf_resource_public_t, 1);
440 /*   surf_workstation_resource->extension_private = xbt_new0(s_surf_workstation_resource_extension_private_t,1); */
441   surf_workstation_resource->extension_public =
442       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
443
444   surf_workstation_resource->common_public->states.ready_action_set =
445       xbt_swag_new(xbt_swag_offset(action, state_hookup));
446   surf_workstation_resource->common_public->states.running_action_set =
447       xbt_swag_new(xbt_swag_offset(action, state_hookup));
448   surf_workstation_resource->common_public->states.failed_action_set =
449       xbt_swag_new(xbt_swag_offset(action, state_hookup));
450   surf_workstation_resource->common_public->states.done_action_set =
451       xbt_swag_new(xbt_swag_offset(action, state_hookup));
452
453   surf_workstation_resource->common_public->name_service = name_service;
454   surf_workstation_resource->common_public->get_resource_name =
455       get_resource_name;
456   surf_workstation_resource->common_public->action_get_state =
457       surf_action_get_state;
458   surf_workstation_resource->common_public->action_get_start_time =
459       surf_action_get_start_time;
460   surf_workstation_resource->common_public->action_get_finish_time =
461       surf_action_get_finish_time;
462   surf_workstation_resource->common_public->action_free = action_free;
463   surf_workstation_resource->common_public->action_use = action_use;
464   surf_workstation_resource->common_public->action_cancel = action_cancel;
465   surf_workstation_resource->common_public->action_recycle =
466       action_recycle;
467   surf_workstation_resource->common_public->action_change_state =
468       action_change_state;
469   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
470   surf_workstation_resource->common_public->name = "Workstation";
471
472   surf_workstation_resource->common_private->resource_used = resource_used;
473   surf_workstation_resource->common_private->share_resources =
474       share_resources;
475   surf_workstation_resource->common_private->update_actions_state =
476       update_actions_state;
477   surf_workstation_resource->common_private->update_resource_state =
478       update_resource_state;
479   surf_workstation_resource->common_private->finalize = finalize;
480
481   surf_workstation_resource->common_public->suspend = action_suspend;
482   surf_workstation_resource->common_public->resume = action_resume;
483   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
484   surf_workstation_resource->common_public->set_max_duration = action_set_max_duration;
485   surf_workstation_resource->common_public->set_priority = action_set_priority;
486
487   surf_workstation_resource->extension_public->execute = execute;
488   surf_workstation_resource->extension_public->sleep = action_sleep;
489   surf_workstation_resource->extension_public->get_state = get_state;
490   surf_workstation_resource->extension_public->get_speed = get_speed;
491   surf_workstation_resource->extension_public->get_available_speed = get_available_speed;
492   surf_workstation_resource->extension_public->communicate = communicate;
493   surf_workstation_resource->extension_public->execute_parallel_task = 
494     execute_parallel_task;
495   surf_workstation_resource->extension_public->get_route = get_route;
496   surf_workstation_resource->extension_public->get_route_size = get_route_size;
497   surf_workstation_resource->extension_public->get_link_name = get_link_name;
498   surf_workstation_resource->extension_public->get_link_bandwidth = get_link_bandwidth;
499   surf_workstation_resource->extension_public->get_link_latency = get_link_latency;
500   workstation_set = xbt_dict_new();
501
502   xbt_assert0(maxmin_system, "surf_init has to be called first!");
503 }
504
505 /********************************************************************/
506 /* The model used in MSG and presented at CCGrid03                  */
507 /********************************************************************/
508 /* @InProceedings{Casanova.CLM_03, */
509 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
510 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
511 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
512 /*   publisher = {"IEEE Computer Society Press"}, */
513 /*   month = {may}, */
514 /*   year = {2003} */
515 /* } */
516 void surf_workstation_resource_init_CLM03(const char *filename)
517 {
518 /*   int i ; */
519 /*   surf_resource_t resource =  NULL; */
520
521   surf_workstation_resource_init_internal();
522   surf_cpu_resource_init_Cas01(filename);
523   surf_network_resource_init_CM02(filename);
524   create_workstations();
525   xbt_dynar_push(resource_list, &surf_workstation_resource);
526 /*   xbt_dynar_foreach(resource_list, i, resource) { */
527 /*     if(resource==surf_cpu_resource) { */
528 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
529 /*       i--;  */
530 /*       continue; */
531 /*     } */
532 /*     if(resource==surf_network_resource) { */
533 /*       xbt_dynar_remove_at(resource_list, i, NULL); */
534 /*       i--;  */
535 /*       continue; */
536 /*     } */
537 /*   } */
538 }