Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
82295356144566cb90090d4c6c881dbdc0f2bcc3
[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 "portable.h"
11 #include "workstation_private.h"
12 #include "cpu_private.h"
13 #include "network_private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
16                                 "Logging specific to the SURF workstation module");
17
18 surf_workstation_model_t surf_workstation_model = NULL;
19 xbt_dict_t workstation_set = NULL;
20
21 static workstation_CLM03_t workstation_new(const char *name,
22                                            void *cpu, void *card)
23 {
24   workstation_CLM03_t workstation = xbt_new0(s_workstation_CLM03_t, 1);
25
26   workstation->model = (surf_model_t) surf_workstation_model;
27   workstation->name = xbt_strdup(name);
28   workstation->cpu = cpu;
29   workstation->network_card = card;
30
31   return workstation;
32 }
33
34 static void workstation_free(void *workstation)
35 {
36   free(((workstation_CLM03_t) workstation)->name);
37   free(workstation);
38 }
39
40 void create_workstations(void)
41 {
42   xbt_dict_cursor_t cursor = NULL;
43   char *name = NULL;
44   void *cpu = NULL;
45   void *nw_card = NULL;
46
47   xbt_dict_foreach(cpu_set, cursor, name, cpu) {
48     nw_card = xbt_dict_get_or_null(network_card_set, name);
49     xbt_assert1(nw_card, "No corresponding card found for %s", name);
50
51     xbt_dict_set(workstation_set, name,
52                  workstation_new(name, cpu, nw_card), workstation_free);
53   }
54 }
55
56 static void *name_service(const char *name)
57 {
58   return xbt_dict_get_or_null(workstation_set, name);
59 }
60
61 static const char *get_resource_name(void *resource_id)
62 {
63   return ((workstation_CLM03_t) resource_id)->name;
64 }
65
66 static int resource_used(void *resource_id)
67 {
68   THROW_IMPOSSIBLE;  /* This model does not implement parallel tasks */
69 }
70
71 static void parallel_action_cancel(surf_action_t action)
72 {
73   THROW_UNIMPLEMENTED;  /* This model does not implement parallel tasks */
74 }
75
76 static int parallel_action_free(surf_action_t action)
77 {
78   THROW_UNIMPLEMENTED;  /* This model does not implement parallel tasks */
79 }
80
81 static void parallel_action_use(surf_action_t action)
82 {
83   THROW_UNIMPLEMENTED;  /* This model does not implement parallel tasks */
84 }
85
86 static int action_free(surf_action_t action)
87 {
88   if (action->model_type == (surf_model_t) surf_network_model)
89     return surf_network_model->common_public->action_free(action);
90   else if (action->model_type == (surf_model_t) surf_cpu_model)
91     return surf_cpu_model->common_public->action_free(action);
92   else if (action->model_type ==
93            (surf_model_t) surf_workstation_model)
94     return parallel_action_free(action);
95   else
96     DIE_IMPOSSIBLE;
97   return 0;
98 }
99
100 static void action_use(surf_action_t action)
101 {
102   if (action->model_type == (surf_model_t) surf_network_model)
103     surf_network_model->common_public->action_use(action);
104   else if (action->model_type == (surf_model_t) surf_cpu_model)
105     surf_cpu_model->common_public->action_use(action);
106   else if (action->model_type ==
107            (surf_model_t) surf_workstation_model)
108     parallel_action_use(action);
109   else
110     DIE_IMPOSSIBLE;
111   return;
112 }
113
114 static void action_cancel(surf_action_t action)
115 {
116   if (action->model_type == (surf_model_t) surf_network_model)
117     surf_network_model->common_public->action_cancel(action);
118   else if (action->model_type == (surf_model_t) surf_cpu_model)
119     surf_cpu_model->common_public->action_cancel(action);
120   else if (action->model_type ==
121            (surf_model_t) surf_workstation_model)
122     parallel_action_cancel(action);
123   else
124     DIE_IMPOSSIBLE;
125   return;
126 }
127
128 static void action_recycle(surf_action_t action)
129 {
130   DIE_IMPOSSIBLE;
131   return;
132 }
133
134 static void action_change_state(surf_action_t action,
135                                 e_surf_action_state_t state)
136 {
137   if (action->model_type == (surf_model_t) surf_network_model)
138     surf_network_model->common_public->action_change_state(action,
139                                                               state);
140   else if (action->model_type == (surf_model_t) surf_cpu_model)
141     surf_cpu_model->common_public->action_change_state(action, state);
142   else if (action->model_type ==
143            (surf_model_t) surf_workstation_model)
144     surf_action_change_state(action, state);
145   else
146     DIE_IMPOSSIBLE;
147   return;
148 }
149
150 static double share_resources(double now)
151 {
152   return -1.0;
153 }
154
155 static void update_actions_state(double now, double delta)
156 {
157   return;
158 }
159
160 static void update_resource_state(void *id,
161                                   tmgr_trace_event_t event_type,
162                                   double value)
163 {
164   THROW_IMPOSSIBLE; /* This model does not implement parallel tasks */
165 }
166
167 static surf_action_t execute(void *workstation, double size)
168 {
169   return surf_cpu_model->extension_public->
170       execute(((workstation_CLM03_t) workstation)->cpu, size);
171 }
172
173 static surf_action_t action_sleep(void *workstation, double duration)
174 {
175   return surf_cpu_model->extension_public->
176       sleep(((workstation_CLM03_t) workstation)->cpu, duration);
177 }
178
179 static void action_suspend(surf_action_t action)
180 {
181   if (action->model_type == (surf_model_t) surf_network_model)
182     surf_network_model->common_public->suspend(action);
183   else if (action->model_type == (surf_model_t) surf_cpu_model)
184     surf_cpu_model->common_public->suspend(action);
185   else
186     DIE_IMPOSSIBLE;
187 }
188
189 static void action_resume(surf_action_t action)
190 {
191   if (action->model_type == (surf_model_t) surf_network_model)
192     surf_network_model->common_public->resume(action);
193   else if (action->model_type == (surf_model_t) surf_cpu_model)
194     surf_cpu_model->common_public->resume(action);
195   else
196     DIE_IMPOSSIBLE;
197 }
198
199 static int action_is_suspended(surf_action_t action)
200 {
201   if (action->model_type == (surf_model_t) surf_network_model)
202     return surf_network_model->common_public->is_suspended(action);
203   if (action->model_type == (surf_model_t) surf_cpu_model)
204     return surf_cpu_model->common_public->is_suspended(action);
205   DIE_IMPOSSIBLE;
206 }
207
208 static void action_set_max_duration(surf_action_t action, double duration)
209 {
210   if (action->model_type == (surf_model_t) surf_network_model)
211     surf_network_model->common_public->set_max_duration(action,
212                                                            duration);
213   else if (action->model_type == (surf_model_t) surf_cpu_model)
214     surf_cpu_model->common_public->set_max_duration(action, duration);
215   else
216     DIE_IMPOSSIBLE;
217 }
218
219 static void action_set_priority(surf_action_t action, double priority)
220 {
221   if (action->model_type == (surf_model_t) surf_network_model)
222     surf_network_model->common_public->set_priority(action, priority);
223   else if (action->model_type == (surf_model_t) surf_cpu_model)
224     surf_cpu_model->common_public->set_priority(action, priority);
225   else
226     DIE_IMPOSSIBLE;
227 }
228
229 static surf_action_t communicate(void *workstation_src,
230                                  void *workstation_dst, double size,
231                                  double rate)
232 {
233   return surf_network_model->extension_public->
234       communicate(((workstation_CLM03_t) workstation_src)->network_card,
235                   ((workstation_CLM03_t) workstation_dst)->network_card,
236                   size, rate);
237 }
238
239 static e_surf_cpu_state_t get_state(void *workstation)
240 {
241   return surf_cpu_model->extension_public->
242       get_state(((workstation_CLM03_t) workstation)->cpu);
243 }
244
245 static double get_speed(void *workstation, double load)
246 {
247   return surf_cpu_model->extension_public->
248       get_speed(((workstation_CLM03_t) workstation)->cpu, load);
249 }
250
251 static double get_available_speed(void *workstation)
252 {
253   return surf_cpu_model->extension_public->
254       get_available_speed(((workstation_CLM03_t) workstation)->cpu);
255 }
256
257 static xbt_dict_t get_properties(void *workstation)
258 {
259   return surf_cpu_model->common_public->
260       get_properties(((workstation_CLM03_t) workstation)->cpu);
261 }
262
263 static surf_action_t execute_parallel_task(int workstation_nb,
264                                            void **workstation_list,
265                                            double *computation_amount,
266                                            double *communication_amount,
267                                            double amount,
268                                            double rate)
269 {
270   THROW_UNIMPLEMENTED;  /* This model does not implement parallel tasks */
271 }
272
273
274 /* returns an array of network_link_CM02_t */
275 static const void **get_route(void *src, void *dst)
276 {
277   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
278   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
279   return surf_network_model->extension_public->
280       get_route(workstation_src->network_card,
281                 workstation_dst->network_card);
282 }
283
284 static int get_route_size(void *src, void *dst)
285 {
286   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
287   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
288   return surf_network_model->extension_public->
289       get_route_size(workstation_src->network_card,
290                      workstation_dst->network_card);
291 }
292
293 static const char *get_link_name(const void *link)
294 {
295   return surf_network_model->extension_public->get_link_name(link);
296 }
297
298 static double get_link_bandwidth(const void *link)
299 {
300   return surf_network_model->extension_public->get_link_bandwidth(link);
301 }
302
303 static double get_link_latency(const void *link)
304 {
305   return surf_network_model->extension_public->get_link_latency(link);
306 }
307
308 static void finalize(void)
309 {
310   xbt_dict_free(&workstation_set);
311   xbt_swag_free(surf_workstation_model->common_public->states.
312                 ready_action_set);
313   xbt_swag_free(surf_workstation_model->common_public->states.
314                 running_action_set);
315   xbt_swag_free(surf_workstation_model->common_public->states.
316                 failed_action_set);
317   xbt_swag_free(surf_workstation_model->common_public->states.
318                 done_action_set);
319
320   free(surf_workstation_model->common_public);
321   free(surf_workstation_model->common_private);
322   free(surf_workstation_model->extension_public);
323
324   free(surf_workstation_model);
325   surf_workstation_model = NULL;
326 }
327
328 static void surf_workstation_model_init_internal(void)
329 {
330   s_surf_action_t action;
331
332   surf_workstation_model = xbt_new0(s_surf_workstation_model_t, 1);
333
334   surf_workstation_model->common_private =
335       xbt_new0(s_surf_model_private_t, 1);
336   surf_workstation_model->common_public =
337       xbt_new0(s_surf_model_public_t, 1);
338 /*   surf_workstation_model->extension_private = xbt_new0(s_surf_workstation_model_extension_private_t,1); */
339   surf_workstation_model->extension_public =
340       xbt_new0(s_surf_workstation_model_extension_public_t, 1);
341
342   surf_workstation_model->common_public->states.ready_action_set =
343       xbt_swag_new(xbt_swag_offset(action, state_hookup));
344   surf_workstation_model->common_public->states.running_action_set =
345       xbt_swag_new(xbt_swag_offset(action, state_hookup));
346   surf_workstation_model->common_public->states.failed_action_set =
347       xbt_swag_new(xbt_swag_offset(action, state_hookup));
348   surf_workstation_model->common_public->states.done_action_set =
349       xbt_swag_new(xbt_swag_offset(action, state_hookup));
350
351   surf_workstation_model->common_public->name_service = name_service;
352   surf_workstation_model->common_public->get_resource_name =
353       get_resource_name;
354   surf_workstation_model->common_public->action_get_state =
355       surf_action_get_state;
356   surf_workstation_model->common_public->action_get_start_time =
357       surf_action_get_start_time;
358   surf_workstation_model->common_public->action_get_finish_time =
359       surf_action_get_finish_time;
360   surf_workstation_model->common_public->action_free = action_free;
361   surf_workstation_model->common_public->action_use = action_use;
362   surf_workstation_model->common_public->action_cancel = action_cancel;
363   surf_workstation_model->common_public->action_recycle =
364       action_recycle;
365   surf_workstation_model->common_public->action_change_state =
366       action_change_state;
367   surf_workstation_model->common_public->action_set_data =
368       surf_action_set_data;
369   surf_workstation_model->common_public->name = "Workstation";
370
371   surf_workstation_model->common_private->resource_used = resource_used;
372   surf_workstation_model->common_private->share_resources =
373       share_resources;
374   surf_workstation_model->common_private->update_actions_state =
375       update_actions_state;
376   surf_workstation_model->common_private->update_resource_state =
377       update_resource_state;
378   surf_workstation_model->common_private->finalize = finalize;
379
380   surf_workstation_model->common_public->suspend = action_suspend;
381   surf_workstation_model->common_public->resume = action_resume;
382   surf_workstation_model->common_public->is_suspended =
383       action_is_suspended;
384   surf_workstation_model->common_public->set_max_duration =
385       action_set_max_duration;
386   surf_workstation_model->common_public->set_priority =
387       action_set_priority;
388
389   surf_workstation_model->extension_public->execute = execute;
390   surf_workstation_model->extension_public->sleep = action_sleep;
391   surf_workstation_model->extension_public->get_state = get_state;
392   surf_workstation_model->extension_public->get_speed = get_speed;
393   surf_workstation_model->extension_public->get_available_speed =
394       get_available_speed;
395
396   /*manage the properties of the workstation*/
397   surf_workstation_model->common_public->get_properties = get_properties;
398
399   surf_workstation_model->extension_public->communicate = communicate;
400   surf_workstation_model->extension_public->execute_parallel_task =
401       execute_parallel_task;
402   surf_workstation_model->extension_public->get_route = get_route;
403   surf_workstation_model->extension_public->get_route_size =
404       get_route_size;
405   surf_workstation_model->extension_public->get_link_name =
406       get_link_name;
407   surf_workstation_model->extension_public->get_link_bandwidth =
408       get_link_bandwidth;
409   surf_workstation_model->extension_public->get_link_latency =
410       get_link_latency;
411   workstation_set = xbt_dict_new();
412 }
413
414 /********************************************************************/
415 /* The model used in MSG and presented at CCGrid03                  */
416 /********************************************************************/
417 /* @InProceedings{Casanova.CLM_03, */
418 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
419 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
420 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
421 /*   publisher = {"IEEE Computer Society Press"}, */
422 /*   month = {may}, */
423 /*   year = {2003} */
424 /* } */
425 void surf_workstation_model_init_CLM03(const char *filename)
426 {
427   surf_workstation_model_init_internal();
428   surf_cpu_model_init_Cas01(filename);
429   surf_network_model_init_CM02(filename);
430 //  create_workstations();
431   update_model_description(surf_workstation_model_description,
432                               surf_workstation_model_description_size,
433                               "CLM03",
434                               (surf_model_t) surf_workstation_model);
435   xbt_dynar_push(model_list, &surf_workstation_model);
436 }
437
438 void surf_workstation_model_init_compound(const char *filename)
439 {
440
441   xbt_assert0(surf_cpu_model, "No CPU model defined yet!");
442   xbt_assert0(surf_network_model, "No network model defined yet!");
443   surf_workstation_model_init_internal();
444   create_workstations();
445
446   update_model_description(surf_workstation_model_description,
447                               surf_workstation_model_description_size,
448                               "compound",
449                               (surf_model_t) surf_workstation_model);
450
451   xbt_dynar_push(model_list, &surf_workstation_model);
452 }