Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add documentation for workstations and fix a few bugs
[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   
17   const char *name = SD_workstation_get_name(workstation);
18   xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy); /* add the workstation to the dictionary */
19   sd_global->workstation_count++;
20
21   return workstation;
22 }
23
24 /** @ingroup SD_workstation_management
25  * @brief Returns a workstation given its name
26  *
27  * If there is no such workstation, the function returns NULL.
28  *
29  * @param name workstation name
30  * @return the workstation, or NULL if there is no such workstation
31  */
32 SD_workstation_t SD_workstation_get_by_name(const char *name) {
33   SD_CHECK_INIT_DONE();
34
35   xbt_assert0(name != NULL, "Invalid parameter");
36
37   return xbt_dict_get_or_null(sd_global->workstations, name);
38 }
39
40 /** @ingroup SD_workstation_management
41  * @brief Returns the workstations list
42  *
43  * Use SD_workstation_get_number to known the array size.
44  *
45  * @return an array of SD_workstation_t containing all workstations
46  * @see SD_workstation_get_number
47  */
48 SD_workstation_t*  SD_workstation_get_list(void) {
49   SD_CHECK_INIT_DONE();
50   xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
51
52   SD_workstation_t* array = xbt_new0(SD_workstation_t, sd_global->workstation_count);
53   
54   xbt_dict_cursor_t cursor;
55   char *key;
56   void *data;
57   int i=0;
58
59   xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
60     array[i++] = (SD_workstation_t) data;
61   }
62
63   return array;
64 }
65
66 /** @ingroup SD_workstation_management
67  * @brief Returns the number of workstations
68  *
69  * @return the number of existing workstations
70  * @see SD_workstation_get_list
71  */
72 int SD_workstation_get_number(void) {
73   SD_CHECK_INIT_DONE();
74   return sd_global->workstation_count;
75 }
76
77 /** @ingroup SD_workstation_management
78  * @brief Sets the user data of a workstation
79  *
80  * The new data can be NULL. The old data should have been freed first
81  * if it was not NULL.
82  *
83  * @param workstation a workstation
84  * @param data the new data you want to associate with this workstation
85  * @see SD_workstation_get_data
86  */
87 void SD_workstation_set_data(SD_workstation_t workstation, void *data) {
88   SD_CHECK_INIT_DONE();
89   xbt_assert0(workstation != NULL, "Invalid parameter");
90   workstation->data = data;
91 }
92
93 /** @ingroup SD_workstation_management
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 NULL)
98  * @see SD_workstation_set_data
99  */
100 void* SD_workstation_get_data(SD_workstation_t workstation) {
101   SD_CHECK_INIT_DONE();
102   xbt_assert0(workstation != NULL, "Invalid parameter");
103   return workstation->data;
104 }
105
106 /** @ingroup SD_workstation_management
107  * @brief Returns the name of a workstation
108  *
109  * @param workstation a workstation
110  * @return the name of this workstation (cannot be NULL)
111  */
112 const char* SD_workstation_get_name(SD_workstation_t workstation) {
113   SD_CHECK_INIT_DONE();
114   xbt_assert0(workstation != NULL, "Invalid parameter");
115   return surf_workstation_resource->common_public->get_resource_name(workstation->surf_workstation);
116 }
117
118 /** @ingroup SD_workstation_management
119  * @brief Returns the route between two workstations
120  *
121  * Use SD_workstation_route_get_size to known the array size.
122  *
123  * @param src a workstation
124  * @param dst another workstation
125  * @return a new array of SD_link_t representating the route between these two workstations
126  * @see SD_workstation_route_get_size
127  */
128 SD_link_t* SD_workstation_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
129   SD_CHECK_INIT_DONE();
130
131   void *surf_src = src->surf_workstation;
132   void *surf_dst = dst->surf_workstation;
133
134   const void **surf_route = surf_workstation_resource->extension_public->get_route(surf_src, surf_dst);
135   int route_size = surf_workstation_resource->extension_public->get_route_size(surf_src, surf_dst);
136
137   SD_link_t* route = xbt_new0(SD_link_t, route_size);
138   const char *link_name;
139   int i;
140   for (i = 0; i < route_size; i++) {
141     link_name = surf_workstation_resource->extension_public->get_link_name(surf_route[i]);
142     route[i] = xbt_dict_get(sd_global->links, link_name);
143   }
144
145   return route;
146 }
147
148 /** @ingroup SD_workstation_management
149  * @brief Returns the number of links on the route between two workstations
150  *
151  * @param src a workstation
152  * @param dst another workstation
153  * @return the number of links on the route between these two workstations
154  * @see SD_workstation_route_get_list
155  */
156 int SD_workstation_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
157   SD_CHECK_INIT_DONE();
158   return surf_workstation_resource->extension_public->
159     get_route_size(src->surf_workstation, dst->surf_workstation);
160 }
161
162 /** @ingroup SD_workstation_management
163  * @brief Returns the total power of a workstation
164  *
165  * @param workstation a workstation
166  * @return the total power of this workstation
167  * @see SD_workstation_get_available_power
168  */
169 double SD_workstation_get_power(SD_workstation_t workstation) {
170   SD_CHECK_INIT_DONE();
171   xbt_assert0(workstation != NULL, "Invalid parameter");
172   return surf_workstation_resource->extension_public->get_speed(workstation->surf_workstation, 1.0);
173 }
174
175 /** @ingroup SD_workstation_management
176  * @brief Returns the proportion of available power in a workstation
177  *
178  * @param workstation a workstation
179  * @return the proportion of power currently available in this workstation (normally a number between 0 and 1)
180  * @see SD_workstation_get_power
181  */
182 double SD_workstation_get_available_power(SD_workstation_t workstation) {
183   SD_CHECK_INIT_DONE();
184   xbt_assert0(workstation != NULL, "Invalid parameter");
185   return surf_workstation_resource->extension_public->get_available_speed(workstation->surf_workstation);
186 }
187
188 /* Destroys a workstation.
189  */
190 void __SD_workstation_destroy(void *workstation) {
191   SD_CHECK_INIT_DONE();
192   xbt_assert0(workstation != NULL, "Invalid parameter");
193   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
194   xbt_free(workstation);
195 }