2 #include "simdag/simdag.h"
4 #include "xbt/sysdep.h"
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
8 "Logging specific to SimDag (workstation)");
10 /* Creates a workstation and registers it in SD.
12 SD_workstation_t __SD_workstation_create(void *surf_workstation, void *data) {
14 SD_workstation_t workstation;
17 xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
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;
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++;
34 * \brief Returns a workstation given its name
36 * If there is no such workstation, the function returns \c NULL.
38 * \param name workstation name
39 * \return the workstation, or \c NULL if there is no such workstation
41 SD_workstation_t SD_workstation_get_by_name(const char *name) {
44 xbt_assert0(name != NULL, "Invalid parameter");
46 return xbt_dict_get_or_null(sd_global->workstations, name);
50 * \brief Returns the workstation list
52 * Use SD_workstation_get_number() to know the array size.
54 * \return an array of \ref SD_workstation_t containing all workstations
55 * \see SD_workstation_get_number()
57 const SD_workstation_t* SD_workstation_get_list(void) {
59 xbt_dict_cursor_t cursor;
65 xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
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);
73 xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
74 sd_global->workstation_list[i++] = (SD_workstation_t) data;
77 return sd_global->workstation_list;
81 * \brief Returns the number of workstations
83 * \return the number of existing workstations
84 * \see SD_workstation_get_list()
86 int SD_workstation_get_number(void) {
88 return sd_global->workstation_count;
92 * \brief Returns the user data of a workstation
94 * \param workstation a workstation
95 * \return the user data associated with this workstation (can be \c NULL)
96 * \see SD_workstation_set_data()
98 void* SD_workstation_get_data(SD_workstation_t workstation) {
100 xbt_assert0(workstation != NULL, "Invalid parameter");
101 return workstation->data;
105 * \brief Sets the user data of a workstation
107 * The new data can be \c NULL. The old data should have been freed first
108 * if it was not \c NULL.
110 * \param workstation a workstation
111 * \param data the new data you want to associate with this workstation
112 * \see SD_workstation_get_data()
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;
121 * \brief Returns the name of a workstation
123 * \param workstation a workstation
124 * \return the name of this workstation (cannot be \c NULL)
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_resource->common_public->get_resource_name(workstation->surf_workstation);
133 * \brief Returns the route between two workstations
135 * Use SD_route_get_size() to know the array size.
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
142 const SD_link_t* SD_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
145 const void **surf_route;
147 const char *link_name;
150 SD_CHECK_INIT_DONE();
152 if (sd_global->recyclable_route == NULL) {
154 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
157 surf_src = src->surf_workstation;
158 surf_dst = dst->surf_workstation;
160 surf_route = surf_workstation_resource->extension_public->get_route(surf_src, surf_dst);
161 route_size = surf_workstation_resource->extension_public->get_route_size(surf_src, surf_dst);
164 for (i = 0; i < route_size; i++) {
165 link_name = surf_workstation_resource->extension_public->get_link_name(surf_route[i]);
166 sd_global->recyclable_route[i] = xbt_dict_get(sd_global->links, link_name);
169 return sd_global->recyclable_route;
173 * \brief Returns the number of links on the route between two workstations
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()
180 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
181 SD_CHECK_INIT_DONE();
182 return surf_workstation_resource->extension_public->
183 get_route_size(src->surf_workstation, dst->surf_workstation);
187 * \brief Returns the total power of a workstation
189 * \param workstation a workstation
190 * \return the total power of this workstation
191 * \see SD_workstation_get_available_power()
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_resource->extension_public->get_speed(workstation->surf_workstation, 1.0);
200 * \brief Returns the proportion of available power in a workstation
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()
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_resource->extension_public->get_available_speed(workstation->surf_workstation);
213 * \brief Returns an approximative estimated time for the given computation amount on a workstation
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)
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);
227 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
228 * between the workstations.
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()
235 double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) {
237 const SD_link_t *links;
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);
248 for (i = 0; i < nb_links; i++) {
249 latency += SD_link_get_current_latency(links[i]);
256 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
257 * between the workstations.
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()
264 double SD_route_get_current_bandwidth(SD_workstation_t src, SD_workstation_t dst) {
266 const SD_link_t *links;
269 double min_bandwidth;
272 SD_CHECK_INIT_DONE();
273 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
275 links = SD_route_get_list(src, dst);
276 nb_links = SD_route_get_size(src, dst);
277 bandwidth = min_bandwidth = -1.0;
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;
286 return min_bandwidth;
290 * \brief Returns an approximative estimated time for the given
291 * communication amount between two workstations
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)
299 double SD_route_get_communication_time(SD_workstation_t src, SD_workstation_t dst,
300 double communication_amount) {
303 /* total time = latency + transmission time of the slowest link
304 transmission time of a link = communication amount / link bandwidth */
306 const SD_link_t *links;
308 double bandwidth, min_bandwidth;
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");
318 if (communication_amount == 0.0)
321 links = SD_route_get_list(src, dst);
322 nb_links = SD_route_get_size(src, dst);
323 min_bandwidth = -1.0;
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;
333 return latency + (communication_amount / min_bandwidth);
337 * \brief Returns the access mode of this workstation.
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
343 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
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;
352 * \brief Sets the access mode for the tasks that will be executed on a workstation
354 * By default, a workstation resource 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.
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
364 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
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");
370 if (access_mode == workstation->access_mode) {
371 return; // nothing is changed
374 workstation->access_mode = access_mode;
376 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
377 xbt_fifo_free(workstation->task_fifo);
378 workstation->task_fifo = NULL;
381 workstation->task_fifo = xbt_fifo_new();
385 /* Returns whether a task can start now on a workstation.
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");
391 return !__SD_workstation_is_busy(workstation) &&
392 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
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.
399 int __SD_workstation_is_busy(SD_workstation_t workstation) {
400 SD_CHECK_INIT_DONE();
401 xbt_assert0(workstation != NULL, "Invalid parameter");
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));
409 return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
410 (workstation->current_task != NULL || xbt_fifo_size(workstation->task_fifo) > 0);
413 /* Destroys a workstation.
415 void __SD_workstation_destroy(void *workstation) {
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 */
423 w = (SD_workstation_t) workstation;
425 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
426 xbt_fifo_free(w->task_fifo);