Logo AND Algorithmique Numérique Distribuée

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