2 #include "simdag/simdag.h"
3 #include "xbt/sysdep.h"
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_task,sd,
7 "Logging specific to SimDag (task)");
9 static void __SD_task_remove_dependencies(SD_task_t task);
10 static void __SD_task_destroy_scheduling_data(SD_task_t task);
13 * \brief Creates a new task.
15 * \param name the name of the task (can be \c NULL)
16 * \param data the user data you want to associate with the task (can be \c NULL)
17 * \return the new task
18 * \see SD_task_destroy()
20 SD_task_t SD_task_create(const char *name, void *data, double amount) {
23 SD_task_t task = xbt_new0(s_SD_task_t, 1);
25 /* general information */
26 task->data = data; /* user data */
28 task->name = xbt_strdup(name);
32 task->state_set = sd_global->not_scheduled_task_set;
33 xbt_swag_insert(task,task->state_set);
35 task->amount = amount;
36 task->surf_action = NULL;
37 task->watch_points = 0;
38 task->state_changed = 0;
41 task->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
42 task->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
44 /* scheduling parameters */
45 task->workstation_nb = 0;
46 task->workstation_list = NULL;
47 task->computation_amount = NULL;
48 task->communication_amount = NULL;
55 * \brief Returns the user data of a task
58 * \return the user data associated with this task (can be \c NULL)
59 * \see SD_task_set_data()
61 void* SD_task_get_data(SD_task_t task) {
63 xbt_assert0(task != NULL, "Invalid parameter");
68 * \brief Sets the user data of a task
70 * The new data can be \c NULL. The old data should have been freed first
71 * if it was not \c NULL.
74 * \param data the new data you want to associate with this task
75 * \see SD_task_get_data()
77 void SD_task_set_data(SD_task_t task, void *data) {
79 xbt_assert0(task != NULL, "Invalid parameter");
84 * \brief Returns the state of a task
87 * \return the current \ref e_SD_task_state_t "state" of this task:
88 * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_READY, #SD_RUNNING, #SD_DONE or #SD_FAILED
89 * \see e_SD_task_state_t
91 e_SD_task_state_t SD_task_get_state(SD_task_t task) {
93 xbt_assert0(task != NULL, "Invalid parameter");
95 if (task->state_set == sd_global->scheduled_task_set)
97 if (task->state_set == sd_global->done_task_set)
99 if (task->state_set == sd_global->running_task_set)
101 if (task->state_set == sd_global->ready_task_set)
103 if (task->state_set == sd_global->not_scheduled_task_set)
104 return SD_NOT_SCHEDULED;
108 /* Changes the state of a task. Updates the swags and the flag sd_global->watch_point_reached.
110 void __SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state) {
111 xbt_swag_remove(task, task->state_set);
113 case SD_NOT_SCHEDULED:
114 task->state_set = sd_global->not_scheduled_task_set;
117 task->state_set = sd_global->scheduled_task_set;
120 task->state_set = sd_global->ready_task_set;
123 task->state_set = sd_global->running_task_set;
126 task->state_set = sd_global->done_task_set;
129 task->state_set = sd_global->failed_task_set;
132 xbt_assert0(0, "Invalid state");
134 xbt_swag_insert(task, task->state_set);
136 if (task->watch_points & new_state) {
137 INFO1("Watch point reached with task '%s'!", SD_task_get_name(task));
138 sd_global->watch_point_reached = 1;
139 SD_task_unwatch(task, new_state); /* remove the watch point */
144 * \brief Returns the name of a task
147 * \return the name of this task (can be \c NULL)
149 const char* SD_task_get_name(SD_task_t task) {
150 SD_CHECK_INIT_DONE();
151 xbt_assert0(task != NULL, "Invalid parameter");
156 * \brief Returns the total amount of a task
159 * \return the total amount of this task
160 * \see SD_task_get_remaining_amount()
162 double SD_task_get_amount(SD_task_t task) {
163 SD_CHECK_INIT_DONE();
164 xbt_assert0(task != NULL, "Invalid parameter");
169 * \brief Returns the remaining amount of a task
172 * \return the remaining amount of this task
173 * \see SD_task_get_amount()
175 double SD_task_get_remaining_amount(SD_task_t task) {
176 SD_CHECK_INIT_DONE();
177 xbt_assert0(task != NULL, "Invalid parameter");
179 if (task->surf_action)
180 return task->surf_action->remains;
185 /* temporary function for debbuging */
186 static void __SD_print_dependencies(SD_task_t task) {
187 INFO1("The following tasks must be executed before %s:", SD_task_get_name(task));
188 xbt_dynar_t dynar = task->tasks_before;
189 int length = xbt_dynar_length(dynar);
191 SD_dependency_t dependency;
192 for (i = 0; i < length; i++) {
193 xbt_dynar_get_cpy(dynar, i, &dependency);
194 INFO1(" %s", SD_task_get_name(dependency->src));
197 INFO1("The following tasks must be executed after %s:", SD_task_get_name(task));
199 dynar = task->tasks_after;
200 length = xbt_dynar_length(dynar);
201 for (i = 0; i < length; i++) {
202 xbt_dynar_get_cpy(dynar, i, &dependency);
203 INFO1(" %s", SD_task_get_name(dependency->dst));
205 INFO0("----------------------------");
208 /* Destroys a dependency between two tasks.
210 static void __SD_task_dependency_destroy(void *dependency) {
211 if (((SD_dependency_t) dependency)->name != NULL)
212 xbt_free(((SD_dependency_t) dependency)->name);
213 xbt_free(dependency);
217 * \brief Adds a dependency between two tasks
219 * \a dst will depend on \a src, ie \a dst will not start before \a src is finished.
220 * Their \ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_READY.
222 * \param name the name of the new dependency (can be \c NULL)
223 * \param data the user data you want to associate with this dependency (can be \c NULL)
224 * \param src the task which must be executed first
225 * \param dst the task you want to make depend on \a src
226 * \see SD_task_dependency_remove()
228 void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task_t dst) {
229 SD_CHECK_INIT_DONE();
230 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
231 xbt_assert1(src != dst, "Cannot add a dependency between task '%s' and itself", SD_task_get_name(src));
232 xbt_assert1(__SD_task_is_not_scheduled(src) || __SD_task_is_scheduled_or_ready(src),
233 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(src));
234 xbt_assert1(__SD_task_is_not_scheduled(dst) || __SD_task_is_scheduled_or_ready(dst),
235 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(dst));
237 xbt_dynar_t dynar = src->tasks_after;
238 int length = xbt_dynar_length(dynar);
241 SD_dependency_t dependency;
242 for (i = 0; i < length && !found; i++) {
243 xbt_dynar_get_cpy(dynar, i, &dependency);
244 found = (dependency->dst == dst);
246 xbt_assert2(!found, "A dependency already exists between task '%s' and task '%s'",
247 SD_task_get_name(src), SD_task_get_name(dst));
249 dependency = xbt_new0(s_SD_dependency_t, 1);
252 dependency->name = xbt_strdup(name);
253 dependency->data = data;
254 dependency->src = src;
255 dependency->dst = dst;
257 /* src must be executed before dst */
258 xbt_dynar_push(src->tasks_after, &dependency);
259 xbt_dynar_push(dst->tasks_before, &dependency);
261 /* if the task was ready, then dst->tasks_before is not empty anymore,
262 so we must go back to state SD_SCHEDULED */
263 if (__SD_task_is_ready(dst)) {
264 DEBUG1("SD_task_dependency_add: %s was ready and becomes scheduled!", SD_task_get_name(dst));
265 __SD_task_set_state(dst, SD_SCHEDULED);
268 /* __SD_print_dependencies(src);
269 __SD_print_dependencies(dst); */
273 * \brief Remove a dependency between two tasks
276 * \param dst a task depending on \a src
277 * \see SD_task_dependency_add()
279 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst) {
280 SD_CHECK_INIT_DONE();
281 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
283 /* remove the dependency from src->tasks_after */
284 xbt_dynar_t dynar = src->tasks_after;
285 int length = xbt_dynar_length(dynar);
288 SD_dependency_t dependency;
289 for (i = 0; i < length && !found; i++) {
290 xbt_dynar_get_cpy(dynar, i, &dependency);
291 if (dependency->dst == dst) {
292 xbt_dynar_remove_at(dynar, i, NULL);
296 xbt_assert4(found, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
297 SD_task_get_name(src), SD_task_get_name(dst), SD_task_get_name(dst), SD_task_get_name(src));
299 /* remove the dependency from dst->tasks_before */
300 dynar = dst->tasks_before;
301 length = xbt_dynar_length(dynar);
304 for (i = 0; i < length && !found; i++) {
305 xbt_dynar_get_cpy(dynar, i, &dependency);
306 if (dependency->src == src) {
307 xbt_dynar_remove_at(dynar, i, NULL);
308 __SD_task_dependency_destroy(dependency);
312 xbt_assert4(found, "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
313 SD_task_get_name(dst), SD_task_get_name(src), SD_task_get_name(src), SD_task_get_name(dst)); /* should never happen... */
315 /* if the task was scheduled and dst->tasks_before is empty now, we can make it ready */
316 if (xbt_dynar_length(dst->tasks_before) == 0 && __SD_task_is_scheduled(dst))
317 __SD_task_set_state(dst, SD_READY);
319 /* __SD_print_dependencies(src);
320 __SD_print_dependencies(dst);*/
324 * \brief Returns the user data associated with a dependency between two tasks
327 * \param dst a task depending on \a src
328 * \return the user data associated with this dependency (can be \c NULL)
329 * \see SD_task_dependency_add()
331 void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst) {
332 SD_CHECK_INIT_DONE();
333 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
335 xbt_dynar_t dynar = src->tasks_after;
336 int length = xbt_dynar_length(dynar);
339 SD_dependency_t dependency;
340 for (i = 0; i < length && !found; i++) {
341 xbt_dynar_get_cpy(dynar, i, &dependency);
342 found = (dependency->dst == dst);
344 xbt_assert2(found, "No dependency found between task '%s' and '%s'", SD_task_get_name(src), SD_task_get_name(dst));
345 return dependency->data;
348 /* temporary function for debugging */
349 static void __SD_print_watch_points(SD_task_t task) {
350 static const int state_masks[] = {SD_SCHEDULED, SD_RUNNING, SD_READY, SD_DONE, SD_FAILED};
351 static const char* state_names[] = {"scheduled", "running", "ready", "done", "failed"};
353 INFO2("Task '%s' watch points (%x): ", SD_task_get_name(task), task->watch_points);
356 for (i = 0; i < 5; i++) {
357 if (task->watch_points & state_masks[i])
358 INFO1("%s ", state_names[i]);
363 * \brief Adds a watch point to a task
365 * SD_simulate() will stop as soon as the \ref e_SD_task_state_t "state" of this task becomes the one given in argument. The
366 * watch point is then automatically removed.
369 * \param state the \ref e_SD_task_state_t "state" you want to watch (cannot be #SD_NOT_SCHEDULED)
370 * \see SD_task_unwatch()
372 void SD_task_watch(SD_task_t task, e_SD_task_state_t state) {
373 SD_CHECK_INIT_DONE();
374 xbt_assert0(task != NULL, "Invalid parameter");
375 xbt_assert0(state != SD_NOT_SCHEDULED, "Cannot add a watch point for state SD_NOT_SCHEDULED");
377 task->watch_points = task->watch_points | state;
378 /* __SD_print_watch_points(task);*/
382 * \brief Removes a watch point from a task
385 * \param state the \ref e_SD_task_state_t "state" you no longer want to watch
386 * \see SD_task_watch()
388 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state) {
389 SD_CHECK_INIT_DONE();
390 xbt_assert0(task != NULL, "Invalid parameter");
391 xbt_assert0(state != SD_NOT_SCHEDULED, "Cannot have a watch point for state SD_NOT_SCHEDULED");
393 task->watch_points = task->watch_points & ~state;
394 /* __SD_print_watch_points(task);*/
398 * \brief Returns an approximative estimation of the execution time of a task.
400 * The estimation is very approximative because the value returned is the time
401 * the task would take if it was executed now and if it was the only task.
403 * \param task the task to evaluate
404 * \param workstation_nb number of workstations on which the task would be executed
405 * \param workstation_list the workstations on which the task would be executed
406 * \param computation_amount computation amount for each workstation
407 * \param communication_amount communication amount between each pair of workstations
408 * \param rate task execution speed rate
411 double SD_task_get_execution_time(SD_task_t task,
413 const SD_workstation_t *workstation_list,
414 const double *computation_amount,
415 const double *communication_amount,
417 /* the task execution time is the maximum execution time of the parallel tasks */
418 double time, max_time = 0.0;
420 for (i = 0; i < workstation_nb; i++) {
421 time = SD_workstation_get_computation_time(workstation_list[i], computation_amount[i]);
423 for (j = 0; j < workstation_nb; j++) {
424 time += SD_route_get_communication_time(workstation_list[i], workstation_list[j],
425 communication_amount[i * workstation_nb + j]);
428 if (time > max_time) {
432 return max_time * SD_task_get_amount(task);
436 * \brief Schedules a task
438 * The task state must be #SD_NOT_SCHEDULED.
439 * Once scheduled, a task will be executed as soon as possible in SD_simulate(),
440 * i.e. when its dependencies are satisfied.
442 * \param task the task you want to schedule
443 * \param workstation_nb number of workstations on which the task will be executed
444 * \param workstation_list the workstations on which the task will be executed
445 * \param computation_amount computation amount for each workstation
446 * \param communication_amount communication amount between each pair of workstations
447 * \param rate task execution speed rate
448 * \see SD_task_unschedule()
450 void SD_task_schedule(SD_task_t task, int workstation_nb,
451 const SD_workstation_t *workstation_list, const double *computation_amount,
452 const double *communication_amount, double rate) {
453 SD_CHECK_INIT_DONE();
454 xbt_assert0(task, "Invalid parameter");
455 xbt_assert1(__SD_task_is_not_scheduled(task), "Task '%s' has already been scheduled.", SD_task_get_name(task));
456 xbt_assert0(workstation_nb > 0, "workstation_nb must be positive");
458 task->workstation_nb = workstation_nb;
461 task->computation_amount = xbt_new0(double, workstation_nb);
462 memcpy(task->computation_amount, computation_amount, sizeof(double) * workstation_nb);
464 int communication_nb = workstation_nb * workstation_nb;
465 task->communication_amount = xbt_new0(double, communication_nb);
466 memcpy(task->communication_amount, communication_amount, sizeof(double) * communication_nb);
468 /* we have to create a Surf workstation array instead of the SimDag workstation array */
469 task->workstation_list = xbt_new0(void*, workstation_nb);
471 for (i = 0; i < workstation_nb; i++) {
472 task->workstation_list[i] = workstation_list[i]->surf_workstation;
475 /* update the task state */
476 if (xbt_dynar_length(task->tasks_before) == 0)
477 __SD_task_set_state(task, SD_READY);
479 __SD_task_set_state(task, SD_SCHEDULED);
483 * \brief Unschedules a task
485 * The task state must be #SD_SCHEDULED, #SD_READY, #SD_RUNNING or #SD_FAILED.
486 * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
487 * Call SD_task_schedule() to schedule it again.
489 * \param task the task you want to unschedule
490 * \see SD_task_schedule()
492 void SD_task_unschedule(SD_task_t task) {
493 SD_CHECK_INIT_DONE();
494 xbt_assert0(task != NULL, "Invalid parameter");
495 xbt_assert1(task->state_set == sd_global->scheduled_task_set ||
496 task->state_set == sd_global->ready_task_set ||
497 task->state_set == sd_global->running_task_set ||
498 task->state_set == sd_global->failed_task_set,
499 "Task %s: the state must be SD_SCHEDULED, SD_READY, SD_RUNNING or SD_FAILED",
500 SD_task_get_name(task));
502 if (__SD_task_is_scheduled_or_ready(task)) /* if the task is scheduled or ready */
503 __SD_task_destroy_scheduling_data(task);
505 if (__SD_task_is_running(task)) /* the task should become SD_FAILED */
506 surf_workstation_resource->common_public->action_cancel(task->surf_action);
508 __SD_task_set_state(task, SD_NOT_SCHEDULED);
511 /* Destroys the data memorised by SD_task_schedule. Task state must be SD_SCHEDULED or SD_READY.
513 static void __SD_task_destroy_scheduling_data(SD_task_t task) {
514 SD_CHECK_INIT_DONE();
515 xbt_assert1(__SD_task_is_scheduled_or_ready(task),
516 "Task '%s' must be SD_SCHEDULED or SD_READY", SD_task_get_name(task));
517 xbt_free(task->workstation_list);
518 xbt_free(task->computation_amount);
519 xbt_free(task->communication_amount);
522 /* Runs a task. This function is called by SD_simulate() when a scheduled task can start
523 * (ie when its dependencies are satisfied).
525 surf_action_t __SD_task_run(SD_task_t task) {
526 SD_CHECK_INIT_DONE();
527 xbt_assert0(task != NULL, "Invalid parameter");
528 xbt_assert2(__SD_task_is_ready(task), "Task '%s' is not ready! Task state: %d",
529 SD_task_get_name(task), SD_task_get_state(task));
531 surf_action_t surf_action = surf_workstation_resource->extension_public->
532 execute_parallel_task(task->workstation_nb,
533 task->workstation_list,
534 task->computation_amount,
535 task->communication_amount,
539 DEBUG1("surf_action = %p", surf_action);
541 __SD_task_destroy_scheduling_data(task); /* now the scheduling data are not useful anymore */
542 __SD_task_set_state(task, SD_RUNNING);
546 /* Remove all dependencies associated with a task. This function is called when the task is destroyed.
548 static void __SD_task_remove_dependencies(SD_task_t task) {
549 /* we must destroy the dependencies carefuly (with SD_dependency_remove)
550 because each one is stored twice */
551 SD_dependency_t dependency;
552 while (xbt_dynar_length(task->tasks_before) > 0) {
553 xbt_dynar_get_cpy(task->tasks_before, 0, &dependency);
554 SD_task_dependency_remove(dependency->src, dependency->dst);
557 while (xbt_dynar_length(task->tasks_after) > 0) {
558 xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
559 SD_task_dependency_remove(dependency->src, dependency->dst);
564 * \brief Destroys a task.
566 * The user data (if any) should have been destroyed first.
568 * \param task the task you want to destroy
569 * \see SD_task_create()
571 void SD_task_destroy(SD_task_t task) {
572 SD_CHECK_INIT_DONE();
573 xbt_assert0(task != NULL, "Invalid parameter");
575 DEBUG1("Destroying task %s...", SD_task_get_name(task));
577 __SD_task_remove_dependencies(task);
579 /* if the task was scheduled or ready we have to free the scheduling parameters */
580 if (__SD_task_is_scheduled_or_ready(task))
581 __SD_task_destroy_scheduling_data(task);
583 if (task->name != NULL)
584 xbt_free(task->name);
586 xbt_dynar_free(&task->tasks_before);
587 xbt_dynar_free(&task->tasks_after);
590 DEBUG0("Task destroyed.");