1 /* Copyright (c) 2007-2009 Da SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "simdag/simdag.h"
8 #include "xbt/sysdep.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_task, sd,
12 "Logging specific to SimDag (task)");
14 static void __SD_task_remove_dependencies(SD_task_t task);
15 static void __SD_task_destroy_scheduling_data(SD_task_t task);
18 * \brief Creates a new task.
20 * \param name the name of the task (can be \c NULL)
21 * \param data the user data you want to associate with the task (can be \c NULL)
22 * \param amount amount of the task
23 * \return the new task
24 * \see SD_task_destroy()
26 SD_task_t SD_task_create(const char *name, void *data, double amount)
32 task = xbt_new(s_SD_task_t, 1);
34 /* general information */
35 task->data = data; /* user data */
36 task->name = xbt_strdup(name);
37 task->kind = SD_TASK_NOT_TYPED;
38 task->state_hookup.prev = NULL;
39 task->state_hookup.next = NULL;
40 task->state_set = sd_global->not_scheduled_task_set;
41 task->state = SD_NOT_SCHEDULED;
42 xbt_swag_insert(task, task->state_set);
44 task->amount = amount;
45 task->remains = amount;
46 task->start_time = -1.0;
47 task->finish_time = -1.0;
48 task->surf_action = NULL;
49 task->watch_points = 0;
52 task->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
53 task->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
55 /* scheduling parameters */
56 task->workstation_nb = 0;
57 task->workstation_list = NULL;
58 task->computation_amount = NULL;
59 task->communication_amount = NULL;
62 sd_global->task_number++;
68 * \brief Returns the user data of a task
71 * \return the user data associated with this task (can be \c NULL)
72 * \see SD_task_set_data()
74 void *SD_task_get_data(SD_task_t task)
77 xbt_assert0(task != NULL, "Invalid parameter");
82 * \brief Sets the user data of a task
84 * The new data can be \c NULL. The old data should have been freed first
85 * if it was not \c NULL.
88 * \param data the new data you want to associate with this task
89 * \see SD_task_get_data()
91 void SD_task_set_data(SD_task_t task, void *data)
94 xbt_assert0(task != NULL, "Invalid parameter");
99 * \brief Returns the state of a task
102 * \return the current \ref e_SD_task_state_t "state" of this task:
103 * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_READY, #SD_RUNNING, #SD_DONE or #SD_FAILED
104 * \see e_SD_task_state_t
106 e_SD_task_state_t SD_task_get_state(SD_task_t task)
108 SD_CHECK_INIT_DONE();
109 xbt_assert0(task != NULL, "Invalid parameter");
113 /* Changes the state of a task. Updates the swags and the flag sd_global->watch_point_reached.
115 void __SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state)
117 xbt_swag_remove(task, task->state_set);
119 case SD_NOT_SCHEDULED:
120 task->state_set = sd_global->not_scheduled_task_set;
123 task->state_set = sd_global->scheduled_task_set;
126 task->state_set = sd_global->ready_task_set;
129 task->state_set = sd_global->in_fifo_task_set;
132 task->state_set = sd_global->running_task_set;
134 surf_workstation_model->action_get_start_time(task->surf_action);
137 task->state_set = sd_global->done_task_set;
139 surf_workstation_model->action_get_finish_time(task->surf_action);
143 task->state_set = sd_global->failed_task_set;
146 xbt_assert0(0, "Invalid state");
148 xbt_swag_insert(task, task->state_set);
149 task->state = new_state;
151 if (task->watch_points & new_state) {
152 INFO1("Watch point reached with task '%s'!", SD_task_get_name(task));
153 sd_global->watch_point_reached = 1;
154 SD_task_unwatch(task, new_state); /* remove the watch point */
159 * \brief Returns the name of a task
162 * \return the name of this task (can be \c NULL)
164 const char *SD_task_get_name(SD_task_t task)
166 SD_CHECK_INIT_DONE();
167 xbt_assert0(task != NULL, "Invalid parameter");
171 /** @brief Returns the dynar of the parents of a task
174 * \return a newly allocated dynar comprising the parents of this task
177 xbt_dynar_t SD_task_get_parents(SD_task_t task)
182 SD_CHECK_INIT_DONE();
183 xbt_assert0(task != NULL, "Invalid parameter");
185 parents = xbt_dynar_new(sizeof(SD_task_t), NULL);
186 xbt_dynar_foreach(task->tasks_before, i, dep){
187 xbt_dynar_push(parents, &(dep->src));
192 /** @brief Returns the dynar of the parents of a task
195 * \return a newly allocated dynar comprising the parents of this task
197 xbt_dynar_t SD_task_get_children(SD_task_t task)
200 xbt_dynar_t children;
202 SD_CHECK_INIT_DONE();
203 xbt_assert0(task != NULL, "Invalid parameter");
205 children = xbt_dynar_new(sizeof(SD_task_t), NULL);
206 xbt_dynar_foreach(task->tasks_after, i, dep){
207 xbt_dynar_push(children, &(dep->dst));
213 * \brief Returns the amount of workstations involved in a task
215 * Only call this on already scheduled tasks!
218 int SD_task_get_workstation_count(SD_task_t task)
220 SD_CHECK_INIT_DONE();
221 xbt_assert0(task != NULL, "Invalid parameter");
222 // xbt_assert1( task->state_set != sd_global->scheduled_task_set,
223 // "Unscheduled task %s", task->name);
224 return task->workstation_nb;
228 * \brief Returns the list of workstations involved in a task
230 * Only call this on already scheduled tasks!
233 SD_workstation_t* SD_task_get_workstation_list(SD_task_t task)
235 SD_CHECK_INIT_DONE();
236 xbt_assert0(task != NULL, "Invalid parameter");
237 //xbt_assert1( task->state_set != sd_global->scheduled_task_set,
238 // "Unscheduled task %s", task->name);
239 return task->workstation_list;
243 * \brief Returns the total amount of work contained in a task
246 * \return the total amount of work (computation or data transfer) for this task
247 * \see SD_task_get_remaining_amount()
249 double SD_task_get_amount(SD_task_t task)
251 SD_CHECK_INIT_DONE();
252 xbt_assert0(task != NULL, "Invalid parameter");
257 * \brief Returns the remaining amount work to do till the completion of a task
260 * \return the remaining amount of work (computation or data transfer) of this task
261 * \see SD_task_get_amount()
263 double SD_task_get_remaining_amount(SD_task_t task)
265 SD_CHECK_INIT_DONE();
266 xbt_assert0(task != NULL, "Invalid parameter");
268 if (task->surf_action)
269 return surf_workstation_model->get_remains(task->surf_action);
271 return task->remains;
274 int SD_task_get_kind(SD_task_t task) {
278 /** @brief Displays debugging informations about a task */
279 void SD_task_dump(SD_task_t task)
281 unsigned int counter;
282 SD_dependency_t dependency;
285 INFO1("Displaying task %s",SD_task_get_name(task));
286 statename=bprintf("%s %s %s %s %s %s %s",
287 (task->state&SD_NOT_SCHEDULED?"not scheduled":""),
288 (task->state&SD_SCHEDULED?"scheduled":""),
289 (task->state&SD_READY?"ready":"not ready"),
290 (task->state&SD_IN_FIFO?"in fifo":""),
291 (task->state&SD_RUNNING?"running":""),
292 (task->state&SD_DONE?"done":""),
293 (task->state&SD_FAILED?"failed":""));
294 INFO1(" - state: %s",statename);
299 case SD_TASK_COMM_E2E:
300 INFO0(" - kind: end-to-end communication");
302 case SD_TASK_COMP_SEQ:
303 INFO0(" - kind: sequential computation");
306 INFO1(" - (unknown kind %d)",task->kind);
309 INFO1(" - amount: %.0f",SD_task_get_amount(task));
310 if (xbt_dynar_length(task->tasks_before)) {
311 INFO0(" - pre-dependencies:");
312 xbt_dynar_foreach(task->tasks_before,counter,dependency) {
313 INFO1(" %s",SD_task_get_name(dependency->src));
316 if (xbt_dynar_length(task->tasks_after)) {
317 INFO0(" - post-dependencies:");
318 xbt_dynar_foreach(task->tasks_after,counter,dependency) {
319 INFO1(" %s",SD_task_get_name(dependency->dst));
323 /** @brief Dumps the task in dotty formalism into the FILE* passed as second argument */
324 void SD_task_dotty(SD_task_t task,void* out) {
325 unsigned int counter;
326 SD_dependency_t dependency;
327 fprintf(out, " T%p [label=\"%.20s\"",task, task->name);
329 case SD_TASK_COMM_E2E:
330 fprintf(out,", shape=box");
332 case SD_TASK_COMP_SEQ:
333 fprintf(out,", shape=circle");
336 xbt_die("Unknown task type!");
339 xbt_dynar_foreach(task->tasks_before,counter,dependency) {
340 fprintf(out," T%p -> T%p;\n",dependency->src, dependency->dst);
344 /* Destroys a dependency between two tasks.
346 static void __SD_task_dependency_destroy(void *dependency)
348 if (((SD_dependency_t) dependency)->name != NULL)
349 xbt_free(((SD_dependency_t) dependency)->name);
350 xbt_free(dependency);
354 * \brief Adds a dependency between two tasks
356 * \a dst will depend on \a src, ie \a dst will not start before \a src is finished.
357 * Their \ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_READY.
359 * \param name the name of the new dependency (can be \c NULL)
360 * \param data the user data you want to associate with this dependency (can be \c NULL)
361 * \param src the task which must be executed first
362 * \param dst the task you want to make depend on \a src
363 * \see SD_task_dependency_remove()
365 void SD_task_dependency_add(const char *name, void *data, SD_task_t src,
372 SD_dependency_t dependency;
374 SD_CHECK_INIT_DONE();
375 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
377 dynar = src->tasks_after;
378 length = xbt_dynar_length(dynar);
382 "Cannot add a dependency between task '%s' and itself",
383 SD_task_get_name(src));
385 if (!__SD_task_is_not_scheduled(src)
386 && !__SD_task_is_scheduled_or_ready(src))
388 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY",
389 SD_task_get_name(src));
391 if (!__SD_task_is_not_scheduled(dst)
392 && !__SD_task_is_scheduled_or_ready(dst))
394 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY",
395 SD_task_get_name(dst));
397 DEBUG2("SD_task_dependency_add: src = %s, dst = %s", SD_task_get_name(src),
398 SD_task_get_name(dst));
399 for (i = 0; i < length && !found; i++) {
400 xbt_dynar_get_cpy(dynar, i, &dependency);
401 found = (dependency->dst == dst);
402 DEBUG2("Dependency %d: dependency->dst = %s", i,
403 SD_task_get_name(dependency->dst));
408 "A dependency already exists between task '%s' and task '%s'",
409 SD_task_get_name(src), SD_task_get_name(dst));
411 dependency = xbt_new(s_SD_dependency_t, 1);
413 dependency->name = xbt_strdup(name); /* xbt_strdup is cleaver enough to deal with NULL args itself */
414 dependency->data = data;
415 dependency->src = src;
416 dependency->dst = dst;
418 /* src must be executed before dst */
419 xbt_dynar_push(src->tasks_after, &dependency);
420 xbt_dynar_push(dst->tasks_before, &dependency);
422 /* if the task was ready, then dst->tasks_before is not empty anymore,
423 so we must go back to state SD_SCHEDULED */
424 if (__SD_task_is_ready(dst)) {
425 DEBUG1("SD_task_dependency_add: %s was ready and becomes scheduled!",
426 SD_task_get_name(dst));
427 __SD_task_set_state(dst, SD_SCHEDULED);
430 /* __SD_print_dependencies(src);
431 __SD_print_dependencies(dst); */
435 * \brief Indacates whether there is a dependency between two tasks.
438 * \param dst a task depending on \a src
440 * If src is NULL, checks whether dst has any pre-dependency.
441 * If dst is NULL, checks whether src has any post-dependency.
443 int SD_task_dependency_exists(SD_task_t src, SD_task_t dst)
445 unsigned int counter;
446 SD_dependency_t dependency;
448 SD_CHECK_INIT_DONE();
449 xbt_assert0(src != NULL || dst != NULL, "Invalid parameter: both src and dst are NULL");
453 xbt_dynar_foreach(src->tasks_after,counter,dependency) {
454 if (dependency->dst == dst)
458 return xbt_dynar_length(src->tasks_after);
461 return xbt_dynar_length(dst->tasks_before);
467 * \brief Remove a dependency between two tasks
470 * \param dst a task depending on \a src
471 * \see SD_task_dependency_add()
473 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst)
480 SD_dependency_t dependency;
482 SD_CHECK_INIT_DONE();
483 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
485 /* remove the dependency from src->tasks_after */
486 dynar = src->tasks_after;
487 length = xbt_dynar_length(dynar);
489 for (i = 0; i < length && !found; i++) {
490 xbt_dynar_get_cpy(dynar, i, &dependency);
491 if (dependency->dst == dst) {
492 xbt_dynar_remove_at(dynar, i, NULL);
498 "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
499 SD_task_get_name(src), SD_task_get_name(dst),
500 SD_task_get_name(dst), SD_task_get_name(src));
502 /* remove the dependency from dst->tasks_before */
503 dynar = dst->tasks_before;
504 length = xbt_dynar_length(dynar);
507 for (i = 0; i < length && !found; i++) {
508 xbt_dynar_get_cpy(dynar, i, &dependency);
509 if (dependency->src == src) {
510 xbt_dynar_remove_at(dynar, i, NULL);
511 __SD_task_dependency_destroy(dependency);
515 /* should never happen... */
517 "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
518 SD_task_get_name(dst), SD_task_get_name(src),
519 SD_task_get_name(src), SD_task_get_name(dst));
521 /* if the task was scheduled and dst->tasks_before is empty now, we can make it ready */
522 if (xbt_dynar_length(dst->tasks_before) == 0 && __SD_task_is_scheduled(dst))
523 __SD_task_set_state(dst, SD_READY);
525 /* __SD_print_dependencies(src);
526 __SD_print_dependencies(dst); */
530 * \brief Returns the user data associated with a dependency between two tasks
533 * \param dst a task depending on \a src
534 * \return the user data associated with this dependency (can be \c NULL)
535 * \see SD_task_dependency_add()
537 void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst)
544 SD_dependency_t dependency;
547 SD_CHECK_INIT_DONE();
548 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
550 dynar = src->tasks_after;
551 length = xbt_dynar_length(dynar);
553 for (i = 0; i < length && !found; i++) {
554 xbt_dynar_get_cpy(dynar, i, &dependency);
555 found = (dependency->dst == dst);
558 THROW2(arg_error, 0, "No dependency found between task '%s' and '%s'",
559 SD_task_get_name(src), SD_task_get_name(dst));
560 return dependency->data;
563 /* temporary function for debugging */
564 static void __SD_print_watch_points(SD_task_t task)
566 static const int state_masks[] =
567 { SD_SCHEDULED, SD_RUNNING, SD_READY, SD_DONE, SD_FAILED };
568 static const char *state_names[] =
569 { "scheduled", "running", "ready", "done", "failed" };
572 INFO2("Task '%s' watch points (%x): ", SD_task_get_name(task),
576 for (i = 0; i < 5; i++) {
577 if (task->watch_points & state_masks[i])
578 INFO1("%s ", state_names[i]);
583 * \brief Adds a watch point to a task
585 * SD_simulate() will stop as soon as the \ref e_SD_task_state_t "state" of this
586 * task becomes the one given in argument. The
587 * watch point is then automatically removed.
590 * \param state the \ref e_SD_task_state_t "state" you want to watch
591 * (cannot be #SD_NOT_SCHEDULED)
592 * \see SD_task_unwatch()
594 void SD_task_watch(SD_task_t task, e_SD_task_state_t state)
596 SD_CHECK_INIT_DONE();
597 xbt_assert0(task != NULL, "Invalid parameter");
599 if (state & SD_NOT_SCHEDULED)
601 "Cannot add a watch point for state SD_NOT_SCHEDULED");
603 task->watch_points = task->watch_points | state;
604 /* __SD_print_watch_points(task); */
608 * \brief Removes a watch point from a task
611 * \param state the \ref e_SD_task_state_t "state" you no longer want to watch
612 * \see SD_task_watch()
614 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state)
616 SD_CHECK_INIT_DONE();
617 xbt_assert0(task != NULL, "Invalid parameter");
618 xbt_assert0(state != SD_NOT_SCHEDULED,
619 "SimDag error: Cannot have a watch point for state SD_NOT_SCHEDULED");
621 task->watch_points = task->watch_points & ~state;
622 /* __SD_print_watch_points(task); */
626 * \brief Returns an approximative estimation of the execution time of a task.
628 * The estimation is very approximative because the value returned is the time
629 * the task would take if it was executed now and if it was the only task.
631 * \param task the task to evaluate
632 * \param workstation_nb number of workstations on which the task would be executed
633 * \param workstation_list the workstations on which the task would be executed
634 * \param computation_amount computation amount for each workstation
635 * \param communication_amount communication amount between each pair of workstations
638 double SD_task_get_execution_time(SD_task_t task,
640 const SD_workstation_t * workstation_list,
641 const double *computation_amount,
642 const double *communication_amount)
644 double time, max_time = 0.0;
646 SD_CHECK_INIT_DONE();
647 xbt_assert0(task != NULL && workstation_nb > 0 && workstation_list != NULL
648 && computation_amount != NULL
649 && communication_amount != NULL, "Invalid parameter");
651 /* the task execution time is the maximum execution time of the parallel tasks */
653 for (i = 0; i < workstation_nb; i++) {
655 SD_workstation_get_computation_time(workstation_list[i],
656 computation_amount[i]);
658 for (j = 0; j < workstation_nb; j++) {
660 SD_route_get_communication_time(workstation_list[i],
662 communication_amount[i *
667 if (time > max_time) {
673 static inline void SD_task_do_schedule(SD_task_t task) {
674 SD_CHECK_INIT_DONE();
676 if (!__SD_task_is_not_scheduled(task))
677 THROW1(arg_error, 0, "Task '%s' has already been scheduled",
678 SD_task_get_name(task));
680 /* update the task state */
681 if (xbt_dynar_length(task->tasks_before) == 0)
682 __SD_task_set_state(task, SD_READY);
684 __SD_task_set_state(task, SD_SCHEDULED);
688 * \brief Schedules a task
690 * The task state must be #SD_NOT_SCHEDULED.
691 * Once scheduled, a task will be executed as soon as possible in SD_simulate(),
692 * i.e. when its dependencies are satisfied.
694 * \param task the task you want to schedule
695 * \param workstation_nb number of workstations on which the task will be executed
696 * \param workstation_list the workstations on which the task will be executed
697 * \param computation_amount computation amount for each workstation
698 * \param communication_amount communication amount between each pair of workstations
699 * \param rate task execution speed rate
700 * \see SD_task_unschedule()
702 void SD_task_schedule(SD_task_t task, int workstation_count,
703 const SD_workstation_t * workstation_list,
704 const double *computation_amount,
705 const double *communication_amount, double rate)
707 xbt_assert0(workstation_count > 0, "workstation_nb must be positive");
709 int communication_nb;
711 task->workstation_nb = workstation_count;
714 task->computation_amount = xbt_new(double, workstation_count);
715 memcpy(task->computation_amount, computation_amount,
716 sizeof(double) * workstation_count);
718 communication_nb = workstation_count * workstation_count;
719 task->communication_amount = xbt_new(double, communication_nb);
720 memcpy(task->communication_amount, communication_amount,
721 sizeof(double) * communication_nb);
723 task->workstation_list = xbt_new(SD_workstation_t, workstation_count);
724 memcpy(task->workstation_list, workstation_list,
725 sizeof(SD_workstation_t) * workstation_count);
727 SD_task_do_schedule(task);
730 * \brief Unschedules a task
732 * The task state must be #SD_SCHEDULED, #SD_READY, #SD_RUNNING or #SD_FAILED.
733 * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
734 * Call SD_task_schedule() to schedule it again.
736 * \param task the task you want to unschedule
737 * \see SD_task_schedule()
739 void SD_task_unschedule(SD_task_t task)
741 SD_CHECK_INIT_DONE();
742 xbt_assert0(task != NULL, "Invalid parameter");
744 if (task->state_set != sd_global->scheduled_task_set &&
745 task->state_set != sd_global->ready_task_set &&
746 task->state_set != sd_global->running_task_set &&
747 task->state_set != sd_global->failed_task_set)
749 "Task %s: the state must be SD_SCHEDULED, SD_READY, SD_RUNNING or SD_FAILED",
750 SD_task_get_name(task));
752 if (__SD_task_is_scheduled_or_ready(task) /* if the task is scheduled or ready */
753 && task->kind == SD_TASK_NOT_TYPED) /* Don't free scheduling data for typed tasks */
754 __SD_task_destroy_scheduling_data(task);
756 if (__SD_task_is_running(task)) /* the task should become SD_FAILED */
757 surf_workstation_model->action_cancel(task->surf_action);
759 __SD_task_set_state(task, SD_NOT_SCHEDULED);
760 task->remains = task->amount;
761 task->start_time = -1.0;
764 /* Destroys the data memorised by SD_task_schedule. Task state must be SD_SCHEDULED or SD_READY.
766 static void __SD_task_destroy_scheduling_data(SD_task_t task)
768 SD_CHECK_INIT_DONE();
769 if (!__SD_task_is_scheduled_or_ready(task) && !__SD_task_is_in_fifo(task))
771 "Task '%s' must be SD_SCHEDULED, SD_READY or SD_IN_FIFO",
772 SD_task_get_name(task));
774 xbt_free(task->computation_amount);
775 xbt_free(task->communication_amount);
776 task->computation_amount = task->communication_amount = NULL;
779 /* Runs a task. This function is directly called by __SD_task_try_to_run if the task
780 * doesn't have to wait in fifos. Otherwise, it is called by __SD_task_just_done when
781 * the task gets out of its fifos.
783 void __SD_task_really_run(SD_task_t task)
787 void **surf_workstations;
789 SD_CHECK_INIT_DONE();
790 xbt_assert0(task != NULL, "Invalid parameter");
791 xbt_assert2(__SD_task_is_ready_or_in_fifo(task),
792 "Task '%s' is not ready or in a fifo! Task state: %d",
793 SD_task_get_name(task), SD_task_get_state(task));
794 xbt_assert1(task->workstation_list != NULL,
795 "Task '%s': workstation_list is NULL!", SD_task_get_name(task));
799 DEBUG1("Really running task '%s'", SD_task_get_name(task));
801 /* set this task as current task for the workstations in sequential mode */
802 for (i = 0; i < task->workstation_nb; i++) {
803 if (SD_workstation_get_access_mode(task->workstation_list[i]) ==
804 SD_WORKSTATION_SEQUENTIAL_ACCESS) {
805 task->workstation_list[i]->current_task = task;
806 xbt_assert0(__SD_workstation_is_busy(task->workstation_list[i]),
807 "The workstation should be busy now");
811 DEBUG1("Task '%s' set as current task for its workstations",
812 SD_task_get_name(task));
816 /* we have to create a Surf workstation array instead of the SimDag workstation array */
817 surf_workstations = xbt_new(void *, task->workstation_nb);
819 for (i = 0; i < task->workstation_nb; i++) {
820 surf_workstations[i] = task->workstation_list[i]->surf_workstation;
823 task->surf_action = NULL;
824 if ((task->workstation_nb == 1) && (task->communication_amount[0] == 0.0)) {
826 surf_workstation_model->extension.
827 workstation.execute(surf_workstations[0], task->computation_amount[0]);
828 } else if ((task->workstation_nb == 1)
829 && (task->computation_amount[0] == 0.0)) {
831 surf_workstation_model->extension.
832 workstation.communicate(surf_workstations[0], surf_workstations[0],
833 task->communication_amount[0], task->rate);
834 } else if ((task->workstation_nb == 2)
835 && (task->computation_amount[0] == 0.0)
836 && (task->computation_amount[1] == 0.0)) {
840 for (i = 0; i < task->workstation_nb * task->workstation_nb; i++) {
841 if (task->communication_amount[i] > 0.0) {
843 value = task->communication_amount[i];
848 surf_workstation_model->extension.
849 workstation.communicate(surf_workstations[0], surf_workstations[1],
853 if (!task->surf_action) {
854 double *computation_amount = xbt_new(double, task->workstation_nb);
855 double *communication_amount = xbt_new(double, task->workstation_nb *
856 task->workstation_nb);
858 memcpy(computation_amount, task->computation_amount, sizeof(double) *
859 task->workstation_nb);
860 memcpy(communication_amount, task->communication_amount,
861 sizeof(double) * task->workstation_nb * task->workstation_nb);
864 surf_workstation_model->extension.
865 workstation.execute_parallel_task(task->workstation_nb,
866 surf_workstations, computation_amount,
867 communication_amount, task->amount,
870 xbt_free(surf_workstations);
873 surf_workstation_model->action_data_set(task->surf_action, task);
875 DEBUG1("surf_action = %p", task->surf_action);
877 __SD_task_destroy_scheduling_data(task); /* now the scheduling data are not useful anymore */
878 __SD_task_set_state(task, SD_RUNNING);
879 xbt_assert2(__SD_task_is_running(task), "Bad state of task '%s': %d",
880 SD_task_get_name(task), SD_task_get_state(task));
884 /* Tries to run a task. This function is called by SD_simulate() when a scheduled task becomes SD_READY
885 * (ie when its dependencies are satisfied).
886 * If one of the workstations where the task is scheduled on is busy (in sequential mode),
887 * the task doesn't start.
888 * Returns whether the task has started.
890 int __SD_task_try_to_run(SD_task_t task)
895 SD_workstation_t workstation;
897 SD_CHECK_INIT_DONE();
898 xbt_assert0(task != NULL, "Invalid parameter");
899 xbt_assert2(__SD_task_is_ready(task),
900 "Task '%s' is not ready! Task state: %d",
901 SD_task_get_name(task), SD_task_get_state(task));
904 for (i = 0; i < task->workstation_nb; i++) {
905 can_start = !__SD_workstation_is_busy(task->workstation_list[i]);
908 DEBUG2("Task '%s' can start: %d", SD_task_get_name(task), can_start);
910 if (!can_start) { /* if the task cannot start and is not in the fifos yet */
911 for (i = 0; i < task->workstation_nb; i++) {
912 workstation = task->workstation_list[i];
913 if (workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
914 DEBUG2("Pushing task '%s' in the fifo of workstation '%s'",
915 SD_task_get_name(task), SD_workstation_get_name(workstation));
916 xbt_fifo_push(workstation->task_fifo, task);
919 __SD_task_set_state(task, SD_IN_FIFO);
920 xbt_assert2(__SD_task_is_in_fifo(task), "Bad state of task '%s': %d",
921 SD_task_get_name(task), SD_task_get_state(task));
922 DEBUG1("Task '%s' state is now SD_IN_FIFO", SD_task_get_name(task));
924 __SD_task_really_run(task);
930 /* This function is called by SD_simulate when a task is done.
931 * It updates task->state and task->action and executes if necessary the tasks
932 * which were waiting in fifos for the end of `task'
934 void __SD_task_just_done(SD_task_t task)
937 SD_workstation_t workstation;
940 int candidate_nb = 0;
941 int candidate_capacity = 8;
942 SD_task_t *candidates;
945 SD_CHECK_INIT_DONE();
946 xbt_assert0(task != NULL, "Invalid parameter");
947 xbt_assert1(__SD_task_is_running(task),
948 "The task must be running! Task state: %d",
949 SD_task_get_state(task));
950 xbt_assert1(task->workstation_list != NULL,
951 "Task '%s': workstation_list is NULL!", SD_task_get_name(task));
954 candidates = xbt_new(SD_task_t, 8);
956 __SD_task_set_state(task, SD_DONE);
957 surf_workstation_model->action_unref(task->surf_action);
958 task->surf_action = NULL;
960 DEBUG0("Looking for candidates");
962 /* if the task was executed on sequential workstations,
963 maybe we can execute the next task of the fifo for each workstation */
964 for (i = 0; i < task->workstation_nb; i++) {
965 workstation = task->workstation_list[i];
966 DEBUG2("Workstation '%s': access_mode = %d",
967 SD_workstation_get_name(workstation), workstation->access_mode);
968 if (workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
969 xbt_assert1(workstation->task_fifo != NULL,
970 "Workstation '%s' has sequential access but no fifo!",
971 SD_workstation_get_name(workstation));
972 xbt_assert2(workstation->current_task =
973 task, "Workstation '%s': current task should be '%s'",
974 SD_workstation_get_name(workstation),
975 SD_task_get_name(task));
977 /* the task is over so we can release the workstation */
978 workstation->current_task = NULL;
980 DEBUG0("Getting candidate in fifo");
982 xbt_fifo_get_item_content(xbt_fifo_get_first_item
983 (workstation->task_fifo));
985 if (candidate != NULL) {
986 DEBUG1("Candidate: '%s'", SD_task_get_name(candidate));
987 xbt_assert2(__SD_task_is_in_fifo(candidate),
988 "Bad state of candidate '%s': %d",
989 SD_task_get_name(candidate),
990 SD_task_get_state(candidate));
993 DEBUG1("Candidate in fifo: %p", candidate);
995 /* if there was a task waiting for my place */
996 if (candidate != NULL) {
997 /* Unfortunately, we are not sure yet that we can execute the task now,
998 because the task can be waiting more deeply in some other workstation's fifos...
999 So we memorize all candidate tasks, and then we will check for each candidate
1000 whether or not all its workstations are available. */
1002 /* realloc if necessary */
1003 if (candidate_nb == candidate_capacity) {
1004 candidate_capacity *= 2;
1006 xbt_realloc(candidates, sizeof(SD_task_t) * candidate_capacity);
1009 /* register the candidate */
1010 candidates[candidate_nb++] = candidate;
1011 candidate->fifo_checked = 0;
1016 DEBUG1("Candidates found: %d", candidate_nb);
1018 /* now we check every candidate task */
1019 for (i = 0; i < candidate_nb; i++) {
1020 candidate = candidates[i];
1022 if (candidate->fifo_checked) {
1023 continue; /* we have already evaluated that task */
1026 xbt_assert2(__SD_task_is_in_fifo(candidate),
1027 "Bad state of candidate '%s': %d",
1028 SD_task_get_name(candidate), SD_task_get_state(candidate));
1030 for (j = 0; j < candidate->workstation_nb && can_start; j++) {
1031 workstation = candidate->workstation_list[j];
1033 /* I can start on this workstation if the workstation is shared
1034 or if I am the first task in the fifo */
1035 can_start = workstation->access_mode == SD_WORKSTATION_SHARED_ACCESS ||
1037 xbt_fifo_get_item_content(xbt_fifo_get_first_item
1038 (workstation->task_fifo));
1041 DEBUG2("Candidate '%s' can start: %d", SD_task_get_name(candidate),
1044 /* now we are sure that I can start! */
1046 for (j = 0; j < candidate->workstation_nb && can_start; j++) {
1047 workstation = candidate->workstation_list[j];
1049 /* update the fifo */
1050 if (workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
1051 candidate = xbt_fifo_shift(workstation->task_fifo); /* the return value is stored just for debugging */
1052 DEBUG1("Head of the fifo: '%s'",
1053 (candidate != NULL) ? SD_task_get_name(candidate) : "NULL");
1054 xbt_assert0(candidate == candidates[i],
1055 "Error in __SD_task_just_done: bad first task in the fifo");
1057 } /* for each workstation */
1059 /* finally execute the task */
1060 DEBUG2("Task '%s' state: %d", SD_task_get_name(candidate),
1061 SD_task_get_state(candidate));
1062 __SD_task_really_run(candidate);
1065 ("Calling __SD_task_is_running: task '%s', state set: %p, running_task_set: %p, is running: %d",
1066 SD_task_get_name(candidate), candidate->state_set,
1067 sd_global->running_task_set, __SD_task_is_running(candidate));
1068 xbt_assert2(__SD_task_is_running(candidate),
1069 "Bad state of task '%s': %d", SD_task_get_name(candidate),
1070 SD_task_get_state(candidate));
1071 DEBUG0("Okay, the task is running.");
1074 candidate->fifo_checked = 1;
1075 } /* for each candidate */
1077 xbt_free(candidates);
1080 /* Remove all dependencies associated with a task. This function is called when the task is destroyed.
1082 static void __SD_task_remove_dependencies(SD_task_t task)
1084 /* we must destroy the dependencies carefuly (with SD_dependency_remove)
1085 because each one is stored twice */
1086 SD_dependency_t dependency;
1087 while (xbt_dynar_length(task->tasks_before) > 0) {
1088 xbt_dynar_get_cpy(task->tasks_before, 0, &dependency);
1089 SD_task_dependency_remove(dependency->src, dependency->dst);
1092 while (xbt_dynar_length(task->tasks_after) > 0) {
1093 xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
1094 SD_task_dependency_remove(dependency->src, dependency->dst);
1099 * \brief Returns the start time of a task
1101 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
1103 * \param task: a task
1104 * \return the start time of this task
1106 double SD_task_get_start_time(SD_task_t task)
1108 SD_CHECK_INIT_DONE();
1109 xbt_assert0(task != NULL, "Invalid parameter");
1110 if (task->surf_action)
1111 return surf_workstation_model->action_get_start_time(task->surf_action);
1113 return task->start_time;
1117 * \brief Returns the finish time of a task
1119 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
1120 * If the state is not completed yet, the returned value is an
1121 * estimation of the task finish time. This value can fluctuate
1122 * until the task is completed.
1124 * \param task: a task
1125 * \return the start time of this task
1127 double SD_task_get_finish_time(SD_task_t task)
1129 SD_CHECK_INIT_DONE();
1130 xbt_assert0(task != NULL, "Invalid parameter");
1132 if (task->surf_action) /* should never happen as actions are destroyed right after their completion */
1133 return surf_workstation_model->action_get_finish_time(task->surf_action);
1135 return task->finish_time;
1139 * \brief Destroys a task.
1141 * The user data (if any) should have been destroyed first.
1143 * \param task the task you want to destroy
1144 * \see SD_task_create()
1146 void SD_task_destroy(SD_task_t task)
1148 SD_CHECK_INIT_DONE();
1149 xbt_assert0(task != NULL, "Invalid parameter");
1151 DEBUG1("Destroying task %s...", SD_task_get_name(task));
1153 __SD_task_remove_dependencies(task);
1154 /* if the task was scheduled or ready we have to free the scheduling parameters */
1155 if (__SD_task_is_scheduled_or_ready(task))
1156 __SD_task_destroy_scheduling_data(task);
1157 xbt_swag_remove(task,task->state_set);
1159 if (task->name != NULL)
1160 xbt_free(task->name);
1162 if (task->surf_action != NULL)
1163 surf_workstation_model->action_unref(task->surf_action);
1165 if (task->workstation_list != NULL)
1166 xbt_free(task->workstation_list);
1168 if (task->communication_amount)
1169 xbt_free(task->communication_amount);
1171 if (task->computation_amount)
1172 xbt_free(task->computation_amount);
1174 xbt_dynar_free(&task->tasks_before);
1175 xbt_dynar_free(&task->tasks_after);
1178 sd_global->task_number--;
1180 DEBUG0("Task destroyed.");
1184 static inline SD_task_t SD_task_create_sized(const char*name,void*data,double amount,int ws_count) {
1185 SD_task_t task = SD_task_create(name,data,amount);
1186 task->communication_amount = xbt_new0(double,ws_count*ws_count);
1187 task->computation_amount = xbt_new0(double,ws_count);
1188 task->workstation_nb = ws_count;
1189 task->workstation_list = xbt_new0(SD_workstation_t,ws_count);
1192 /** @brief create a end-to-end communication task that can then be auto-scheduled
1194 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This
1195 * allows to specify the task costs at creation, and decorelate them from the
1196 * scheduling process where you just specify which resource should deliver the
1199 * A end-to-end communication must be scheduled on 2 hosts, and the amount
1200 * specified at creation is sent from hosts[0] to hosts[1].
1202 SD_task_t SD_task_create_comm_e2e(const char*name, void *data, double amount) {
1203 SD_task_t res = SD_task_create_sized(name,data,amount,2);
1204 res->communication_amount[2] = amount;
1205 res->kind=SD_TASK_COMM_E2E;
1208 /** @brief create a sequential computation task that can then be auto-scheduled
1210 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This
1211 * allows to specify the task costs at creation, and decorelate them from the
1212 * scheduling process where you just specify which resource should deliver the
1215 * A sequential computation must be scheduled on 1 host, and the amount
1216 * specified at creation to be run on hosts[0].
1218 SD_task_t SD_task_create_comp_seq(const char*name, void *data, double amount) {
1219 SD_task_t res = SD_task_create_sized(name,data,amount,1);
1220 res->computation_amount[0]=amount;
1221 res->kind=SD_TASK_COMP_SEQ;
1225 /** @brief Auto-schedules a task.
1227 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This
1228 * allows to specify the task costs at creation, and decorelate them from the
1229 * scheduling process where you just specify which resource should deliver the
1232 * To be auto-schedulable, a task must be created with SD_task_create_comm_e2e() or
1233 * SD_task_create_comp_seq(). Check their definitions for the exact semantic of each
1237 * We should create tasks kind for the following categories:
1238 * - Point to point communication (done)
1239 * - Sequential computation (done)
1240 * - group communication (redistribution, several kinds)
1241 * - parallel tasks with no internal communication (one kind per speedup model such as amdal)
1242 * - idem+ internal communication. Task type not enough since we cannot store comm cost alongside to comp one)
1244 void SD_task_schedulev(SD_task_t task, int count, const SD_workstation_t*list) {
1246 xbt_assert1(task->kind != 0,"Task %s is not typed. Cannot automatically schedule it.",SD_task_get_name(task));
1247 switch(task->kind) {
1248 case SD_TASK_COMM_E2E:
1249 case SD_TASK_COMP_SEQ:
1250 xbt_assert(task->workstation_nb==count);
1251 for (i=0;i<count;i++)
1252 task->workstation_list[i]=list[i];
1253 SD_task_do_schedule(task);
1256 xbt_die(bprintf("Kind of task %s not supported by SD_task_schedulev()",
1257 SD_task_get_name(task)));
1259 if (task->kind == SD_TASK_COMM_E2E) {
1260 VERB4("Schedule comm task %s between %s -> %s. It costs %.f bytes",
1261 SD_task_get_name(task),
1262 SD_workstation_get_name(task->workstation_list[0]),SD_workstation_get_name(task->workstation_list[1]),
1263 task->communication_amount[2]);
1266 /* Iterate over all childs and parent being COMM_E2E to say where I am located (and start them if ready) */
1267 if (task->kind == SD_TASK_COMP_SEQ) {
1268 VERB3("Schedule computation task %s on %s. It costs %.f flops",
1269 SD_task_get_name(task),SD_workstation_get_name(task->workstation_list[0]),
1270 task->computation_amount[0]);
1271 SD_dependency_t dep;
1273 xbt_dynar_foreach(task->tasks_before,cpt,dep) {
1274 SD_task_t before = dep->src;
1275 if (before->kind == SD_TASK_COMM_E2E) {
1276 before->workstation_list[1] = task->workstation_list[0];
1277 if (before->workstation_list[0] && __SD_task_is_not_scheduled(before)) {
1278 SD_task_do_schedule(before);
1279 VERB4("Auto-Schedule comm task %s between %s -> %s. It costs %.f bytes",
1280 SD_task_get_name(before),
1281 SD_workstation_get_name(before->workstation_list[0]),SD_workstation_get_name(before->workstation_list[1]),
1282 before->communication_amount[2]);
1286 xbt_dynar_foreach(task->tasks_after,cpt,dep) {
1287 SD_task_t after = dep->dst;
1288 if (after->kind == SD_TASK_COMM_E2E) {
1289 after->workstation_list[0] = task->workstation_list[0];
1290 if (after->workstation_list[1] && __SD_task_is_not_scheduled(after)) {
1291 SD_task_do_schedule(after);
1292 VERB4("Auto-Schedule comm task %s between %s -> %s. It costs %.f bytes",
1293 SD_task_get_name(after),
1294 SD_workstation_get_name(after->workstation_list[0]),SD_workstation_get_name(after->workstation_list[1]),
1295 after->communication_amount[2]);
1302 /** @brief autoschedule a task on a list of workstations
1304 * This function is very similar to SD_task_schedulev(),
1305 * but takes the list of workstations to schedule onto as separate parameters.
1306 * It builds a proper vector of workstations and then call SD_task_schedulev()
1308 void SD_task_schedulel(SD_task_t task, int count, ...) {
1310 SD_workstation_t *list=xbt_new(SD_workstation_t,count);
1313 for (i=0;i<count;i++) {
1314 list[i] = va_arg(ap,SD_workstation_t);
1317 SD_task_schedulev(task,count,list);