2 #include "simdag/simdag.h"
4 #include "xbt/sysdep.h"
7 /* Creates a workstation and registers it in SD.
9 SD_workstation_t __SD_workstation_create(void *surf_workstation, void *data) {
11 xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
13 SD_workstation_t workstation = xbt_new(s_SD_workstation_t, 1);
14 workstation->surf_workstation = surf_workstation;
15 workstation->data = data; /* user data */
16 SD_workstation_set_access_mode(workstation, SD_WORKSTATION_SHARED_ACCESS); /* default mode is shared */
17 workstation->task_fifo = NULL;
19 const char *name = SD_workstation_get_name(workstation);
20 xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy); /* add the workstation to the dictionary */
21 sd_global->workstation_count++;
27 * \brief Returns a workstation given its name
29 * If there is no such workstation, the function returns \c NULL.
31 * \param name workstation name
32 * \return the workstation, or \c NULL if there is no such workstation
34 SD_workstation_t SD_workstation_get_by_name(const char *name) {
37 xbt_assert0(name != NULL, "Invalid parameter");
39 return xbt_dict_get_or_null(sd_global->workstations, name);
43 * \brief Returns the workstation list
45 * Use SD_workstation_get_number() to know the array size.
47 * \return an array of \ref SD_workstation_t containing all workstations
48 * \see SD_workstation_get_number()
50 const SD_workstation_t* SD_workstation_get_list(void) {
52 xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
54 xbt_dict_cursor_t cursor;
59 if (sd_global->workstation_list == NULL) { /* this is the first time the function is called */
60 sd_global->workstation_list = xbt_new(SD_workstation_t, sd_global->workstation_count);
63 xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
64 sd_global->workstation_list[i++] = (SD_workstation_t) data;
67 return sd_global->workstation_list;
71 * \brief Returns the number of workstations
73 * \return the number of existing workstations
74 * \see SD_workstation_get_list()
76 int SD_workstation_get_number(void) {
78 return sd_global->workstation_count;
82 * \brief Returns the user data of a workstation
84 * \param workstation a workstation
85 * \return the user data associated with this workstation (can be \c NULL)
86 * \see SD_workstation_set_data()
88 void* SD_workstation_get_data(SD_workstation_t workstation) {
90 xbt_assert0(workstation != NULL, "Invalid parameter");
91 return workstation->data;
95 * \brief Sets the user data of a workstation
97 * The new data can be \c NULL. The old data should have been freed first
98 * if it was not \c NULL.
100 * \param workstation a workstation
101 * \param data the new data you want to associate with this workstation
102 * \see SD_workstation_get_data()
104 void SD_workstation_set_data(SD_workstation_t workstation, void *data) {
105 SD_CHECK_INIT_DONE();
106 xbt_assert0(workstation != NULL, "Invalid parameter");
107 workstation->data = data;
111 * \brief Returns the name of a workstation
113 * \param workstation a workstation
114 * \return the name of this workstation (cannot be \c NULL)
116 const char* SD_workstation_get_name(SD_workstation_t workstation) {
117 SD_CHECK_INIT_DONE();
118 xbt_assert0(workstation != NULL, "Invalid parameter");
119 return surf_workstation_resource->common_public->get_resource_name(workstation->surf_workstation);
123 * \brief Returns the route between two workstations
125 * Use SD_route_get_size() to know the array size.
127 * \param src a workstation
128 * \param dst another workstation
129 * \return a new array of \ref SD_link_t representating the route between these two workstations
130 * \see SD_route_get_size(), SD_link_t
132 const SD_link_t* SD_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
133 SD_CHECK_INIT_DONE();
135 if (sd_global->recyclable_route == NULL) {
137 sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
140 void *surf_src = src->surf_workstation;
141 void *surf_dst = dst->surf_workstation;
143 const void **surf_route = surf_workstation_resource->extension_public->get_route(surf_src, surf_dst);
144 int route_size = surf_workstation_resource->extension_public->get_route_size(surf_src, surf_dst);
146 const char *link_name;
148 for (i = 0; i < route_size; i++) {
149 link_name = surf_workstation_resource->extension_public->get_link_name(surf_route[i]);
150 sd_global->recyclable_route[i] = xbt_dict_get(sd_global->links, link_name);
153 return sd_global->recyclable_route;
157 * \brief Returns the number of links on the route between two workstations
159 * \param src a workstation
160 * \param dst another workstation
161 * \return the number of links on the route between these two workstations
162 * \see SD_route_get_list()
164 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
165 SD_CHECK_INIT_DONE();
166 return surf_workstation_resource->extension_public->
167 get_route_size(src->surf_workstation, dst->surf_workstation);
171 * \brief Returns the total power of a workstation
173 * \param workstation a workstation
174 * \return the total power of this workstation
175 * \see SD_workstation_get_available_power()
177 double SD_workstation_get_power(SD_workstation_t workstation) {
178 SD_CHECK_INIT_DONE();
179 xbt_assert0(workstation != NULL, "Invalid parameter");
180 return surf_workstation_resource->extension_public->get_speed(workstation->surf_workstation, 1.0);
184 * \brief Returns the proportion of available power in a workstation
186 * \param workstation a workstation
187 * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
188 * \see SD_workstation_get_power()
190 double SD_workstation_get_available_power(SD_workstation_t workstation) {
191 SD_CHECK_INIT_DONE();
192 xbt_assert0(workstation != NULL, "Invalid parameter");
193 return surf_workstation_resource->extension_public->get_available_speed(workstation->surf_workstation);
197 * \brief Returns an approximative estimated time for the given computation amount on a workstation
199 * \param workstation a workstation
200 * \param computation_amount the computation amount you want to evaluate (in flops)
201 * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
203 double SD_workstation_get_computation_time(SD_workstation_t workstation, double computation_amount) {
204 SD_CHECK_INIT_DONE();
205 xbt_assert0(workstation != NULL, "Invalid parameter");
206 xbt_assert0(computation_amount >= 0, "computation_amount must be greater than or equal to zero");
207 return computation_amount / SD_workstation_get_power(workstation);
211 * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
212 * between the workstations.
214 * \param src the first workstation
215 * \param dst the second workstation
216 * \return the latency of the route between the two workstations (in seconds)
217 * \see SD_route_get_current_bandwidth()
219 double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) {
220 SD_CHECK_INIT_DONE();
221 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
222 const SD_link_t *links = SD_route_get_list(src, dst);
223 int nb_links = SD_route_get_size(src, dst);
224 double latency = 0.0;
227 for (i = 0; i < nb_links; i++) {
228 latency += SD_link_get_current_latency(links[i]);
235 * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
236 * between the workstations.
238 * \param src the first workstation
239 * \param dst the second workstation
240 * \return the bandwidth of the route between the two workstations (in bytes/second)
241 * \see SD_route_get_current_latency()
243 double SD_route_get_current_bandwidth(SD_workstation_t src, SD_workstation_t dst) {
244 SD_CHECK_INIT_DONE();
245 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
246 const SD_link_t *links = SD_route_get_list(src, dst);
247 int nb_links = SD_route_get_size(src, dst);
248 double bandwidth, min_bandwidth = -1.0;
251 for (i = 0; i < nb_links; i++) {
252 bandwidth = SD_link_get_current_bandwidth(links[i]);
253 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
254 min_bandwidth = bandwidth;
257 return min_bandwidth;
261 * \brief Returns an approximative estimated time for the given
262 * communication amount between two workstations
264 * \param src the first workstation
265 * \param dst the second workstation
266 * \param communication_amount the communication amount you want to evaluate (in bytes)
267 * \return an approximative astimated computation time for the given communication amount
268 * between the workstations (in seconds)
270 double SD_route_get_communication_time(SD_workstation_t src, SD_workstation_t dst,
271 double communication_amount) {
272 /* total time = latency + transmission time of the slowest link
273 transmission time of a link = communication amount / link bandwidth */
274 SD_CHECK_INIT_DONE();
275 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
276 xbt_assert0(communication_amount >= 0, "communication_amount must be greater than or equal to zero");
278 const SD_link_t *links;
280 double bandwidth, min_bandwidth;
284 if (communication_amount == 0.0)
287 links = SD_route_get_list(src, dst);
288 nb_links = SD_route_get_size(src, dst);
289 min_bandwidth = -1.0;
292 for (i = 0; i < nb_links; i++) {
293 latency += SD_link_get_current_latency(links[i]);
294 bandwidth = SD_link_get_current_bandwidth(links[i]);
295 if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
296 min_bandwidth = bandwidth;
299 return latency + (communication_amount / min_bandwidth);
303 * \brief Returns the access mode of this workstation.
305 * \param workstation a workstation
306 * \return the access mode for the tasks running on this workstation:
307 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
309 * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
311 e_SD_workstation_access_mode_t SD_workstation_get_access_mode(SD_workstation_t workstation) {
312 SD_CHECK_INIT_DONE();
313 xbt_assert0(workstation != NULL, "Invalid parameter");
314 return workstation->access_mode;
318 * \brief Sets the access mode for the tasks that will be executed on a workstation
320 * By default, a workstation resource is shared, i.e. several tasks
321 * can be executed at the same time on a workstation. The CPU power of
322 * the workstation is shared between the running tasks on the workstation.
323 * In sequential mode, only one task can use the workstation, and the other
324 * tasks wait in a FIFO.
326 * \param workstation a workstation
327 * \param access_mode the access mode you want to set to this workstation:
328 * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
330 * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
332 void SD_workstation_set_access_mode(SD_workstation_t workstation, e_SD_workstation_access_mode_t access_mode) {
333 SD_CHECK_INIT_DONE();
334 xbt_assert0(workstation != NULL, "Invalid parameter");
336 if (access_mode == workstation->access_mode) {
337 return; // nothing is changed
340 workstation->access_mode = access_mode;
342 if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
343 xbt_fifo_free(workstation->task_fifo);
344 workstation->task_fifo = NULL;
347 workstation->task_fifo = xbt_fifo_new();
351 /* Returns whether a task can start now on a workstation.
353 int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
354 SD_CHECK_INIT_DONE();
355 xbt_assert0(workstation != NULL && task != NULL, "Invalid parameter");
357 return !__SD_workstation_is_busy(workstation) &&
358 (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
362 /* Returns whether a workstation is busy. A workstation is busy is it is
363 * in sequential mode and a task is running on it or the fifo is not empty.
365 int __SD_workstation_is_busy(SD_workstation_t workstation) {
366 SD_CHECK_INIT_DONE();
367 xbt_assert0(workstation != NULL, "Invalid parameter");
369 return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
370 (workstation->current_task != NULL || xbt_fifo_size(workstation->task_fifo) > 0);
373 /* Destroys a workstation.
375 void __SD_workstation_destroy(void *workstation) {
376 SD_CHECK_INIT_DONE();
377 xbt_assert0(workstation != NULL, "Invalid parameter");
378 /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
380 SD_workstation_t w = (SD_workstation_t) workstation;
382 if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
383 xbt_fifo_free(w->task_fifo);