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 xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
16 SD_workstation_t workstation = xbt_new(s_SD_workstation_t, 1);
17 workstation->surf_workstation = surf_workstation;
18 workstation->data = data; /* user data */
19 workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
20 workstation->task_fifo = NULL;
21 workstation->current_task = NULL;
23 const char *name = SD_workstation_get_name(workstation);
24 xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy); /* add the workstation to the dictionary */
25 sd_global->workstation_count++;
31 * \brief Returns a workstation given its name
33 * If there is no such workstation, the function returns \c NULL.
35 * \param name workstation name
36 * \return the workstation, or \c NULL if there is no such workstation
38 SD_workstation_t SD_workstation_get_by_name(const char *name) {
41 xbt_assert0(name != NULL, "Invalid parameter");
43 return xbt_dict_get_or_null(sd_global->workstations, name);
47 * \brief Returns the workstation list
49 * Use SD_workstation_get_number() to know the array size.
51 * \return an array of \ref SD_workstation_t containing all workstations
52 * \see SD_workstation_get_number()
54 const SD_workstation_t* SD_workstation_get_list(void) {
56 xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
58 xbt_dict_cursor_t cursor;
63 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
64 sd_global->workstation_list = xbt_new(SD_workstation_t, sd_global->workstation_count);
67 xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
68 sd_global->workstation_list[i++] = (SD_workstation_t) data;
71 return sd_global->workstation_list;
75 * \brief Returns the number of workstations
77 * \return the number of existing workstations
78 * \see SD_workstation_get_list()
80 int SD_workstation_get_number(void) {
82 return sd_global->workstation_count;
86 * \brief Returns the user data of a workstation
88 * \param workstation a workstation
89 * \return the user data associated with this workstation (can be \c NULL)
90 * \see SD_workstation_set_data()
92 void* SD_workstation_get_data(SD_workstation_t workstation) {
94 xbt_assert0(workstation != NULL, "Invalid parameter");
95 return workstation->data;
99 * \brief Sets the user data of a workstation
101 * The new data can be \c NULL. The old data should have been freed first
102 * if it was not \c NULL.
104 * \param workstation a workstation
105 * \param data the new data you want to associate with this workstation
106 * \see SD_workstation_get_data()
108 void SD_workstation_set_data(SD_workstation_t workstation, void *data) {
109 SD_CHECK_INIT_DONE();
110 xbt_assert0(workstation != NULL, "Invalid parameter");
111 workstation->data = data;
115 * \brief Returns the name of a workstation
117 * \param workstation a workstation
118 * \return the name of this workstation (cannot be \c NULL)
120 const char* SD_workstation_get_name(SD_workstation_t workstation) {
121 SD_CHECK_INIT_DONE();
122 xbt_assert0(workstation != NULL, "Invalid parameter");
123 return surf_workstation_resource->common_public->get_resource_name(workstation->surf_workstation);
127 * \brief Returns the route between two workstations
129 * Use SD_route_get_size() to know the array size.
131 * \param src a workstation
132 * \param dst another workstation
133 * \return a new array of \ref SD_link_t representating the route between these two workstations
134 * \see SD_route_get_size(), SD_link_t
136 const SD_link_t* SD_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
137 SD_CHECK_INIT_DONE();
139 if (sd_global->recyclable_route == NULL) {
141 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
144 void *surf_src = src->surf_workstation;
145 void *surf_dst = dst->surf_workstation;
147 const void **surf_route = surf_workstation_resource->extension_public->get_route(surf_src, surf_dst);
148 int route_size = surf_workstation_resource->extension_public->get_route_size(surf_src, surf_dst);
150 const char *link_name;
152 for (i = 0; i < route_size; i++) {
153 link_name = surf_workstation_resource->extension_public->get_link_name(surf_route[i]);
154 sd_global->recyclable_route[i] = xbt_dict_get(sd_global->links, link_name);
157 return sd_global->recyclable_route;
161 * \brief Returns the number of links on the route between two workstations
163 * \param src a workstation
164 * \param dst another workstation
165 * \return the number of links on the route between these two workstations
166 * \see SD_route_get_list()
168 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
169 SD_CHECK_INIT_DONE();
170 return surf_workstation_resource->extension_public->
171 get_route_size(src->surf_workstation, dst->surf_workstation);
175 * \brief Returns the total power of a workstation
177 * \param workstation a workstation
178 * \return the total power of this workstation
179 * \see SD_workstation_get_available_power()
181 double SD_workstation_get_power(SD_workstation_t workstation) {
182 SD_CHECK_INIT_DONE();
183 xbt_assert0(workstation != NULL, "Invalid parameter");
184 return surf_workstation_resource->extension_public->get_speed(workstation->surf_workstation, 1.0);
188 * \brief Returns the proportion of available power in a workstation
190 * \param workstation a workstation
191 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
192 * \see SD_workstation_get_power()
194 double SD_workstation_get_available_power(SD_workstation_t workstation) {
195 SD_CHECK_INIT_DONE();
196 xbt_assert0(workstation != NULL, "Invalid parameter");
197 return surf_workstation_resource->extension_public->get_available_speed(workstation->surf_workstation);
201 * \brief Returns an approximative estimated time for the given computation amount on a workstation
203 * \param workstation a workstation
204 * \param computation_amount the computation amount you want to evaluate (in flops)
205 * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
207 double SD_workstation_get_computation_time(SD_workstation_t workstation, double computation_amount) {
208 SD_CHECK_INIT_DONE();
209 xbt_assert0(workstation != NULL, "Invalid parameter");
210 xbt_assert0(computation_amount >= 0, "computation_amount must be greater than or equal to zero");
211 return computation_amount / SD_workstation_get_power(workstation);
215 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
216 * between the workstations.
218 * \param src the first workstation
219 * \param dst the second workstation
220 * \return the latency of the route between the two workstations (in seconds)
221 * \see SD_route_get_current_bandwidth()
223 double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) {
224 SD_CHECK_INIT_DONE();
225 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
226 const SD_link_t *links = SD_route_get_list(src, dst);
227 int nb_links = SD_route_get_size(src, dst);
228 double latency = 0.0;
231 for (i = 0; i < nb_links; i++) {
232 latency += SD_link_get_current_latency(links[i]);
239 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
240 * between the workstations.
242 * \param src the first workstation
243 * \param dst the second workstation
244 * \return the bandwidth of the route between the two workstations (in bytes/second)
245 * \see SD_route_get_current_latency()
247 double SD_route_get_current_bandwidth(SD_workstation_t src, SD_workstation_t dst) {
248 SD_CHECK_INIT_DONE();
249 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
250 const SD_link_t *links = SD_route_get_list(src, dst);
251 int nb_links = SD_route_get_size(src, dst);
252 double bandwidth, min_bandwidth = -1.0;
255 for (i = 0; i < nb_links; i++) {
256 bandwidth = SD_link_get_current_bandwidth(links[i]);
257 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
258 min_bandwidth = bandwidth;
261 return min_bandwidth;
265 * \brief Returns an approximative estimated time for the given
266 * communication amount between two workstations
268 * \param src the first workstation
269 * \param dst the second workstation
270 * \param communication_amount the communication amount you want to evaluate (in bytes)
271 * \return an approximative astimated computation time for the given communication amount
272 * between the workstations (in seconds)
274 double SD_route_get_communication_time(SD_workstation_t src, SD_workstation_t dst,
275 double communication_amount) {
276 /* total time = latency + transmission time of the slowest link
277 transmission time of a link = communication amount / link bandwidth */
278 SD_CHECK_INIT_DONE();
279 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
280 xbt_assert0(communication_amount >= 0, "communication_amount must be greater than or equal to zero");
282 const SD_link_t *links;
284 double bandwidth, min_bandwidth;
288 if (communication_amount == 0.0)
291 links = SD_route_get_list(src, dst);
292 nb_links = SD_route_get_size(src, dst);
293 min_bandwidth = -1.0;
296 for (i = 0; i < nb_links; i++) {
297 latency += SD_link_get_current_latency(links[i]);
298 bandwidth = SD_link_get_current_bandwidth(links[i]);
299 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
300 min_bandwidth = bandwidth;
303 return latency + (communication_amount / min_bandwidth);
307 * \brief Returns the access mode of this workstation.
309 * \param workstation a workstation
310 * \return the access mode for the tasks running on this workstation:
311 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
313 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
315 e_SD_workstation_access_mode_t SD_workstation_get_access_mode(SD_workstation_t workstation) {
316 SD_CHECK_INIT_DONE();
317 xbt_assert0(workstation != NULL, "Invalid parameter");
318 return workstation->access_mode;
322 * \brief Sets the access mode for the tasks that will be executed on a workstation
324 * By default, a workstation resource is shared, i.e. several tasks
325 * can be executed at the same time on a workstation. The CPU power of
326 * the workstation is shared between the running tasks on the workstation.
327 * In sequential mode, only one task can use the workstation, and the other
328 * tasks wait in a FIFO.
330 * \param workstation a workstation
331 * \param access_mode the access mode you want to set to this workstation:
332 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
334 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
336 void SD_workstation_set_access_mode(SD_workstation_t workstation, e_SD_workstation_access_mode_t access_mode) {
337 SD_CHECK_INIT_DONE();
338 xbt_assert0(workstation != NULL, "Invalid parameter");
340 if (access_mode == workstation->access_mode) {
341 return; // nothing is changed
344 workstation->access_mode = access_mode;
346 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
347 xbt_fifo_free(workstation->task_fifo);
348 workstation->task_fifo = NULL;
351 workstation->task_fifo = xbt_fifo_new();
355 /* Returns whether a task can start now on a workstation.
357 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
358 SD_CHECK_INIT_DONE();
359 xbt_assert0(workstation != NULL && task != NULL, "Invalid parameter");
361 return !__SD_workstation_is_busy(workstation) &&
362 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
366 /* Returns whether a workstation is busy. A workstation is busy is it is
367 * in sequential mode and a task is running on it or the fifo is not empty.
369 int __SD_workstation_is_busy(SD_workstation_t workstation) {
370 SD_CHECK_INIT_DONE();
371 xbt_assert0(workstation != NULL, "Invalid parameter");
373 DEBUG4("Workstation '%s' access mode: %d, current task: %s, fifo size: %d",
374 SD_workstation_get_name(workstation),
375 workstation->access_mode,
376 (workstation->current_task ? SD_task_get_name(workstation->current_task) : "none"),
377 (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) : 0));
379 return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
380 (workstation->current_task != NULL || xbt_fifo_size(workstation->task_fifo) > 0);
383 /* Destroys a workstation.
385 void __SD_workstation_destroy(void *workstation) {
386 SD_CHECK_INIT_DONE();
387 xbt_assert0(workstation != NULL, "Invalid parameter");
388 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
390 SD_workstation_t w = (SD_workstation_t) workstation;
392 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
393 xbt_fifo_free(w->task_fifo);