Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
92e8a9dc8ff118ddede542682ac4c6cdd91b1db2
[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 "surf_private.h"
12 #include "network_common.h"
13
14 typedef struct workstation_CLM03 {
15   s_surf_resource_t generic_resource; /* Must remain first to add this to a trace */
16   void *cpu;
17   int id;
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,
26                                            void *cpu, int id)
27 {
28   workstation_CLM03_t workstation = xbt_new0(s_workstation_CLM03_t, 1);
29
30   workstation->generic_resource.model = surf_workstation_model;
31   workstation->generic_resource.name = xbt_strdup(name);
32   workstation->cpu = cpu;
33   workstation->id = id;
34
35   xbt_dict_set(surf_model_resource_set(surf_workstation_model), name,
36                workstation, surf_resource_free);
37
38   return workstation;
39 }
40
41 void create_workstations(void)
42 {
43   xbt_dict_cursor_t cursor = NULL;
44   char *name = NULL;
45   void *cpu = NULL;
46
47   xbt_dict_foreach(surf_model_resource_set(surf_cpu_model), cursor, name, cpu) {
48     int *id = xbt_dict_get_or_null(used_routing->host_id,name);
49     xbt_assert1(id, "No host %s found in the platform file", name);
50
51     workstation_new(name, cpu, *id);
52   }
53 }
54
55 static int resource_used(void *resource_id)
56 {
57   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
58 }
59
60 static void parallel_action_cancel(surf_action_t action)
61 {
62   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
63 }
64
65 static int parallel_action_free(surf_action_t action)
66 {
67   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
68 }
69
70 static int action_unref(surf_action_t action)
71 {
72   if (action->model_type == surf_network_model)
73     return surf_network_model->action_unref(action);
74   else if (action->model_type == surf_cpu_model)
75     return surf_cpu_model->action_unref(action);
76   else if (action->model_type == surf_workstation_model)
77     return parallel_action_free(action);
78   else
79     DIE_IMPOSSIBLE;
80   return 0;
81 }
82
83 static void action_cancel(surf_action_t action)
84 {
85   if (action->model_type == surf_network_model)
86     surf_network_model->action_cancel(action);
87   else if (action->model_type == surf_cpu_model)
88     surf_cpu_model->action_cancel(action);
89   else if (action->model_type == surf_workstation_model)
90     parallel_action_cancel(action);
91   else
92     DIE_IMPOSSIBLE;
93   return;
94 }
95
96 static void ws_action_state_set(surf_action_t action,
97                                 e_surf_action_state_t state)
98 {
99   if (action->model_type == surf_network_model)
100     surf_network_model->action_state_set(action, state);
101   else if (action->model_type == surf_cpu_model)
102     surf_cpu_model->action_state_set(action, state);
103   else if (action->model_type == surf_workstation_model)
104     surf_action_state_set(action, state);
105   else
106     DIE_IMPOSSIBLE;
107   return;
108 }
109
110 static double share_resources(double now)
111 {
112   return -1.0;
113 }
114
115 static void update_actions_state(double now, double delta)
116 {
117   return;
118 }
119
120 static void update_resource_state(void *id,
121                                   tmgr_trace_event_t event_type,
122                                   double value, double date)
123 {
124   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
125 }
126
127 static surf_action_t execute(void *workstation, double size)
128 {
129   return surf_cpu_model->extension.
130     cpu.execute(((workstation_CLM03_t) workstation)->cpu, size);
131 }
132
133 static surf_action_t action_sleep(void *workstation, double duration)
134 {
135   return surf_cpu_model->extension.
136     cpu.sleep(((workstation_CLM03_t) workstation)->cpu, duration);
137 }
138
139 static void action_suspend(surf_action_t action)
140 {
141   if (action->model_type == surf_network_model)
142     surf_network_model->suspend(action);
143   else if (action->model_type == surf_cpu_model)
144     surf_cpu_model->suspend(action);
145   else
146     DIE_IMPOSSIBLE;
147 }
148
149 static void action_resume(surf_action_t action)
150 {
151   if (action->model_type == surf_network_model)
152     surf_network_model->resume(action);
153   else if (action->model_type == surf_cpu_model)
154     surf_cpu_model->resume(action);
155   else
156     DIE_IMPOSSIBLE;
157 }
158
159 static int action_is_suspended(surf_action_t action)
160 {
161   if (action->model_type == surf_network_model)
162     return surf_network_model->is_suspended(action);
163   if (action->model_type == surf_cpu_model)
164     return surf_cpu_model->is_suspended(action);
165   DIE_IMPOSSIBLE;
166 }
167
168 static void action_set_max_duration(surf_action_t action, double duration)
169 {
170   if (action->model_type == surf_network_model)
171     surf_network_model->set_max_duration(action, duration);
172   else if (action->model_type == surf_cpu_model)
173     surf_cpu_model->set_max_duration(action, duration);
174   else
175     DIE_IMPOSSIBLE;
176 }
177
178 static void action_set_priority(surf_action_t action, double priority)
179 {
180   if (action->model_type == surf_network_model)
181     surf_network_model->set_priority(action, priority);
182   else if (action->model_type == surf_cpu_model)
183     surf_cpu_model->set_priority(action, priority);
184   else
185     DIE_IMPOSSIBLE;
186 }
187
188 static surf_action_t communicate(void *workstation_src,
189                                  void *workstation_dst, double size,
190                                  double rate)
191 {
192   workstation_CLM03_t src = (workstation_CLM03_t) workstation_src;
193   workstation_CLM03_t dst = (workstation_CLM03_t) workstation_dst;
194   return surf_network_model->extension.
195     network.communicate(surf_resource_name(src->cpu),surf_resource_name(dst->cpu),
196         src->id,dst->id,
197         size, rate);
198 }
199
200 static e_surf_cpu_state_t get_state(void *workstation)
201 {
202   return surf_cpu_model->extension.
203     cpu.get_state(((workstation_CLM03_t) workstation)->cpu);
204 }
205
206 static double get_speed(void *workstation, double load)
207 {
208   return surf_cpu_model->extension.
209     cpu.get_speed(((workstation_CLM03_t) workstation)->cpu, load);
210 }
211
212 static double get_available_speed(void *workstation)
213 {
214   return surf_cpu_model->extension.
215     cpu.get_available_speed(((workstation_CLM03_t)
216                              workstation)->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 xbt_dynar_t 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->id,
235                                                          workstation_dst->id);
236 }
237
238 static double get_link_bandwidth(const void *link)
239 {
240   return surf_network_model->extension.network.get_link_bandwidth(link);
241 }
242
243 static double get_link_latency(const void *link)
244 {
245   return surf_network_model->extension.network.get_link_latency(link);
246 }
247
248 static int link_shared(const void *link)
249 {
250   return surf_network_model->extension.network.get_link_latency(link);
251 }
252
253 static void finalize(void)
254 {
255   surf_model_exit(surf_workstation_model);
256   surf_workstation_model = NULL;
257 }
258
259 static void surf_workstation_model_init_internal(void)
260 {
261   surf_workstation_model = surf_model_init();
262
263   surf_workstation_model->name = "Workstation";
264   surf_workstation_model->action_unref = action_unref;
265   surf_workstation_model->action_cancel = action_cancel;
266   surf_workstation_model->action_state_set = ws_action_state_set;
267
268   surf_workstation_model->model_private->resource_used = resource_used;
269   surf_workstation_model->model_private->share_resources = share_resources;
270   surf_workstation_model->model_private->update_actions_state =
271     update_actions_state;
272   surf_workstation_model->model_private->update_resource_state =
273     update_resource_state;
274   surf_workstation_model->model_private->finalize = finalize;
275
276   surf_workstation_model->suspend = action_suspend;
277   surf_workstation_model->resume = action_resume;
278   surf_workstation_model->is_suspended = action_is_suspended;
279   surf_workstation_model->set_max_duration = action_set_max_duration;
280   surf_workstation_model->set_priority = action_set_priority;
281
282   surf_workstation_model->extension.workstation.execute = execute;
283   surf_workstation_model->extension.workstation.sleep = action_sleep;
284   surf_workstation_model->extension.workstation.get_state = get_state;
285   surf_workstation_model->extension.workstation.get_speed = get_speed;
286   surf_workstation_model->extension.workstation.get_available_speed =
287     get_available_speed;
288
289   surf_workstation_model->extension.workstation.communicate = communicate;
290   surf_workstation_model->extension.workstation.get_route = get_route;
291   surf_workstation_model->extension.workstation.execute_parallel_task =
292     execute_parallel_task;
293   surf_workstation_model->extension.workstation.get_link_bandwidth =
294     get_link_bandwidth;
295   surf_workstation_model->extension.workstation.get_link_latency =
296     get_link_latency;
297   surf_workstation_model->extension.workstation.link_shared = link_shared;
298 }
299
300 /********************************************************************/
301 /* The model used in MSG and presented at CCGrid03                  */
302 /********************************************************************/
303 /* @InProceedings{Casanova.CLM_03, */
304 /*   author = {Henri Casanova and Arnaud Legrand and Loris Marchal}, */
305 /*   title = {Scheduling Distributed Applications: the SimGrid Simulation Framework}, */
306 /*   booktitle = {Proceedings of the third IEEE International Symposium on Cluster Computing and the Grid (CCGrid'03)}, */
307 /*   publisher = {"IEEE Computer Society Press"}, */
308 /*   month = {may}, */
309 /*   year = {2003} */
310 /* } */
311 void surf_workstation_model_init_CLM03(const char *filename)
312 {
313   surf_workstation_model_init_internal();
314   surf_cpu_model_init_Cas01(filename);
315   surf_network_model_init_CM02(filename);
316   update_model_description(surf_workstation_model_description,
317                            "CLM03", surf_workstation_model);
318   xbt_dynar_push(model_list, &surf_workstation_model);
319 }
320
321 void surf_workstation_model_init_compound(const char *filename)
322 {
323
324   xbt_assert0(surf_cpu_model, "No CPU model defined yet!");
325   xbt_assert0(surf_network_model, "No network model defined yet!");
326   surf_workstation_model_init_internal();
327
328   update_model_description(surf_workstation_model_description,
329                            "compound", surf_workstation_model);
330
331   xbt_dynar_push(model_list, &surf_workstation_model);
332 }