Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use defined interface for accessing to lib properties.
[simgrid.git] / src / simdag / sd_workstation.c
1 /* Copyright (c) 2006-2011. 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 "private.h"
8 #include "simdag/simdag.h"
9 #include "xbt/dict.h"
10 #include "xbt/sysdep.h"
11 #include "surf/surf.h"
12 #include "surf/surf_resource.h"
13
14
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
17                                 "Logging specific to SimDag (workstation)");
18
19 /* Creates a workstation and registers it in SD.
20  */
21 SD_workstation_t __SD_workstation_create(void *surf_workstation,
22                                          void *data)
23 {
24
25   SD_workstation_t workstation;
26   const char *name;
27
28   workstation = xbt_new(s_SD_workstation_t, 1);
29   workstation->surf_workstation = surf_workstation;
30   workstation->data = data;     /* user data */
31   workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS;      /* default mode is shared */
32   workstation->task_fifo = NULL;
33   workstation->current_task = NULL;
34
35   name = SD_workstation_get_name(workstation);
36   xbt_lib_set(host_lib,name,SD_HOST_LEVEL,workstation);
37   return workstation;
38 }
39
40 /**
41  * \brief Returns a workstation given its name
42  *
43  * If there is no such workstation, the function returns \c NULL.
44  *
45  * \param name workstation name
46  * \return the workstation, or \c NULL if there is no such workstation
47  */
48 SD_workstation_t SD_workstation_get_by_name(const char *name)
49 {
50   return xbt_lib_get_or_null(host_lib, name, SD_HOST_LEVEL);
51 }
52
53 /**
54  * \brief Returns the workstation list
55  *
56  * Use SD_workstation_get_number() to know the array size.
57  *
58  * \return an array of \ref SD_workstation_t containing all workstations
59  * \see SD_workstation_get_number()
60  */
61 const SD_workstation_t *SD_workstation_get_list(void)
62 {
63
64   xbt_lib_cursor_t cursor;
65   char *key;
66   void **data;
67   int i;
68
69   xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
70
71   if (sd_global->workstation_list == NULL) {    /* this is the first time the function is called */
72     sd_global->workstation_list =
73       xbt_new(SD_workstation_t, xbt_lib_length(host_lib));
74
75     i = 0;
76     xbt_lib_foreach(host_lib, cursor, key, data) {
77       if(data[SD_HOST_LEVEL])
78           sd_global->workstation_list[i++] = (SD_workstation_t) data[SD_HOST_LEVEL];
79     }
80   }
81   return sd_global->workstation_list;
82 }
83
84 /**
85  * \brief Returns the number of workstations
86  *
87  * \return the number of existing workstations
88  * \see SD_workstation_get_list()
89  */
90 int SD_workstation_get_number(void)
91 {
92   return xbt_lib_length(host_lib);
93 }
94
95 /**
96  * \brief Returns the user data of a workstation
97  *
98  * \param workstation a workstation
99  * \return the user data associated with this workstation (can be \c NULL)
100  * \see SD_workstation_set_data()
101  */
102 void *SD_workstation_get_data(SD_workstation_t workstation)
103 {
104   return workstation->data;
105 }
106
107 /**
108  * \brief Sets the user data of a workstation
109  *
110  * The new data can be \c NULL. The old data should have been freed first
111  * if it was not \c NULL.
112  *
113  * \param workstation a workstation
114  * \param data the new data you want to associate with this workstation
115  * \see SD_workstation_get_data()
116  */
117 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
118 {
119   workstation->data = data;
120 }
121
122 /**
123  * \brief Returns the name of a workstation
124  *
125  * \param workstation a workstation
126  * \return the name of this workstation (cannot be \c NULL)
127  */
128 const char *SD_workstation_get_name(SD_workstation_t workstation)
129 {
130   return surf_resource_name(workstation->surf_workstation);
131 }
132
133 /**
134  * \brief Returns the value of a given workstation property
135  *
136  * \param ws a workstation
137  * \param name a property name
138  * \return value of a property (or NULL if property not set)
139  */
140 const char *SD_workstation_get_property_value(SD_workstation_t ws,
141                                               const char *name)
142 {
143   return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
144 }
145
146
147 /**
148  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
149  *
150  * \param workstation a workstation
151  * \return the dictionary containing the properties associated with the workstation
152  */
153 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
154 {
155   return surf_workstation_model->extension.
156       workstation.get_properties(workstation->surf_workstation);
157
158 }
159
160
161 /**
162  * \brief Returns the route between two workstations
163  *
164  * Use SD_route_get_size() to know the array size.
165  *
166  * \param src a workstation
167  * \param dst another workstation
168  * \return a new array of \ref SD_link_t representating the route between these two workstations
169  * \see SD_route_get_size(), SD_link_t
170  */
171 const SD_link_t *SD_route_get_list(SD_workstation_t src,
172                                    SD_workstation_t dst)
173 {
174   void *surf_src;
175   void *surf_dst;
176   xbt_dynar_t surf_route;
177   const char *link_name;
178   void *surf_link;
179   unsigned int cpt;
180
181   if (sd_global->recyclable_route == NULL) {
182     /* first run */
183     sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
184   }
185
186   surf_src = src->surf_workstation;
187   surf_dst = dst->surf_workstation;
188   surf_route =
189       surf_workstation_model->extension.workstation.get_route(surf_src,
190                                                               surf_dst);
191
192   xbt_dynar_foreach(surf_route, cpt, surf_link) {
193     link_name = surf_resource_name(surf_link);
194     sd_global->recyclable_route[cpt] =
195         xbt_lib_get_or_null(link_lib, link_name, SD_LINK_LEVEL);
196   }
197   return sd_global->recyclable_route;
198 }
199
200 /**
201  * \brief Returns the number of links on the route between two workstations
202  *
203  * \param src a workstation
204  * \param dst another workstation
205  * \return the number of links on the route between these two workstations
206  * \see SD_route_get_list()
207  */
208 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
209 {
210   return xbt_dynar_length(surf_workstation_model->extension.
211                           workstation.get_route(src->surf_workstation,
212                                                 dst->surf_workstation));
213 }
214
215 /**
216  * \brief Returns the total power of a workstation
217  *
218  * \param workstation a workstation
219  * \return the total power of this workstation
220  * \see SD_workstation_get_available_power()
221  */
222 double SD_workstation_get_power(SD_workstation_t workstation)
223 {
224   return surf_workstation_model->extension.workstation.
225       get_speed(workstation->surf_workstation, 1.0);
226 }
227
228 /**
229  * \brief Returns the proportion of available power in a workstation
230  *
231  * \param workstation a workstation
232  * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
233  * \see SD_workstation_get_power()
234  */
235 double SD_workstation_get_available_power(SD_workstation_t workstation)
236 {
237   return surf_workstation_model->extension.
238       workstation.get_available_speed(workstation->surf_workstation);
239 }
240
241 /**
242  * \brief Returns an approximative estimated time for the given computation amount on a workstation
243  *
244  * \param workstation a workstation
245  * \param computation_amount the computation amount you want to evaluate (in flops)
246  * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
247  */
248 double SD_workstation_get_computation_time(SD_workstation_t workstation,
249                                            double computation_amount)
250 {
251   xbt_assert(computation_amount >= 0,
252               "computation_amount must be greater than or equal to zero");
253   return computation_amount / SD_workstation_get_power(workstation);
254 }
255
256 /**
257  * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
258  * between the workstations.
259  *
260  * \param src the first workstation
261  * \param dst the second workstation
262  * \return the latency of the route between the two workstations (in seconds)
263  * \see SD_route_get_current_bandwidth()
264  */
265 double SD_route_get_current_latency(SD_workstation_t src,
266                                     SD_workstation_t dst)
267 {
268
269   const SD_link_t *links;
270   int nb_links;
271   double latency;
272   int i;
273
274   links = SD_route_get_list(src, dst);
275   nb_links = SD_route_get_size(src, dst);
276   latency = 0.0;
277
278   for (i = 0; i < nb_links; i++) {
279     latency += SD_link_get_current_latency(links[i]);
280   }
281
282   return latency;
283 }
284
285 /**
286  * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
287  * between the workstations.
288  *
289  * \param src the first workstation
290  * \param dst the second workstation
291  * \return the bandwidth of the route between the two workstations (in bytes/second)
292  * \see SD_route_get_current_latency()
293  */
294 double SD_route_get_current_bandwidth(SD_workstation_t src,
295                                       SD_workstation_t dst)
296 {
297
298   const SD_link_t *links;
299   int nb_links;
300   double bandwidth;
301   double min_bandwidth;
302   int i;
303
304   links = SD_route_get_list(src, dst);
305   nb_links = SD_route_get_size(src, dst);
306   bandwidth = min_bandwidth = -1.0;
307
308
309   for (i = 0; i < nb_links; i++) {
310     bandwidth = SD_link_get_current_bandwidth(links[i]);
311     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
312       min_bandwidth = bandwidth;
313   }
314
315   return min_bandwidth;
316 }
317
318 /**
319  * \brief Returns an approximative estimated time for the given
320  * communication amount between two workstations
321  *
322  * \param src the first workstation
323  * \param dst the second workstation
324  * \param communication_amount the communication amount you want to evaluate (in bytes)
325  * \return an approximative astimated computation time for the given communication amount
326  * between the workstations (in seconds)
327  */
328 double SD_route_get_communication_time(SD_workstation_t src,
329                                        SD_workstation_t dst,
330                                        double communication_amount)
331 {
332
333
334   /* total time = latency + transmission time of the slowest link
335      transmission time of a link = communication amount / link bandwidth */
336
337   const SD_link_t *links;
338   int nb_links;
339   double bandwidth, min_bandwidth;
340   double latency;
341   int i;
342
343   xbt_assert(communication_amount >= 0,
344               "communication_amount must be greater than or equal to zero");
345
346
347
348   if (communication_amount == 0.0)
349     return 0.0;
350
351   links = SD_route_get_list(src, dst);
352   nb_links = SD_route_get_size(src, dst);
353   min_bandwidth = -1.0;
354   latency = 0;
355
356   for (i = 0; i < nb_links; i++) {
357     latency += SD_link_get_current_latency(links[i]);
358     bandwidth = SD_link_get_current_bandwidth(links[i]);
359     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
360       min_bandwidth = bandwidth;
361   }
362
363   return latency + (communication_amount / min_bandwidth);
364 }
365
366 /**
367  * \brief Returns the access mode of this workstation.
368  *
369  * \param workstation a workstation
370  * \return the access mode for the tasks running on this workstation:
371  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
372  *
373  * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
374  */
375 e_SD_workstation_access_mode_t
376 SD_workstation_get_access_mode(SD_workstation_t workstation)
377 {
378   return workstation->access_mode;
379 }
380
381 /**
382  * \brief Sets the access mode for the tasks that will be executed on a workstation
383  *
384  * By default, a workstation model is shared, i.e. several tasks
385  * can be executed at the same time on a workstation. The CPU power of
386  * the workstation is shared between the running tasks on the workstation.
387  * In sequential mode, only one task can use the workstation, and the other
388  * tasks wait in a FIFO.
389  *
390  * \param workstation a workstation
391  * \param access_mode the access mode you want to set to this workstation:
392  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
393  *
394  * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
395  */
396 void SD_workstation_set_access_mode(SD_workstation_t workstation,
397                                     e_SD_workstation_access_mode_t
398                                     access_mode)
399 {
400   if (access_mode == workstation->access_mode) {
401     return;                     // nothing is changed
402   }
403
404   workstation->access_mode = access_mode;
405
406   if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
407     xbt_fifo_free(workstation->task_fifo);
408     workstation->task_fifo = NULL;
409   } else {
410     workstation->task_fifo = xbt_fifo_new();
411   }
412 }
413
414 /* Returns whether a task can start now on a workstation.
415                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               *//*
416                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
417                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    SD_CHECK_INIT_DONE();
418                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
419
420                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return !__SD_workstation_is_busy(workstation) &&
421                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
422                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
423                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  */
424
425 /* Returns whether a workstation is busy. A workstation is busy is it is
426  * in sequential mode and a task is running on it or the fifo is not empty.
427  */
428 int __SD_workstation_is_busy(SD_workstation_t workstation)
429 {
430   XBT_DEBUG
431       ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
432        SD_workstation_get_name(workstation),
433        (workstation->access_mode ==
434         SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
435        (workstation->current_task ?
436         SD_task_get_name(workstation->current_task)
437         : "none"),
438        (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) :
439         0));
440
441   return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
442       (workstation->current_task != NULL
443        || xbt_fifo_size(workstation->task_fifo) > 0);
444 }
445
446 /* Destroys a workstation.
447  */
448 void __SD_workstation_destroy(void *workstation)
449 {
450
451   SD_workstation_t w;
452
453   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
454
455   w = (SD_workstation_t) workstation;
456
457   if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
458     xbt_fifo_free(w->task_fifo);
459   }
460   xbt_free(w);
461 }
462
463 /** 
464  * \brief Returns the kind of the task currently running on a workstation
465  * Only call this with sequential access mode set
466  * \param workstation a workstation */
467 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
468 {
469   xbt_assert(workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
470               "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
471               " to use this function");
472
473   return (workstation->current_task);
474 }