Logo AND Algorithmique Numérique Distribuée

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