Logo AND Algorithmique Numérique Distribuée

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