Logo AND Algorithmique Numérique Distribuée

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