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/HostImpl.hpp"
8 #include "src/surf/surf_interface.hpp"
9 #include "src/simdag/simdag_private.h"
10 #include "simgrid/simdag.h"
11 #include "src/instr/instr_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_task, sd, "Logging specific to SimDag (task)");
15 /* Destroys the data memorized by SD_task_schedule. Task state must be SD_SCHEDULED or SD_RUNNABLE. */
16 static void __SD_task_destroy_scheduling_data(SD_task_t task)
18 if (task->state != SD_SCHEDULED && task->state != SD_RUNNABLE)
19 THROWF(arg_error, 0, "Task '%s' must be SD_SCHEDULED or SD_RUNNABLE", SD_task_get_name(task));
21 xbt_free(task->flops_amount);
22 xbt_free(task->bytes_amount);
23 task->flops_amount = nullptr;
24 task->bytes_amount = nullptr;
29 SD_task_t task = xbt_new0(s_SD_task_t, 1);
31 task->inputs = new std::set<SD_task_t>();
32 task->outputs = new std::set<SD_task_t>();
33 task->predecessors = new std::set<SD_task_t>();
34 task->successors = new std::set<SD_task_t>();
38 void SD_task_recycle_f(void *t)
40 SD_task_t task = static_cast<SD_task_t>(t);
42 /* Reset the content */
43 task->kind = SD_TASK_NOT_TYPED;
44 task->state= SD_NOT_SCHEDULED;
45 sd_global->initial_tasks->insert(task);
49 task->start_time = -1.0;
50 task->finish_time = -1.0;
51 task->surf_action = nullptr;
52 task->watch_points = 0;
55 task->inputs->clear();
56 task->outputs->clear();
57 task->predecessors->clear();
58 task->successors->clear();
60 /* scheduling parameters */
62 task->host_list = nullptr;
63 task->flops_amount = nullptr;
64 task->bytes_amount = nullptr;
68 void SD_task_free_f(void *t)
70 SD_task_t task = static_cast<SD_task_t>(t);
74 delete task->predecessors;
75 delete task->successors;
81 * \brief Creates a new task.
83 * \param name the name of the task (can be \c nullptr)
84 * \param data the user data you want to associate with the task (can be \c nullptr)
85 * \param amount amount of the task
86 * \return the new task
87 * \see SD_task_destroy()
89 SD_task_t SD_task_create(const char *name, void *data, double amount)
91 SD_task_t task = static_cast<SD_task_t>(xbt_mallocator_get(sd_global->task_mallocator));
94 task->name = xbt_strdup(name);
95 task->amount = amount;
96 task->remains = amount;
101 static inline SD_task_t SD_task_create_sized(const char *name, void *data, double amount, int ws_count)
103 SD_task_t task = SD_task_create(name, data, amount);
104 task->bytes_amount = xbt_new0(double, ws_count * ws_count);
105 task->flops_amount = xbt_new0(double, ws_count);
106 task->host_count = ws_count;
107 task->host_list = xbt_new0(sg_host_t, ws_count);
111 /** @brief create a end-to-end communication task that can then be auto-scheduled
113 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
114 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
117 * A end-to-end communication must be scheduled on 2 hosts, and the amount specified at creation is sent from hosts[0]
120 SD_task_t SD_task_create_comm_e2e(const char *name, void *data, double amount)
122 SD_task_t res = SD_task_create_sized(name, data, amount, 2);
123 res->bytes_amount[2] = amount;
124 res->kind = SD_TASK_COMM_E2E;
129 /** @brief create a sequential computation task that can then be auto-scheduled
131 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
132 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
135 * A sequential computation must be scheduled on 1 host, and the amount specified at creation to be run on hosts[0].
137 * \param name the name of the task (can be \c nullptr)
138 * \param data the user data you want to associate with the task (can be \c nullptr)
139 * \param flops_amount amount of compute work to be done by the task
140 * \return the new SD_TASK_COMP_SEQ typed task
142 SD_task_t SD_task_create_comp_seq(const char *name, void *data, double flops_amount)
144 SD_task_t res = SD_task_create_sized(name, data, flops_amount, 1);
145 res->flops_amount[0] = flops_amount;
146 res->kind = SD_TASK_COMP_SEQ;
151 /** @brief create a parallel computation task that can then be auto-scheduled
153 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
154 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
157 * A parallel computation can be scheduled on any number of host.
158 * The underlying speedup model is Amdahl's law.
159 * To be auto-scheduled, \see SD_task_distribute_comp_amdahl has to be called first.
160 * \param name the name of the task (can be \c nullptr)
161 * \param data the user data you want to associate with the task (can be \c nullptr)
162 * \param flops_amount amount of compute work to be done by the task
163 * \param alpha purely serial fraction of the work to be done (in [0.;1.[)
164 * \return the new task
166 SD_task_t SD_task_create_comp_par_amdahl(const char *name, void *data, double flops_amount, double alpha)
168 xbt_assert(alpha < 1. && alpha >= 0., "Invalid parameter: alpha must be in [0.;1.[");
170 SD_task_t res = SD_task_create(name, data, flops_amount);
172 res->kind = SD_TASK_COMP_PAR_AMDAHL;
177 /** @brief create a complex data redistribution task that can then be auto-scheduled
179 * Auto-scheduling mean that the task can be used with SD_task_schedulev().
180 * This allows to specify the task costs at creation, and decouple them from the scheduling process where you just
181 * specify which resource should communicate.
183 * A data redistribution can be scheduled on any number of host.
184 * The assumed distribution is a 1D block distribution. Each host owns the same share of the \see amount.
185 * To be auto-scheduled, \see SD_task_distribute_comm_mxn_1d_block has to be called first.
186 * \param name the name of the task (can be \c nullptr)
187 * \param data the user data you want to associate with the task (can be \c nullptr)
188 * \param amount amount of data to redistribute by the task
189 * \return the new task
191 SD_task_t SD_task_create_comm_par_mxn_1d_block(const char *name, void *data, double amount)
193 SD_task_t res = SD_task_create(name, data, amount);
194 res->host_list=nullptr;
195 res->kind = SD_TASK_COMM_PAR_MXN_1D_BLOCK;
201 * \brief Destroys a task.
203 * The user data (if any) should have been destroyed first.
205 * \param task the task you want to destroy
206 * \see SD_task_create()
208 void SD_task_destroy(SD_task_t task)
210 XBT_DEBUG("Destroying task %s...", SD_task_get_name(task));
212 /* First Remove all dependencies associated with the task. */
213 while (!task->predecessors->empty())
214 SD_task_dependency_remove(*(task->predecessors->begin()), task);
215 while (!task->inputs->empty())
216 SD_task_dependency_remove(*(task->inputs->begin()), task);
217 while (!task->successors->empty())
218 SD_task_dependency_remove(task, *(task->successors->begin()));
219 while (!task->outputs->empty())
220 SD_task_dependency_remove(task, *(task->outputs->begin()));
222 if (task->state == SD_SCHEDULED || task->state == SD_RUNNABLE)
223 __SD_task_destroy_scheduling_data(task);
225 int idx = xbt_dynar_search_or_negative(sd_global->return_set, &task);
227 xbt_dynar_remove_at(sd_global->return_set, idx, nullptr);
230 xbt_free(task->name);
232 if (task->surf_action != nullptr)
233 task->surf_action->unref();
235 xbt_free(task->host_list);
236 xbt_free(task->bytes_amount);
237 xbt_free(task->flops_amount);
239 xbt_mallocator_release(sd_global->task_mallocator,task);
241 XBT_DEBUG("Task destroyed.");
245 * \brief Returns the user data of a task
248 * \return the user data associated with this task (can be \c nullptr)
249 * \see SD_task_set_data()
251 void *SD_task_get_data(SD_task_t task)
257 * \brief Sets the user data of a task
259 * The new data can be \c nullptr. The old data should have been freed first
260 * if it was not \c nullptr.
263 * \param data the new data you want to associate with this task
264 * \see SD_task_get_data()
266 void SD_task_set_data(SD_task_t task, void *data)
272 * \brief Sets the rate of a task
274 * This will change the network bandwidth a task can use. This rate cannot be dynamically changed. Once the task has
275 * started, this call is ineffective. This rate depends on both the nominal bandwidth on the route onto which the task
276 * is scheduled (\see SD_task_get_current_bandwidth) and the amount of data to transfer.
278 * To divide the nominal bandwidth by 2, the rate then has to be :
279 * rate = bandwidth/(2*amount)
281 * \param task a \see SD_TASK_COMM_E2E task (end-to-end communication)
282 * \param rate the new rate you want to associate with this task.
284 void SD_task_set_rate(SD_task_t task, double rate)
286 xbt_assert(task->kind == SD_TASK_COMM_E2E, "The rate can be modified for end-to-end communications only.");
287 if(task->state < SD_RUNNING) {
290 XBT_WARN("Task %p has started. Changing rate is ineffective.", task);
295 * \brief Returns the state of a task
298 * \return the current \ref e_SD_task_state_t "state" of this task:
299 * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING, #SD_DONE or #SD_FAILED
300 * \see e_SD_task_state_t
302 e_SD_task_state_t SD_task_get_state(SD_task_t task)
307 /* Changes the state of a task. Updates the sd_global->watch_point_reached flag.
309 void SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state)
311 std::set<SD_task_t>::iterator idx;
312 XBT_DEBUG("Set state of '%s' to %d", task->name, new_state);
313 if ((new_state == SD_NOT_SCHEDULED || new_state == SD_SCHEDULABLE) && task->state == SD_FAILED){
314 sd_global->completed_tasks->erase(task);
315 sd_global->initial_tasks->insert(task);
318 if (new_state == SD_SCHEDULED && task->state == SD_RUNNABLE){
319 sd_global->initial_tasks->insert(task);
320 sd_global->runnable_tasks->erase(task);
323 if (new_state == SD_RUNNABLE){
324 idx = sd_global->initial_tasks->find(task);
325 if (idx != sd_global->initial_tasks->end()) {
326 sd_global->runnable_tasks->insert(*idx);
327 sd_global->initial_tasks->erase(idx);
331 if (new_state == SD_RUNNING)
332 sd_global->runnable_tasks->erase(task);
334 if (new_state == SD_DONE || new_state == SD_FAILED){
335 sd_global->completed_tasks->insert(task);
336 task->start_time = task->surf_action->getStartTime();
337 if (new_state == SD_DONE){
338 task->finish_time = task->surf_action->getFinishTime();
341 jedule_log_sd_event(task);
344 task->finish_time = surf_get_clock();
345 task->surf_action->unref();
346 task->surf_action = nullptr;
349 task->state = new_state;
351 if (task->watch_points & new_state) {
352 XBT_VERB("Watch point reached with task '%s'!", SD_task_get_name(task));
353 sd_global->watch_point_reached = true;
354 SD_task_unwatch(task, new_state); /* remove the watch point */
359 * \brief Returns the name of a task
362 * \return the name of this task (can be \c nullptr)
364 const char *SD_task_get_name(SD_task_t task)
369 /** @brief Allows to change the name of a task */
370 void SD_task_set_name(SD_task_t task, const char *name)
372 xbt_free(task->name);
373 task->name = xbt_strdup(name);
376 /** @brief Returns the dynar of the parents of a task
379 * \return a newly allocated dynar comprising the parents of this task
382 xbt_dynar_t SD_task_get_parents(SD_task_t task)
384 xbt_dynar_t parents = xbt_dynar_new(sizeof(SD_task_t), nullptr);
386 for (auto it : *task->predecessors)
387 xbt_dynar_push(parents, &it);
388 for (auto it : *task->inputs)
389 xbt_dynar_push(parents, &it);
394 /** @brief Returns the dynar of the parents of a task
397 * \return a newly allocated dynar comprising the parents of this task
399 xbt_dynar_t SD_task_get_children(SD_task_t task)
401 xbt_dynar_t children = xbt_dynar_new(sizeof(SD_task_t), nullptr);
403 for (auto it : *task->successors)
404 xbt_dynar_push(children, &it);
405 for (auto it : *task->outputs)
406 xbt_dynar_push(children, &it);
412 * \brief Returns the number of workstations involved in a task
414 * Only call this on already scheduled tasks!
417 int SD_task_get_workstation_count(SD_task_t task)
419 return task->host_count;
423 * \brief Returns the list of workstations involved in a task
425 * Only call this on already scheduled tasks!
428 sg_host_t *SD_task_get_workstation_list(SD_task_t task)
430 return task->host_list;
434 * \brief Returns the total amount of work contained in a task
437 * \return the total amount of work (computation or data transfer) for this task
438 * \see SD_task_get_remaining_amount()
440 double SD_task_get_amount(SD_task_t task)
445 /** @brief Sets the total amount of work of a task
446 * For sequential typed tasks (COMP_SEQ and COMM_E2E), it also sets the appropriate values in the flops_amount and
447 * bytes_amount arrays respectively. Nothing more than modifying task->amount is done for parallel typed tasks
448 * (COMP_PAR_AMDAHL and COMM_PAR_MXN_1D_BLOCK) as the distribution of the amount of work is done at scheduling time.
451 * \param amount the new amount of work to execute
453 void SD_task_set_amount(SD_task_t task, double amount)
455 task->amount = amount;
456 if (task->kind == SD_TASK_COMP_SEQ)
457 task->flops_amount[0] = amount;
458 if (task->kind == SD_TASK_COMM_E2E)
459 task->bytes_amount[2] = amount;
463 * \brief Returns the alpha parameter of a SD_TASK_COMP_PAR_AMDAHL task
465 * \param task a parallel task assuming Amdahl's law as speedup model
466 * \return the alpha parameter (serial part of a task in percent) for this task
468 double SD_task_get_alpha(SD_task_t task)
470 xbt_assert(SD_task_get_kind(task) == SD_TASK_COMP_PAR_AMDAHL, "Alpha parameter is not defined for this kind of task");
475 * \brief Returns the remaining amount work to do till the completion of a task
478 * \return the remaining amount of work (computation or data transfer) of this task
479 * \see SD_task_get_amount()
481 double SD_task_get_remaining_amount(SD_task_t task)
483 if (task->surf_action)
484 return task->surf_action->getRemains();
486 return task->remains;
489 e_SD_task_kind_t SD_task_get_kind(SD_task_t task)
494 /** @brief Displays debugging information about a task */
495 void SD_task_dump(SD_task_t task)
497 XBT_INFO("Displaying task %s", SD_task_get_name(task));
498 char *statename = bprintf("%s%s%s%s%s%s%s",
499 (task->state == SD_NOT_SCHEDULED ? " not scheduled" : ""),
500 (task->state == SD_SCHEDULABLE ? " schedulable" : ""),
501 (task->state == SD_SCHEDULED ? " scheduled" : ""),
502 (task->state == SD_RUNNABLE ? " runnable" : " not runnable"),
503 (task->state == SD_RUNNING ? " running" : ""),
504 (task->state == SD_DONE ? " done" : ""),
505 (task->state == SD_FAILED ? " failed" : ""));
506 XBT_INFO(" - state:%s", statename);
509 if (task->kind != 0) {
510 switch (task->kind) {
511 case SD_TASK_COMM_E2E:
512 XBT_INFO(" - kind: end-to-end communication");
514 case SD_TASK_COMP_SEQ:
515 XBT_INFO(" - kind: sequential computation");
517 case SD_TASK_COMP_PAR_AMDAHL:
518 XBT_INFO(" - kind: parallel computation following Amdahl's law");
520 case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
521 XBT_INFO(" - kind: MxN data redistribution assuming 1D block distribution");
524 XBT_INFO(" - (unknown kind %d)", task->kind);
528 XBT_INFO(" - amount: %.0f", SD_task_get_amount(task));
529 if (task->kind == SD_TASK_COMP_PAR_AMDAHL)
530 XBT_INFO(" - alpha: %.2f", task->alpha);
531 XBT_INFO(" - Dependencies to satisfy: %zu", task->inputs->size()+ task->predecessors->size());
532 if ((task->inputs->size()+ task->predecessors->size()) > 0) {
533 XBT_INFO(" - pre-dependencies:");
534 for (auto it : *task->predecessors)
535 XBT_INFO(" %s", SD_task_get_name(it));
537 for (auto it: *task->inputs)
538 XBT_INFO(" %s", SD_task_get_name(it));
540 if ((task->outputs->size() + task->successors->size()) > 0) {
541 XBT_INFO(" - post-dependencies:");
543 for (auto it : *task->successors)
544 XBT_INFO(" %s", SD_task_get_name(it));
545 for (auto it : *task->outputs)
546 XBT_INFO(" %s", SD_task_get_name(it));
550 /** @brief Dumps the task in dotty formalism into the FILE* passed as second argument */
551 void SD_task_dotty(SD_task_t task, void *out)
553 FILE *fout = static_cast<FILE*>(out);
554 fprintf(fout, " T%p [label=\"%.20s\"", task, task->name);
555 switch (task->kind) {
556 case SD_TASK_COMM_E2E:
557 case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
558 fprintf(fout, ", shape=box");
560 case SD_TASK_COMP_SEQ:
561 case SD_TASK_COMP_PAR_AMDAHL:
562 fprintf(fout, ", shape=circle");
565 xbt_die("Unknown task type!");
567 fprintf(fout, "];\n");
568 for (auto it : *task->predecessors)
569 fprintf(fout, " T%p -> T%p;\n", it, task);
570 for (auto it : *task->inputs)
571 fprintf(fout, " T%p -> T%p;\n", it, task);
575 * \brief Adds a dependency between two tasks
577 * \a dst will depend on \a src, ie \a dst will not start before \a src is finished.
578 * Their \ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_RUNNABLE.
580 * \param name the name of the new dependency (can be \c nullptr)
581 * \param data the user data you want to associate with this dependency (can be \c nullptr)
582 * \param src the task which must be executed first
583 * \param dst the task you want to make depend on \a src
584 * \see SD_task_dependency_remove()
586 void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task_t dst)
589 THROWF(arg_error, 0, "Cannot add a dependency between task '%s' and itself", SD_task_get_name(src));
591 if (src->state == SD_DONE || src->state == SD_FAILED)
592 THROWF(arg_error, 0, "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, SD_RUNNABLE, or SD_RUNNING",
595 if (dst->state == SD_DONE || dst->state == SD_FAILED || dst->state == SD_RUNNING)
596 THROWF(arg_error, 0, "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, or SD_RUNNABLE",
599 if (dst->inputs->find(src) != dst->inputs->end() || src->outputs->find(dst) != src->outputs->end() ||
600 src->successors->find(dst) != src->successors->end() || dst->predecessors->find(src) != dst->predecessors->end())
601 THROWF(arg_error, 0, "A dependency already exists between task '%s' and task '%s'", src->name, dst->name);
603 XBT_DEBUG("SD_task_dependency_add: src = %s, dst = %s", src->name, dst->name);
605 if (src->kind == SD_TASK_COMM_E2E || src->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK){
606 if (dst->kind == SD_TASK_COMP_SEQ || dst->kind == SD_TASK_COMP_PAR_AMDAHL)
607 dst->inputs->insert(src);
609 dst->predecessors->insert(src);
610 src->successors->insert(dst);
612 if (dst->kind == SD_TASK_COMM_E2E|| dst->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK)
613 src->outputs->insert(dst);
615 src->successors->insert(dst);
616 dst->predecessors->insert(src);
619 /* if the task was runnable, the task goes back to SD_SCHEDULED because of the new dependency*/
620 if (dst->state == SD_RUNNABLE) {
621 XBT_DEBUG("SD_task_dependency_add: %s was runnable and becomes scheduled!", dst->name);
622 SD_task_set_state(dst, SD_SCHEDULED);
627 * \brief Indicates whether there is a dependency between two tasks.
630 * \param dst a task depending on \a src
632 * If src is nullptr, checks whether dst has any pre-dependency.
633 * If dst is nullptr, checks whether src has any post-dependency.
635 int SD_task_dependency_exists(SD_task_t src, SD_task_t dst)
637 xbt_assert(src != nullptr || dst != nullptr, "Invalid parameter: both src and dst are nullptr");
641 return (src->successors->find(dst) != src->successors->end() || src->outputs->find(dst) != src->outputs->end());
643 return src->successors->size() + src->outputs->size();
646 return dst->predecessors->size() + dst->inputs->size();
652 * \brief Remove a dependency between two tasks
655 * \param dst a task depending on \a src
656 * \see SD_task_dependency_add()
658 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst)
660 XBT_DEBUG("SD_task_dependency_remove: src = %s, dst = %s", SD_task_get_name(src), SD_task_get_name(dst));
662 if (src->successors->find(dst) == src->successors->end() && src->outputs->find(dst) == src->outputs->end())
663 THROWF(arg_error, 0, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
664 src->name, dst->name, dst->name, src->name);
666 if (src->kind == SD_TASK_COMM_E2E || src->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK){
667 if (dst->kind == SD_TASK_COMP_SEQ || dst->kind == SD_TASK_COMP_PAR_AMDAHL)
668 dst->inputs->erase(src);
670 dst->predecessors->erase(src);
671 src->successors->erase(dst);
673 if (dst->kind == SD_TASK_COMM_E2E|| dst->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK)
674 src->outputs->erase(dst);
676 src->successors->erase(dst);
677 dst->predecessors->erase(src);
680 /* if the task was scheduled and dependencies are satisfied, we can make it runnable */
681 if (dst->predecessors->empty() && dst->inputs->empty() && dst->state == SD_SCHEDULED)
682 SD_task_set_state(dst, SD_RUNNABLE);
686 * \brief Adds a watch point to a task
688 * SD_simulate() will stop as soon as the \ref e_SD_task_state_t "state" of this task becomes the one given in argument.
689 * The watch point is then automatically removed.
692 * \param state the \ref e_SD_task_state_t "state" you want to watch (cannot be #SD_NOT_SCHEDULED)
693 * \see SD_task_unwatch()
695 void SD_task_watch(SD_task_t task, e_SD_task_state_t state)
697 if (state & SD_NOT_SCHEDULED)
698 THROWF(arg_error, 0, "Cannot add a watch point for state SD_NOT_SCHEDULED");
700 task->watch_points = task->watch_points | state;
704 * \brief Removes a watch point from a task
707 * \param state the \ref e_SD_task_state_t "state" you no longer want to watch
708 * \see SD_task_watch()
710 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state)
712 xbt_assert(state != SD_NOT_SCHEDULED, "SimDag error: Cannot have a watch point for state SD_NOT_SCHEDULED");
713 task->watch_points = task->watch_points & ~state;
717 * \brief Returns an approximative estimation of the execution time of a task.
719 * The estimation is very approximative because the value returned is the time the task would take if it was executed
720 * now and if it was the only task.
722 * \param task the task to evaluate
723 * \param host_count number of hosts on which the task would be executed
724 * \param host_list the hosts on which the task would be executed
725 * \param flops_amount computation amount for each host(i.e., an array of host_count doubles)
726 * \param bytes_amount communication amount between each pair of hosts (i.e., a matrix of host_count*host_count doubles)
729 double SD_task_get_execution_time(SD_task_t task, int host_count, const sg_host_t *host_list,
730 const double *flops_amount, const double *bytes_amount)
732 xbt_assert(host_count > 0, "Invalid parameter");
733 double max_time = 0.0;
735 /* the task execution time is the maximum execution time of the parallel tasks */
736 for (int i = 0; i < host_count; i++) {
738 if (flops_amount != nullptr)
739 time = flops_amount[i] / host_list[i]->speed();
741 if (bytes_amount != nullptr)
742 for (int j = 0; j < host_count; j++)
743 if (bytes_amount[i * host_count + j] != 0)
744 time += (SD_route_get_latency(host_list[i], host_list[j]) +
745 bytes_amount[i * host_count + j] / SD_route_get_bandwidth(host_list[i], host_list[j]));
753 static inline void SD_task_do_schedule(SD_task_t task)
755 if (SD_task_get_state(task) > SD_SCHEDULABLE)
756 THROWF(arg_error, 0, "Task '%s' has already been scheduled", SD_task_get_name(task));
758 if (task->predecessors->empty() && task->inputs->empty())
759 SD_task_set_state(task, SD_RUNNABLE);
761 SD_task_set_state(task, SD_SCHEDULED);
765 * \brief Schedules a task
767 * The task state must be #SD_NOT_SCHEDULED.
768 * Once scheduled, a task is executed as soon as possible in \see SD_simulate, i.e. when its dependencies are satisfied.
770 * \param task the task you want to schedule
771 * \param host_count number of hosts on which the task will be executed
772 * \param workstation_list the hosts on which the task will be executed
773 * \param flops_amount computation amount for each hosts (i.e., an array of host_count doubles)
774 * \param bytes_amount communication amount between each pair of hosts (i.e., a matrix of host_count*host_count doubles)
775 * \param rate task execution speed rate
776 * \see SD_task_unschedule()
778 void SD_task_schedule(SD_task_t task, int host_count, const sg_host_t * host_list,
779 const double *flops_amount, const double *bytes_amount, double rate)
781 xbt_assert(host_count > 0, "workstation_nb must be positive");
783 task->host_count = host_count;
787 task->flops_amount = static_cast<double*>(xbt_realloc(task->flops_amount, sizeof(double) * host_count));
788 memcpy(task->flops_amount, flops_amount, sizeof(double) * host_count);
790 xbt_free(task->flops_amount);
791 task->flops_amount = nullptr;
794 int communication_nb = host_count * host_count;
796 task->bytes_amount = static_cast<double*>(xbt_realloc(task->bytes_amount, sizeof(double) * communication_nb));
797 memcpy(task->bytes_amount, bytes_amount, sizeof(double) * communication_nb);
799 xbt_free(task->bytes_amount);
800 task->bytes_amount = nullptr;
803 task->host_list = static_cast<sg_host_t*>(xbt_realloc(task->host_list, sizeof(sg_host_t) * host_count));
804 memcpy(task->host_list, host_list, sizeof(sg_host_t) * host_count);
806 SD_task_do_schedule(task);
810 * \brief Unschedules a task
812 * The task state must be #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING or #SD_FAILED.
813 * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
814 * Call SD_task_schedule() to schedule it again.
816 * \param task the task you want to unschedule
817 * \see SD_task_schedule()
819 void SD_task_unschedule(SD_task_t task)
821 if (task->state == SD_NOT_SCHEDULED || task->state == SD_SCHEDULABLE)
822 THROWF(arg_error, 0, "Task %s: the state must be SD_SCHEDULED, SD_RUNNABLE, SD_RUNNING or SD_FAILED", task->name);
824 if ((task->state == SD_SCHEDULED || task->state == SD_RUNNABLE) /* if the task is scheduled or runnable */
825 && ((task->kind == SD_TASK_COMP_PAR_AMDAHL) || (task->kind == SD_TASK_COMM_PAR_MXN_1D_BLOCK))) {
826 /* Don't free scheduling data for typed tasks */
827 __SD_task_destroy_scheduling_data(task);
828 xbt_free(task->host_list);
829 task->host_list=nullptr;
830 task->host_count = 0;
833 if (SD_task_get_state(task) == SD_RUNNING)
834 /* the task should become SD_FAILED */
835 task->surf_action->cancel();
837 if (task->predecessors->empty() && task->inputs->empty())
838 SD_task_set_state(task, SD_SCHEDULABLE);
840 SD_task_set_state(task, SD_NOT_SCHEDULED);
842 task->remains = task->amount;
843 task->start_time = -1.0;
847 void SD_task_run(SD_task_t task)
849 xbt_assert(task->state == SD_RUNNABLE, "Task '%s' is not runnable! Task state: %d", task->name, (int) task->state);
850 xbt_assert(task->host_list != nullptr, "Task '%s': workstation_list is nullptr!", task->name);
852 XBT_VERB("Executing task '%s'", task->name);
854 /* Copy the elements of the task into the action */
855 int host_nb = task->host_count;
856 sg_host_t *hosts = xbt_new(sg_host_t, host_nb);
858 for (int i = 0; i < host_nb; i++)
859 hosts[i] = task->host_list[i];
861 double *flops_amount = xbt_new0(double, host_nb);
862 double *bytes_amount = xbt_new0(double, host_nb * host_nb);
864 if(task->flops_amount)
865 memcpy(flops_amount, task->flops_amount, sizeof(double) * host_nb);
866 if(task->bytes_amount)
867 memcpy(bytes_amount, task->bytes_amount, sizeof(double) * host_nb * host_nb);
869 task->surf_action = surf_host_model->executeParallelTask(host_nb, hosts, flops_amount, bytes_amount, task->rate);
871 task->surf_action->setData(task);
873 XBT_DEBUG("surf_action = %p", task->surf_action);
875 __SD_task_destroy_scheduling_data(task); /* now the scheduling data are not useful anymore */
876 SD_task_set_state(task, SD_RUNNING);
877 xbt_dynar_push(sd_global->return_set, &task);
881 * \brief Returns the start time of a task
883 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
885 * \param task: a task
886 * \return the start time of this task
888 double SD_task_get_start_time(SD_task_t task)
890 if (task->surf_action)
891 return task->surf_action->getStartTime();
893 return task->start_time;
897 * \brief Returns the finish time of a task
899 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
900 * If the state is not completed yet, the returned value is an estimation of the task finish time. This value can
901 * vary until the task is completed.
903 * \param task: a task
904 * \return the start time of this task
906 double SD_task_get_finish_time(SD_task_t task)
908 if (task->surf_action) /* should never happen as actions are destroyed right after their completion */
909 return task->surf_action->getFinishTime();
911 return task->finish_time;
914 void SD_task_distribute_comp_amdahl(SD_task_t task, int ws_count)
916 xbt_assert(task->kind == SD_TASK_COMP_PAR_AMDAHL, "Task %s is not a SD_TASK_COMP_PAR_AMDAHL typed task."
917 "Cannot use this function.", task->name);
918 task->flops_amount = xbt_new0(double, ws_count);
919 task->bytes_amount = xbt_new0(double, ws_count * ws_count);
920 xbt_free(task->host_list);
921 task->host_count = ws_count;
922 task->host_list = xbt_new0(sg_host_t, ws_count);
924 for (int i=0; i<ws_count; i++){
925 task->flops_amount[i] = (task->alpha + (1 - task->alpha)/ws_count) * task->amount;
929 /** @brief Auto-schedules a task.
931 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows to specify the task costs at
932 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
935 * To be auto-schedulable, a task must be type and created with one of the specialized creation functions.
938 * We should create tasks kind for the following categories:
939 * - Point to point communication (done)
940 * - Sequential computation (done)
941 * - group communication (redistribution, several kinds)
942 * - parallel tasks with no internal communication (one kind per speedup model such as Amdahl)
943 * - idem+ internal communication. Task type not enough since we cannot store comm cost alongside to comp one)
945 void SD_task_schedulev(SD_task_t task, int count, const sg_host_t * list)
947 xbt_assert(task->kind != 0, "Task %s is not typed. Cannot automatically schedule it.", SD_task_get_name(task));
948 switch (task->kind) {
949 case SD_TASK_COMP_PAR_AMDAHL:
950 SD_task_distribute_comp_amdahl(task, count);
952 case SD_TASK_COMM_E2E:
953 case SD_TASK_COMP_SEQ:
954 xbt_assert(task->host_count == count, "Got %d locations, but were expecting %d locations", count,task->host_count);
955 for (int i=0; i<count; i++)
956 task->host_list[i] = list[i];
957 if (SD_task_get_kind(task)== SD_TASK_COMP_SEQ && !task->flops_amount){
958 /*This task has failed and is rescheduled. Reset the flops_amount*/
959 task->flops_amount = xbt_new0(double, 1);
960 task->flops_amount[0] = task->remains;
962 SD_task_do_schedule(task);
965 xbt_die("Kind of task %s not supported by SD_task_schedulev()", SD_task_get_name(task));
968 if (task->kind == SD_TASK_COMM_E2E) {
969 XBT_VERB("Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(task),
970 sg_host_get_name(task->host_list[0]), sg_host_get_name(task->host_list[1]), task->bytes_amount[2]);
973 /* Iterate over all inputs and outputs to say where I am located (and start them if runnable) */
974 if (task->kind == SD_TASK_COMP_SEQ) {
975 XBT_VERB("Schedule computation task %s on %s. It costs %.f flops", SD_task_get_name(task),
976 sg_host_get_name(task->host_list[0]), task->flops_amount[0]);
978 for (auto input : *task->inputs){
979 input->host_list[1] = task->host_list[0];
980 if (input->host_list[0] && (SD_task_get_state(input) < SD_SCHEDULED)) {
981 SD_task_do_schedule(input);
982 XBT_VERB ("Auto-Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(input),
983 sg_host_get_name(input->host_list[0]), sg_host_get_name(input->host_list[1]), input->bytes_amount[2]);
987 for (auto output : *task->outputs){
988 output->host_list[0] = task->host_list[0];
989 if (output->host_list[1] && (SD_task_get_state(output) < SD_SCHEDULED)) {
990 SD_task_do_schedule(output);
991 XBT_VERB ("Auto-Schedule comm task %s between %s -> %s. It costs %.f bytes", SD_task_get_name(output),
992 sg_host_get_name(output->host_list[0]), sg_host_get_name(output->host_list[1]),
993 output->bytes_amount[2]);
998 /* Iterate over all children and parents being MXN_1D_BLOCK to say where I am located (and start them if runnable) */
999 if (task->kind == SD_TASK_COMP_PAR_AMDAHL) {
1000 XBT_VERB("Schedule computation task %s on %d workstations. %.f flops will be distributed following Amdahl's Law",
1001 SD_task_get_name(task), task->host_count, task->flops_amount[0]);
1002 for (auto input : *task->inputs){
1003 if (!input->host_list){
1004 XBT_VERB("Sender side of Task %s is not scheduled yet", SD_task_get_name(input));
1005 input->host_list = xbt_new0(sg_host_t, count);
1006 input->host_count = count;
1007 XBT_VERB("Fill the workstation list with list of Task '%s'", SD_task_get_name(task));
1008 for (int i=0; i<count; i++)
1009 input->host_list[i] = task->host_list[i];
1011 XBT_VERB("Build communication matrix for task '%s'", SD_task_get_name(input));
1013 double src_start, src_end, dst_start, dst_end;
1014 src_nb = input->host_count;
1016 input->host_list = static_cast<sg_host_t*>(xbt_realloc(input->host_list, (input->host_count+count)*sizeof(sg_host_t)));
1017 for (int i=0; i<count; i++)
1018 input->host_list[input->host_count+i] = task->host_list[i];
1020 input->host_count += count;
1021 xbt_free(input->flops_amount);
1022 xbt_free(input->bytes_amount);
1023 input->flops_amount = xbt_new0(double, input->host_count);
1024 input->bytes_amount = xbt_new0(double, input->host_count* input->host_count);
1026 for (int i=0; i<src_nb; i++) {
1027 src_start = i*input->amount/src_nb;
1028 src_end = src_start + input->amount/src_nb;
1029 for (int j=0; j<dst_nb; j++) {
1030 dst_start = j*input->amount/dst_nb;
1031 dst_end = dst_start + input->amount/dst_nb;
1032 XBT_VERB("(%s->%s): (%.2f, %.2f)-> (%.2f, %.2f)", sg_host_get_name(input->host_list[i]),
1033 sg_host_get_name(input->host_list[src_nb+j]), src_start, src_end, dst_start, dst_end);
1034 if ((src_end <= dst_start) || (dst_end <= src_start)) {
1035 input->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]=0.0;
1037 input->bytes_amount[i*(src_nb+dst_nb)+src_nb+j] = MIN(src_end, dst_end) - MAX(src_start, dst_start);
1039 XBT_VERB("==> %.2f", input->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]);
1043 if (SD_task_get_state(input)< SD_SCHEDULED) {
1044 SD_task_do_schedule(input);
1045 XBT_VERB ("Auto-Schedule redistribution task %s. Send %.f bytes from %d hosts to %d hosts.",
1046 SD_task_get_name(input),input->amount, src_nb, dst_nb);
1051 for (auto output : *task->outputs) {
1052 if (!output->host_list){
1053 XBT_VERB("Receiver side of Task '%s' is not scheduled yet", SD_task_get_name(output));
1054 output->host_list = xbt_new0(sg_host_t, count);
1055 output->host_count = count;
1056 XBT_VERB("Fill the workstation list with list of Task '%s'", SD_task_get_name(task));
1057 for (int i=0; i<count; i++)
1058 output->host_list[i] = task->host_list[i];
1060 double src_start, src_end, dst_start, dst_end;
1062 int dst_nb = output->host_count;
1063 output->host_list = static_cast<sg_host_t*>(xbt_realloc(output->host_list, (output->host_count+count)*sizeof(sg_host_t)));
1064 for (int i=output->host_count - 1; i>=0; i--)
1065 output->host_list[count+i] = output->host_list[i];
1066 for (int i=0; i<count; i++)
1067 output->host_list[i] = task->host_list[i];
1069 output->host_count += count;
1071 xbt_free(output->flops_amount);
1072 xbt_free(output->bytes_amount);
1074 output->flops_amount = xbt_new0(double, output->host_count);
1075 output->bytes_amount = xbt_new0(double, output->host_count* output->host_count);
1077 for (int i=0; i<src_nb; i++) {
1078 src_start = i*output->amount/src_nb;
1079 src_end = src_start + output->amount/src_nb;
1080 for (int j=0; j<dst_nb; j++) {
1081 dst_start = j*output->amount/dst_nb;
1082 dst_end = dst_start + output->amount/dst_nb;
1083 XBT_VERB("(%d->%d): (%.2f, %.2f)-> (%.2f, %.2f)", i, j, src_start, src_end, dst_start, dst_end);
1084 if ((src_end <= dst_start) || (dst_end <= src_start)) {
1085 output->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]=0.0;
1087 output->bytes_amount[i*(src_nb+dst_nb)+src_nb+j] = MIN(src_end, dst_end)- MAX(src_start, dst_start);
1089 XBT_VERB("==> %.2f", output->bytes_amount[i*(src_nb+dst_nb)+src_nb+j]);
1093 if (SD_task_get_state(output)< SD_SCHEDULED) {
1094 SD_task_do_schedule(output);
1095 XBT_VERB ("Auto-Schedule redistribution task %s. Send %.f bytes from %d hosts to %d hosts.",
1096 output->name, output->amount, src_nb, dst_nb);
1103 /** @brief autoschedule a task on a list of workstations
1105 * This function is very similar to SD_task_schedulev(), but takes the list of workstations to schedule onto as
1106 * separate parameters.
1107 * It builds a proper vector of workstations and then call SD_task_schedulev()
1109 void SD_task_schedulel(SD_task_t task, int count, ...)
1112 sg_host_t *list = xbt_new(sg_host_t, count);
1113 va_start(ap, count);
1114 for (int i=0; i<count; i++) {
1115 list[i] = va_arg(ap, sg_host_t);
1118 SD_task_schedulev(task, count, list);