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 * \param amount amount of the task
18 * \return the new task
19 * \see SD_task_destroy()
21 SD_task_t SD_task_create(const char *name, void *data, double amount) {
24 SD_task_t task = xbt_new0(s_SD_task_t, 1);
26 /* general information */
27 task->data = data; /* user data */
29 task->name = xbt_strdup(name);
33 task->state_set = sd_global->not_scheduled_task_set;
34 xbt_swag_insert(task,task->state_set);
36 task->amount = amount;
37 task->remains = amount;
38 task->start_time = -1.0;
39 task->finish_time = -1.0;
40 task->surf_action = NULL;
41 task->watch_points = 0;
42 task->state_changed = 0;
45 task->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
46 task->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
48 /* scheduling parameters */
49 task->workstation_nb = 0;
50 task->workstation_list = NULL;
51 task->computation_amount = NULL;
52 task->communication_amount = NULL;
59 * \brief Returns the user data of a task
62 * \return the user data associated with this task (can be \c NULL)
63 * \see SD_task_set_data()
65 void* SD_task_get_data(SD_task_t task) {
67 xbt_assert0(task != NULL, "Invalid parameter");
72 * \brief Sets the user data of a task
74 * The new data can be \c NULL. The old data should have been freed first
75 * if it was not \c NULL.
78 * \param data the new data you want to associate with this task
79 * \see SD_task_get_data()
81 void SD_task_set_data(SD_task_t task, void *data) {
83 xbt_assert0(task != NULL, "Invalid parameter");
88 * \brief Returns the state of a task
91 * \return the current \ref e_SD_task_state_t "state" of this task:
92 * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_READY, #SD_RUNNING, #SD_DONE or #SD_FAILED
93 * \see e_SD_task_state_t
95 e_SD_task_state_t SD_task_get_state(SD_task_t task) {
97 xbt_assert0(task != NULL, "Invalid parameter");
99 if (task->state_set == sd_global->scheduled_task_set)
101 if (task->state_set == sd_global->done_task_set)
103 if (task->state_set == sd_global->running_task_set)
105 if (task->state_set == sd_global->ready_task_set)
107 if (task->state_set == sd_global->not_scheduled_task_set)
108 return SD_NOT_SCHEDULED;
112 /* Changes the state of a task. Updates the swags and the flag sd_global->watch_point_reached.
114 void __SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state) {
115 xbt_swag_remove(task, task->state_set);
117 case SD_NOT_SCHEDULED:
118 task->state_set = sd_global->not_scheduled_task_set;
121 task->state_set = sd_global->scheduled_task_set;
124 task->state_set = sd_global->ready_task_set;
127 task->state_set = sd_global->running_task_set;
128 task->start_time = surf_workstation_resource->common_public->
129 action_get_start_time(task->surf_action);
132 task->state_set = sd_global->done_task_set;
133 task->finish_time = surf_workstation_resource->common_public->
134 action_get_finish_time(task->surf_action);
138 task->state_set = sd_global->failed_task_set;
141 xbt_assert0(0, "Invalid state");
143 xbt_swag_insert(task, task->state_set);
145 if (task->watch_points & new_state) {
146 INFO1("Watch point reached with task '%s'!", SD_task_get_name(task));
147 sd_global->watch_point_reached = 1;
148 SD_task_unwatch(task, new_state); /* remove the watch point */
153 * \brief Returns the name of a task
156 * \return the name of this task (can be \c NULL)
158 const char* SD_task_get_name(SD_task_t task) {
159 SD_CHECK_INIT_DONE();
160 xbt_assert0(task != NULL, "Invalid parameter");
165 * \brief Returns the total amount of a task
168 * \return the total amount of this task
169 * \see SD_task_get_remaining_amount()
171 double SD_task_get_amount(SD_task_t task) {
172 SD_CHECK_INIT_DONE();
173 xbt_assert0(task != NULL, "Invalid parameter");
178 * \brief Returns the remaining amount of a task
181 * \return the remaining amount of this task
182 * \see SD_task_get_amount()
184 double SD_task_get_remaining_amount(SD_task_t task) {
185 SD_CHECK_INIT_DONE();
186 xbt_assert0(task != NULL, "Invalid parameter");
188 if (task->surf_action)
189 return task->surf_action->remains;
191 return task->remains;
194 /* temporary function for debbuging */
195 static void __SD_print_dependencies(SD_task_t task) {
196 INFO1("The following tasks must be executed before %s:", SD_task_get_name(task));
197 xbt_dynar_t dynar = task->tasks_before;
198 int length = xbt_dynar_length(dynar);
200 SD_dependency_t dependency;
201 for (i = 0; i < length; i++) {
202 xbt_dynar_get_cpy(dynar, i, &dependency);
203 INFO1(" %s", SD_task_get_name(dependency->src));
206 INFO1("The following tasks must be executed after %s:", SD_task_get_name(task));
208 dynar = task->tasks_after;
209 length = xbt_dynar_length(dynar);
210 for (i = 0; i < length; i++) {
211 xbt_dynar_get_cpy(dynar, i, &dependency);
212 INFO1(" %s", SD_task_get_name(dependency->dst));
214 INFO0("----------------------------");
217 /* Destroys a dependency between two tasks.
219 static void __SD_task_dependency_destroy(void *dependency) {
220 if (((SD_dependency_t) dependency)->name != NULL)
221 xbt_free(((SD_dependency_t) dependency)->name);
222 xbt_free(dependency);
226 * \brief Adds a dependency between two tasks
228 * \a dst will depend on \a src, ie \a dst will not start before \a src is finished.
229 * Their \ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_READY.
231 * \param name the name of the new dependency (can be \c NULL)
232 * \param data the user data you want to associate with this dependency (can be \c NULL)
233 * \param src the task which must be executed first
234 * \param dst the task you want to make depend on \a src
235 * \see SD_task_dependency_remove()
237 void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task_t dst) {
238 SD_CHECK_INIT_DONE();
239 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
241 xbt_dynar_t dynar = src->tasks_after;
242 int length = xbt_dynar_length(dynar);
245 SD_dependency_t dependency;
248 THROW1(arg_error, 0, "Cannot add a dependency between task '%s' and itself",
249 SD_task_get_name(src));
251 if (!__SD_task_is_not_scheduled(src) && !__SD_task_is_scheduled_or_ready(src))
252 THROW1(arg_error, 0, "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(src));
254 if (!__SD_task_is_not_scheduled(dst) && !__SD_task_is_scheduled_or_ready(dst))
255 THROW1(arg_error, 0, "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(dst));
257 DEBUG2("SD_task_dependency_add: src = %s, dst = %s", SD_task_get_name(src), SD_task_get_name(dst));
258 for (i = 0; i < length && !found; i++) {
259 xbt_dynar_get_cpy(dynar, i, &dependency);
260 found = (dependency->dst == dst);
261 DEBUG2("Dependency %d: dependency->dst = %s", i, SD_task_get_name(dependency->dst));
265 THROW2(arg_error, 0, "A dependency already exists between task '%s' and task '%s'",
266 SD_task_get_name(src), SD_task_get_name(dst));
268 dependency = xbt_new0(s_SD_dependency_t, 1);
271 dependency->name = xbt_strdup(name);
273 dependency->name = NULL;
275 dependency->data = data;
276 dependency->src = src;
277 dependency->dst = dst;
279 /* src must be executed before dst */
280 xbt_dynar_push(src->tasks_after, &dependency);
281 xbt_dynar_push(dst->tasks_before, &dependency);
283 /* if the task was ready, then dst->tasks_before is not empty anymore,
284 so we must go back to state SD_SCHEDULED */
285 if (__SD_task_is_ready(dst)) {
286 DEBUG1("SD_task_dependency_add: %s was ready and becomes scheduled!", SD_task_get_name(dst));
287 __SD_task_set_state(dst, SD_SCHEDULED);
290 /* __SD_print_dependencies(src);
291 __SD_print_dependencies(dst); */
295 * \brief Remove a dependency between two tasks
298 * \param dst a task depending on \a src
299 * \see SD_task_dependency_add()
301 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst) {
302 SD_CHECK_INIT_DONE();
303 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
305 /* remove the dependency from src->tasks_after */
306 xbt_dynar_t dynar = src->tasks_after;
307 int length = xbt_dynar_length(dynar);
310 SD_dependency_t dependency;
311 for (i = 0; i < length && !found; i++) {
312 xbt_dynar_get_cpy(dynar, i, &dependency);
313 if (dependency->dst == dst) {
314 xbt_dynar_remove_at(dynar, i, NULL);
320 "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
321 SD_task_get_name(src), SD_task_get_name(dst), SD_task_get_name(dst), SD_task_get_name(src));
323 /* remove the dependency from dst->tasks_before */
324 dynar = dst->tasks_before;
325 length = xbt_dynar_length(dynar);
328 for (i = 0; i < length && !found; i++) {
329 xbt_dynar_get_cpy(dynar, i, &dependency);
330 if (dependency->src == src) {
331 xbt_dynar_remove_at(dynar, i, NULL);
332 __SD_task_dependency_destroy(dependency);
336 /* should never happen... */
337 xbt_assert4(found, "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
338 SD_task_get_name(dst), SD_task_get_name(src), SD_task_get_name(src), SD_task_get_name(dst));
340 /* if the task was scheduled and dst->tasks_before is empty now, we can make it ready */
341 if (xbt_dynar_length(dst->tasks_before) == 0 && __SD_task_is_scheduled(dst))
342 __SD_task_set_state(dst, SD_READY);
344 /* __SD_print_dependencies(src);
345 __SD_print_dependencies(dst);*/
349 * \brief Returns the user data associated with a dependency between two tasks
352 * \param dst a task depending on \a src
353 * \return the user data associated with this dependency (can be \c NULL)
354 * \see SD_task_dependency_add()
356 void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst) {
357 SD_CHECK_INIT_DONE();
358 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
360 xbt_dynar_t dynar = src->tasks_after;
361 int length = xbt_dynar_length(dynar);
364 SD_dependency_t dependency;
365 for (i = 0; i < length && !found; i++) {
366 xbt_dynar_get_cpy(dynar, i, &dependency);
367 found = (dependency->dst == dst);
370 THROW2(arg_error, 0, "No dependency found between task '%s' and '%s'", SD_task_get_name(src), SD_task_get_name(dst));
371 return dependency->data;
374 /* temporary function for debugging */
375 static void __SD_print_watch_points(SD_task_t task) {
376 static const int state_masks[] = {SD_SCHEDULED, SD_RUNNING, SD_READY, SD_DONE, SD_FAILED};
377 static const char* state_names[] = {"scheduled", "running", "ready", "done", "failed"};
379 INFO2("Task '%s' watch points (%x): ", SD_task_get_name(task), task->watch_points);
382 for (i = 0; i < 5; i++) {
383 if (task->watch_points & state_masks[i])
384 INFO1("%s ", state_names[i]);
389 * \brief Adds a watch point to a task
391 * SD_simulate() will stop as soon as the \ref e_SD_task_state_t "state" of this
392 * task becomes the one given in argument. The
393 * watch point is then automatically removed.
396 * \param state the \ref e_SD_task_state_t "state" you want to watch
397 * (cannot be #SD_NOT_SCHEDULED)
398 * \see SD_task_unwatch()
400 void SD_task_watch(SD_task_t task, e_SD_task_state_t state) {
401 SD_CHECK_INIT_DONE();
402 xbt_assert0(task != NULL, "Invalid parameter");
404 if (state & SD_NOT_SCHEDULED)
405 THROW0(arg_error, 0, "Cannot add a watch point for state SD_NOT_SCHEDULED");
407 task->watch_points = task->watch_points | state;
408 /* __SD_print_watch_points(task);*/
412 * \brief Removes a watch point from a task
415 * \param state the \ref e_SD_task_state_t "state" you no longer want to watch
416 * \see SD_task_watch()
418 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state) {
419 SD_CHECK_INIT_DONE();
420 xbt_assert0(task != NULL, "Invalid parameter");
421 xbt_assert0(state != SD_NOT_SCHEDULED,
422 "SimDag error: Cannot have a watch point for state SD_NOT_SCHEDULED");
424 task->watch_points = task->watch_points & ~state;
425 /* __SD_print_watch_points(task);*/
429 * \brief Returns an approximative estimation of the execution time of a task.
431 * The estimation is very approximative because the value returned is the time
432 * the task would take if it was executed now and if it was the only task.
434 * \param task the task to evaluate
435 * \param workstation_nb number of workstations on which the task would be executed
436 * \param workstation_list the workstations on which the task would be executed
437 * \param computation_amount computation amount for each workstation
438 * \param communication_amount communication amount between each pair of workstations
439 * \param rate task execution speed rate
442 double SD_task_get_execution_time(SD_task_t task,
444 const SD_workstation_t *workstation_list,
445 const double *computation_amount,
446 const double *communication_amount,
448 SD_CHECK_INIT_DONE();
449 xbt_assert0(task != NULL && workstation_nb > 0 && workstation_list != NULL &&
450 computation_amount != NULL && communication_amount != NULL,
451 "Invalid parameter");
453 /* the task execution time is the maximum execution time of the parallel tasks */
454 double time, max_time = 0.0;
456 for (i = 0; i < workstation_nb; i++) {
457 time = SD_workstation_get_computation_time(workstation_list[i], computation_amount[i]);
459 for (j = 0; j < workstation_nb; j++) {
460 time += SD_route_get_communication_time(workstation_list[i], workstation_list[j],
461 communication_amount[i * workstation_nb + j]);
464 if (time > max_time) {
468 return max_time * SD_task_get_amount(task);
472 * \brief Schedules a task
474 * The task state must be #SD_NOT_SCHEDULED.
475 * Once scheduled, a task will be executed as soon as possible in SD_simulate(),
476 * i.e. when its dependencies are satisfied.
478 * \param task the task you want to schedule
479 * \param workstation_nb number of workstations on which the task will be executed
480 * \param workstation_list the workstations on which the task will be executed
481 * \param computation_amount computation amount for each workstation
482 * \param communication_amount communication amount between each pair of workstations
483 * \param rate task execution speed rate
484 * \see SD_task_unschedule()
486 void SD_task_schedule(SD_task_t task, int workstation_nb,
487 const SD_workstation_t *workstation_list, const double *computation_amount,
488 const double *communication_amount, double rate) {
489 SD_CHECK_INIT_DONE();
490 xbt_assert0(task != NULL, "Invalid parameter");
491 xbt_assert0(workstation_nb > 0, "workstation_nb must be positive");
493 if (!__SD_task_is_not_scheduled(task))
494 THROW1(arg_error, 0, "Task '%s' has already been scheduled", SD_task_get_name(task));
496 task->workstation_nb = workstation_nb;
499 task->computation_amount = xbt_new0(double, workstation_nb);
500 memcpy(task->computation_amount, computation_amount, sizeof(double) * workstation_nb);
502 int communication_nb = workstation_nb * workstation_nb;
503 task->communication_amount = xbt_new0(double, communication_nb);
504 memcpy(task->communication_amount, communication_amount, sizeof(double) * communication_nb);
506 /* we have to create a Surf workstation array instead of the SimDag workstation array */
507 task->workstation_list = xbt_new0(void*, workstation_nb);
509 for (i = 0; i < workstation_nb; i++) {
510 task->workstation_list[i] = workstation_list[i]->surf_workstation;
513 /* update the task state */
514 if (xbt_dynar_length(task->tasks_before) == 0)
515 __SD_task_set_state(task, SD_READY);
517 __SD_task_set_state(task, SD_SCHEDULED);
521 * \brief Unschedules a task
523 * The task state must be #SD_SCHEDULED, #SD_READY, #SD_RUNNING or #SD_FAILED.
524 * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
525 * Call SD_task_schedule() to schedule it again.
527 * \param task the task you want to unschedule
528 * \see SD_task_schedule()
530 void SD_task_unschedule(SD_task_t task) {
531 SD_CHECK_INIT_DONE();
532 xbt_assert0(task != NULL, "Invalid parameter");
534 if (task->state_set != sd_global->scheduled_task_set &&
535 task->state_set != sd_global->ready_task_set &&
536 task->state_set != sd_global->running_task_set &&
537 task->state_set != sd_global->failed_task_set)
538 THROW1(arg_error, 0, "Task %s: the state must be SD_SCHEDULED, SD_READY, SD_RUNNING or SD_FAILED",
539 SD_task_get_name(task));
541 if (__SD_task_is_scheduled_or_ready(task)) /* if the task is scheduled or ready */
542 __SD_task_destroy_scheduling_data(task);
544 if (__SD_task_is_running(task)) /* the task should become SD_FAILED */
545 surf_workstation_resource->common_public->action_cancel(task->surf_action);
547 __SD_task_set_state(task, SD_NOT_SCHEDULED);
548 task->remains = task->amount;
549 task->start_time = -1.0;
552 /* Destroys the data memorised by SD_task_schedule. Task state must be SD_SCHEDULED or SD_READY.
554 static void __SD_task_destroy_scheduling_data(SD_task_t task) {
555 SD_CHECK_INIT_DONE();
556 if (!__SD_task_is_scheduled_or_ready(task))
557 THROW1(arg_error, 0, "Task '%s' must be SD_SCHEDULED or SD_READY", SD_task_get_name(task));
559 xbt_free(task->workstation_list);
560 xbt_free(task->computation_amount);
561 xbt_free(task->communication_amount);
564 /* Runs a task. This function is called by SD_simulate() when a scheduled task can start
565 * (ie when its dependencies are satisfied).
567 surf_action_t __SD_task_run(SD_task_t task) {
568 SD_CHECK_INIT_DONE();
569 xbt_assert0(task != NULL, "Invalid parameter");
571 if (!__SD_task_is_ready(task))
572 THROW2(arg_error, 0, "Task '%s' is not ready! Task state: %d",
573 SD_task_get_name(task), SD_task_get_state(task));
575 task->surf_action = surf_workstation_resource->extension_public->
576 execute_parallel_task(task->workstation_nb,
577 task->workstation_list,
578 task->computation_amount,
579 task->communication_amount,
583 DEBUG1("surf_action = %p", task->surf_action);
585 __SD_task_destroy_scheduling_data(task); /* now the scheduling data are not useful anymore */
586 __SD_task_set_state(task, SD_RUNNING);
588 return task->surf_action;
590 /* Remove all dependencies associated with a task. This function is called when the task is destroyed.
592 static void __SD_task_remove_dependencies(SD_task_t task) {
593 /* we must destroy the dependencies carefuly (with SD_dependency_remove)
594 because each one is stored twice */
595 SD_dependency_t dependency;
596 while (xbt_dynar_length(task->tasks_before) > 0) {
597 xbt_dynar_get_cpy(task->tasks_before, 0, &dependency);
598 SD_task_dependency_remove(dependency->src, dependency->dst);
601 while (xbt_dynar_length(task->tasks_after) > 0) {
602 xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
603 SD_task_dependency_remove(dependency->src, dependency->dst);
608 * \brief Returns the start time of a task
610 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
612 * \param task: a task
613 * \return the start time of this task
615 double SD_task_get_start_time(SD_task_t task) {
616 SD_CHECK_INIT_DONE();
617 xbt_assert0(task != NULL, "Invalid parameter");
618 if(task->surf_action)
619 return surf_workstation_resource->common_public->action_get_start_time(task->surf_action);
621 return task->start_time;
625 * \brief Returns the finish time of a task
627 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
628 * If the state is not completed yet, the returned value is an
629 * estimation of the task finish time. This value can fluctuate
630 * until the task is completed.
632 * \param task: a task
633 * \return the start time of this task
635 double SD_task_get_finish_time(SD_task_t task) {
636 SD_CHECK_INIT_DONE();
637 xbt_assert0(task != NULL, "Invalid parameter");
639 if(task->surf_action) /* should never happen as actions are destroyed right after their completion */
640 return surf_workstation_resource->common_public->action_get_finish_time(task->surf_action);
642 return task->finish_time;
646 * \brief Destroys a task.
648 * The user data (if any) should have been destroyed first.
650 * \param task the task you want to destroy
651 * \see SD_task_create()
653 void SD_task_destroy(SD_task_t task) {
654 SD_CHECK_INIT_DONE();
655 xbt_assert0(task != NULL, "Invalid parameter");
657 DEBUG1("Destroying task %s...", SD_task_get_name(task));
659 __SD_task_remove_dependencies(task);
661 /* if the task was scheduled or ready we have to free the scheduling parameters */
662 if (__SD_task_is_scheduled_or_ready(task))
663 __SD_task_destroy_scheduling_data(task);
665 if (task->name != NULL)
666 xbt_free(task->name);
668 if (task->surf_action != NULL)
669 surf_workstation_resource->common_public->action_free(task->surf_action);
671 xbt_dynar_free(&task->tasks_before);
672 xbt_dynar_free(&task->tasks_after);
675 DEBUG0("Task destroyed.");