Logo AND Algorithmique Numérique Distribuée

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