Logo AND Algorithmique Numérique Distribuée

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