Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill link_set (use surf_network_model->resource_set instead)
[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->get_properties
166           (workstation->surf_workstation));
167
168 }
169
170
171 /**
172  * \brief Returns the route between two workstations
173  *
174  * Use SD_route_get_size() to know the array size.
175  *
176  * \param src a workstation
177  * \param dst another workstation
178  * \return a new array of \ref SD_link_t representating the route between these two workstations
179  * \see SD_route_get_size(), SD_link_t
180  */
181 const SD_link_t *SD_route_get_list(SD_workstation_t src, SD_workstation_t dst)
182 {
183   void *surf_src;
184   void *surf_dst;
185   xbt_dynar_t surf_route;
186   const char *link_name;
187   void *surf_link;
188   unsigned int cpt;
189
190   SD_CHECK_INIT_DONE();
191
192   if (sd_global->recyclable_route == NULL) {
193     /* first run */
194     sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
195   }
196
197   surf_src = src->surf_workstation;
198   surf_dst = dst->surf_workstation;
199
200   surf_route =
201     surf_workstation_model->extension.workstation.get_route(surf_src,
202                                                             surf_dst);
203
204   xbt_dynar_foreach(surf_route, cpt, surf_link) {
205     link_name = surf_resource_name(surf_link);
206     sd_global->recyclable_route[cpt] =
207       xbt_dict_get(sd_global->links, link_name);
208   }
209
210   return sd_global->recyclable_route;
211 }
212
213 /**
214  * \brief Returns the number of links on the route between two workstations
215  *
216  * \param src a workstation
217  * \param dst another workstation
218  * \return the number of links on the route between these two workstations
219  * \see SD_route_get_list()
220  */
221 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
222 {
223   SD_CHECK_INIT_DONE();
224   return xbt_dynar_length(surf_workstation_model->extension.workstation.get_route(
225         src->surf_workstation,dst->surf_workstation));
226 }
227
228 /**
229  * \brief Returns the total power of a workstation
230  *
231  * \param workstation a workstation
232  * \return the total power of this workstation
233  * \see SD_workstation_get_available_power()
234  */
235 double SD_workstation_get_power(SD_workstation_t workstation)
236 {
237   SD_CHECK_INIT_DONE();
238   xbt_assert0(workstation != NULL, "Invalid parameter");
239   return surf_workstation_model->extension.workstation.
240     get_speed(workstation->surf_workstation, 1.0);
241 }
242
243 /**
244  * \brief Returns the proportion of available power in a workstation
245  *
246  * \param workstation a workstation
247  * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
248  * \see SD_workstation_get_power()
249  */
250 double SD_workstation_get_available_power(SD_workstation_t workstation)
251 {
252   SD_CHECK_INIT_DONE();
253   xbt_assert0(workstation != NULL, "Invalid parameter");
254   return surf_workstation_model->extension.
255     workstation.get_available_speed(workstation->surf_workstation);
256 }
257
258 /**
259  * \brief Returns an approximative estimated time for the given computation amount on a workstation
260  *
261  * \param workstation a workstation
262  * \param computation_amount the computation amount you want to evaluate (in flops)
263  * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
264  */
265 double SD_workstation_get_computation_time(SD_workstation_t workstation,
266                                            double computation_amount)
267 {
268   SD_CHECK_INIT_DONE();
269   xbt_assert0(workstation != NULL, "Invalid parameter");
270   xbt_assert0(computation_amount >= 0,
271               "computation_amount must be greater than or equal to zero");
272   return computation_amount / SD_workstation_get_power(workstation);
273 }
274
275 /**
276  * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
277  * between the workstations.
278  *
279  * \param src the first workstation
280  * \param dst the second workstation
281  * \return the latency of the route between the two workstations (in seconds)
282  * \see SD_route_get_current_bandwidth()
283  */
284 double SD_route_get_current_latency(SD_workstation_t src,
285                                     SD_workstation_t dst)
286 {
287
288   const SD_link_t *links;
289   int nb_links;
290   double latency;
291   int i;
292
293   SD_CHECK_INIT_DONE();
294   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
295   links = SD_route_get_list(src, dst);
296   nb_links = SD_route_get_size(src, dst);
297   latency = 0.0;
298
299   for (i = 0; i < nb_links; i++) {
300     latency += SD_link_get_current_latency(links[i]);
301   }
302
303   return latency;
304 }
305
306 /**
307  * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
308  * between the workstations.
309  *
310  * \param src the first workstation
311  * \param dst the second workstation
312  * \return the bandwidth of the route between the two workstations (in bytes/second)
313  * \see SD_route_get_current_latency()
314  */
315 double SD_route_get_current_bandwidth(SD_workstation_t src,
316                                       SD_workstation_t dst)
317 {
318
319   const SD_link_t *links;
320   int nb_links;
321   double bandwidth;
322   double min_bandwidth;
323   int i;
324
325   SD_CHECK_INIT_DONE();
326   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
327
328   links = SD_route_get_list(src, dst);
329   nb_links = SD_route_get_size(src, dst);
330   bandwidth = min_bandwidth = -1.0;
331
332
333   for (i = 0; i < nb_links; i++) {
334     bandwidth = SD_link_get_current_bandwidth(links[i]);
335     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
336       min_bandwidth = bandwidth;
337   }
338
339   return min_bandwidth;
340 }
341
342 /**
343  * \brief Returns an approximative estimated time for the given
344  * communication amount between two workstations
345  *
346  * \param src the first workstation
347  * \param dst the second workstation
348  * \param communication_amount the communication amount you want to evaluate (in bytes)
349  * \return an approximative astimated computation time for the given communication amount
350  * between the workstations (in seconds)
351  */
352 double SD_route_get_communication_time(SD_workstation_t src,
353                                        SD_workstation_t dst,
354                                        double communication_amount)
355 {
356
357
358   /* total time = latency + transmission time of the slowest link
359      transmission time of a link = communication amount / link bandwidth */
360
361   const SD_link_t *links;
362   int nb_links;
363   double bandwidth, min_bandwidth;
364   double latency;
365   int i;
366
367   SD_CHECK_INIT_DONE();
368   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
369   xbt_assert0(communication_amount >= 0,
370               "communication_amount must be greater than or equal to zero");
371
372
373
374   if (communication_amount == 0.0)
375     return 0.0;
376
377   links = SD_route_get_list(src, dst);
378   nb_links = SD_route_get_size(src, dst);
379   min_bandwidth = -1.0;
380   latency = 0;
381
382   for (i = 0; i < nb_links; i++) {
383     latency += SD_link_get_current_latency(links[i]);
384     bandwidth = SD_link_get_current_bandwidth(links[i]);
385     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
386       min_bandwidth = bandwidth;
387   }
388
389   return latency + (communication_amount / min_bandwidth);
390 }
391
392 /**
393  * \brief Returns the access mode of this workstation.
394  *
395  * \param workstation a workstation
396  * \return the access mode for the tasks running on this workstation:
397  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
398  *
399  * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
400  */
401 e_SD_workstation_access_mode_t SD_workstation_get_access_mode(SD_workstation_t
402                                                               workstation)
403 {
404   SD_CHECK_INIT_DONE();
405   xbt_assert0(workstation != NULL, "Invalid parameter");
406   return workstation->access_mode;
407 }
408
409 /**
410  * \brief Sets the access mode for the tasks that will be executed on a workstation
411  *
412  * By default, a workstation model is shared, i.e. several tasks
413  * can be executed at the same time on a workstation. The CPU power of
414  * the workstation is shared between the running tasks on the workstation.
415  * In sequential mode, only one task can use the workstation, and the other
416  * tasks wait in a FIFO.
417  *
418  * \param workstation a workstation
419  * \param access_mode the access mode you want to set to this workstation:
420  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
421  *
422  * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
423  */
424 void SD_workstation_set_access_mode(SD_workstation_t workstation,
425                                     e_SD_workstation_access_mode_t
426                                     access_mode)
427 {
428   SD_CHECK_INIT_DONE();
429   xbt_assert0(workstation != NULL, "Invalid parameter");
430
431   if (access_mode == workstation->access_mode) {
432     return;                     // nothing is changed
433   }
434
435   workstation->access_mode = access_mode;
436
437   if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
438     xbt_fifo_free(workstation->task_fifo);
439     workstation->task_fifo = NULL;
440   } else {
441     workstation->task_fifo = xbt_fifo_new();
442   }
443 }
444
445 /* Returns whether a task can start now on a workstation.
446                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               *//*
447                                                                                                                                                                                                                                                                                                                                                                                                    int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
448                                                                                                                                                                                                                                                                                                                                                                                                    SD_CHECK_INIT_DONE();
449                                                                                                                                                                                                                                                                                                                                                                                                    xbt_assert0(workstation != NULL && task != NULL, "Invalid parameter");
450
451                                                                                                                                                                                                                                                                                                                                                                                                    return !__SD_workstation_is_busy(workstation) &&
452                                                                                                                                                                                                                                                                                                                                                                                                    (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
453                                                                                                                                                                                                                                                                                                                                                                                                    }
454                                                                                                                                                                                                                                                                                                                                                                                                  */
455
456 /* Returns whether a workstation is busy. A workstation is busy is it is
457  * in sequential mode and a task is running on it or the fifo is not empty.
458  */
459 int __SD_workstation_is_busy(SD_workstation_t workstation)
460 {
461   SD_CHECK_INIT_DONE();
462   xbt_assert0(workstation != NULL, "Invalid parameter");
463
464   DEBUG4
465     ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
466      SD_workstation_get_name(workstation),
467      (workstation->access_mode ==
468       SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
469      (workstation->current_task ? SD_task_get_name(workstation->current_task)
470       : "none"),
471      (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) : 0));
472
473   return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
474     (workstation->current_task != NULL
475      || xbt_fifo_size(workstation->task_fifo) > 0);
476 }
477
478 /* Destroys a workstation.
479  */
480 void __SD_workstation_destroy(void *workstation)
481 {
482
483   SD_workstation_t w;
484
485   SD_CHECK_INIT_DONE();
486   xbt_assert0(workstation != NULL, "Invalid parameter");
487   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
488
489   w = (SD_workstation_t) workstation;
490
491   if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
492     xbt_fifo_free(w->task_fifo);
493   }
494   xbt_free(w);
495 }