Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Call function execute from cpu surf model.
[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   return -1;
54 }
55
56 static void ws_parallel_action_cancel(surf_action_t action)
57 {
58   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
59 }
60
61 static int ws_parallel_action_free(surf_action_t action)
62 {
63   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
64   return -1;
65 }
66
67 static int ws_action_unref(surf_action_t action)
68 {
69   if (action->model_type == surf_network_model)
70     return surf_network_model->action_unref(action);
71   else if (action->model_type == surf_cpu_model)
72     return surf_cpu_model->action_unref(action);
73   else if (action->model_type == surf_workstation_model)
74     return ws_parallel_action_free(action);
75   else
76     DIE_IMPOSSIBLE;
77   return 0;
78 }
79
80 static void ws_action_cancel(surf_action_t action)
81 {
82   if (action->model_type == surf_network_model)
83     surf_network_model->action_cancel(action);
84   else if (action->model_type == surf_cpu_model)
85     surf_cpu_model->action_cancel(action);
86   else if (action->model_type == surf_workstation_model)
87     ws_parallel_action_cancel(action);
88   else
89     DIE_IMPOSSIBLE;
90   return;
91 }
92
93 static void ws_action_state_set(surf_action_t action,
94                                 e_surf_action_state_t state)
95 {
96   if (action->model_type == surf_network_model)
97     surf_network_model->action_state_set(action, state);
98   else if (action->model_type == surf_cpu_model)
99     surf_cpu_model->action_state_set(action, state);
100   else if (action->model_type == surf_workstation_model)
101     surf_action_state_set(action, state);
102   else
103     DIE_IMPOSSIBLE;
104   return;
105 }
106
107 static double ws_share_resources(double now)
108 {
109   return -1.0;
110 }
111
112 static void ws_update_actions_state(double now, double delta)
113 {
114   return;
115 }
116
117 static void ws_update_resource_state(void *id,
118                                      tmgr_trace_event_t event_type,
119                                      double value, double date)
120 {
121   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
122 }
123
124 static surf_action_t ws_execute(void *workstation, double size)
125 {
126   void *cpu = ((workstation_CLM03_t) workstation)->cpu;
127   return ((surf_resource_t) cpu)->model->extension.cpu.execute(cpu, size);
128 }
129
130 static surf_action_t ws_action_sleep(void *workstation, double duration)
131 {
132   return surf_cpu_model->extension.cpu.
133       sleep(((workstation_CLM03_t) workstation)->cpu, duration);
134 }
135
136 static void ws_action_suspend(surf_action_t action)
137 {
138   if (action->model_type == surf_network_model)
139     surf_network_model->suspend(action);
140   else if (action->model_type == surf_cpu_model)
141     surf_cpu_model->suspend(action);
142   else
143     DIE_IMPOSSIBLE;
144 }
145
146 static void ws_action_resume(surf_action_t action)
147 {
148   if (action->model_type == surf_network_model)
149     surf_network_model->resume(action);
150   else if (action->model_type == surf_cpu_model)
151     surf_cpu_model->resume(action);
152   else
153     DIE_IMPOSSIBLE;
154 }
155
156 static int ws_action_is_suspended(surf_action_t action)
157 {
158   if (action->model_type == surf_network_model)
159     return surf_network_model->is_suspended(action);
160   if (action->model_type == surf_cpu_model)
161     return surf_cpu_model->is_suspended(action);
162   DIE_IMPOSSIBLE;
163   return -1;
164 }
165
166 static void ws_action_set_max_duration(surf_action_t action,
167                                        double duration)
168 {
169   if (action->model_type == surf_network_model)
170     surf_network_model->set_max_duration(action, duration);
171   else if (action->model_type == surf_cpu_model)
172     surf_cpu_model->set_max_duration(action, duration);
173   else
174     DIE_IMPOSSIBLE;
175 }
176
177 static void ws_action_set_priority(surf_action_t action, double priority)
178 {
179   if (action->model_type == surf_network_model)
180     surf_network_model->set_priority(action, priority);
181   else if (action->model_type == surf_cpu_model)
182     surf_cpu_model->set_priority(action, priority);
183   else
184     DIE_IMPOSSIBLE;
185 }
186
187 #ifdef HAVE_TRACING
188 static void ws_action_set_category(surf_action_t action, const char *category)
189 {
190   if (action->model_type == surf_network_model)
191     surf_network_model->set_category(action, category);
192   else if (action->model_type == surf_cpu_model)
193     surf_cpu_model->set_category(action, category);
194   else
195     DIE_IMPOSSIBLE;
196 }
197 #endif
198
199 #ifdef HAVE_LATENCY_BOUND_TRACKING
200 static int ws_get_latency_limited(surf_action_t action)
201 {
202   if (action->model_type == surf_network_model)
203     return surf_network_model->get_latency_limited(action);
204   else
205     return 0;
206 }
207 #endif
208
209 static double ws_action_get_remains(surf_action_t action)
210 {
211   if (action->model_type == surf_network_model)
212     return surf_network_model->get_remains(action);
213   if (action->model_type == surf_cpu_model)
214     return surf_cpu_model->get_remains(action);
215   DIE_IMPOSSIBLE;
216   return -1.0;
217 }
218
219 static surf_action_t ws_communicate(void *workstation_src,
220                                     void *workstation_dst, double size,
221                                     double rate)
222 {
223   workstation_CLM03_t src = (workstation_CLM03_t) workstation_src;
224   workstation_CLM03_t dst = (workstation_CLM03_t) workstation_dst;
225   return surf_network_model->extension.network.
226       communicate(surf_resource_name(src->cpu),
227                   surf_resource_name(dst->cpu), size, rate);
228 }
229
230 static e_surf_resource_state_t ws_get_state(void *workstation)
231 {
232   return surf_cpu_model->extension.cpu.
233       get_state(((workstation_CLM03_t) workstation)->cpu);
234 }
235
236 static double ws_get_speed(void *workstation, double load)
237 {
238   return surf_cpu_model->extension.cpu.
239       get_speed(((workstation_CLM03_t) workstation)->cpu, load);
240 }
241
242 static double ws_get_available_speed(void *workstation)
243 {
244   return surf_cpu_model->extension.cpu.
245       get_available_speed(((workstation_CLM03_t)
246                            workstation)->cpu);
247 }
248
249 static surf_action_t ws_execute_parallel_task(int workstation_nb,
250                                               void **workstation_list,
251                                               double *computation_amount,
252                                               double *communication_amount,
253                                               double amount, double rate)
254 {
255   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
256   return NULL;
257 }
258
259
260 /* returns an array of network_link_CM02_t */
261 static xbt_dynar_t ws_get_route(void *src, void *dst)
262 {
263   return surf_network_model->extension.
264       network.get_route(surf_resource_name(src), surf_resource_name(dst));
265 }
266
267 static double ws_get_link_bandwidth(const void *link)
268 {
269   return surf_network_model->extension.network.get_link_bandwidth(link);
270 }
271
272 static double ws_get_link_latency(const void *link)
273 {
274   return surf_network_model->extension.network.get_link_latency(link);
275 }
276
277 static int ws_link_shared(const void *link)
278 {
279   return surf_network_model->extension.network.link_shared(link);
280 }
281
282 static void ws_finalize(void)
283 {
284   surf_model_exit(surf_workstation_model);
285   surf_workstation_model = NULL;
286 }
287
288 static xbt_dict_t ws_get_properties(const void *ws)
289 {
290   return surf_resource_properties(((workstation_CLM03_t) ws)->cpu);
291 }
292
293 static surf_action_t ws_action_open(void *workstation, const char* path, const char* mode)
294 {
295   return surf_cpu_model->extension.cpu.
296       sleep(((workstation_CLM03_t) workstation)->cpu, 1);
297 }
298
299 static surf_action_t ws_action_close(void *workstation, surf_file_t fp)
300 {
301   return surf_cpu_model->extension.cpu.
302       sleep(((workstation_CLM03_t) workstation)->cpu, 2);
303 }
304
305 static surf_action_t ws_action_read(void *workstation, void* ptr, size_t size, size_t nmemb, surf_file_t stream)
306 {
307   return surf_cpu_model->extension.cpu.
308       sleep(((workstation_CLM03_t) workstation)->cpu, 3);
309 }
310
311 static surf_action_t ws_action_write(void *workstation, const void* ptr, size_t size, size_t nmemb, surf_file_t stream)
312 {
313   return surf_cpu_model->extension.cpu.
314       sleep(((workstation_CLM03_t) workstation)->cpu, 4);
315 }
316
317 static surf_action_t ws_action_stat(void *workstation, int fd, void* buf)
318 {
319   return surf_cpu_model->extension.cpu.
320       sleep(((workstation_CLM03_t) workstation)->cpu, 5);
321 }
322
323 static void surf_workstation_model_init_internal(void)
324 {
325   surf_workstation_model = surf_model_init();
326
327   surf_workstation_model->name = "Workstation";
328   surf_workstation_model->action_unref = ws_action_unref;
329   surf_workstation_model->action_cancel = ws_action_cancel;
330   surf_workstation_model->action_state_set = ws_action_state_set;
331
332   surf_workstation_model->model_private->resource_used = ws_resource_used;
333   surf_workstation_model->model_private->share_resources =
334       ws_share_resources;
335   surf_workstation_model->model_private->update_actions_state =
336       ws_update_actions_state;
337   surf_workstation_model->model_private->update_resource_state =
338       ws_update_resource_state;
339   surf_workstation_model->model_private->finalize = ws_finalize;
340
341   surf_workstation_model->suspend = ws_action_suspend;
342   surf_workstation_model->resume = ws_action_resume;
343   surf_workstation_model->is_suspended = ws_action_is_suspended;
344   surf_workstation_model->set_max_duration = ws_action_set_max_duration;
345   surf_workstation_model->set_priority = ws_action_set_priority;
346 #ifdef HAVE_TRACING
347   surf_workstation_model->set_category = ws_action_set_category;
348 #endif
349   surf_workstation_model->get_remains = ws_action_get_remains;
350 #ifdef HAVE_LATENCY_BOUND_TRACKING
351   surf_workstation_model->get_latency_limited = ws_get_latency_limited;
352 #endif
353
354   surf_workstation_model->extension.workstation.execute = ws_execute;
355   surf_workstation_model->extension.workstation.sleep = ws_action_sleep;
356   surf_workstation_model->extension.workstation.get_state = ws_get_state;
357   surf_workstation_model->extension.workstation.get_speed = ws_get_speed;
358   surf_workstation_model->extension.workstation.get_available_speed =
359       ws_get_available_speed;
360
361   surf_workstation_model->extension.workstation.communicate =
362       ws_communicate;
363   surf_workstation_model->extension.workstation.get_route = ws_get_route;
364   surf_workstation_model->extension.workstation.execute_parallel_task =
365       ws_execute_parallel_task;
366   surf_workstation_model->extension.workstation.get_link_bandwidth =
367       ws_get_link_bandwidth;
368   surf_workstation_model->extension.workstation.get_link_latency =
369       ws_get_link_latency;
370   surf_workstation_model->extension.workstation.link_shared =
371       ws_link_shared;
372   surf_workstation_model->extension.workstation.get_properties =
373       ws_get_properties;
374
375   surf_workstation_model->extension.workstation.open = ws_action_open;
376   surf_workstation_model->extension.workstation.close = ws_action_close;
377   surf_workstation_model->extension.workstation.read = ws_action_read;
378   surf_workstation_model->extension.workstation.write = ws_action_write;
379   surf_workstation_model->extension.workstation.stat = ws_action_stat;
380 }
381
382 void surf_workstation_model_init_current_default(void)
383 {
384   surf_workstation_model_init_internal();
385   xbt_cfg_setdefault_int(_surf_cfg_set, "network/crosstraffic", 1);
386   surf_cpu_model_init_Cas01();
387   surf_network_model_init_LegrandVelho();
388
389   xbt_dynar_push(model_list, &surf_workstation_model);
390   sg_platf_postparse_add_cb(create_workstations);
391 }
392
393 void surf_workstation_model_init_compound()
394 {
395
396   xbt_assert(surf_cpu_model, "No CPU model defined yet!");
397   xbt_assert(surf_network_model, "No network model defined yet!");
398   surf_workstation_model_init_internal();
399   xbt_dynar_push(model_list, &surf_workstation_model);
400   sg_platf_postparse_add_cb(create_workstations);
401 }