Logo AND Algorithmique Numérique Distribuée

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