Logo AND Algorithmique Numérique Distribuée

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