Logo AND Algorithmique Numérique Distribuée

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