1 /* Copyright (c) 2006-2016. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "src/surf/surf_interface.hpp"
8 #include "src/surf/host_interface.hpp"
9 #include "src/simdag/simdag_private.h"
10 #include "simgrid/simdag.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/dynar.h"
13 #include "src/instr/instr_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_task, sd, "Logging specific to SimDag (task)");
17 static void __SD_task_remove_dependencies(SD_task_t task);
18 static void __SD_task_destroy_scheduling_data(SD_task_t task);
20 void* SD_task_new_f(void)
22 SD_task_t task = xbt_new0(s_SD_task_t, 1);
23 task->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
24 task->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
29 void SD_task_recycle_f(void *t)
31 SD_task_t task = (SD_task_t) t;
33 /* Reset the content */
34 task->kind = SD_TASK_NOT_TYPED;
35 task->state= SD_NOT_SCHEDULED;
36 xbt_dynar_push(sd_global->initial_task_set,&task);
40 task->start_time = -1.0;
41 task->finish_time = -1.0;
42 task->surf_action = NULL;
43 task->watch_points = 0;
46 xbt_dynar_reset(task->tasks_before);
47 xbt_dynar_reset(task->tasks_after);
48 task->unsatisfied_dependencies = 0;
49 task->is_not_ready = 0;
51 /* scheduling parameters */
53 task->host_list = NULL;
54 task->flops_amount = NULL;
55 task->bytes_amount = NULL;
59 void SD_task_free_f(void *t)
61 SD_task_t task = (SD_task_t)t;
63 xbt_dynar_free(&task->tasks_before);
64 xbt_dynar_free(&task->tasks_after);
69 * \brief Creates a new task.
71 * \param name the name of the task (can be \c NULL)
72 * \param data the user data you want to associate with the task (can be \c NULL)
73 * \param amount amount of the task
74 * \return the new task
75 * \see SD_task_destroy()
77 SD_task_t SD_task_create(const char *name, void *data, double amount)
79 SD_task_t task = (SD_task_t)xbt_mallocator_get(sd_global->task_mallocator);
81 /* general information */
82 task->data = data; /* user data */
83 task->name = xbt_strdup(name);
84 task->amount = amount;
85 task->remains = amount;
90 static XBT_INLINE SD_task_t SD_task_create_sized(const char *name, void *data, double amount, int ws_count)
92 SD_task_t task = SD_task_create(name, data, amount);
93 task->bytes_amount = xbt_new0(double, ws_count * ws_count);
94 task->flops_amount = xbt_new0(double, ws_count);
95 task->host_count = ws_count;
96 task->host_list = xbt_new0(sg_host_t, ws_count);
100 /** @brief create a end-to-end communication task that can then be auto-scheduled
102 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
103 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
106 * A end-to-end communication must be scheduled on 2 hosts, and the amount specified at creation is sent from hosts[0]
109 SD_task_t SD_task_create_comm_e2e(const char *name, void *data, double amount)
111 SD_task_t res = SD_task_create_sized(name, data, amount, 2);
112 res->bytes_amount[2] = amount;
113 res->kind = SD_TASK_COMM_E2E;
118 /** @brief create a sequential computation task that can then be auto-scheduled
120 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
121 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
124 * A sequential computation must be scheduled on 1 host, and the amount specified at creation to be run on hosts[0].
126 * \param name the name of the task (can be \c NULL)
127 * \param data the user data you want to associate with the task (can be \c NULL)
128 * \param flops_amount amount of compute work to be done by the task
129 * \return the new SD_TASK_COMP_SEQ typed task
131 SD_task_t SD_task_create_comp_seq(const char *name, void *data, double flops_amount)
133 SD_task_t res = SD_task_create_sized(name, data, flops_amount, 1);
134 res->flops_amount[0] = flops_amount;
135 res->kind = SD_TASK_COMP_SEQ;
140 /** @brief create a parallel computation task that can then be auto-scheduled
142 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
143 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
146 * A parallel computation can be scheduled on any number of host.
147 * The underlying speedup model is Amdahl's law.
148 * To be auto-scheduled, \see SD_task_distribute_comp_amdahl has to be called first.
149 * \param name the name of the task (can be \c NULL)
150 * \param data the user data you want to associate with the task (can be \c NULL)
151 * \param flops_amount amount of compute work to be done by the task
152 * \param alpha purely serial fraction of the work to be done (in [0.;1.[)
153 * \return the new task
155 SD_task_t SD_task_create_comp_par_amdahl(const char *name, void *data, double flops_amount, double alpha)
157 xbt_assert(alpha < 1. && alpha >= 0., "Invalid parameter: alpha must be in [0.;1.[");
159 SD_task_t res = SD_task_create(name, data, flops_amount);
161 res->kind = SD_TASK_COMP_PAR_AMDAHL;
166 /** @brief create a complex data redistribution task that can then be auto-scheduled
168 * Auto-scheduling mean that the task can be used with SD_task_schedulev().
169 * This allows to specify the task costs at creation, and decouple them from the scheduling process where you just
170 * specify which resource should communicate.
172 * A data redistribution can be scheduled on any number of host.
173 * The assumed distribution is a 1D block distribution. Each host owns the same share of the \see amount.
174 * To be auto-scheduled, \see SD_task_distribute_comm_mxn_1d_block has to be called first.
175 * \param name the name of the task (can be \c NULL)
176 * \param data the user data you want to associate with the task (can be \c NULL)
177 * \param amount amount of data to redistribute by the task
178 * \return the new task
180 SD_task_t SD_task_create_comm_par_mxn_1d_block(const char *name, void *data, double amount)
182 SD_task_t res = SD_task_create(name, data, amount);
184 res->kind = SD_TASK_COMM_PAR_MXN_1D_BLOCK;
190 * \brief Destroys a task.
192 * The user data (if any) should have been destroyed first.
194 * \param task the task you want to destroy
195 * \see SD_task_create()
197 void SD_task_destroy(SD_task_t task)
199 XBT_DEBUG("Destroying task %s...", SD_task_get_name(task));
201 __SD_task_remove_dependencies(task);
203 if (task->state == SD_SCHEDULED || task->state == SD_RUNNABLE)
204 __SD_task_destroy_scheduling_data(task);
206 int idx = xbt_dynar_search_or_negative(sd_global->return_set, &task);
208 xbt_dynar_remove_at(sd_global->return_set, idx, NULL);
211 xbt_free(task->name);
213 if (task->surf_action != NULL)
214 task->surf_action->unref();
216 xbt_free(task->host_list);
217 xbt_free(task->bytes_amount);
218 xbt_free(task->flops_amount);
220 xbt_mallocator_release(sd_global->task_mallocator,task);
222 XBT_DEBUG("Task destroyed.");
226 * \brief Returns the user data of a task
229 * \return the user data associated with this task (can be \c NULL)
230 * \see SD_task_set_data()
232 void *SD_task_get_data(SD_task_t task)
238 * \brief Sets the user data of a task
240 * The new data can be \c NULL. The old data should have been freed first
241 * if it was not \c NULL.
244 * \param data the new data you want to associate with this task
245 * \see SD_task_get_data()
247 void SD_task_set_data(SD_task_t task, void *data)
253 * \brief Sets the rate of a task
255 * This will change the network bandwidth a task can use. This rate cannot be dynamically changed. Once the task has
256 * started, this call is ineffective. This rate depends on both the nominal bandwidth on the route onto which the task
257 * is scheduled (\see SD_task_get_current_bandwidth) and the amount of data to transfer.
259 * To divide the nominal bandwidth by 2, the rate then has to be :
260 * rate = bandwidth/(2*amount)
262 * \param task a \see SD_TASK_COMM_E2E task (end-to-end communication)
263 * \param rate the new rate you want to associate with this task.
265 void SD_task_set_rate(SD_task_t task, double rate)
267 xbt_assert(task->kind == SD_TASK_COMM_E2E, "The rate can be modified for end-to-end communications only.");
268 if(task->start_time<0) {
271 XBT_WARN("Task %p has started. Changing rate is ineffective.", task);
276 * \brief Returns the state of a task
279 * \return the current \ref e_SD_task_state_t "state" of this task:
280 * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING, #SD_DONE or #SD_FAILED
281 * \see e_SD_task_state_t
283 e_SD_task_state_t SD_task_get_state(SD_task_t task)
288 /* Changes the state of a task. Updates the sd_global->watch_point_reached flag.
290 void SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state)
294 case SD_NOT_SCHEDULED:
296 if (SD_task_get_state(task) == SD_FAILED){
297 xbt_dynar_remove_at(sd_global->completed_task_set,
298 xbt_dynar_search(sd_global->completed_task_set, &task), NULL);
299 xbt_dynar_push(sd_global->initial_task_set,&task);
303 if (SD_task_get_state(task) == SD_RUNNABLE){
304 xbt_dynar_remove_at(sd_global->executable_task_set,
305 xbt_dynar_search(sd_global->executable_task_set, &task), NULL);
306 xbt_dynar_push(sd_global->initial_task_set,&task);
310 idx = xbt_dynar_search_or_negative(sd_global->initial_task_set, &task);
312 xbt_dynar_remove_at(sd_global->initial_task_set, idx, NULL);
313 xbt_dynar_push(sd_global->executable_task_set,&task);
317 xbt_dynar_remove_at(sd_global->executable_task_set,
318 xbt_dynar_search(sd_global->executable_task_set, &task), NULL);
321 xbt_dynar_push(sd_global->completed_task_set,&task);
322 task->finish_time = task->surf_action->getFinishTime();
325 jedule_log_sd_event(task);
329 xbt_dynar_push(sd_global->completed_task_set,&task);
332 xbt_die( "Invalid state");
335 task->state = new_state;
337 if (task->watch_points & new_state) {
338 XBT_VERB("Watch point reached with task '%s'!", SD_task_get_name(task));
339 sd_global->watch_point_reached = 1;
340 SD_task_unwatch(task, new_state); /* remove the watch point */
345 * \brief Returns the name of a task
348 * \return the name of this task (can be \c NULL)
350 const char *SD_task_get_name(SD_task_t task)
355 /** @brief Allows to change the name of a task */
356 void SD_task_set_name(SD_task_t task, const char *name)
358 xbt_free(task->name);
359 task->name = xbt_strdup(name);
362 /** @brief Returns the dynar of the parents of a task
365 * \return a newly allocated dynar comprising the parents of this task
368 xbt_dynar_t SD_task_get_parents(SD_task_t task)
373 xbt_dynar_t parents = xbt_dynar_new(sizeof(SD_task_t), NULL);
374 xbt_dynar_foreach(task->tasks_before, i, dep) {
375 xbt_dynar_push(parents, &(dep->src));
380 /** @brief Returns the dynar of the parents of a task
383 * \return a newly allocated dynar comprising the parents of this task
385 xbt_dynar_t SD_task_get_children(SD_task_t task)
390 xbt_dynar_t children = xbt_dynar_new(sizeof(SD_task_t), NULL);
391 xbt_dynar_foreach(task->tasks_after, i, dep) {
392 xbt_dynar_push(children, &(dep->dst));
398 * \brief Returns the amount of workstations involved in a task
400 * Only call this on already scheduled tasks!
403 int SD_task_get_workstation_count(SD_task_t task)
405 return task->host_count;
409 * \brief Returns the list of workstations involved in a task
411 * Only call this on already scheduled tasks!
414 sg_host_t *SD_task_get_workstation_list(SD_task_t task)
416 return task->host_list;
420 * \brief Returns the total amount of work contained in a task
423 * \return the total amount of work (computation or data transfer) for this task
424 * \see SD_task_get_remaining_amount()
426 double SD_task_get_amount(SD_task_t task)
431 /** @brief Sets the total amount of work of a task
432 * For sequential typed tasks (COMP_SEQ and COMM_E2E), it also sets the appropriate values in the flops_amount and
433 * bytes_amount arrays respectively. Nothing more than modifying task->amount is done for parallel typed tasks
434 * (COMP_PAR_AMDAHL and COMM_PAR_MXN_1D_BLOCK) as the distribution of the amount of work is done at scheduling time.
437 * \param amount the new amount of work to execute
439 void SD_task_set_amount(SD_task_t task, double amount)
441 task->amount = amount;
442 if (task->kind == SD_TASK_COMP_SEQ)
443 task->flops_amount[0] = amount;
444 if (task->kind == SD_TASK_COMM_E2E)
445 task->bytes_amount[2] = amount;
449 * \brief Returns the alpha parameter of a SD_TASK_COMP_PAR_AMDAHL task
451 * \param task a parallel task assuming Amdahl's law as speedup model
452 * \return the alpha parameter (serial part of a task in percent) for this task
454 double SD_task_get_alpha(SD_task_t task)
456 xbt_assert(SD_task_get_kind(task) == SD_TASK_COMP_PAR_AMDAHL, "Alpha parameter is not defined for this kind of task");
462 * \brief Returns the remaining amount work to do till the completion of a task
465 * \return the remaining amount of work (computation or data transfer) of this task
466 * \see SD_task_get_amount()
468 double SD_task_get_remaining_amount(SD_task_t task)
470 if (task->surf_action)
471 return task->surf_action->getRemains();
473 return task->remains;
476 e_SD_task_kind_t SD_task_get_kind(SD_task_t task)
481 /** @brief Displays debugging informations about a task */
482 void SD_task_dump(SD_task_t task)
484 unsigned int counter;
485 SD_dependency_t dependency;
487 XBT_INFO("Displaying task %s", SD_task_get_name(task));
488 char *statename = bprintf("%s%s%s%s%s%s%s",
489 (task->state == SD_NOT_SCHEDULED ? " not scheduled" : ""),
490 (task->state == SD_SCHEDULABLE ? " schedulable" : ""),
491 (task->state == SD_SCHEDULED ? " scheduled" : ""),
492 (task->state == SD_RUNNABLE ? " runnable" : " not runnable"),
493 (task->state == SD_RUNNING ? " running" : ""),
494 (task->state == SD_DONE ? " done" : ""),
495 (task->state == SD_FAILED ? " failed" : ""));
496 XBT_INFO(" - state:%s", statename);
499 if (task->kind != 0) {
500 switch (task->kind) {
501 case SD_TASK_COMM_E2E:
502 XBT_INFO(" - kind: end-to-end communication");
504 case SD_TASK_COMP_SEQ:
505 XBT_INFO(" - kind: sequential computation");
507 case SD_TASK_COMP_PAR_AMDAHL:
508 XBT_INFO(" - kind: parallel computation following Amdahl's law");
510 case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
511 XBT_INFO(" - kind: MxN data redistribution assuming 1D block distribution");
514 XBT_INFO(" - (unknown kind %d)", task->kind);
519 XBT_INFO(" - tracing category: %s", task->category);
521 XBT_INFO(" - amount: %.0f", SD_task_get_amount(task));
522 if (task->kind == SD_TASK_COMP_PAR_AMDAHL)
523 XBT_INFO(" - alpha: %.2f", task->alpha);
524 XBT_INFO(" - Dependencies to satisfy: %d", task->unsatisfied_dependencies);
525 if (!xbt_dynar_is_empty(task->tasks_before)) {
526 XBT_INFO(" - pre-dependencies:");
527 xbt_dynar_foreach(task->tasks_before, counter, dependency) {
528 XBT_INFO(" %s", SD_task_get_name(dependency->src));
531 if (!xbt_dynar_is_empty(task->tasks_after)) {
532 XBT_INFO(" - post-dependencies:");
533 xbt_dynar_foreach(task->tasks_after, counter, dependency) {
534 XBT_INFO(" %s", SD_task_get_name(dependency->dst));
539 /** @brief Dumps the task in dotty formalism into the FILE* passed as second argument */
540 void SD_task_dotty(SD_task_t task, void *out)
542 unsigned int counter;
543 SD_dependency_t dependency;
544 FILE *fout = (FILE*)out;
545 fprintf(fout, " T%p [label=\"%.20s\"", task, task->name);
546 switch (task->kind) {
547 case SD_TASK_COMM_E2E:
548 case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
549 fprintf(fout, ", shape=box");
551 case SD_TASK_COMP_SEQ:
552 case SD_TASK_COMP_PAR_AMDAHL:
553 fprintf(fout, ", shape=circle");
556 xbt_die("Unknown task type!");
558 fprintf(fout, "];\n");
559 xbt_dynar_foreach(task->tasks_before, counter, dependency) {
560 fprintf(fout, " T%p -> T%p;\n", dependency->src, dependency->dst);
564 /* Destroys a dependency between two tasks.
566 static void __SD_task_dependency_destroy(void *dependency)
568 xbt_free(((SD_dependency_t)dependency)->name);
569 xbt_free(dependency);
573 * \brief Adds a dependency between two tasks
575 * \a dst will depend on \a src, ie \a dst will not start before \a src is finished.
576 * Their \ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_RUNNABLE.
578 * \param name the name of the new dependency (can be \c NULL)
579 * \param data the user data you want to associate with this dependency (can be \c NULL)
580 * \param src the task which must be executed first
581 * \param dst the task you want to make depend on \a src
582 * \see SD_task_dependency_remove()
584 void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task_t dst)
587 SD_dependency_t dependency;
589 unsigned long length = xbt_dynar_length(src->tasks_after);
592 THROWF(arg_error, 0, "Cannot add a dependency between task '%s' and itself", SD_task_get_name(src));
594 e_SD_task_state_t state = SD_task_get_state(src);
595 if (state != SD_NOT_SCHEDULED && state != SD_SCHEDULABLE && state != SD_RUNNING && state != SD_SCHEDULED &&
596 state != SD_RUNNABLE)
597 THROWF(arg_error, 0, "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, SD_RUNNABLE, or SD_RUNNING",
598 SD_task_get_name(src));
600 state = SD_task_get_state(dst);
601 if (state != SD_NOT_SCHEDULED && state != SD_SCHEDULABLE && state != SD_SCHEDULED && state != SD_RUNNABLE)
602 THROWF(arg_error, 0, "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, or SD_RUNNABLE",
603 SD_task_get_name(dst));
605 XBT_DEBUG("SD_task_dependency_add: src = %s, dst = %s", SD_task_get_name(src), SD_task_get_name(dst));
606 for (unsigned long i = 0; i < length && !found; i++) {
607 xbt_dynar_get_cpy(src->tasks_after, i, &dependency);
608 found = (dependency->dst == dst);
609 XBT_DEBUG("Dependency %lu: dependency->dst = %s", i, SD_task_get_name(dependency->dst));
613 THROWF(arg_error, 0, "A dependency already exists between task '%s' and task '%s'",
614 SD_task_get_name(src), SD_task_get_name(dst));
616 dependency = xbt_new(s_SD_dependency_t, 1);
618 dependency->name = xbt_strdup(name); /* xbt_strdup is cleaver enough to deal with NULL args itself */
619 dependency->data = data;
620 dependency->src = src;
621 dependency->dst = dst;
623 /* src must be executed before dst */
624 xbt_dynar_push(src->tasks_after, &dependency);
625 xbt_dynar_push(dst->tasks_before, &dependency);
627 dst->unsatisfied_dependencies++;
630 /* if the task was runnable, then dst->tasks_before is not empty anymore, so we must go back to state SD_SCHEDULED */
631 if (SD_task_get_state(dst) == SD_RUNNABLE) {
632 XBT_DEBUG("SD_task_dependency_add: %s was runnable and becomes scheduled!", SD_task_get_name(dst));
633 SD_task_set_state(dst, SD_SCHEDULED);
638 * \brief Returns the name given as input when dependency has been created..
641 * \param dst a task depending on \a src
644 const char *SD_task_dependency_get_name(SD_task_t src, SD_task_t dst){
646 SD_dependency_t dependency;
648 xbt_dynar_foreach(src->tasks_after, i, dependency){
649 if (dependency->dst == dst)
650 return dependency->name;
656 * \brief Indicates whether there is a dependency between two tasks.
659 * \param dst a task depending on \a src
661 * If src is NULL, checks whether dst has any pre-dependency.
662 * If dst is NULL, checks whether src has any post-dependency.
664 int SD_task_dependency_exists(SD_task_t src, SD_task_t dst)
666 xbt_assert(src != NULL || dst != NULL, "Invalid parameter: both src and dst are NULL");
670 unsigned int counter;
671 SD_dependency_t dependency;
672 xbt_dynar_foreach(src->tasks_after, counter, dependency) {
673 if (dependency->dst == dst)
677 return xbt_dynar_length(src->tasks_after);
680 return xbt_dynar_length(dst->tasks_before);
686 * \brief Remove a dependency between two tasks
689 * \param dst a task depending on \a src
690 * \see SD_task_dependency_add()
692 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst)
694 unsigned long length;
696 SD_dependency_t dependency;
698 /* remove the dependency from src->tasks_after */
699 length = xbt_dynar_length(src->tasks_after);
701 for (unsigned long i = 0; i < length && !found; i++) {
702 xbt_dynar_get_cpy(src->tasks_after, i, &dependency);
703 if (dependency->dst == dst) {
704 xbt_dynar_remove_at(src->tasks_after, i, NULL);
709 THROWF(arg_error, 0, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
710 SD_task_get_name(src), SD_task_get_name(dst), SD_task_get_name(dst), SD_task_get_name(src));
712 /* remove the dependency from dst->tasks_before */
713 length = xbt_dynar_length(dst->tasks_before);
716 for (unsigned long i = 0; i < length && !found; i++) {
717 xbt_dynar_get_cpy(dst->tasks_before, i, &dependency);
718 if (dependency->src == src) {
719 xbt_dynar_remove_at(dst->tasks_before, i, NULL);
720 __SD_task_dependency_destroy(dependency);
721 dst->unsatisfied_dependencies--;
726 /* should never happen... */
727 xbt_assert(found, "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
728 SD_task_get_name(dst), SD_task_get_name(src), SD_task_get_name(src), SD_task_get_name(dst));
730 /* if the task was scheduled and dst->tasks_before is empty now, we can make it runnable */
731 if (dst->unsatisfied_dependencies == 0) {
732 if (SD_task_get_state(dst) == SD_SCHEDULED)
733 SD_task_set_state(dst, SD_RUNNABLE);
735 SD_task_set_state(dst, SD_SCHEDULABLE);
738 if (dst->is_not_ready == 0)
739 SD_task_set_state(dst, SD_SCHEDULABLE);
743 * \brief Returns the user data associated with a dependency between two tasks
746 * \param dst a task depending on \a src
747 * \return the user data associated with this dependency (can be \c NULL)
748 * \see SD_task_dependency_add()
750 void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst)
753 SD_dependency_t dependency;
755 unsigned long length = xbt_dynar_length(src->tasks_after);
757 for (unsigned long i = 0; i < length && !found; i++) {
758 xbt_dynar_get_cpy(src->tasks_after, i, &dependency);
759 found = (dependency->dst == dst);
762 THROWF(arg_error, 0, "No dependency found between task '%s' and '%s'",
763 SD_task_get_name(src), SD_task_get_name(dst));
764 return dependency->data;
768 * \brief Adds a watch point to a task
770 * SD_simulate() will stop as soon as the \ref e_SD_task_state_t "state" of this task becomes the one given in argument.
771 * The watch point is then automatically removed.
774 * \param state the \ref e_SD_task_state_t "state" you want to watch (cannot be #SD_NOT_SCHEDULED)
775 * \see SD_task_unwatch()
777 void SD_task_watch(SD_task_t task, e_SD_task_state_t state)
779 if (state & SD_NOT_SCHEDULED)
780 THROWF(arg_error, 0, "Cannot add a watch point for state SD_NOT_SCHEDULED");
782 task->watch_points = task->watch_points | state;
786 * \brief Removes a watch point from a task
789 * \param state the \ref e_SD_task_state_t "state" you no longer want to watch
790 * \see SD_task_watch()
792 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state)
794 xbt_assert(state != SD_NOT_SCHEDULED, "SimDag error: Cannot have a watch point for state SD_NOT_SCHEDULED");
795 task->watch_points = task->watch_points & ~state;
799 * \brief Returns an approximative estimation of the execution time of a task.
801 * The estimation is very approximative because the value returned is the time the task would take if it was executed
802 * now and if it was the only task.
804 * \param task the task to evaluate
805 * \param workstation_nb number of workstations on which the task would be executed
806 * \param workstation_list the workstations on which the task would be executed
807 * \param flops_amount computation amount for each workstation
808 * \param bytes_amount communication amount between each pair of workstations
811 double SD_task_get_execution_time(SD_task_t task, int workstation_nb, const sg_host_t *workstation_list,
812 const double *flops_amount, const double *bytes_amount)
814 xbt_assert(workstation_nb > 0, "Invalid parameter");
815 double max_time = 0.0;
817 /* the task execution time is the maximum execution time of the parallel tasks */
818 for (int i = 0; i < workstation_nb; i++) {
820 if (flops_amount != NULL)
821 time = flops_amount[i] / sg_host_speed(workstation_list[i]);
823 if (bytes_amount != NULL)
824 for (int j = 0; j < workstation_nb; j++) {
825 if (bytes_amount[i * workstation_nb + j] !=0 ) {
826 time += (SD_route_get_latency(workstation_list[i], workstation_list[j]) +
827 bytes_amount[i * workstation_nb + j] /
828 SD_route_get_bandwidth(workstation_list[i], workstation_list[j]));
832 if (time > max_time) {
839 static XBT_INLINE void SD_task_do_schedule(SD_task_t task)
841 if (SD_task_get_state(task) > SD_SCHEDULABLE)
842 THROWF(arg_error, 0, "Task '%s' has already been scheduled", SD_task_get_name(task));
844 if (task->unsatisfied_dependencies == 0)
845 SD_task_set_state(task, SD_RUNNABLE);
847 SD_task_set_state(task, SD_SCHEDULED);
851 * \brief Schedules a task
853 * The task state must be #SD_NOT_SCHEDULED.
854 * Once scheduled, a task is executed as soon as possible in \see SD_simulate, i.e. when its dependencies are satisfied.
856 * \param task the task you want to schedule
857 * \param host_count number of workstations on which the task will be executed
858 * \param workstation_list the workstations on which the task will be executed
859 * \param flops_amount computation amount for each workstation
860 * \param bytes_amount communication amount between each pair of workstations
861 * \param rate task execution speed rate
862 * \see SD_task_unschedule()
864 void SD_task_schedule(SD_task_t task, int host_count, const sg_host_t * workstation_list,
865 const double *flops_amount, const double *bytes_amount, double rate)
867 xbt_assert(host_count > 0, "workstation_nb must be positive");
869 task->host_count = host_count;
873 task->flops_amount = (double*)xbt_realloc(task->flops_amount, sizeof(double) * host_count);
874 memcpy(task->flops_amount, flops_amount, sizeof(double) * host_count);
876 xbt_free(task->flops_amount);
877 task->flops_amount = NULL;
880 int communication_nb = host_count * host_count;
882 task->bytes_amount = (double*)xbt_realloc(task->bytes_amount, sizeof(double) * communication_nb);
883 memcpy(task->bytes_amount, bytes_amount, sizeof(double) * communication_nb);
885 xbt_free(task->bytes_amount);
886 task->bytes_amount = NULL;
889 task->host_list = (sg_host_t*) xbt_realloc(task->host_list, sizeof(sg_host_t) * host_count);
890 memcpy(task->host_list, workstation_list, sizeof(sg_host_t) * host_count);
892 SD_task_do_schedule(task);
896 * \brief Unschedules a task
898 * The task state must be #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING or #SD_FAILED.
899 * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
900 * Call SD_task_schedule() to schedule it again.
902 * \param task the task you want to unschedule
903 * \see SD_task_schedule()
905 void SD_task_unschedule(SD_task_t task)
907 if (task->state != SD_SCHEDULED && task->state != SD_RUNNABLE && task->state != SD_RUNNING &&
908 task->state != SD_FAILED)
909 THROWF(arg_error, 0, "Task %s: the state must be SD_SCHEDULED, SD_RUNNABLE, SD_RUNNING or SD_FAILED",
910 SD_task_get_name(task));
912 if ((task->state == SD_SCHEDULED || task->state == SD_RUNNABLE)
913 /* if the task is scheduled or runnable */
914 && ((task->kind == SD_TASK_COMP_PAR_AMDAHL) || (task->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK))) {
915 /* Don't free scheduling data for typed tasks */
916 __SD_task_destroy_scheduling_data(task);
917 xbt_free(task->host_list);
918 task->host_list=NULL;
919 task->host_count = 0;
922 if (SD_task_get_state(task) == SD_RUNNING)
923 /* the task should become SD_FAILED */
924 task->surf_action->cancel();
926 if (task->unsatisfied_dependencies == 0)
927 SD_task_set_state(task, SD_SCHEDULABLE);
929 SD_task_set_state(task, SD_NOT_SCHEDULED);
931 task->remains = task->amount;
932 task->start_time = -1.0;
935 /* Destroys the data memorized by SD_task_schedule. Task state must be SD_SCHEDULED or SD_RUNNABLE.
937 static void __SD_task_destroy_scheduling_data(SD_task_t task)
939 if (task->state != SD_SCHEDULED && task->state != SD_RUNNABLE)
940 THROWF(arg_error, 0, "Task '%s' must be SD_SCHEDULED or SD_RUNNABLE", SD_task_get_name(task));
942 xbt_free(task->flops_amount);
943 xbt_free(task->bytes_amount);
944 task->flops_amount = task->bytes_amount = NULL;
948 void SD_task_run(SD_task_t task)
950 xbt_assert(SD_task_get_state(task) == SD_RUNNABLE, "Task '%s' is not runnable! Task state: %d",
951 SD_task_get_name(task), (int)SD_task_get_state(task));
952 xbt_assert(task->host_list != NULL, "Task '%s': workstation_list is NULL!", SD_task_get_name(task));
954 XBT_DEBUG("Running task '%s'", SD_task_get_name(task));
956 /* Copy the elements of the task into the action */
957 int host_nb = task->host_count;
958 sg_host_t *hosts = xbt_new(sg_host_t, host_nb);
960 for (int i = 0; i < host_nb; i++)
961 hosts[i] = task->host_list[i];
963 double *flops_amount = xbt_new0(double, host_nb);
964 double *bytes_amount = xbt_new0(double, host_nb * host_nb);
966 if(task->flops_amount)
967 memcpy(flops_amount, task->flops_amount, sizeof(double) * host_nb);
968 if(task->bytes_amount)
969 memcpy(bytes_amount, task->bytes_amount, sizeof(double) * host_nb * host_nb);
971 task->surf_action = surf_host_model->executeParallelTask(host_nb, hosts, flops_amount, bytes_amount, task->rate);
973 task->surf_action->setData(task);
975 XBT_DEBUG("surf_action = %p", task->surf_action);
978 TRACE_surf_action(task->surf_action, task->category);
980 __SD_task_destroy_scheduling_data(task); /* now the scheduling data are not useful anymore */
981 SD_task_set_state(task, SD_RUNNING);
985 * Remove all dependencies associated with a task. This function is called when the task is destroyed.
987 static void __SD_task_remove_dependencies(SD_task_t task)
989 /* we must destroy the dependencies carefuly (with SD_dependency_remove) because each one is stored twice */
990 SD_dependency_t dependency;
991 while (!xbt_dynar_is_empty(task->tasks_before)) {
992 xbt_dynar_get_cpy(task->tasks_before, 0, &dependency);
993 SD_task_dependency_remove(dependency->src, dependency->dst);
996 while (!xbt_dynar_is_empty(task->tasks_after)) {
997 xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
998 SD_task_dependency_remove(dependency->src, dependency->dst);
1003 * \brief Returns the start time of a task
1005 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
1007 * \param task: a task
1008 * \return the start time of this task
1010 double SD_task_get_start_time(SD_task_t task)
1012 if (task->surf_action)
1013 return task->surf_action->getStartTime();
1015 return task->start_time;
1019 * \brief Returns the finish time of a task
1021 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
1022 * If the state is not completed yet, the returned value is an estimation of the task finish time. This value can
1023 * vary until the task is completed.
1025 * \param task: a task
1026 * \return the start time of this task
1028 double SD_task_get_finish_time(SD_task_t task)
1030 if (task->surf_action) /* should never happen as actions are destroyed right after their completion */
1031 return task->surf_action->getFinishTime();
1033 return task->finish_time;
1036 void SD_task_distribute_comp_amdahl(SD_task_t task, int ws_count)
1038 xbt_assert(task->kind == SD_TASK_COMP_PAR_AMDAHL, "Task %s is not a SD_TASK_COMP_PAR_AMDAHL typed task."
1039 "Cannot use this function.", SD_task_get_name(task));
1040 task->flops_amount = xbt_new0(double, ws_count);
1041 task->bytes_amount = xbt_new0(double, ws_count * ws_count);
1042 xbt_free(task->host_list);
1043 task->host_count = ws_count;
1044 task->host_list = xbt_new0(sg_host_t, ws_count);
1046 for(int i=0;i<ws_count;i++){
1047 task->flops_amount[i] = (task->alpha + (1 - task->alpha)/ws_count) * task->amount;
1052 /** @brief Auto-schedules a task.
1054 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
1055 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
1058 * To be auto-schedulable, a task must be type and created with one of the specialized creation functions.
1061 * We should create tasks kind for the following categories:
1062 * - Point to point communication (done)
1063 * - Sequential computation (done)
1064 * - group communication (redistribution, several kinds)
1065 * - parallel tasks with no internal communication (one kind per speedup model such as Amdahl)
1066 * - idem+ internal communication. Task type not enough since we cannot store comm cost alongside to comp one)
1068 void SD_task_schedulev(SD_task_t task, int count, const sg_host_t * list)
1071 SD_dependency_t dep;
1073 xbt_assert(task->kind != 0, "Task %s is not typed. Cannot automatically schedule it.", SD_task_get_name(task));
1074 switch (task->kind) {
1075 case SD_TASK_COMP_PAR_AMDAHL:
1076 SD_task_distribute_comp_amdahl(task, count);
1077 case SD_TASK_COMM_E2E:
1078 case SD_TASK_COMP_SEQ:
1079 xbt_assert(task->host_count == count, "Got %d locations, but were expecting %d locations", count,task->host_count);
1080 for (i = 0; i < count; i++)
1081 task->host_list[i] = list[i];
1082 if (SD_task_get_kind(task)== SD_TASK_COMP_SEQ && !task->flops_amount){
1083 /*This task has failed and is rescheduled. Reset the flops_amount*/
1084 task->flops_amount = xbt_new0(double, 1);
1085 task->flops_amount[0] = task->remains;
1087 SD_task_do_schedule(task);
1090 xbt_die("Kind of task %s not supported by SD_task_schedulev()", SD_task_get_name(task));
1093 if (task->kind == SD_TASK_COMM_E2E) {
1094 XBT_VERB("Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(task),
1095 sg_host_get_name(task->host_list[0]), sg_host_get_name(task->host_list[1]), task->bytes_amount[2]);
1098 /* Iterate over all children and parents being COMM_E2E to say where I am located (and start them if runnable) */
1099 if (task->kind == SD_TASK_COMP_SEQ) {
1100 XBT_VERB("Schedule computation task %s on %s. It costs %.f flops", SD_task_get_name(task),
1101 sg_host_get_name(task->host_list[0]), task->flops_amount[0]);
1103 xbt_dynar_foreach(task->tasks_before, cpt, dep) {
1104 SD_task_t before = dep->src;
1105 if (before->kind == SD_TASK_COMM_E2E) {
1106 before->host_list[1] = task->host_list[0];
1108 if (before->host_list[0] && (SD_task_get_state(before) < SD_SCHEDULED)) {
1109 SD_task_do_schedule(before);
1110 XBT_VERB ("Auto-Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(before),
1111 sg_host_get_name(before->host_list[0]), sg_host_get_name(before->host_list[1]), before->bytes_amount[2]);
1115 xbt_dynar_foreach(task->tasks_after, cpt, dep) {
1116 SD_task_t after = dep->dst;
1117 if (after->kind == SD_TASK_COMM_E2E) {
1118 after->host_list[0] = task->host_list[0];
1119 if (after->host_list[1] && (SD_task_get_state(after) < SD_SCHEDULED)) {
1120 SD_task_do_schedule(after);
1121 XBT_VERB ("Auto-Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(after),
1122 sg_host_get_name(after->host_list[0]), sg_host_get_name(after->host_list[1]), after->bytes_amount[2]);
1127 /* Iterate over all children and parents being MXN_1D_BLOCK to say where I am located (and start them if runnable) */
1128 if (task->kind == SD_TASK_COMP_PAR_AMDAHL) {
1129 XBT_VERB("Schedule computation task %s on %d workstations. %.f flops will be distributed following Amdahl's Law",
1130 SD_task_get_name(task), task->host_count, task->flops_amount[0]);
1131 xbt_dynar_foreach(task->tasks_before, cpt, dep) {
1132 SD_task_t before = dep->src;
1133 if (before->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK){
1134 if (!before->host_list){
1135 XBT_VERB("Sender side of Task %s is not scheduled yet", SD_task_get_name(before));
1136 before->host_list = xbt_new0(sg_host_t, count);
1137 before->host_count = count;
1138 XBT_VERB("Fill the workstation list with list of Task '%s'", SD_task_get_name(task));
1139 for (i=0;i<count;i++)
1140 before->host_list[i] = task->host_list[i];
1142 XBT_VERB("Build communication matrix for task '%s'", SD_task_get_name(before));
1144 double src_start, src_end, dst_start, dst_end;
1145 src_nb = before->host_count;
1147 before->host_list = (sg_host_t*) xbt_realloc(before->host_list, (before->host_count+count)*sizeof(sg_host_t));
1148 for(i=0; i<count; i++)
1149 before->host_list[before->host_count+i] = task->host_list[i];
1151 before->host_count += count;
1152 xbt_free(before->flops_amount);
1153 xbt_free(before->bytes_amount);
1154 before->flops_amount = xbt_new0(double, before->host_count);
1155 before->bytes_amount = xbt_new0(double, before->host_count* before->host_count);
1157 for(i=0;i<src_nb;i++){
1158 src_start = i*before->amount/src_nb;
1159 src_end = src_start + before->amount/src_nb;
1160 for(j=0; j<dst_nb; j++){
1161 dst_start = j*before->amount/dst_nb;
1162 dst_end = dst_start + before->amount/dst_nb;
1163 XBT_VERB("(%s->%s): (%.2f, %.2f)-> (%.2f, %.2f)", sg_host_get_name(before->host_list[i]),
1164 sg_host_get_name(before->host_list[src_nb+j]), src_start, src_end, dst_start, dst_end);
1165 if ((src_end <= dst_start) || (dst_end <= src_start)) {
1166 before->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]=0.0;
1168 before->bytes_amount[i*(src_nb+dst_nb)+src_nb+j] = MIN(src_end, dst_end) - MAX(src_start, dst_start);
1170 XBT_VERB("==> %.2f", before->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]);
1174 if (SD_task_get_state(before)< SD_SCHEDULED) {
1175 SD_task_do_schedule(before);
1177 ("Auto-Schedule redistribution task %s. Send %.f bytes from %d hosts to %d hosts.",
1178 SD_task_get_name(before),before->amount, src_nb, dst_nb);
1183 xbt_dynar_foreach(task->tasks_after, cpt, dep) {
1184 SD_task_t after = dep->dst;
1185 if (after->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK){
1186 if (!after->host_list){
1187 XBT_VERB("Receiver side of Task '%s' is not scheduled yet", SD_task_get_name(after));
1188 after->host_list = xbt_new0(sg_host_t, count);
1189 after->host_count = count;
1190 XBT_VERB("Fill the workstation list with list of Task '%s'", SD_task_get_name(task));
1191 for (i=0;i<count;i++)
1192 after->host_list[i] = task->host_list[i];
1195 double src_start, src_end, dst_start, dst_end;
1197 dst_nb = after->host_count;
1198 after->host_list = (sg_host_t*) xbt_realloc(after->host_list, (after->host_count+count)*sizeof(sg_host_t));
1199 for(i=after->host_count - 1; i>=0; i--)
1200 after->host_list[count+i] = after->host_list[i];
1201 for(i=0; i<count; i++)
1202 after->host_list[i] = task->host_list[i];
1204 after->host_count += count;
1206 xbt_free(after->flops_amount);
1207 xbt_free(after->bytes_amount);
1209 after->flops_amount = xbt_new0(double, after->host_count);
1210 after->bytes_amount = xbt_new0(double, after->host_count* after->host_count);
1212 for(i=0;i<src_nb;i++){
1213 src_start = i*after->amount/src_nb;
1214 src_end = src_start + after->amount/src_nb;
1215 for(j=0; j<dst_nb; j++){
1216 dst_start = j*after->amount/dst_nb;
1217 dst_end = dst_start + after->amount/dst_nb;
1218 XBT_VERB("(%d->%d): (%.2f, %.2f)-> (%.2f, %.2f)", i, j, src_start, src_end, dst_start, dst_end);
1219 if ((src_end <= dst_start) || (dst_end <= src_start)) {
1220 after->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]=0.0;
1222 after->bytes_amount[i*(src_nb+dst_nb)+src_nb+j] = MIN(src_end, dst_end)- MAX(src_start, dst_start);
1224 XBT_VERB("==> %.2f", after->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]);
1228 if (SD_task_get_state(after)< SD_SCHEDULED) {
1229 SD_task_do_schedule(after);
1230 XBT_VERB ("Auto-Schedule redistribution task %s. Send %.f bytes from %d hosts to %d hosts.",
1231 SD_task_get_name(after),after->amount, src_nb, dst_nb);
1239 /** @brief autoschedule a task on a list of workstations
1241 * This function is very similar to SD_task_schedulev(), but takes the list of workstations to schedule onto as
1242 * separate parameters.
1243 * It builds a proper vector of workstations and then call SD_task_schedulev()
1245 void SD_task_schedulel(SD_task_t task, int count, ...)
1248 sg_host_t *list = xbt_new(sg_host_t, count);
1249 va_start(ap, count);
1250 for (int i = 0; i < count; i++) {
1251 list[i] = va_arg(ap, sg_host_t);
1254 SD_task_schedulev(task, count, list);