1 /* Copyright (c) 2006-2011. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simdag/simdag.h"
10 #include "xbt/sysdep.h"
11 #include "surf/surf.h"
12 #include "surf/surf_resource.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
17 "Logging specific to SimDag (workstation)");
19 /* Creates a workstation and registers it in SD.
21 SD_workstation_t __SD_workstation_create(void *surf_workstation,
25 SD_workstation_t workstation;
28 workstation = xbt_new(s_SD_workstation_t, 1);
29 workstation->surf_workstation = surf_workstation;
30 workstation->data = data; /* user data */
31 workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS; /* default mode is shared */
32 workstation->task_fifo = NULL;
33 workstation->current_task = NULL;
35 name = SD_workstation_get_name(workstation);
36 xbt_lib_set(host_lib,name,SD_HOST_LEVEL,workstation);
41 * \brief Returns a workstation given its name
43 * If there is no such workstation, the function returns \c NULL.
45 * \param name workstation name
46 * \return the workstation, or \c NULL if there is no such workstation
48 SD_workstation_t SD_workstation_get_by_name(const char *name)
50 return xbt_lib_get_or_null(host_lib, name, SD_HOST_LEVEL);
54 * \brief Returns the workstation list
56 * Use SD_workstation_get_number() to know the array size.
58 * \return an array of \ref SD_workstation_t containing all workstations
59 * \see SD_workstation_get_number()
61 const SD_workstation_t *SD_workstation_get_list(void)
64 xbt_lib_cursor_t cursor;
69 xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
71 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
72 sd_global->workstation_list =
73 xbt_new(SD_workstation_t, xbt_lib_length(host_lib));
76 xbt_lib_foreach(host_lib, cursor, key, data) {
77 if(data[SD_HOST_LEVEL])
78 sd_global->workstation_list[i++] = (SD_workstation_t) data[SD_HOST_LEVEL];
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)
92 return xbt_lib_length(host_lib);
96 * \brief Returns the user data of a workstation
98 * \param workstation a workstation
99 * \return the user data associated with this workstation (can be \c NULL)
100 * \see SD_workstation_set_data()
102 void *SD_workstation_get_data(SD_workstation_t workstation)
104 return workstation->data;
108 * \brief Sets the user data of a workstation
110 * The new data can be \c NULL. The old data should have been freed first
111 * if it was not \c NULL.
113 * \param workstation a workstation
114 * \param data the new data you want to associate with this workstation
115 * \see SD_workstation_get_data()
117 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
119 workstation->data = data;
123 * \brief Returns the name of a workstation
125 * \param workstation a workstation
126 * \return the name of this workstation (cannot be \c NULL)
128 const char *SD_workstation_get_name(SD_workstation_t workstation)
130 return surf_resource_name(workstation->surf_workstation);
134 * \brief Returns the value of a given workstation property
136 * \param ws a workstation
137 * \param name a property name
138 * \return value of a property (or NULL if property not set)
140 const char *SD_workstation_get_property_value(SD_workstation_t ws,
143 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
148 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
150 * \param workstation a workstation
151 * \return the dictionary containing the properties associated with the workstation
153 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
155 return surf_workstation_model->extension.
156 workstation.get_properties(workstation->surf_workstation);
160 /** @brief Displays debugging informations about a workstation */
161 void SD_workstation_dump(SD_workstation_t ws)
164 xbt_dict_cursor_t cursor=NULL;
166 SD_task_t task = NULL;
168 XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
169 XBT_INFO(" - power: %.0f", SD_workstation_get_power(ws));
170 XBT_INFO(" - available power: %.2f", SD_workstation_get_available_power(ws));
171 switch (ws->access_mode){
172 case SD_WORKSTATION_SHARED_ACCESS:
173 XBT_INFO(" - access mode: Space shared");
175 case SD_WORKSTATION_SEQUENTIAL_ACCESS:
176 XBT_INFO(" - access mode: Exclusive");
177 task = SD_workstation_get_current_task(ws);
179 XBT_INFO(" current running task: %s",
180 SD_task_get_name(task));
182 XBT_INFO(" no task running");
185 XBT_INFO(" - (unknown access mode)");
187 props = SD_workstation_get_properties(ws);
189 if (!xbt_dict_is_empty(props)){
190 XBT_INFO(" - properties:");
192 xbt_dict_foreach(props,cursor,key,data) {
193 XBT_INFO(" %s->%s",key,data);
199 * \brief Returns the route between two workstations
201 * Use SD_route_get_size() to know the array size.
203 * \param src a workstation
204 * \param dst another workstation
205 * \return a new array of \ref SD_link_t representating the route between these two workstations
206 * \see SD_route_get_size(), SD_link_t
208 const SD_link_t *SD_route_get_list(SD_workstation_t src,
209 SD_workstation_t dst)
213 xbt_dynar_t surf_route;
214 const char *link_name;
218 if (sd_global->recyclable_route == NULL) {
220 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
223 surf_src = src->surf_workstation;
224 surf_dst = dst->surf_workstation;
226 surf_workstation_model->extension.workstation.get_route(surf_src,
229 xbt_dynar_foreach(surf_route, cpt, surf_link) {
230 link_name = surf_resource_name(surf_link);
231 sd_global->recyclable_route[cpt] =
232 xbt_lib_get_or_null(link_lib, link_name, SD_LINK_LEVEL);
234 return sd_global->recyclable_route;
238 * \brief Returns the number of links on the route between two workstations
240 * \param src a workstation
241 * \param dst another workstation
242 * \return the number of links on the route between these two workstations
243 * \see SD_route_get_list()
245 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
247 return xbt_dynar_length(surf_workstation_model->extension.
248 workstation.get_route(src->surf_workstation,
249 dst->surf_workstation));
253 * \brief Returns the total power of a workstation
255 * \param workstation a workstation
256 * \return the total power of this workstation
257 * \see SD_workstation_get_available_power()
259 double SD_workstation_get_power(SD_workstation_t workstation)
261 return surf_workstation_model->extension.workstation.
262 get_speed(workstation->surf_workstation, 1.0);
266 * \brief Returns the proportion of available power in a workstation
268 * \param workstation a workstation
269 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
270 * \see SD_workstation_get_power()
272 double SD_workstation_get_available_power(SD_workstation_t workstation)
274 return surf_workstation_model->extension.
275 workstation.get_available_speed(workstation->surf_workstation);
279 * \brief Returns an approximative estimated time for the given computation amount on a workstation
281 * \param workstation a workstation
282 * \param computation_amount the computation amount you want to evaluate (in flops)
283 * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
285 double SD_workstation_get_computation_time(SD_workstation_t workstation,
286 double computation_amount)
288 xbt_assert(computation_amount >= 0,
289 "computation_amount must be greater than or equal to zero");
290 return computation_amount / SD_workstation_get_power(workstation);
294 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
295 * between the workstations.
297 * \param src the first workstation
298 * \param dst the second workstation
299 * \return the latency of the route between the two workstations (in seconds)
300 * \see SD_route_get_current_bandwidth()
302 double SD_route_get_current_latency(SD_workstation_t src,
303 SD_workstation_t dst)
306 const SD_link_t *links;
311 links = SD_route_get_list(src, dst);
312 nb_links = SD_route_get_size(src, dst);
315 for (i = 0; i < nb_links; i++) {
316 latency += SD_link_get_current_latency(links[i]);
323 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
324 * between the workstations.
326 * \param src the first workstation
327 * \param dst the second workstation
328 * \return the bandwidth of the route between the two workstations (in bytes/second)
329 * \see SD_route_get_current_latency()
331 double SD_route_get_current_bandwidth(SD_workstation_t src,
332 SD_workstation_t dst)
335 const SD_link_t *links;
338 double min_bandwidth;
341 links = SD_route_get_list(src, dst);
342 nb_links = SD_route_get_size(src, dst);
343 bandwidth = min_bandwidth = -1.0;
346 for (i = 0; i < nb_links; i++) {
347 bandwidth = SD_link_get_current_bandwidth(links[i]);
348 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
349 min_bandwidth = bandwidth;
352 return min_bandwidth;
356 * \brief Returns an approximative estimated time for the given
357 * communication amount between two workstations
359 * \param src the first workstation
360 * \param dst the second workstation
361 * \param communication_amount the communication amount you want to evaluate (in bytes)
362 * \return an approximative astimated computation time for the given communication amount
363 * between the workstations (in seconds)
365 double SD_route_get_communication_time(SD_workstation_t src,
366 SD_workstation_t dst,
367 double communication_amount)
371 /* total time = latency + transmission time of the slowest link
372 transmission time of a link = communication amount / link bandwidth */
374 const SD_link_t *links;
376 double bandwidth, min_bandwidth;
380 xbt_assert(communication_amount >= 0,
381 "communication_amount must be greater than or equal to zero");
385 if (communication_amount == 0.0)
388 links = SD_route_get_list(src, dst);
389 nb_links = SD_route_get_size(src, dst);
390 min_bandwidth = -1.0;
393 for (i = 0; i < nb_links; i++) {
394 latency += SD_link_get_current_latency(links[i]);
395 bandwidth = SD_link_get_current_bandwidth(links[i]);
396 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
397 min_bandwidth = bandwidth;
400 return latency + (communication_amount / min_bandwidth);
404 * \brief Returns the access mode of this workstation.
406 * \param workstation a workstation
407 * \return the access mode for the tasks running on this workstation:
408 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
410 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
412 e_SD_workstation_access_mode_t
413 SD_workstation_get_access_mode(SD_workstation_t workstation)
415 return workstation->access_mode;
419 * \brief Sets the access mode for the tasks that will be executed on a workstation
421 * By default, a workstation model is shared, i.e. several tasks
422 * can be executed at the same time on a workstation. The CPU power of
423 * the workstation is shared between the running tasks on the workstation.
424 * In sequential mode, only one task can use the workstation, and the other
425 * tasks wait in a FIFO.
427 * \param workstation a workstation
428 * \param access_mode the access mode you want to set to this workstation:
429 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
431 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
433 void SD_workstation_set_access_mode(SD_workstation_t workstation,
434 e_SD_workstation_access_mode_t
437 if (access_mode == workstation->access_mode) {
438 return; // nothing is changed
441 workstation->access_mode = access_mode;
443 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
444 xbt_fifo_free(workstation->task_fifo);
445 workstation->task_fifo = NULL;
447 workstation->task_fifo = xbt_fifo_new();
451 /* Returns whether a task can start now on a workstation.
453 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
454 SD_CHECK_INIT_DONE();
455 xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
457 return !__SD_workstation_is_busy(workstation) &&
458 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
462 /* Returns whether a workstation is busy. A workstation is busy is it is
463 * in sequential mode and a task is running on it or the fifo is not empty.
465 int __SD_workstation_is_busy(SD_workstation_t workstation)
468 ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
469 SD_workstation_get_name(workstation),
470 (workstation->access_mode ==
471 SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
472 (workstation->current_task ?
473 SD_task_get_name(workstation->current_task)
475 (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) :
478 return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
479 (workstation->current_task != NULL
480 || xbt_fifo_size(workstation->task_fifo) > 0);
483 /* Destroys a workstation.
485 void __SD_workstation_destroy(void *workstation)
490 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
492 w = (SD_workstation_t) workstation;
494 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
495 xbt_fifo_free(w->task_fifo);
501 * \brief Returns the kind of the task currently running on a workstation
502 * Only call this with sequential access mode set
503 * \param workstation a workstation */
504 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
506 xbt_assert(workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
507 "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
508 " to use this function");
510 return (workstation->current_task);