Logo AND Algorithmique Numérique Distribuée

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