Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reindent everything (possibly breaking all branches, but for the last time)
[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_common.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 == (surf_model_t) surf_workstation_model)
93     return parallel_action_free(action);
94   else
95     DIE_IMPOSSIBLE;
96   return 0;
97 }
98
99 static void action_use(surf_action_t action)
100 {
101   if (action->model_type == (surf_model_t) surf_network_model)
102     surf_network_model->common_public->action_use(action);
103   else if (action->model_type == (surf_model_t) surf_cpu_model)
104     surf_cpu_model->common_public->action_use(action);
105   else if (action->model_type == (surf_model_t) surf_workstation_model)
106     parallel_action_use(action);
107   else
108     DIE_IMPOSSIBLE;
109   return;
110 }
111
112 static void action_cancel(surf_action_t action)
113 {
114   if (action->model_type == (surf_model_t) surf_network_model)
115     surf_network_model->common_public->action_cancel(action);
116   else if (action->model_type == (surf_model_t) surf_cpu_model)
117     surf_cpu_model->common_public->action_cancel(action);
118   else if (action->model_type == (surf_model_t) surf_workstation_model)
119     parallel_action_cancel(action);
120   else
121     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->model_type == (surf_model_t) surf_network_model)
135     surf_network_model->common_public->action_change_state(action, state);
136   else if (action->model_type == (surf_model_t) surf_cpu_model)
137     surf_cpu_model->common_public->action_change_state(action, state);
138   else if (action->model_type == (surf_model_t) surf_workstation_model)
139     surf_action_change_state(action, state);
140   else
141     DIE_IMPOSSIBLE;
142   return;
143 }
144
145 static double share_resources(double now)
146 {
147   return -1.0;
148 }
149
150 static void update_actions_state(double now, double delta)
151 {
152   return;
153 }
154
155 static void update_resource_state(void *id,
156                                   tmgr_trace_event_t event_type,
157                                   double value, double date)
158 {
159   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
160 }
161
162 static surf_action_t execute(void *workstation, double size)
163 {
164   return surf_cpu_model->
165     extension_public->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->
171     extension_public->sleep(((workstation_CLM03_t) workstation)->cpu,
172                             duration);
173 }
174
175 static void action_suspend(surf_action_t action)
176 {
177   if (action->model_type == (surf_model_t) surf_network_model)
178     surf_network_model->common_public->suspend(action);
179   else if (action->model_type == (surf_model_t) surf_cpu_model)
180     surf_cpu_model->common_public->suspend(action);
181   else
182     DIE_IMPOSSIBLE;
183 }
184
185 static void action_resume(surf_action_t action)
186 {
187   if (action->model_type == (surf_model_t) surf_network_model)
188     surf_network_model->common_public->resume(action);
189   else if (action->model_type == (surf_model_t) surf_cpu_model)
190     surf_cpu_model->common_public->resume(action);
191   else
192     DIE_IMPOSSIBLE;
193 }
194
195 static int action_is_suspended(surf_action_t action)
196 {
197   if (action->model_type == (surf_model_t) surf_network_model)
198     return surf_network_model->common_public->is_suspended(action);
199   if (action->model_type == (surf_model_t) surf_cpu_model)
200     return surf_cpu_model->common_public->is_suspended(action);
201   DIE_IMPOSSIBLE;
202 }
203
204 static void action_set_max_duration(surf_action_t action, double duration)
205 {
206   if (action->model_type == (surf_model_t) surf_network_model)
207     surf_network_model->common_public->set_max_duration(action, 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->
229     extension_public->communicate(((workstation_CLM03_t) workstation_src)->
230                                   network_card,
231                                   ((workstation_CLM03_t) workstation_dst)->
232                                   network_card, size, rate);
233 }
234
235 static e_surf_cpu_state_t get_state(void *workstation)
236 {
237   return surf_cpu_model->
238     extension_public->get_state(((workstation_CLM03_t) workstation)->cpu);
239 }
240
241 static double get_speed(void *workstation, double load)
242 {
243   return surf_cpu_model->
244     extension_public->get_speed(((workstation_CLM03_t) workstation)->cpu,
245                                 load);
246 }
247
248 static double get_available_speed(void *workstation)
249 {
250   return surf_cpu_model->
251     extension_public->get_available_speed(((workstation_CLM03_t)
252                                            workstation)->cpu);
253 }
254
255 static xbt_dict_t get_properties(void *workstation)
256 {
257   return surf_cpu_model->
258     common_public->get_properties(((workstation_CLM03_t) workstation)->cpu);
259 }
260
261 static surf_action_t execute_parallel_task(int workstation_nb,
262                                            void **workstation_list,
263                                            double *computation_amount,
264                                            double *communication_amount,
265                                            double amount, double rate)
266 {
267   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
268 }
269
270
271 /* returns an array of network_link_CM02_t */
272 static const void **get_route(void *src, void *dst)
273 {
274   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
275   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
276   return surf_network_model->extension_public->get_route(workstation_src->
277                                                          network_card,
278                                                          workstation_dst->
279                                                          network_card);
280 }
281
282 static int get_route_size(void *src, void *dst)
283 {
284   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
285   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
286   return surf_network_model->
287     extension_public->get_route_size(workstation_src->network_card,
288                                      workstation_dst->network_card);
289 }
290
291 static const char *get_link_name(const void *link)
292 {
293   return surf_network_model->extension_public->get_link_name(link);
294 }
295
296 static double get_link_bandwidth(const void *link)
297 {
298   return surf_network_model->extension_public->get_link_bandwidth(link);
299 }
300
301 static double get_link_latency(const void *link)
302 {
303   return surf_network_model->extension_public->get_link_latency(link);
304 }
305
306 static int link_shared(const void *link)
307 {
308   return surf_network_model->extension_public->get_link_latency(link);
309 }
310
311 static void finalize(void)
312 {
313   xbt_dict_free(&workstation_set);
314   xbt_swag_free(surf_workstation_model->common_public->
315                 states.ready_action_set);
316   xbt_swag_free(surf_workstation_model->common_public->
317                 states.running_action_set);
318   xbt_swag_free(surf_workstation_model->common_public->
319                 states.failed_action_set);
320   xbt_swag_free(surf_workstation_model->common_public->
321                 states.done_action_set);
322
323   free(surf_workstation_model->common_public);
324   free(surf_workstation_model->common_private);
325   free(surf_workstation_model->extension_public);
326
327   free(surf_workstation_model);
328   surf_workstation_model = NULL;
329 }
330
331 static void surf_workstation_model_init_internal(void)
332 {
333   s_surf_action_t action;
334
335   surf_workstation_model = xbt_new0(s_surf_workstation_model_t, 1);
336
337   surf_workstation_model->common_private =
338     xbt_new0(s_surf_model_private_t, 1);
339   surf_workstation_model->common_public = xbt_new0(s_surf_model_public_t, 1);
340 /*   surf_workstation_model->extension_private = xbt_new0(s_surf_workstation_model_extension_private_t,1); */
341   surf_workstation_model->extension_public =
342     xbt_new0(s_surf_workstation_model_extension_public_t, 1);
343
344   surf_workstation_model->common_public->states.ready_action_set =
345     xbt_swag_new(xbt_swag_offset(action, state_hookup));
346   surf_workstation_model->common_public->states.running_action_set =
347     xbt_swag_new(xbt_swag_offset(action, state_hookup));
348   surf_workstation_model->common_public->states.failed_action_set =
349     xbt_swag_new(xbt_swag_offset(action, state_hookup));
350   surf_workstation_model->common_public->states.done_action_set =
351     xbt_swag_new(xbt_swag_offset(action, state_hookup));
352
353   surf_workstation_model->common_public->name_service = name_service;
354   surf_workstation_model->common_public->get_resource_name =
355     get_resource_name;
356   surf_workstation_model->common_public->action_get_state =
357     surf_action_get_state;
358   surf_workstation_model->common_public->action_get_start_time =
359     surf_action_get_start_time;
360   surf_workstation_model->common_public->action_get_finish_time =
361     surf_action_get_finish_time;
362   surf_workstation_model->common_public->action_free = action_free;
363   surf_workstation_model->common_public->action_use = action_use;
364   surf_workstation_model->common_public->action_cancel = action_cancel;
365   surf_workstation_model->common_public->action_recycle = action_recycle;
366   surf_workstation_model->common_public->action_change_state =
367     action_change_state;
368   surf_workstation_model->common_public->action_set_data =
369     surf_action_set_data;
370   surf_workstation_model->common_public->name = "Workstation";
371
372   surf_workstation_model->common_private->resource_used = resource_used;
373   surf_workstation_model->common_private->share_resources = 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 = action_is_suspended;
383   surf_workstation_model->common_public->set_max_duration =
384     action_set_max_duration;
385   surf_workstation_model->common_public->set_priority = action_set_priority;
386
387   surf_workstation_model->extension_public->execute = execute;
388   surf_workstation_model->extension_public->sleep = action_sleep;
389   surf_workstation_model->extension_public->get_state = get_state;
390   surf_workstation_model->extension_public->get_speed = get_speed;
391   surf_workstation_model->extension_public->get_available_speed =
392     get_available_speed;
393
394   /*manage the properties of the workstation */
395   surf_workstation_model->common_public->get_properties = get_properties;
396
397   surf_workstation_model->extension_public->communicate = communicate;
398   surf_workstation_model->extension_public->execute_parallel_task =
399     execute_parallel_task;
400   surf_workstation_model->extension_public->get_route = get_route;
401   surf_workstation_model->extension_public->get_route_size = get_route_size;
402   surf_workstation_model->extension_public->get_link_name = get_link_name;
403   surf_workstation_model->extension_public->get_link_bandwidth =
404     get_link_bandwidth;
405   surf_workstation_model->extension_public->get_link_latency =
406     get_link_latency;
407   surf_workstation_model->extension_public->link_shared = link_shared;
408
409   workstation_set = xbt_dict_new();
410 }
411
412 /********************************************************************/
413 /* The model used in MSG and presented at CCGrid03                  */
414 /********************************************************************/
415 /* @InProceedings{Casanova.CLM_03, */
416 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
417 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
418 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
419 /*   publisher = {"IEEE Computer Society Press"}, */
420 /*   month = {may}, */
421 /*   year = {2003} */
422 /* } */
423 void surf_workstation_model_init_CLM03(const char *filename)
424 {
425   surf_workstation_model_init_internal();
426   surf_cpu_model_init_Cas01(filename);
427   surf_network_model_init_CM02(filename);
428   update_model_description(surf_workstation_model_description,
429                            "CLM03", (surf_model_t) surf_workstation_model);
430   xbt_dynar_push(model_list, &surf_workstation_model);
431 }
432
433 void surf_workstation_model_init_compound(const char *filename)
434 {
435
436   xbt_assert0(surf_cpu_model, "No CPU model defined yet!");
437   xbt_assert0(surf_network_model, "No network model defined yet!");
438   surf_workstation_model_init_internal();
439
440   update_model_description(surf_workstation_model_description,
441                            "compound", (surf_model_t) surf_workstation_model);
442
443   xbt_dynar_push(model_list, &surf_workstation_model);
444 }