Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
changes for solve warrings
[simgrid.git] / src / surf / workstation.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/ex.h"
8 #include "xbt/dict.h"
9 #include "portable.h"
10 #include "surf_private.h"
11
12 typedef struct workstation_CLM03 {
13   s_surf_resource_t generic_resource;   /* Must remain first to add this to a trace */
14   void *cpu;
15   int id;
16 } s_workstation_CLM03_t, *workstation_CLM03_t;
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
19                                 "Logging specific to the SURF workstation module");
20
21 surf_model_t surf_workstation_model = NULL;
22
23 static workstation_CLM03_t workstation_new(const char *name,
24                                            void *cpu, int id)
25 {
26   workstation_CLM03_t workstation = xbt_new0(s_workstation_CLM03_t, 1);
27
28   workstation->generic_resource.model = surf_workstation_model;
29   workstation->generic_resource.name = xbt_strdup(name);
30   workstation->cpu = cpu;
31   workstation->id = id;
32
33   xbt_dict_set(surf_model_resource_set(surf_workstation_model), name,
34                workstation, surf_resource_free);
35
36   return workstation;
37 }
38
39 void create_workstations(void)
40 {
41   xbt_dict_cursor_t cursor = NULL;
42   char *name = NULL;
43   void *cpu = NULL;
44
45   xbt_dict_foreach(surf_model_resource_set(surf_cpu_model), cursor, name, cpu) {
46     int *id = NULL;
47     if (used_routing && used_routing->host_id)
48       id = xbt_dict_get_or_null(used_routing->host_id,name);
49
50     workstation_new(name, cpu, id ? *id : 0);
51   }
52 }
53
54 static int ws_resource_used(void *resource_id)
55 {
56   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
57 }
58
59 static void ws_parallel_action_cancel(surf_action_t action)
60 {
61   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
62 }
63
64 static int ws_parallel_action_free(surf_action_t action)
65 {
66   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
67 }
68
69 static int ws_action_unref(surf_action_t action)
70 {
71   if (action->model_type == surf_network_model)
72     return surf_network_model->action_unref(action);
73   else if (action->model_type == surf_cpu_model)
74     return surf_cpu_model->action_unref(action);
75   else if (action->model_type == surf_workstation_model)
76     return ws_parallel_action_free(action);
77   else
78     DIE_IMPOSSIBLE;
79   return 0;
80 }
81
82 static void ws_action_cancel(surf_action_t action)
83 {
84   if (action->model_type == surf_network_model)
85     surf_network_model->action_cancel(action);
86   else if (action->model_type == surf_cpu_model)
87     surf_cpu_model->action_cancel(action);
88   else if (action->model_type == surf_workstation_model)
89     ws_parallel_action_cancel(action);
90   else
91     DIE_IMPOSSIBLE;
92   return;
93 }
94
95 static void ws_action_state_set(surf_action_t action,
96                                 e_surf_action_state_t state)
97 {
98   if (action->model_type == surf_network_model)
99     surf_network_model->action_state_set(action, state);
100   else if (action->model_type == surf_cpu_model)
101     surf_cpu_model->action_state_set(action, state);
102   else if (action->model_type == surf_workstation_model)
103     surf_action_state_set(action, state);
104   else
105     DIE_IMPOSSIBLE;
106   return;
107 }
108
109 static double ws_share_resources(double now)
110 {
111   return -1.0;
112 }
113
114 static void ws_update_actions_state(double now, double delta)
115 {
116   return;
117 }
118
119 static void ws_update_resource_state(void *id,
120                                   tmgr_trace_event_t event_type,
121                                   double value, double date)
122 {
123   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
124 }
125
126 static surf_action_t ws_execute(void *workstation, double size)
127 {
128   return surf_cpu_model->extension.cpu.
129     execute(((workstation_CLM03_t) workstation)->cpu, size);
130 }
131
132 static surf_action_t ws_action_sleep(void *workstation, double duration)
133 {
134   return surf_cpu_model->extension.cpu.
135     sleep(((workstation_CLM03_t) workstation)->cpu, duration);
136 }
137
138 static void ws_action_suspend(surf_action_t action)
139 {
140   if (action->model_type == surf_network_model)
141     surf_network_model->suspend(action);
142   else if (action->model_type == surf_cpu_model)
143     surf_cpu_model->suspend(action);
144   else
145     DIE_IMPOSSIBLE;
146 }
147
148 static void ws_action_resume(surf_action_t action)
149 {
150   if (action->model_type == surf_network_model)
151     surf_network_model->resume(action);
152   else if (action->model_type == surf_cpu_model)
153     surf_cpu_model->resume(action);
154   else
155     DIE_IMPOSSIBLE;
156 }
157
158 static int ws_action_is_suspended(surf_action_t action)
159 {
160   if (action->model_type == surf_network_model)
161     return surf_network_model->is_suspended(action);
162   if (action->model_type == surf_cpu_model)
163     return surf_cpu_model->is_suspended(action);
164   DIE_IMPOSSIBLE;
165 }
166
167 static void ws_action_set_max_duration(surf_action_t action, double duration)
168 {
169   if (action->model_type == surf_network_model)
170     surf_network_model->set_max_duration(action, duration);
171   else if (action->model_type == surf_cpu_model)
172     surf_cpu_model->set_max_duration(action, duration);
173   else
174     DIE_IMPOSSIBLE;
175 }
176
177 static void ws_action_set_priority(surf_action_t action, double priority)
178 {
179   if (action->model_type == surf_network_model)
180     surf_network_model->set_priority(action, priority);
181   else if (action->model_type == surf_cpu_model)
182     surf_cpu_model->set_priority(action, priority);
183   else
184     DIE_IMPOSSIBLE;
185 }
186
187 static int ws_get_latency_limited(surf_action_t action)
188 {
189   if (action->model_type == surf_network_model)
190     return surf_network_model->get_latency_limited(action);
191   INFO0("You tried to ask if a non network action is limited by latency, aborting...");
192   DIE_IMPOSSIBLE;
193 }
194
195 static double ws_action_get_remains(surf_action_t action)
196 {
197   if (action->model_type == surf_network_model)
198     return surf_network_model->get_remains(action);
199   if (action->model_type == surf_cpu_model)
200     return surf_cpu_model->get_remains(action);
201   DIE_IMPOSSIBLE;
202 }
203
204 static surf_action_t ws_communicate(void *workstation_src,
205                                  void *workstation_dst, double size,
206                                  double rate)
207 {
208   workstation_CLM03_t src = (workstation_CLM03_t) workstation_src;
209   workstation_CLM03_t dst = (workstation_CLM03_t) workstation_dst;
210   return surf_network_model->extension.network.
211     communicate(surf_resource_name(src->cpu), surf_resource_name(dst->cpu),
212                 src->id, dst->id, size, rate);
213 }
214
215 static e_surf_resource_state_t ws_get_state(void *workstation)
216 {
217   return surf_cpu_model->extension.cpu.
218     get_state(((workstation_CLM03_t) workstation)->cpu);
219 }
220
221 static double ws_get_speed(void *workstation, double load)
222 {
223   return surf_cpu_model->extension.cpu.
224     get_speed(((workstation_CLM03_t) workstation)->cpu, load);
225 }
226
227 static double ws_get_available_speed(void *workstation)
228 {
229   return surf_cpu_model->extension.cpu.
230     get_available_speed(((workstation_CLM03_t)
231                          workstation)->cpu);
232 }
233
234 static surf_action_t ws_execute_parallel_task(int workstation_nb,
235                                            void **workstation_list,
236                                            double *computation_amount,
237                                            double *communication_amount,
238                                            double amount, double rate)
239 {
240   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
241 }
242
243
244 /* returns an array of network_link_CM02_t */
245 static xbt_dynar_t ws_get_route(void *src, void *dst)
246 {
247   workstation_CLM03_t workstation_src = (workstation_CLM03_t) src;
248   workstation_CLM03_t workstation_dst = (workstation_CLM03_t) dst;
249   return surf_network_model->extension.network.get_route(workstation_src->id,
250                                                          workstation_dst->id);
251 }
252
253 static double ws_get_link_bandwidth(const void *link)
254 {
255   return surf_network_model->extension.network.get_link_bandwidth(link);
256 }
257
258 static double ws_get_link_latency(const void *link)
259 {
260   return surf_network_model->extension.network.get_link_latency(link);
261 }
262
263 static int ws_link_shared(const void *link)
264 {
265   return surf_network_model->extension.network.get_link_latency(link);
266 }
267
268 static void ws_finalize(void)
269 {
270   surf_model_exit(surf_workstation_model);
271   surf_workstation_model = NULL;
272 }
273
274 static xbt_dict_t ws_get_properties(const void *ws)
275 {
276   return surf_resource_properties(((workstation_CLM03_t) ws)->cpu);
277 }
278
279 static void surf_workstation_model_init_internal(void)
280 {
281   surf_workstation_model = surf_model_init();
282
283   surf_workstation_model->name = "Workstation";
284   surf_workstation_model->action_unref = ws_action_unref;
285   surf_workstation_model->action_cancel = ws_action_cancel;
286   surf_workstation_model->action_state_set = ws_action_state_set;
287
288   surf_workstation_model->model_private->resource_used = ws_resource_used;
289   surf_workstation_model->model_private->share_resources = ws_share_resources;
290   surf_workstation_model->model_private->update_actions_state =
291     ws_update_actions_state;
292   surf_workstation_model->model_private->update_resource_state =
293     ws_update_resource_state;
294   surf_workstation_model->model_private->finalize = ws_finalize;
295
296   surf_workstation_model->suspend = ws_action_suspend;
297   surf_workstation_model->resume = ws_action_resume;
298   surf_workstation_model->is_suspended = ws_action_is_suspended;
299   surf_workstation_model->set_max_duration = ws_action_set_max_duration;
300   surf_workstation_model->set_priority = ws_action_set_priority;
301   surf_workstation_model->get_remains = ws_action_get_remains;
302   surf_workstation_model->get_latency_limited = ws_get_latency_limited;
303
304   surf_workstation_model->extension.workstation.execute = ws_execute;
305   surf_workstation_model->extension.workstation.sleep = ws_action_sleep;
306   surf_workstation_model->extension.workstation.get_state = ws_get_state;
307   surf_workstation_model->extension.workstation.get_speed = ws_get_speed;
308   surf_workstation_model->extension.workstation.get_available_speed =
309     ws_get_available_speed;
310
311   surf_workstation_model->extension.workstation.communicate = ws_communicate;
312   surf_workstation_model->extension.workstation.get_route = ws_get_route;
313   surf_workstation_model->extension.workstation.execute_parallel_task =
314     ws_execute_parallel_task;
315   surf_workstation_model->extension.workstation.get_link_bandwidth =
316     ws_get_link_bandwidth;
317   surf_workstation_model->extension.workstation.get_link_latency =
318     ws_get_link_latency;
319   surf_workstation_model->extension.workstation.link_shared = ws_link_shared;
320   surf_workstation_model->extension.workstation.get_properties =
321     ws_get_properties;
322
323 }
324
325 /********************************************************************/
326 /* The model used in MSG and presented at CCGrid03                  */
327 /********************************************************************/
328 /* @InProceedings{Casanova.CLM_03, */
329 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
330 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
331 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
332 /*   publisher = {"IEEE Computer Society Press"}, */
333 /*   month = {may}, */
334 /*   year = {2003} */
335 /* } */
336 void surf_workstation_model_init_CLM03(const char *filename)
337 {
338   surf_workstation_model_init_internal();
339   surf_cpu_model_init_Cas01_im(filename);
340   surf_network_model_init_LegrandVelho(filename);
341   update_model_description(surf_workstation_model_description,
342                            "CLM03", surf_workstation_model);
343   xbt_dynar_push(model_list, &surf_workstation_model);
344 }
345
346 void surf_workstation_model_init_compound(const char *filename)
347 {
348
349   xbt_assert0(surf_cpu_model, "No CPU model defined yet!");
350   xbt_assert0(surf_network_model, "No network model defined yet!");
351   surf_workstation_model_init_internal();
352
353   update_model_description(surf_workstation_model_description,
354                            "compound", surf_workstation_model);
355
356   xbt_dynar_push(model_list, &surf_workstation_model);
357 }