Logo AND Algorithmique Numérique Distribuée

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