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");
240 xbt_assert1(src != dst, "Cannot add a dependency between task '%s' and itself", SD_task_get_name(src));
241 xbt_assert1(__SD_task_is_not_scheduled(src) || __SD_task_is_scheduled_or_ready(src),
242 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(src));
243 xbt_assert1(__SD_task_is_not_scheduled(dst) || __SD_task_is_scheduled_or_ready(dst),
244 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULED or SD_READY", SD_task_get_name(dst));
246 xbt_dynar_t dynar = src->tasks_after;
247 int length = xbt_dynar_length(dynar);
250 SD_dependency_t dependency;
251 for (i = 0; i < length && !found; i++) {
252 xbt_dynar_get_cpy(dynar, i, &dependency);
253 found = (dependency->dst == dst);
255 xbt_assert2(!found, "A dependency already exists between task '%s' and task '%s'",
256 SD_task_get_name(src), SD_task_get_name(dst));
258 dependency = xbt_new0(s_SD_dependency_t, 1);
261 dependency->name = xbt_strdup(name);
263 dependency->name = NULL;
265 dependency->data = data;
266 dependency->src = src;
267 dependency->dst = dst;
269 /* src must be executed before dst */
270 xbt_dynar_push(src->tasks_after, &dependency);
271 xbt_dynar_push(dst->tasks_before, &dependency);
273 /* if the task was ready, then dst->tasks_before is not empty anymore,
274 so we must go back to state SD_SCHEDULED */
275 if (__SD_task_is_ready(dst)) {
276 DEBUG1("SD_task_dependency_add: %s was ready and becomes scheduled!", SD_task_get_name(dst));
277 __SD_task_set_state(dst, SD_SCHEDULED);
280 /* __SD_print_dependencies(src);
281 __SD_print_dependencies(dst); */
285 * \brief Remove a dependency between two tasks
288 * \param dst a task depending on \a src
289 * \see SD_task_dependency_add()
291 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst) {
292 SD_CHECK_INIT_DONE();
293 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
295 /* remove the dependency from src->tasks_after */
296 xbt_dynar_t dynar = src->tasks_after;
297 int length = xbt_dynar_length(dynar);
300 SD_dependency_t dependency;
301 for (i = 0; i < length && !found; i++) {
302 xbt_dynar_get_cpy(dynar, i, &dependency);
303 if (dependency->dst == dst) {
304 xbt_dynar_remove_at(dynar, i, NULL);
308 xbt_assert4(found, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
309 SD_task_get_name(src), SD_task_get_name(dst), SD_task_get_name(dst), SD_task_get_name(src));
311 /* remove the dependency from dst->tasks_before */
312 dynar = dst->tasks_before;
313 length = xbt_dynar_length(dynar);
316 for (i = 0; i < length && !found; i++) {
317 xbt_dynar_get_cpy(dynar, i, &dependency);
318 if (dependency->src == src) {
319 xbt_dynar_remove_at(dynar, i, NULL);
320 __SD_task_dependency_destroy(dependency);
324 xbt_assert4(found, "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
325 SD_task_get_name(dst), SD_task_get_name(src), SD_task_get_name(src), SD_task_get_name(dst)); /* should never happen... */
327 /* if the task was scheduled and dst->tasks_before is empty now, we can make it ready */
328 if (xbt_dynar_length(dst->tasks_before) == 0 && __SD_task_is_scheduled(dst))
329 __SD_task_set_state(dst, SD_READY);
331 /* __SD_print_dependencies(src);
332 __SD_print_dependencies(dst);*/
336 * \brief Returns the user data associated with a dependency between two tasks
339 * \param dst a task depending on \a src
340 * \return the user data associated with this dependency (can be \c NULL)
341 * \see SD_task_dependency_add()
343 void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst) {
344 SD_CHECK_INIT_DONE();
345 xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
347 xbt_dynar_t dynar = src->tasks_after;
348 int length = xbt_dynar_length(dynar);
351 SD_dependency_t dependency;
352 for (i = 0; i < length && !found; i++) {
353 xbt_dynar_get_cpy(dynar, i, &dependency);
354 found = (dependency->dst == dst);
356 xbt_assert2(found, "No dependency found between task '%s' and '%s'", SD_task_get_name(src), SD_task_get_name(dst));
357 return dependency->data;
360 /* temporary function for debugging */
361 static void __SD_print_watch_points(SD_task_t task) {
362 static const int state_masks[] = {SD_SCHEDULED, SD_RUNNING, SD_READY, SD_DONE, SD_FAILED};
363 static const char* state_names[] = {"scheduled", "running", "ready", "done", "failed"};
365 INFO2("Task '%s' watch points (%x): ", SD_task_get_name(task), task->watch_points);
368 for (i = 0; i < 5; i++) {
369 if (task->watch_points & state_masks[i])
370 INFO1("%s ", state_names[i]);
375 * \brief Adds a watch point to a task
377 * 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
378 * watch point is then automatically removed.
381 * \param state the \ref e_SD_task_state_t "state" you want to watch (cannot be #SD_NOT_SCHEDULED)
382 * \see SD_task_unwatch()
384 void SD_task_watch(SD_task_t task, e_SD_task_state_t state) {
385 SD_CHECK_INIT_DONE();
386 xbt_assert0(task != NULL, "Invalid parameter");
387 xbt_assert0(state != SD_NOT_SCHEDULED, "Cannot add a watch point for state SD_NOT_SCHEDULED");
389 task->watch_points = task->watch_points | state;
390 /* __SD_print_watch_points(task);*/
394 * \brief Removes a watch point from a task
397 * \param state the \ref e_SD_task_state_t "state" you no longer want to watch
398 * \see SD_task_watch()
400 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state) {
401 SD_CHECK_INIT_DONE();
402 xbt_assert0(task != NULL, "Invalid parameter");
403 xbt_assert0(state != SD_NOT_SCHEDULED, "Cannot have a watch point for state SD_NOT_SCHEDULED");
405 task->watch_points = task->watch_points & ~state;
406 /* __SD_print_watch_points(task);*/
410 * \brief Returns an approximative estimation of the execution time of a task.
412 * The estimation is very approximative because the value returned is the time
413 * the task would take if it was executed now and if it was the only task.
415 * \param task the task to evaluate
416 * \param workstation_nb number of workstations on which the task would be executed
417 * \param workstation_list the workstations on which the task would be executed
418 * \param computation_amount computation amount for each workstation
419 * \param communication_amount communication amount between each pair of workstations
420 * \param rate task execution speed rate
423 double SD_task_get_execution_time(SD_task_t task,
425 const SD_workstation_t *workstation_list,
426 const double *computation_amount,
427 const double *communication_amount,
429 /* the task execution time is the maximum execution time of the parallel tasks */
430 double time, max_time = 0.0;
432 for (i = 0; i < workstation_nb; i++) {
433 time = SD_workstation_get_computation_time(workstation_list[i], computation_amount[i]);
435 for (j = 0; j < workstation_nb; j++) {
436 time += SD_route_get_communication_time(workstation_list[i], workstation_list[j],
437 communication_amount[i * workstation_nb + j]);
440 if (time > max_time) {
444 return max_time * SD_task_get_amount(task);
448 * \brief Schedules a task
450 * The task state must be #SD_NOT_SCHEDULED.
451 * Once scheduled, a task will be executed as soon as possible in SD_simulate(),
452 * i.e. when its dependencies are satisfied.
454 * \param task the task you want to schedule
455 * \param workstation_nb number of workstations on which the task will be executed
456 * \param workstation_list the workstations on which the task will be executed
457 * \param computation_amount computation amount for each workstation
458 * \param communication_amount communication amount between each pair of workstations
459 * \param rate task execution speed rate
460 * \see SD_task_unschedule()
462 void SD_task_schedule(SD_task_t task, int workstation_nb,
463 const SD_workstation_t *workstation_list, const double *computation_amount,
464 const double *communication_amount, double rate) {
465 SD_CHECK_INIT_DONE();
466 xbt_assert0(task, "Invalid parameter");
467 xbt_assert1(__SD_task_is_not_scheduled(task), "Task '%s' has already been scheduled.", SD_task_get_name(task));
468 xbt_assert0(workstation_nb > 0, "workstation_nb must be positive");
470 task->workstation_nb = workstation_nb;
473 task->computation_amount = xbt_new0(double, workstation_nb);
474 memcpy(task->computation_amount, computation_amount, sizeof(double) * workstation_nb);
476 int communication_nb = workstation_nb * workstation_nb;
477 task->communication_amount = xbt_new0(double, communication_nb);
478 memcpy(task->communication_amount, communication_amount, sizeof(double) * communication_nb);
480 /* we have to create a Surf workstation array instead of the SimDag workstation array */
481 task->workstation_list = xbt_new0(void*, workstation_nb);
483 for (i = 0; i < workstation_nb; i++) {
484 task->workstation_list[i] = workstation_list[i]->surf_workstation;
487 /* update the task state */
488 if (xbt_dynar_length(task->tasks_before) == 0)
489 __SD_task_set_state(task, SD_READY);
491 __SD_task_set_state(task, SD_SCHEDULED);
495 * \brief Unschedules a task
497 * The task state must be #SD_SCHEDULED, #SD_READY, #SD_RUNNING or #SD_FAILED.
498 * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
499 * Call SD_task_schedule() to schedule it again.
501 * \param task the task you want to unschedule
502 * \see SD_task_schedule()
504 void SD_task_unschedule(SD_task_t task) {
505 SD_CHECK_INIT_DONE();
506 xbt_assert0(task != NULL, "Invalid parameter");
507 xbt_assert1(task->state_set == sd_global->scheduled_task_set ||
508 task->state_set == sd_global->ready_task_set ||
509 task->state_set == sd_global->running_task_set ||
510 task->state_set == sd_global->failed_task_set,
511 "Task %s: the state must be SD_SCHEDULED, SD_READY, SD_RUNNING or SD_FAILED",
512 SD_task_get_name(task));
514 if (__SD_task_is_scheduled_or_ready(task)) /* if the task is scheduled or ready */
515 __SD_task_destroy_scheduling_data(task);
517 if (__SD_task_is_running(task)) /* the task should become SD_FAILED */
518 surf_workstation_resource->common_public->action_cancel(task->surf_action);
520 __SD_task_set_state(task, SD_NOT_SCHEDULED);
521 task->remains = task->amount;
522 task->start_time = -1.0;
525 /* Destroys the data memorised by SD_task_schedule. Task state must be SD_SCHEDULED or SD_READY.
527 static void __SD_task_destroy_scheduling_data(SD_task_t task) {
528 SD_CHECK_INIT_DONE();
529 xbt_assert1(__SD_task_is_scheduled_or_ready(task),
530 "Task '%s' must be SD_SCHEDULED or SD_READY", SD_task_get_name(task));
531 xbt_free(task->workstation_list);
532 xbt_free(task->computation_amount);
533 xbt_free(task->communication_amount);
536 /* Runs a task. This function is called by SD_simulate() when a scheduled task can start
537 * (ie when its dependencies are satisfied).
539 surf_action_t __SD_task_run(SD_task_t task) {
540 SD_CHECK_INIT_DONE();
541 xbt_assert0(task != NULL, "Invalid parameter");
542 xbt_assert2(__SD_task_is_ready(task), "Task '%s' is not ready! Task state: %d",
543 SD_task_get_name(task), SD_task_get_state(task));
545 task->surf_action = surf_workstation_resource->extension_public->
546 execute_parallel_task(task->workstation_nb,
547 task->workstation_list,
548 task->computation_amount,
549 task->communication_amount,
553 DEBUG1("surf_action = %p", task->surf_action);
555 __SD_task_destroy_scheduling_data(task); /* now the scheduling data are not useful anymore */
556 __SD_task_set_state(task, SD_RUNNING);
558 return task->surf_action;
560 /* Remove all dependencies associated with a task. This function is called when the task is destroyed.
562 static void __SD_task_remove_dependencies(SD_task_t task) {
563 /* we must destroy the dependencies carefuly (with SD_dependency_remove)
564 because each one is stored twice */
565 SD_dependency_t dependency;
566 while (xbt_dynar_length(task->tasks_before) > 0) {
567 xbt_dynar_get_cpy(task->tasks_before, 0, &dependency);
568 SD_task_dependency_remove(dependency->src, dependency->dst);
571 while (xbt_dynar_length(task->tasks_after) > 0) {
572 xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
573 SD_task_dependency_remove(dependency->src, dependency->dst);
578 * \brief Returns the start time of a task
580 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
583 * \return the start time of this task
585 double SD_task_get_start_time(SD_task_t task) {
586 SD_CHECK_INIT_DONE();
587 xbt_assert0(task != NULL, "Invalid parameter");
588 if(task->surf_action)
589 return surf_workstation_resource->common_public->action_get_start_time(task->surf_action);
591 return task->start_time;
595 * \brief Returns the finish time of a task
597 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
598 * If the state is not completed yet, the returned value is an
599 * estimation of the task finish time. This value can fluctuate
600 * until the task is completed.
603 * \return the start time of this task
605 double SD_task_get_finish_time(SD_task_t task) {
606 SD_CHECK_INIT_DONE();
607 xbt_assert0(task != NULL, "Invalid parameter");
609 if(task->surf_action)
610 return surf_workstation_resource->common_public->action_get_finish_time(task->surf_action); /* should never happen as actions are destroyed right after their completion */
612 return task->finish_time;
616 * \brief Destroys a task.
618 * The user data (if any) should have been destroyed first.
620 * \param task the task you want to destroy
621 * \see SD_task_create()
623 void SD_task_destroy(SD_task_t task) {
624 SD_CHECK_INIT_DONE();
625 xbt_assert0(task != NULL, "Invalid parameter");
627 DEBUG1("Destroying task %s...", SD_task_get_name(task));
629 __SD_task_remove_dependencies(task);
631 /* if the task was scheduled or ready we have to free the scheduling parameters */
632 if (__SD_task_is_scheduled_or_ready(task))
633 __SD_task_destroy_scheduling_data(task);
635 if (task->name != NULL)
636 xbt_free(task->name);
638 if (task->surf_action != NULL)
639 surf_workstation_resource->common_public->action_free(task->surf_action);
641 xbt_dynar_free(&task->tasks_before);
642 xbt_dynar_free(&task->tasks_after);
645 DEBUG0("Task destroyed.");