Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
* Cleanup the DTD by renaming:
[simgrid.git] / src / simdag / sd_workstation.c
1 #include "private.h"
2 #include "simdag/simdag.h"
3 #include "xbt/dict.h"
4 #include "xbt/sysdep.h"
5 #include "surf/surf.h"
6
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
8                                 "Logging specific to SimDag (workstation)");
9
10 /* Creates a workstation and registers it in SD.
11  */
12 SD_workstation_t __SD_workstation_create(void *surf_workstation, void *data) {
13
14   SD_workstation_t workstation;
15   const char *name;
16   SD_CHECK_INIT_DONE();
17   xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
18
19   workstation = xbt_new(s_SD_workstation_t, 1);
20   workstation->surf_workstation = surf_workstation;
21   workstation->data = data; /* user data */
22   workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
23   workstation->task_fifo = NULL;
24   workstation->current_task = NULL;
25   
26   name = SD_workstation_get_name(workstation);
27   xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy); /* add the workstation to the dictionary */
28   sd_global->workstation_count++;
29
30   return workstation;
31 }
32
33 /**
34  * \brief Returns a workstation given its name
35  *
36  * If there is no such workstation, the function returns \c NULL.
37  *
38  * \param name workstation name
39  * \return the workstation, or \c NULL if there is no such workstation
40  */
41 SD_workstation_t SD_workstation_get_by_name(const char *name) {
42   SD_CHECK_INIT_DONE();
43
44   xbt_assert0(name != NULL, "Invalid parameter");
45
46   return xbt_dict_get_or_null(sd_global->workstations, name);
47 }
48
49 /**
50  * \brief Returns the workstation list
51  *
52  * Use SD_workstation_get_number() to know the array size.
53  *
54  * \return an array of \ref SD_workstation_t containing all workstations
55  * \see SD_workstation_get_number()
56  */
57 const SD_workstation_t* SD_workstation_get_list(void) {
58
59   xbt_dict_cursor_t cursor;
60   char *key;
61   void *data;
62   int i;
63
64   SD_CHECK_INIT_DONE();
65   xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
66
67
68
69   if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
70     sd_global->workstation_list = xbt_new(SD_workstation_t, sd_global->workstation_count);
71   
72     i = 0;
73     xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
74       sd_global->workstation_list[i++] = (SD_workstation_t) data;
75     }
76   }
77   return sd_global->workstation_list;
78 }
79
80 /**
81  * \brief Returns the number of workstations
82  *
83  * \return the number of existing workstations
84  * \see SD_workstation_get_list()
85  */
86 int SD_workstation_get_number(void) {
87   SD_CHECK_INIT_DONE();
88   return sd_global->workstation_count;
89 }
90
91 /**
92  * \brief Returns the user data of a workstation
93  *
94  * \param workstation a workstation
95  * \return the user data associated with this workstation (can be \c NULL)
96  * \see SD_workstation_set_data()
97  */
98 void* SD_workstation_get_data(SD_workstation_t workstation) {
99   SD_CHECK_INIT_DONE();
100   xbt_assert0(workstation != NULL, "Invalid parameter");
101   return workstation->data;
102 }
103
104 /**
105  * \brief Sets the user data of a workstation
106  *
107  * The new data can be \c NULL. The old data should have been freed first
108  * if it was not \c NULL.
109  *
110  * \param workstation a workstation
111  * \param data the new data you want to associate with this workstation
112  * \see SD_workstation_get_data()
113  */
114 void SD_workstation_set_data(SD_workstation_t workstation, void *data) {
115   SD_CHECK_INIT_DONE();
116   xbt_assert0(workstation != NULL, "Invalid parameter");
117   workstation->data = data;
118 }
119
120 /**
121  * \brief Returns the name of a workstation
122  *
123  * \param workstation a workstation
124  * \return the name of this workstation (cannot be \c NULL)
125  */
126 const char* SD_workstation_get_name(SD_workstation_t workstation) {
127   SD_CHECK_INIT_DONE();
128   xbt_assert0(workstation != NULL, "Invalid parameter");
129   return surf_workstation_model->common_public->get_resource_name(workstation->surf_workstation);
130 }
131
132 /**
133  * \brief Returns the value of a certain workstation property
134  *
135  * \param workstation a workstation
136  * \param name a property name
137  * \return value of a property
138  */
139 const char* SD_workstation_get_property_value(SD_workstation_t ws, char* name)
140 {
141   return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
142 }
143
144
145 /**
146  * \brief Returns a xbt_dynar_t consisting of the list of properties assigned to this workstation
147  *
148  * \param workstation a workstation
149  * \return the dictionary containing the properties associated with the workstation
150  */
151 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
152 {
153   SD_CHECK_INIT_DONE();
154   xbt_assert0((workstation != NULL), "Invalid parameters");
155
156   return (surf_workstation_model->common_public->get_cpu_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, SD_workstation_t dst) {
172   void *surf_src;
173   void *surf_dst;
174   const void **surf_route;
175   int route_size;
176   const char *link_name;
177   int i;
178   
179   SD_CHECK_INIT_DONE();
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
189   surf_route = surf_workstation_model->extension_public->get_route(surf_src, surf_dst);
190   route_size = surf_workstation_model->extension_public->get_route_size(surf_src, surf_dst);
191
192
193   for (i = 0; i < route_size; i++) {
194     link_name = surf_workstation_model->extension_public->get_link_name(surf_route[i]);
195     sd_global->recyclable_route[i] = xbt_dict_get(sd_global->links, link_name);
196   }
197
198   return sd_global->recyclable_route;
199 }
200
201 /**
202  * \brief Returns the number of links on the route between two workstations
203  *
204  * \param src a workstation
205  * \param dst another workstation
206  * \return the number of links on the route between these two workstations
207  * \see SD_route_get_list()
208  */
209 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
210   SD_CHECK_INIT_DONE();
211   return surf_workstation_model->extension_public->
212     get_route_size(src->surf_workstation, 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   SD_CHECK_INIT_DONE();
224   xbt_assert0(workstation != NULL, "Invalid parameter");
225   return surf_workstation_model->extension_public->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   SD_CHECK_INIT_DONE();
237   xbt_assert0(workstation != NULL, "Invalid parameter");
238   return surf_workstation_model->extension_public->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, double computation_amount) {
249   SD_CHECK_INIT_DONE();
250   xbt_assert0(workstation != NULL, "Invalid parameter");
251   xbt_assert0(computation_amount >= 0, "computation_amount must be greater than or equal to zero");
252   return computation_amount / SD_workstation_get_power(workstation);
253 }
254
255 /**
256  * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
257  * between the workstations.
258  *
259  * \param src the first workstation
260  * \param dst the second workstation
261  * \return the latency of the route between the two workstations (in seconds)
262  * \see SD_route_get_current_bandwidth()
263  */
264 double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) {
265
266   const SD_link_t *links;
267   int nb_links;
268   double latency ;
269   int i;
270
271   SD_CHECK_INIT_DONE();
272   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
273   links = SD_route_get_list(src, dst);
274   nb_links = SD_route_get_size(src, dst);
275   latency = 0.0;
276   
277   for (i = 0; i < nb_links; i++) {
278     latency += SD_link_get_current_latency(links[i]);
279   }
280
281   return latency;
282 }
283
284 /**
285  * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
286  * between the workstations.
287  *
288  * \param src the first workstation
289  * \param dst the second workstation
290  * \return the bandwidth of the route between the two workstations (in bytes/second)
291  * \see SD_route_get_current_latency()
292  */
293 double SD_route_get_current_bandwidth(SD_workstation_t src, SD_workstation_t dst) {
294
295   const SD_link_t *links;
296   int nb_links;
297   double bandwidth;
298   double min_bandwidth;
299   int i;
300
301   SD_CHECK_INIT_DONE();
302   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
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, SD_workstation_t dst,
329                                                    double communication_amount) {
330
331
332   /* total time = latency + transmission time of the slowest link
333      transmission time of a link = communication amount / link bandwidth */
334
335   const SD_link_t *links;
336   int nb_links;
337   double bandwidth, min_bandwidth;
338   double latency;
339   int i;
340
341   SD_CHECK_INIT_DONE();
342   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
343   xbt_assert0(communication_amount >= 0, "communication_amount must be greater than or equal to zero");
344
345
346   
347   if (communication_amount == 0.0)
348     return 0.0;
349
350   links = SD_route_get_list(src, dst);
351   nb_links = SD_route_get_size(src, dst);
352   min_bandwidth = -1.0;
353   latency = 0;
354
355   for (i = 0; i < nb_links; i++) {
356     latency += SD_link_get_current_latency(links[i]);
357     bandwidth = SD_link_get_current_bandwidth(links[i]);
358     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
359       min_bandwidth = bandwidth;
360   }
361
362   return latency + (communication_amount / min_bandwidth);
363 }
364
365 /**
366  * \brief Returns the access mode of this workstation.
367  *
368  * \param workstation a workstation
369  * \return the access mode for the tasks running on this workstation:
370  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
371  *
372  * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
373  */
374  e_SD_workstation_access_mode_t SD_workstation_get_access_mode(SD_workstation_t workstation) {
375   SD_CHECK_INIT_DONE();
376   xbt_assert0(workstation != NULL, "Invalid parameter");
377   return workstation->access_mode;
378 }
379
380 /**
381  * \brief Sets the access mode for the tasks that will be executed on a workstation
382  *
383  * By default, a workstation model is shared, i.e. several tasks
384  * can be executed at the same time on a workstation. The CPU power of
385  * the workstation is shared between the running tasks on the workstation.
386  * In sequential mode, only one task can use the workstation, and the other
387  * tasks wait in a FIFO.
388  *
389  * \param workstation a workstation
390  * \param access_mode the access mode you want to set to this workstation:
391  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
392  *
393  * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
394  */
395 void SD_workstation_set_access_mode(SD_workstation_t workstation, e_SD_workstation_access_mode_t access_mode) {
396   SD_CHECK_INIT_DONE();
397   xbt_assert0(workstation != NULL, "Invalid parameter");
398
399   if (access_mode == workstation->access_mode) {
400     return; // nothing is changed
401   }
402
403   workstation->access_mode = access_mode;
404
405   if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
406     xbt_fifo_free(workstation->task_fifo);
407     workstation->task_fifo = NULL;
408   }
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_assert0(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   SD_CHECK_INIT_DONE();
430   xbt_assert0(workstation != NULL, "Invalid parameter");
431   
432   DEBUG4("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
433          SD_workstation_get_name(workstation),
434          (workstation->access_mode==SD_WORKSTATION_SHARED_ACCESS)?"SHARED":"FIFO",
435          (workstation->current_task ? SD_task_get_name(workstation->current_task) : "none"),
436          (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) : 0));
437
438   return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
439     (workstation->current_task != NULL || xbt_fifo_size(workstation->task_fifo) > 0);
440 }
441
442 /* Destroys a workstation.
443  */
444 void __SD_workstation_destroy(void *workstation) {
445
446   SD_workstation_t w;
447
448   SD_CHECK_INIT_DONE();
449   xbt_assert0(workstation != NULL, "Invalid parameter");
450   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
451
452   w = (SD_workstation_t) workstation;
453
454   if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
455     xbt_fifo_free(w->task_fifo);
456   }
457   xbt_free(w);
458 }