Logo AND Algorithmique Numérique Distribuée

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