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)
15 SD_workstation_t workstation;
18 xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
20 workstation = xbt_new(s_SD_workstation_t, 1);
21 workstation->surf_workstation = surf_workstation;
22 workstation->data = data; /* user data */
23 workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
24 workstation->task_fifo = NULL;
25 workstation->current_task = NULL;
27 name = SD_workstation_get_name(workstation);
28 xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy); /* add the workstation to the dictionary */
29 sd_global->workstation_count++;
35 * \brief Returns a workstation given its name
37 * If there is no such workstation, the function returns \c NULL.
39 * \param name workstation name
40 * \return the workstation, or \c NULL if there is no such workstation
42 SD_workstation_t SD_workstation_get_by_name(const char *name)
46 xbt_assert0(name != NULL, "Invalid parameter");
48 return xbt_dict_get_or_null(sd_global->workstations, name);
52 * \brief Returns the workstation list
54 * Use SD_workstation_get_number() to know the array size.
56 * \return an array of \ref SD_workstation_t containing all workstations
57 * \see SD_workstation_get_number()
59 const SD_workstation_t *SD_workstation_get_list(void)
62 xbt_dict_cursor_t cursor;
68 xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
72 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
73 sd_global->workstation_list =
74 xbt_new(SD_workstation_t, sd_global->workstation_count);
77 xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
78 sd_global->workstation_list[i++] = (SD_workstation_t) data;
81 return sd_global->workstation_list;
85 * \brief Returns the number of workstations
87 * \return the number of existing workstations
88 * \see SD_workstation_get_list()
90 int SD_workstation_get_number(void)
93 return sd_global->workstation_count;
97 * \brief Returns the user data of a workstation
99 * \param workstation a workstation
100 * \return the user data associated with this workstation (can be \c NULL)
101 * \see SD_workstation_set_data()
103 void *SD_workstation_get_data(SD_workstation_t workstation)
105 SD_CHECK_INIT_DONE();
106 xbt_assert0(workstation != NULL, "Invalid parameter");
107 return workstation->data;
111 * \brief Sets the user data of a workstation
113 * The new data can be \c NULL. The old data should have been freed first
114 * if it was not \c NULL.
116 * \param workstation a workstation
117 * \param data the new data you want to associate with this workstation
118 * \see SD_workstation_get_data()
120 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
122 SD_CHECK_INIT_DONE();
123 xbt_assert0(workstation != NULL, "Invalid parameter");
124 workstation->data = data;
128 * \brief Returns the name of a workstation
130 * \param workstation a workstation
131 * \return the name of this workstation (cannot be \c NULL)
133 const char *SD_workstation_get_name(SD_workstation_t workstation)
135 SD_CHECK_INIT_DONE();
136 xbt_assert0(workstation != NULL, "Invalid parameter");
137 return surf_workstation_model->common_public.
138 get_resource_name(workstation->surf_workstation);
142 * \brief Returns the value of a given workstation property
144 * \param ws a workstation
145 * \param name a property name
146 * \return value of a property (or NULL if property not set)
148 const char *SD_workstation_get_property_value(SD_workstation_t ws,
151 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
156 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
158 * \param workstation a workstation
159 * \return the dictionary containing the properties associated with the workstation
161 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
163 SD_CHECK_INIT_DONE();
164 xbt_assert0((workstation != NULL), "Invalid parameters");
166 return (surf_workstation_model->common_public.
167 get_properties(workstation->surf_workstation));
173 * \brief Returns the route between two workstations
175 * Use SD_route_get_size() to know the array size.
177 * \param src a workstation
178 * \param dst another workstation
179 * \return a new array of \ref SD_link_t representating the route between these two workstations
180 * \see SD_route_get_size(), SD_link_t
182 const SD_link_t *SD_route_get_list(SD_workstation_t src, SD_workstation_t dst)
186 const void **surf_route;
188 const char *link_name;
191 SD_CHECK_INIT_DONE();
193 if (sd_global->recyclable_route == NULL) {
195 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
198 surf_src = src->surf_workstation;
199 surf_dst = dst->surf_workstation;
202 surf_workstation_model->extension.workstation.get_route(surf_src,
205 surf_workstation_model->extension.workstation.get_route_size(surf_src,
209 for (i = 0; i < route_size; i++) {
211 surf_workstation_model->extension.
212 workstation.get_link_name(surf_route[i]);
213 sd_global->recyclable_route[i] =
214 xbt_dict_get(sd_global->links, link_name);
217 return sd_global->recyclable_route;
221 * \brief Returns the number of links on the route between two workstations
223 * \param src a workstation
224 * \param dst another workstation
225 * \return the number of links on the route between these two workstations
226 * \see SD_route_get_list()
228 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
230 SD_CHECK_INIT_DONE();
231 return surf_workstation_model->extension.workstation.get_route_size(src->
238 * \brief Returns the total power of a workstation
240 * \param workstation a workstation
241 * \return the total power of this workstation
242 * \see SD_workstation_get_available_power()
244 double SD_workstation_get_power(SD_workstation_t workstation)
246 SD_CHECK_INIT_DONE();
247 xbt_assert0(workstation != NULL, "Invalid parameter");
248 return surf_workstation_model->extension.workstation.get_speed(workstation->
254 * \brief Returns the proportion of available power in a workstation
256 * \param workstation a workstation
257 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
258 * \see SD_workstation_get_power()
260 double SD_workstation_get_available_power(SD_workstation_t workstation)
262 SD_CHECK_INIT_DONE();
263 xbt_assert0(workstation != NULL, "Invalid parameter");
264 return surf_workstation_model->extension.workstation.
265 get_available_speed(workstation->surf_workstation);
269 * \brief Returns an approximative estimated time for the given computation amount on a workstation
271 * \param workstation a workstation
272 * \param computation_amount the computation amount you want to evaluate (in flops)
273 * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
275 double SD_workstation_get_computation_time(SD_workstation_t workstation,
276 double computation_amount)
278 SD_CHECK_INIT_DONE();
279 xbt_assert0(workstation != NULL, "Invalid parameter");
280 xbt_assert0(computation_amount >= 0,
281 "computation_amount must be greater than or equal to zero");
282 return computation_amount / SD_workstation_get_power(workstation);
286 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
287 * between the workstations.
289 * \param src the first workstation
290 * \param dst the second workstation
291 * \return the latency of the route between the two workstations (in seconds)
292 * \see SD_route_get_current_bandwidth()
294 double SD_route_get_current_latency(SD_workstation_t src,
295 SD_workstation_t dst)
298 const SD_link_t *links;
303 SD_CHECK_INIT_DONE();
304 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
305 links = SD_route_get_list(src, dst);
306 nb_links = SD_route_get_size(src, dst);
309 for (i = 0; i < nb_links; i++) {
310 latency += SD_link_get_current_latency(links[i]);
317 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
318 * between the workstations.
320 * \param src the first workstation
321 * \param dst the second workstation
322 * \return the bandwidth of the route between the two workstations (in bytes/second)
323 * \see SD_route_get_current_latency()
325 double SD_route_get_current_bandwidth(SD_workstation_t src,
326 SD_workstation_t dst)
329 const SD_link_t *links;
332 double min_bandwidth;
335 SD_CHECK_INIT_DONE();
336 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
338 links = SD_route_get_list(src, dst);
339 nb_links = SD_route_get_size(src, dst);
340 bandwidth = min_bandwidth = -1.0;
343 for (i = 0; i < nb_links; i++) {
344 bandwidth = SD_link_get_current_bandwidth(links[i]);
345 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
346 min_bandwidth = bandwidth;
349 return min_bandwidth;
353 * \brief Returns an approximative estimated time for the given
354 * communication amount between two workstations
356 * \param src the first workstation
357 * \param dst the second workstation
358 * \param communication_amount the communication amount you want to evaluate (in bytes)
359 * \return an approximative astimated computation time for the given communication amount
360 * between the workstations (in seconds)
362 double SD_route_get_communication_time(SD_workstation_t src,
363 SD_workstation_t dst,
364 double communication_amount)
368 /* total time = latency + transmission time of the slowest link
369 transmission time of a link = communication amount / link bandwidth */
371 const SD_link_t *links;
373 double bandwidth, min_bandwidth;
377 SD_CHECK_INIT_DONE();
378 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
379 xbt_assert0(communication_amount >= 0,
380 "communication_amount must be greater than or equal to zero");
384 if (communication_amount == 0.0)
387 links = SD_route_get_list(src, dst);
388 nb_links = SD_route_get_size(src, dst);
389 min_bandwidth = -1.0;
392 for (i = 0; i < nb_links; i++) {
393 latency += SD_link_get_current_latency(links[i]);
394 bandwidth = SD_link_get_current_bandwidth(links[i]);
395 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
396 min_bandwidth = bandwidth;
399 return latency + (communication_amount / min_bandwidth);
403 * \brief Returns the access mode of this workstation.
405 * \param workstation a workstation
406 * \return the access mode for the tasks running on this workstation:
407 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
409 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
411 e_SD_workstation_access_mode_t SD_workstation_get_access_mode(SD_workstation_t
414 SD_CHECK_INIT_DONE();
415 xbt_assert0(workstation != NULL, "Invalid parameter");
416 return workstation->access_mode;
420 * \brief Sets the access mode for the tasks that will be executed on a workstation
422 * By default, a workstation model is shared, i.e. several tasks
423 * can be executed at the same time on a workstation. The CPU power of
424 * the workstation is shared between the running tasks on the workstation.
425 * In sequential mode, only one task can use the workstation, and the other
426 * tasks wait in a FIFO.
428 * \param workstation a workstation
429 * \param access_mode the access mode you want to set to this workstation:
430 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
432 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
434 void SD_workstation_set_access_mode(SD_workstation_t workstation,
435 e_SD_workstation_access_mode_t
438 SD_CHECK_INIT_DONE();
439 xbt_assert0(workstation != NULL, "Invalid parameter");
441 if (access_mode == workstation->access_mode) {
442 return; // nothing is changed
445 workstation->access_mode = access_mode;
447 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
448 xbt_fifo_free(workstation->task_fifo);
449 workstation->task_fifo = NULL;
451 workstation->task_fifo = xbt_fifo_new();
455 /* Returns whether a task can start now on a workstation.
457 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
458 SD_CHECK_INIT_DONE();
459 xbt_assert0(workstation != NULL && task != NULL, "Invalid parameter");
461 return !__SD_workstation_is_busy(workstation) &&
462 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
466 /* Returns whether a workstation is busy. A workstation is busy is it is
467 * in sequential mode and a task is running on it or the fifo is not empty.
469 int __SD_workstation_is_busy(SD_workstation_t workstation)
471 SD_CHECK_INIT_DONE();
472 xbt_assert0(workstation != NULL, "Invalid parameter");
475 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
476 SD_workstation_get_name(workstation),
477 (workstation->access_mode ==
478 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
479 (workstation->current_task ? SD_task_get_name(workstation->current_task)
481 (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) : 0));
483 return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
484 (workstation->current_task != NULL
485 || xbt_fifo_size(workstation->task_fifo) > 0);
488 /* Destroys a workstation.
490 void __SD_workstation_destroy(void *workstation)
495 SD_CHECK_INIT_DONE();
496 xbt_assert0(workstation != NULL, "Invalid parameter");
497 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
499 w = (SD_workstation_t) workstation;
501 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
502 xbt_fifo_free(w->task_fifo);