Logo AND Algorithmique Numérique Distribuée

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