Logo AND Algorithmique Numérique Distribuée

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