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_model->common_public->get_resource_name(workstation->surf_workstation);
133 * \brief Returns the value of a given workstation property
135 * \param workstation a workstation
136 * \param name a property name
137 * \return value of a property (or NULL if property not set)
139 const char* SD_workstation_get_property_value(SD_workstation_t ws, const char* name)
141 return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
146 * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
148 * \param workstation a workstation
149 * \return the dictionary containing the properties associated with the workstation
151 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
153 SD_CHECK_INIT_DONE();
154 xbt_assert0((workstation != NULL), "Invalid parameters");
156 return (surf_workstation_model->common_public->get_properties(workstation->surf_workstation));
162 * \brief Returns the route between two workstations
164 * Use SD_route_get_size() to know the array size.
166 * \param src a workstation
167 * \param dst another workstation
168 * \return a new array of \ref SD_link_t representating the route between these two workstations
169 * \see SD_route_get_size(), SD_link_t
171 const SD_link_t* SD_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
174 const void **surf_route;
176 const char *link_name;
179 SD_CHECK_INIT_DONE();
181 if (sd_global->recyclable_route == NULL) {
183 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
186 surf_src = src->surf_workstation;
187 surf_dst = dst->surf_workstation;
189 surf_route = surf_workstation_model->extension_public->get_route(surf_src, surf_dst);
190 route_size = surf_workstation_model->extension_public->get_route_size(surf_src, surf_dst);
193 for (i = 0; i < route_size; i++) {
194 link_name = surf_workstation_model->extension_public->get_link_name(surf_route[i]);
195 sd_global->recyclable_route[i] = xbt_dict_get(sd_global->links, link_name);
198 return sd_global->recyclable_route;
202 * \brief Returns the number of links on the route between two workstations
204 * \param src a workstation
205 * \param dst another workstation
206 * \return the number of links on the route between these two workstations
207 * \see SD_route_get_list()
209 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
210 SD_CHECK_INIT_DONE();
211 return surf_workstation_model->extension_public->
212 get_route_size(src->surf_workstation, dst->surf_workstation);
216 * \brief Returns the total power of a workstation
218 * \param workstation a workstation
219 * \return the total power of this workstation
220 * \see SD_workstation_get_available_power()
222 double SD_workstation_get_power(SD_workstation_t workstation) {
223 SD_CHECK_INIT_DONE();
224 xbt_assert0(workstation != NULL, "Invalid parameter");
225 return surf_workstation_model->extension_public->get_speed(workstation->surf_workstation, 1.0);
229 * \brief Returns the proportion of available power in a workstation
231 * \param workstation a workstation
232 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
233 * \see SD_workstation_get_power()
235 double SD_workstation_get_available_power(SD_workstation_t workstation) {
236 SD_CHECK_INIT_DONE();
237 xbt_assert0(workstation != NULL, "Invalid parameter");
238 return surf_workstation_model->extension_public->get_available_speed(workstation->surf_workstation);
242 * \brief Returns an approximative estimated time for the given computation amount on a workstation
244 * \param workstation a workstation
245 * \param computation_amount the computation amount you want to evaluate (in flops)
246 * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
248 double SD_workstation_get_computation_time(SD_workstation_t workstation, double computation_amount) {
249 SD_CHECK_INIT_DONE();
250 xbt_assert0(workstation != NULL, "Invalid parameter");
251 xbt_assert0(computation_amount >= 0, "computation_amount must be greater than or equal to zero");
252 return computation_amount / SD_workstation_get_power(workstation);
256 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
257 * between the workstations.
259 * \param src the first workstation
260 * \param dst the second workstation
261 * \return the latency of the route between the two workstations (in seconds)
262 * \see SD_route_get_current_bandwidth()
264 double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) {
266 const SD_link_t *links;
271 SD_CHECK_INIT_DONE();
272 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
273 links = SD_route_get_list(src, dst);
274 nb_links = SD_route_get_size(src, dst);
277 for (i = 0; i < nb_links; i++) {
278 latency += SD_link_get_current_latency(links[i]);
285 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
286 * between the workstations.
288 * \param src the first workstation
289 * \param dst the second workstation
290 * \return the bandwidth of the route between the two workstations (in bytes/second)
291 * \see SD_route_get_current_latency()
293 double SD_route_get_current_bandwidth(SD_workstation_t src, SD_workstation_t dst) {
295 const SD_link_t *links;
298 double min_bandwidth;
301 SD_CHECK_INIT_DONE();
302 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
304 links = SD_route_get_list(src, dst);
305 nb_links = SD_route_get_size(src, dst);
306 bandwidth = min_bandwidth = -1.0;
309 for (i = 0; i < nb_links; i++) {
310 bandwidth = SD_link_get_current_bandwidth(links[i]);
311 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
312 min_bandwidth = bandwidth;
315 return min_bandwidth;
319 * \brief Returns an approximative estimated time for the given
320 * communication amount between two workstations
322 * \param src the first workstation
323 * \param dst the second workstation
324 * \param communication_amount the communication amount you want to evaluate (in bytes)
325 * \return an approximative astimated computation time for the given communication amount
326 * between the workstations (in seconds)
328 double SD_route_get_communication_time(SD_workstation_t src, SD_workstation_t dst,
329 double communication_amount) {
332 /* total time = latency + transmission time of the slowest link
333 transmission time of a link = communication amount / link bandwidth */
335 const SD_link_t *links;
337 double bandwidth, min_bandwidth;
341 SD_CHECK_INIT_DONE();
342 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
343 xbt_assert0(communication_amount >= 0, "communication_amount must be greater than or equal to zero");
347 if (communication_amount == 0.0)
350 links = SD_route_get_list(src, dst);
351 nb_links = SD_route_get_size(src, dst);
352 min_bandwidth = -1.0;
355 for (i = 0; i < nb_links; i++) {
356 latency += SD_link_get_current_latency(links[i]);
357 bandwidth = SD_link_get_current_bandwidth(links[i]);
358 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
359 min_bandwidth = bandwidth;
362 return latency + (communication_amount / min_bandwidth);
366 * \brief Returns the access mode of this workstation.
368 * \param workstation a workstation
369 * \return the access mode for the tasks running on this workstation:
370 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
372 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
374 e_SD_workstation_access_mode_t SD_workstation_get_access_mode(SD_workstation_t workstation) {
375 SD_CHECK_INIT_DONE();
376 xbt_assert0(workstation != NULL, "Invalid parameter");
377 return workstation->access_mode;
381 * \brief Sets the access mode for the tasks that will be executed on a workstation
383 * By default, a workstation model is shared, i.e. several tasks
384 * can be executed at the same time on a workstation. The CPU power of
385 * the workstation is shared between the running tasks on the workstation.
386 * In sequential mode, only one task can use the workstation, and the other
387 * tasks wait in a FIFO.
389 * \param workstation a workstation
390 * \param access_mode the access mode you want to set to this workstation:
391 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
393 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
395 void SD_workstation_set_access_mode(SD_workstation_t workstation, e_SD_workstation_access_mode_t access_mode) {
396 SD_CHECK_INIT_DONE();
397 xbt_assert0(workstation != NULL, "Invalid parameter");
399 if (access_mode == workstation->access_mode) {
400 return; // nothing is changed
403 workstation->access_mode = access_mode;
405 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
406 xbt_fifo_free(workstation->task_fifo);
407 workstation->task_fifo = NULL;
410 workstation->task_fifo = xbt_fifo_new();
414 /* Returns whether a task can start now on a workstation.
416 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
417 SD_CHECK_INIT_DONE();
418 xbt_assert0(workstation != NULL && task != NULL, "Invalid parameter");
420 return !__SD_workstation_is_busy(workstation) &&
421 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
425 /* Returns whether a workstation is busy. A workstation is busy is it is
426 * in sequential mode and a task is running on it or the fifo is not empty.
428 int __SD_workstation_is_busy(SD_workstation_t workstation) {
429 SD_CHECK_INIT_DONE();
430 xbt_assert0(workstation != NULL, "Invalid parameter");
432 DEBUG4("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
433 SD_workstation_get_name(workstation),
434 (workstation->access_mode==SD_WORKSTATION_SHARED_ACCESS)?"SHARED":"FIFO",
435 (workstation->current_task ? SD_task_get_name(workstation->current_task) : "none"),
436 (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) : 0));
438 return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
439 (workstation->current_task != NULL || xbt_fifo_size(workstation->task_fifo) > 0);
442 /* Destroys a workstation.
444 void __SD_workstation_destroy(void *workstation) {
448 SD_CHECK_INIT_DONE();
449 xbt_assert0(workstation != NULL, "Invalid parameter");
450 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
452 w = (SD_workstation_t) workstation;
454 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
455 xbt_fifo_free(w->task_fifo);