2 #include "simdag/simdag.h"
4 #include "xbt/sysdep.h"
7 /* Creates a workstation and registers it in SD.
9 SD_workstation_t __SD_workstation_create(void *surf_workstation, void *data) {
11 xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
13 SD_workstation_t workstation = xbt_new0(s_SD_workstation_t, 1);
14 workstation->surf_workstation = surf_workstation;
15 workstation->data = data; /* user data */
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++;
25 * \brief Returns a workstation given its name
27 * If there is no such workstation, the function returns \c NULL.
29 * \param name workstation name
30 * \return the workstation, or \c NULL if there is no such workstation
32 SD_workstation_t SD_workstation_get_by_name(const char *name) {
35 xbt_assert0(name != NULL, "Invalid parameter");
37 return xbt_dict_get_or_null(sd_global->workstations, name);
41 * \brief Returns the workstation list
43 * Use SD_workstation_get_number() to know the array size.
45 * \return an array of \ref SD_workstation_t containing all workstations
46 * \see SD_workstation_get_number()
48 const SD_workstation_t* SD_workstation_get_list(void) {
50 xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
52 xbt_dict_cursor_t cursor;
57 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
58 sd_global->workstation_list = xbt_new0(SD_workstation_t, sd_global->workstation_count);
61 xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
62 sd_global->workstation_list[i++] = (SD_workstation_t) data;
65 return sd_global->workstation_list;
69 * \brief Returns the number of workstations
71 * \return the number of existing workstations
72 * \see SD_workstation_get_list()
74 int SD_workstation_get_number(void) {
76 return sd_global->workstation_count;
80 * \brief Returns the user data of a workstation
82 * \param workstation a workstation
83 * \return the user data associated with this workstation (can be \c NULL)
84 * \see SD_workstation_set_data()
86 void* SD_workstation_get_data(SD_workstation_t workstation) {
88 xbt_assert0(workstation != NULL, "Invalid parameter");
89 return workstation->data;
93 * \brief Sets the user data of a workstation
95 * The new data can be \c NULL. The old data should have been freed first
96 * if it was not \c NULL.
98 * \param workstation a workstation
99 * \param data the new data you want to associate with this workstation
100 * \see SD_workstation_get_data()
102 void SD_workstation_set_data(SD_workstation_t workstation, void *data) {
103 SD_CHECK_INIT_DONE();
104 xbt_assert0(workstation != NULL, "Invalid parameter");
105 workstation->data = data;
109 * \brief Returns the name of a workstation
111 * \param workstation a workstation
112 * \return the name of this workstation (cannot be \c NULL)
114 const char* SD_workstation_get_name(SD_workstation_t workstation) {
115 SD_CHECK_INIT_DONE();
116 xbt_assert0(workstation != NULL, "Invalid parameter");
117 return surf_workstation_resource->common_public->get_resource_name(workstation->surf_workstation);
121 * \brief Returns the route between two workstations
123 * Use SD_route_get_size() to know the array size. Don't forget to free the array after use.
125 * \param src a workstation
126 * \param dst another workstation
127 * \return a new array of \ref SD_link_t representating the route between these two workstations
128 * \see SD_route_get_size(), SD_link_t
130 SD_link_t* SD_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
131 SD_CHECK_INIT_DONE();
133 void *surf_src = src->surf_workstation;
134 void *surf_dst = dst->surf_workstation;
136 const void **surf_route = surf_workstation_resource->extension_public->get_route(surf_src, surf_dst);
137 int route_size = surf_workstation_resource->extension_public->get_route_size(surf_src, surf_dst);
139 SD_link_t* route = xbt_new0(SD_link_t, route_size);
140 const char *link_name;
142 for (i = 0; i < route_size; i++) {
143 link_name = surf_workstation_resource->extension_public->get_link_name(surf_route[i]);
144 route[i] = xbt_dict_get(sd_global->links, link_name);
151 * \brief Returns the number of links on the route between two workstations
153 * \param src a workstation
154 * \param dst another workstation
155 * \return the number of links on the route between these two workstations
156 * \see SD_route_get_list()
158 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
159 SD_CHECK_INIT_DONE();
160 return surf_workstation_resource->extension_public->
161 get_route_size(src->surf_workstation, dst->surf_workstation);
165 * \brief Returns the total power of a workstation
167 * \param workstation a workstation
168 * \return the total power of this workstation
169 * \see SD_workstation_get_available_power()
171 double SD_workstation_get_power(SD_workstation_t workstation) {
172 SD_CHECK_INIT_DONE();
173 xbt_assert0(workstation != NULL, "Invalid parameter");
174 return surf_workstation_resource->extension_public->get_speed(workstation->surf_workstation, 1.0);
178 * \brief Returns the proportion of available power in a workstation
180 * \param workstation a workstation
181 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
182 * \see SD_workstation_get_power()
184 double SD_workstation_get_available_power(SD_workstation_t workstation) {
185 SD_CHECK_INIT_DONE();
186 xbt_assert0(workstation != NULL, "Invalid parameter");
187 return surf_workstation_resource->extension_public->get_available_speed(workstation->surf_workstation);
191 * \brief Returns an approximative estimated time for the given computation amount on a workstation
193 * \param workstation a workstation
194 * \param computation_amount the computation amount you want to evaluate (in flops)
195 * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
197 double SD_workstation_get_computation_time(SD_workstation_t workstation, double computation_amount) {
198 SD_CHECK_INIT_DONE();
199 xbt_assert0(workstation != NULL, "Invalid parameter");
200 xbt_assert0(computation_amount >= 0, "computation_amount must be greater than or equal to zero");
201 return computation_amount / SD_workstation_get_power(workstation);
205 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
206 * between the workstations.
208 * \param src the first workstation
209 * \param dst the second workstation
210 * \return the latency of the route between the two workstations (in seconds)
211 * \see SD_route_get_current_bandwidth()
213 double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) {
214 SD_CHECK_INIT_DONE();
215 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
216 SD_link_t *links = SD_route_get_list(src, dst);
217 int nb_links = SD_route_get_size(src, dst);
218 double latency = 0.0;
221 for (i = 0; i < nb_links; i++) {
222 latency += SD_link_get_current_latency(links[i]);
230 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
231 * between the workstations.
233 * \param src the first workstation
234 * \param dst the second workstation
235 * \return the bandwidth of the route between the two workstations (in bytes/second)
236 * \see SD_route_get_current_latency()
238 double SD_route_get_current_bandwidth(SD_workstation_t src, SD_workstation_t dst) {
239 SD_CHECK_INIT_DONE();
240 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
241 SD_link_t *links = SD_route_get_list(src, dst);
242 int nb_links = SD_route_get_size(src, dst);
243 double bandwidth, min_bandwidth = -1.0;
246 for (i = 0; i < nb_links; i++) {
247 bandwidth = SD_link_get_current_bandwidth(links[i]);
248 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
249 min_bandwidth = bandwidth;
253 return min_bandwidth;
257 * \brief Returns an approximative estimated time for the given
258 * communication amount between two workstations
260 * \param src the first workstation
261 * \param dst the second workstation
262 * \param communication_amount the communication amount you want to evaluate (in bytes)
263 * \return an approximative astimated computation time for the given communication amount
264 * between the workstations (in seconds)
266 double SD_route_get_communication_time(SD_workstation_t src, SD_workstation_t dst,
267 double communication_amount) {
268 /* total time = latency + transmission time of the slowest link
269 transmission time of a link = communication amount / link bandwidth */
270 SD_CHECK_INIT_DONE();
271 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
272 xbt_assert0(communication_amount >= 0, "communication_amount must be greater than or equal to zero");
276 double bandwidth, min_bandwidth;
280 if (communication_amount == 0.0)
283 links = SD_route_get_list(src, dst);
284 nb_links = SD_route_get_size(src, dst);
285 min_bandwidth = -1.0;
288 for (i = 0; i < nb_links; i++) {
289 latency += SD_link_get_current_latency(links[i]);
290 bandwidth = SD_link_get_current_bandwidth(links[i]);
291 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
292 min_bandwidth = bandwidth;
296 return latency + (communication_amount / min_bandwidth);
299 /* Destroys a workstation.
301 void __SD_workstation_destroy(void *workstation) {
302 SD_CHECK_INIT_DONE();
303 xbt_assert0(workstation != NULL, "Invalid parameter");
304 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
305 xbt_free(workstation);