1 /* Copyright (c) 2006-2021. 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 "simdag_private.hpp"
8 #include "simgrid/kernel/routing/NetPoint.hpp"
9 #include "src/surf/HostImpl.hpp"
10 #include "src/surf/surf_interface.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_task, sd, "Logging specific to SimDag (task)");
17 template class xbt::Extendable<sd::Task>;
21 Task* Task::create(const std::string& name, double amount, void* userdata)
23 auto task = new Task();
25 task->set_amount(amount);
26 task->set_data(userdata);
27 task->allocation_ = new std::vector<sg_host_t>();
28 sd_global->initial_tasks.insert(task);
33 Task* Task::create_comm_e2e(const std::string& name, double amount, void* userdata)
35 auto task = create(name, amount, userdata);
36 task->bytes_amount_ = xbt_new0(double, 4);
37 task->bytes_amount_[2] = amount;
38 task->set_kind(SD_TASK_COMM_E2E);
43 Task* Task::create_comp_seq(const std::string& name, double amount, void* userdata)
45 auto task = create(name, amount, userdata);
46 task->flops_amount_ = xbt_new0(double, 1);
47 task->flops_amount_[0] = amount;
48 task->set_kind(SD_TASK_COMP_SEQ);
53 Task* Task::create_comp_par_amdahl(const std::string& name, double amount, void* userdata, double alpha)
55 xbt_assert(alpha < 1. && alpha >= 0., "Invalid parameter: alpha must be in [0.;1.[");
57 auto task = create(name, amount, userdata);
58 task->set_alpha(alpha);
59 task->set_kind(SD_TASK_COMP_PAR_AMDAHL);
64 Task* Task::create_comm_par_mxn_1d_block(const std::string& name, double amount, void* userdata)
66 auto task = create(name, amount, userdata);
67 task->set_kind(SD_TASK_COMM_PAR_MXN_1D_BLOCK);
72 void Task::distribute_comp_amdahl(int count)
74 xbt_assert(kind_ == SD_TASK_COMP_PAR_AMDAHL,
75 "Task %s is not a SD_TASK_COMP_PAR_AMDAHL typed task."
76 "Cannot use this function.",
78 flops_amount_ = xbt_new0(double, count);
79 for (int i = 0; i < count; i++)
80 flops_amount_[i] = (alpha_ + (1 - alpha_) / count) * amount_;
83 void Task::build_MxN_1D_block_matrix(int src_nb, int dst_nb)
85 xbt_assert(kind_ == SD_TASK_COMM_PAR_MXN_1D_BLOCK,
86 "Task %s is not a SD_TASK_COMM_PAR_MXN_1D_BLOCK typed task."
87 "Cannot use this function.",
89 xbt_free(bytes_amount_);
90 bytes_amount_ = xbt_new0(double, allocation_->size() * allocation_->size());
92 for (int i = 0; i < src_nb; i++) {
93 double src_start = i * amount_ / src_nb;
94 double src_end = src_start + amount_ / src_nb;
95 for (int j = 0; j < dst_nb; j++) {
96 double dst_start = j * amount_ / dst_nb;
97 double dst_end = dst_start + amount_ / dst_nb;
98 XBT_VERB("(%d->%d): (%.2f, %.2f)-> (%.2f, %.2f)", i, j, src_start, src_end, dst_start, dst_end);
99 bytes_amount_[i * (src_nb + dst_nb) + src_nb + j] = 0.0;
100 if ((src_end > dst_start) && (dst_end > src_start)) { /* There is something to send */
101 bytes_amount_[i * (src_nb + dst_nb) + src_nb + j] = std::min(src_end, dst_end) - std::max(src_start, dst_start);
102 XBT_VERB("==> %.2f", bytes_amount_[i * (src_nb + dst_nb) + src_nb + j]);
108 bool Task::is_parent_of(Task* task) const
110 return (successors_.find(task) != successors_.end() || outputs_.find(task) != outputs_.end());
113 bool Task::is_child_of(Task* task) const
115 return (inputs_.find(task) != inputs_.end() || predecessors_.find(task) != predecessors_.end());
118 void Task::set_amount(double amount)
121 if (kind_ == SD_TASK_COMP_SEQ)
122 flops_amount_[0] = amount;
123 if (kind_ == SD_TASK_COMM_E2E) {
124 bytes_amount_[2] = amount;
128 void Task::set_rate(double rate)
130 xbt_assert(kind_ == SD_TASK_COMM_E2E, "The rate can be modified for end-to-end communications only.");
131 if (state_ < SD_RUNNING) {
134 XBT_WARN("Task %p has started. Changing rate is ineffective.", this);
137 void Task::set_state(e_SD_task_state_t new_state)
139 std::set<Task*>::iterator idx;
140 XBT_DEBUG("Set state of '%s' to %d", get_cname(), new_state);
141 if ((new_state == SD_NOT_SCHEDULED || new_state == SD_SCHEDULABLE) && state_ == SD_FAILED) {
142 sd_global->completed_tasks.erase(this);
143 sd_global->initial_tasks.insert(this);
146 if (new_state == SD_SCHEDULED && state_ == SD_RUNNABLE) {
147 sd_global->initial_tasks.insert(this);
148 sd_global->runnable_tasks.erase(this);
151 if (new_state == SD_RUNNABLE) {
152 idx = sd_global->initial_tasks.find(this);
153 if (idx != sd_global->initial_tasks.end()) {
154 sd_global->runnable_tasks.insert(*idx);
155 sd_global->initial_tasks.erase(idx);
159 if (new_state == SD_RUNNING)
160 sd_global->runnable_tasks.erase(this);
162 if (new_state == SD_DONE || new_state == SD_FAILED) {
163 sd_global->completed_tasks.insert(this);
164 start_time_ = surf_action_->get_start_time();
165 if (new_state == SD_DONE) {
166 finish_time_ = surf_action_->get_finish_time();
167 #if SIMGRID_HAVE_JEDULE
168 jedule_log_sd_event(this);
171 finish_time_ = simgrid_get_clock();
172 surf_action_->unref();
173 surf_action_ = nullptr;
174 allocation_->clear();
179 if (watch_points_ & new_state) {
180 XBT_VERB("Watch point reached with task '%s'!", get_cname());
181 sd_global->watch_point_reached = true;
182 unwatch(new_state); /* remove the watch point */
186 double Task::get_alpha() const
188 xbt_assert(kind_ == SD_TASK_COMP_PAR_AMDAHL, "Alpha parameter is not defined for this kind of task");
192 double Task::get_remaining_amount() const
195 return surf_action_->get_remains();
197 return (state_ == SD_DONE) ? 0 : amount_;
200 double Task::get_start_time() const
203 return surf_action_->get_start_time();
208 double Task::get_finish_time() const
210 if (surf_action_) /* should never happen as actions are destroyed right after their completion */
211 return surf_action_->get_finish_time();
216 void Task::set_sender_side_allocation(unsigned long count, const std::vector<s4u::Host*>* sender)
218 for (unsigned long i = 0; i < count; i++)
219 allocation_->push_back(sender->at(i));
222 void Task::set_receiver_side_allocation(unsigned long count, const std::vector<s4u::Host*>* receiver)
224 for (unsigned long i = 0; i < count; i++)
225 allocation_->insert(allocation_->begin() + i, receiver->at(i));
228 void Task::watch(e_SD_task_state_t state)
230 if (state & SD_NOT_SCHEDULED)
231 throw std::invalid_argument("Cannot add a watch point for state SD_NOT_SCHEDULED");
233 watch_points_ = watch_points_ | state;
236 void Task::unwatch(e_SD_task_state_t state)
238 xbt_assert(state != SD_NOT_SCHEDULED, "SimDag error: Cannot have a watch point for state SD_NOT_SCHEDULED");
239 watch_points_ = watch_points_ & ~state;
242 void Task::dump() const
244 XBT_INFO("Displaying task %s", get_cname());
245 if (state_ == SD_RUNNABLE)
246 XBT_INFO(" - state: runnable");
247 else if (state_ < SD_RUNNABLE)
248 XBT_INFO(" - state: %s not runnable", __get_state_name(state_));
250 XBT_INFO(" - state: not runnable %s", __get_state_name(state_));
254 case SD_TASK_COMM_E2E:
255 XBT_INFO(" - kind: end-to-end communication");
257 case SD_TASK_COMP_SEQ:
258 XBT_INFO(" - kind: sequential computation");
260 case SD_TASK_COMP_PAR_AMDAHL:
261 XBT_INFO(" - kind: parallel computation following Amdahl's law");
263 case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
264 XBT_INFO(" - kind: MxN data redistribution assuming 1D block distribution");
267 XBT_INFO(" - (unknown kind %d)", kind_);
271 XBT_INFO(" - amount: %.0f", amount_);
272 if (kind_ == SD_TASK_COMP_PAR_AMDAHL)
273 XBT_INFO(" - alpha: %.2f", alpha_);
274 XBT_INFO(" - Dependencies to satisfy: %lu", has_unsolved_dependencies());
275 if (has_unsolved_dependencies() > 0) {
276 XBT_INFO(" - pre-dependencies:");
277 for (auto const& it : predecessors_)
278 XBT_INFO(" %s", it->get_cname());
280 for (auto const& it : inputs_)
281 XBT_INFO(" %s", it->get_cname());
283 if (is_waited_by() > 0) {
284 XBT_INFO(" - post-dependencies:");
286 for (auto const& it : successors_)
287 XBT_INFO(" %s", it->get_cname());
288 for (auto const& it : outputs_)
289 XBT_INFO(" %s", it->get_cname());
293 void Task::released_by(Task* pred)
295 predecessors_.erase(pred);
297 XBT_DEBUG("Release dependency on %s: %lu remain(s). Becomes schedulable if %zu=0", get_cname(),
298 has_unsolved_dependencies(), predecessors_.size());
300 if (state_ == SD_NOT_SCHEDULED && predecessors_.empty())
301 set_state(SD_SCHEDULABLE);
303 if (state_ == SD_SCHEDULED && has_unsolved_dependencies() == 0)
304 set_state(SD_RUNNABLE);
306 if (state_ == SD_RUNNABLE && not sd_global->watch_point_reached)
310 void Task::produced_by(Task* pred)
312 start_time_ = pred->get_finish_time();
313 predecessors_.erase(pred);
314 if (state_ == SD_SCHEDULED)
315 set_state(SD_RUNNABLE);
317 set_state(SD_SCHEDULABLE);
319 Task* comm_dst = *(successors_.begin());
320 if (comm_dst->get_state() == SD_NOT_SCHEDULED && comm_dst->get_predecessors().empty()) {
321 XBT_DEBUG("%s is a transfer, %s may be ready now if %zu=0", get_cname(), comm_dst->get_cname(),
322 comm_dst->get_predecessors().size());
323 comm_dst->set_state(SD_SCHEDULABLE);
325 if (state_ == SD_RUNNABLE && not sd_global->watch_point_reached)
329 void Task::do_schedule()
331 if (state_ > SD_SCHEDULABLE)
332 throw std::invalid_argument(simgrid::xbt::string_printf("Task '%s' has already been scheduled", get_cname()));
334 if (has_unsolved_dependencies() == 0)
335 set_state(SD_RUNNABLE);
337 set_state(SD_SCHEDULED);
340 void Task::schedule(const std::vector<s4u::Host*>& hosts, const double* flops_amount, const double* bytes_amount,
343 unsigned long host_count = hosts.size();
347 flops_amount_ = static_cast<double*>(xbt_realloc(flops_amount_, sizeof(double) * host_count));
348 memcpy(flops_amount_, flops_amount, sizeof(double) * host_count);
350 xbt_free(flops_amount_);
351 flops_amount_ = nullptr;
354 unsigned long communication_nb = host_count * host_count;
356 bytes_amount_ = static_cast<double*>(xbt_realloc(bytes_amount_, sizeof(double) * communication_nb));
357 memcpy(bytes_amount_, bytes_amount, sizeof(double) * communication_nb);
359 xbt_free(bytes_amount_);
360 bytes_amount_ = nullptr;
363 for (unsigned long i = 0; i < host_count; i++)
364 allocation_->push_back(hosts[i]);
369 void Task::schedulev(const std::vector<s4u::Host*>& hosts)
371 xbt_assert(kind_ == SD_TASK_COMP_SEQ || kind_ == SD_TASK_COMP_PAR_AMDAHL,
372 "Task %s is not typed. Cannot automatically schedule it.", get_cname());
374 for (unsigned long i = 0; i < hosts.size(); i++)
375 allocation_->push_back(hosts[i]);
377 XBT_VERB("Schedule computation task %s on %zu host(s)", get_cname(), allocation_->size());
379 if (kind_ == SD_TASK_COMP_SEQ) {
380 if (not flops_amount_) { /*This task has failed and is rescheduled. Reset the flops_amount*/
381 flops_amount_ = xbt_new0(double, 1);
382 flops_amount_[0] = amount_;
384 XBT_VERB("It costs %.f flops", flops_amount_[0]);
387 if (kind_ == SD_TASK_COMP_PAR_AMDAHL) {
388 distribute_comp_amdahl(hosts.size());
389 XBT_VERB("%.f flops will be distributed following Amdahl's Law", flops_amount_[0]);
394 /* Iterate over all inputs and outputs to say where I am located (and start them if runnable) */
395 for (auto const& input : inputs_) {
396 unsigned long src_nb = input->get_allocation_size();
397 unsigned long dst_nb = hosts.size();
399 XBT_VERB("Sender side of '%s' not scheduled. Set receiver side to '%s''s allocation", input->get_cname(),
401 input->set_sender_side_allocation(dst_nb, allocation_);
403 if (input->get_allocation_size() > allocation_->size()) {
404 if (kind_ == SD_TASK_COMP_PAR_AMDAHL)
405 input->build_MxN_1D_block_matrix(src_nb, dst_nb);
407 input->do_schedule();
408 XBT_VERB("Auto-Schedule Communication task '%s'. Send %.f bytes from %zu hosts to %zu hosts.", input->get_cname(),
409 input->get_amount(), src_nb, dst_nb);
413 for (auto const& output : outputs_) {
414 unsigned long src_nb = hosts.size();
415 unsigned long dst_nb = output->get_allocation_size();
417 XBT_VERB("Receiver side of '%s' not scheduled. Set sender side to '%s''s allocation", output->get_cname(),
419 output->set_receiver_side_allocation(src_nb, allocation_);
421 if (output->get_allocation_size() > allocation_->size()) {
422 if (kind_ == SD_TASK_COMP_PAR_AMDAHL)
423 output->build_MxN_1D_block_matrix(src_nb, dst_nb);
425 output->do_schedule();
426 XBT_VERB("Auto-Schedule Communication task %s. Send %.f bytes from %lu hosts to %lu hosts.", output->get_cname(),
427 output->get_amount(), src_nb, dst_nb);
432 void Task::unschedule()
434 if (state_ == SD_NOT_SCHEDULED || state_ == SD_SCHEDULABLE)
435 throw std::invalid_argument(xbt::string_printf(
436 "Task %s: the state must be SD_SCHEDULED, SD_RUNNABLE, SD_RUNNING or SD_FAILED", get_cname()));
438 if (state_ == SD_SCHEDULED || state_ == SD_RUNNABLE) /* if the task is scheduled or runnable */ {
439 allocation_->clear();
440 if (kind_ == SD_TASK_COMP_PAR_AMDAHL || kind_ == SD_TASK_COMM_PAR_MXN_1D_BLOCK) {
441 /* Don't free scheduling data for typed tasks */
442 xbt_free(flops_amount_);
443 xbt_free(bytes_amount_);
444 bytes_amount_ = nullptr;
445 flops_amount_ = nullptr;
449 if (state_ == SD_RUNNING)
450 /* the task should become SD_FAILED */
451 surf_action_->cancel();
453 if (has_unsolved_dependencies() == 0)
454 set_state(SD_SCHEDULABLE);
456 set_state(SD_NOT_SCHEDULED);
463 xbt_assert(state_ == SD_RUNNABLE, "Task '%s' is not runnable! Task state: %d", get_cname(), (int)state_);
464 xbt_assert(not allocation_->empty(), "Task '%s': host_list is empty!", get_cname());
466 XBT_VERB("Executing task '%s'", get_cname());
468 /* Beware! The scheduling data are now used by the surf action directly! no copy was done */
469 auto host_model = allocation_->front()->get_netpoint()->get_englobing_zone()->get_host_model();
470 surf_action_ = host_model->execute_parallel(*allocation_, flops_amount_, bytes_amount_, rate_);
472 surf_action_->set_data(this);
474 XBT_DEBUG("surf_action = %p", surf_action_);
476 set_state(SD_RUNNING);
477 sd_global->return_set.insert(this);
482 XBT_DEBUG("Destroying task %s...", get_cname());
484 /* First Remove all dependencies associated with the task. */
485 while (not predecessors_.empty())
486 SD_task_dependency_remove(*(predecessors_.begin()), this);
487 while (not inputs_.empty())
488 SD_task_dependency_remove(*(inputs_.begin()), this);
489 while (not successors_.empty())
490 SD_task_dependency_remove(this, *(successors_.begin()));
491 while (not outputs_.empty())
492 SD_task_dependency_remove(this, *(outputs_.begin()));
494 if (state_ == SD_SCHEDULED || state_ == SD_RUNNABLE) {
495 xbt_free(flops_amount_);
496 xbt_free(bytes_amount_);
497 bytes_amount_ = nullptr;
498 flops_amount_ = nullptr;
501 xbt_free(flops_amount_);
502 xbt_free(bytes_amount_);
506 if (surf_action_ != nullptr)
507 surf_action_->unref();
509 XBT_DEBUG("Task destroyed.");
512 } // namespace simgrid
514 /* **************************** Public C interface *************************** */
517 * @brief Creates a new task.
519 * @param name the name of the task (can be @c nullptr)
520 * @param data the user data you want to associate with the task (can be @c nullptr)
521 * @param amount amount of the task
522 * @return the new task
523 * @see SD_task_destroy()
525 SD_task_t SD_task_create(const char* name, void* data, double amount)
527 return simgrid::sd::Task::create(name, amount, data);
530 /** @brief create an end-to-end communication task that can then be auto-scheduled
532 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows one to specify the task costs at
533 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
536 * A end-to-end communication must be scheduled on 2 hosts, and the amount specified at creation is sent from hosts[0]
539 SD_task_t SD_task_create_comm_e2e(const char* name, void* data, double amount)
541 return simgrid::sd::Task::create_comm_e2e(name, amount, data);
544 /** @brief create a sequential computation task that can then be auto-scheduled
546 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows one to specify the task costs at
547 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
550 * A sequential computation must be scheduled on 1 host, and the amount specified at creation to be run on hosts[0].
552 * @param name the name of the task (can be @c nullptr)
553 * @param data the user data you want to associate with the task (can be @c nullptr)
554 * @param flops_amount amount of compute work to be done by the task
555 * @return the new SD_TASK_COMP_SEQ typed task
557 SD_task_t SD_task_create_comp_seq(const char* name, void* data, double flops_amount)
559 return simgrid::sd::Task::create_comp_seq(name, flops_amount, data);
562 /** @brief create a parallel computation task that can then be auto-scheduled
564 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows one to specify the task costs at
565 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
568 * A parallel computation can be scheduled on any number of host.
569 * The underlying speedup model is Amdahl's law.
570 * To be auto-scheduled, @see SD_task_distribute_comp_amdahl has to be called first.
571 * @param name the name of the task (can be @c nullptr)
572 * @param data the user data you want to associate with the task (can be @c nullptr)
573 * @param flops_amount amount of compute work to be done by the task
574 * @param alpha purely serial fraction of the work to be done (in [0.;1.[)
575 * @return the new task
577 SD_task_t SD_task_create_comp_par_amdahl(const char* name, void* data, double flops_amount, double alpha)
579 return simgrid::sd::Task::create_comp_par_amdahl(name, flops_amount, data, alpha);
582 /** @brief create a complex data redistribution task that can then be auto-scheduled
584 * Auto-scheduling mean that the task can be used with SD_task_schedulev().
585 * This allows one to specify the task costs at creation, and decouple them from the scheduling process where you just
586 * specify which resource should communicate.
588 * A data redistribution can be scheduled on any number of host.
589 * The assumed distribution is a 1D block distribution. Each host owns the same share of the @see amount.
590 * To be auto-scheduled, @see SD_task_distribute_comm_mxn_1d_block has to be called first.
591 * @param name the name of the task (can be @c nullptr)
592 * @param data the user data you want to associate with the task (can be @c nullptr)
593 * @param amount amount of data to redistribute by the task
594 * @return the new task
596 SD_task_t SD_task_create_comm_par_mxn_1d_block(const char* name, void* data, double amount)
598 return simgrid::sd::Task::create_comm_par_mxn_1d_block(name, amount, data);
602 * @brief Destroys a task.
604 * The user data (if any) should have been destroyed first.
606 * @param task the task you want to destroy
607 * @see SD_task_create()
609 void SD_task_destroy(SD_task_t task)
615 * @brief Returns the user data of a task
618 * @return the user data associated with this task (can be @c nullptr)
619 * @see SD_task_set_data()
621 void* SD_task_get_data(const_SD_task_t task)
623 return task->get_data();
627 * @brief Sets the user data of a task
629 * The new data can be @c nullptr. The old data should have been freed first, if it was not @c nullptr.
632 * @param data the new data you want to associate with this task
633 * @see SD_task_get_data()
635 void SD_task_set_data(SD_task_t task, void* data)
637 task->set_data(data);
641 * @brief Sets the rate of a task
643 * This will change the network bandwidth a task can use. This rate cannot be dynamically changed. Once the task has
644 * started, this call is ineffective. This rate depends on both the nominal bandwidth on the route onto which the task
645 * is scheduled (@see SD_task_get_current_bandwidth) and the amount of data to transfer.
647 * To divide the nominal bandwidth by 2, the rate then has to be :
648 * rate = bandwidth/(2*amount)
650 * @param task a @see SD_TASK_COMM_E2E task (end-to-end communication)
651 * @param rate the new rate you want to associate with this task.
653 void SD_task_set_rate(SD_task_t task, double rate)
655 task->set_rate(rate);
659 * @brief Returns the state of a task
662 * @return the current @ref e_SD_task_state_t "state" of this task:
663 * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING, #SD_DONE or #SD_FAILED
664 * @see e_SD_task_state_t
666 e_SD_task_state_t SD_task_get_state(const_SD_task_t task)
668 return task->get_state();
671 * @brief Returns the name of a task
674 * @return the name of this task (can be @c nullptr)
676 const char* SD_task_get_name(const_SD_task_t task)
678 return task->get_cname();
681 /** @brief Allows to change the name of a task */
682 void SD_task_set_name(SD_task_t task, const char* name)
684 task->set_name(name);
687 /** @brief Returns the dynar of the parents of a task
690 * @return a newly allocated dynar comprising the parents of this task
693 xbt_dynar_t SD_task_get_parents(const_SD_task_t task)
695 xbt_dynar_t parents = xbt_dynar_new(sizeof(SD_task_t), nullptr);
697 for (auto const& it : task->get_predecessors())
698 xbt_dynar_push(parents, &it);
699 for (auto const& it : task->get_inputs())
700 xbt_dynar_push(parents, &it);
705 /** @brief Returns the dynar of the parents of a task
708 * @return a newly allocated dynar comprising the parents of this task
710 xbt_dynar_t SD_task_get_children(const_SD_task_t task)
712 xbt_dynar_t children = xbt_dynar_new(sizeof(SD_task_t), nullptr);
714 for (auto const& it : task->get_successors())
715 xbt_dynar_push(children, &it);
716 for (auto const& it : task->get_outputs())
717 xbt_dynar_push(children, &it);
723 * @brief Returns the number of workstations involved in a task
725 * Only call this on already scheduled tasks!
728 int SD_task_get_workstation_count(const_SD_task_t task)
730 return static_cast<int>(task->get_allocation_size());
734 * @brief Returns the list of workstations involved in a task
736 * Only call this on already scheduled tasks!
739 sg_host_t* SD_task_get_workstation_list(const_SD_task_t task)
741 return task->get_allocation()->data();
745 * @brief Returns the total amount of work contained in a task
748 * @return the total amount of work (computation or data transfer) for this task
749 * @see SD_task_get_remaining_amount()
751 double SD_task_get_amount(const_SD_task_t task)
753 return task->get_amount();
756 /** @brief Sets the total amount of work of a task
757 * For sequential typed tasks (COMP_SEQ and COMM_E2E), it also sets the appropriate values in the flops_amount and
758 * bytes_amount arrays respectively. Nothing more than modifying task->amount is done for parallel typed tasks
759 * (COMP_PAR_AMDAHL and COMM_PAR_MXN_1D_BLOCK) as the distribution of the amount of work is done at scheduling time.
762 * @param amount the new amount of work to execute
764 void SD_task_set_amount(SD_task_t task, double amount)
766 task->set_amount(amount);
770 * @brief Returns the alpha parameter of a SD_TASK_COMP_PAR_AMDAHL task
772 * @param task a parallel task assuming Amdahl's law as speedup model
773 * @return the alpha parameter (serial part of a task in percent) for this task
775 double SD_task_get_alpha(const_SD_task_t task)
777 return task->get_alpha();
781 * @brief Returns the remaining amount work to do till the completion of a task
784 * @return the remaining amount of work (computation or data transfer) of this task
785 * @see SD_task_get_amount()
787 double SD_task_get_remaining_amount(const_SD_task_t task)
789 return task->get_remaining_amount();
792 e_SD_task_kind_t SD_task_get_kind(const_SD_task_t task)
794 return task->get_kind();
797 /** @brief Displays debugging information about a task */
798 void SD_task_dump(const_SD_task_t task)
803 /** @brief Dumps the task in dotty formalism into the FILE* passed as second argument */
804 void SD_task_dotty(const_SD_task_t task, void* out)
806 auto* fout = static_cast<FILE*>(out);
807 fprintf(fout, " T%p [label=\"%.20s\"", task, task->get_cname());
808 switch (task->get_kind()) {
809 case SD_TASK_COMM_E2E:
810 case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
811 fprintf(fout, ", shape=box");
813 case SD_TASK_COMP_SEQ:
814 case SD_TASK_COMP_PAR_AMDAHL:
815 fprintf(fout, ", shape=circle");
818 xbt_die("Unknown task type!");
820 fprintf(fout, "];\n");
821 for (auto const& it : task->get_predecessors())
822 fprintf(fout, " T%p -> T%p;\n", it, task);
823 for (auto const& it : task->get_inputs())
824 fprintf(fout, " T%p -> T%p;\n", it, task);
828 * @brief Adds a dependency between two tasks
830 * @a dst will depend on @a src, ie @a dst will not start before @a src is finished.
831 * Their @ref e_SD_task_state_t "state" must be #SD_NOT_SCHEDULED, #SD_SCHEDULED or #SD_RUNNABLE.
833 * @param src the task which must be executed first
834 * @param dst the task you want to make depend on @a src
835 * @see SD_task_dependency_remove()
837 void SD_task_dependency_add(SD_task_t src, SD_task_t dst)
840 throw std::invalid_argument(
841 simgrid::xbt::string_printf("Cannot add a dependency between task '%s' and itself", SD_task_get_name(src)));
843 if (src->get_state() == SD_DONE || src->get_state() == SD_FAILED)
844 throw std::invalid_argument(simgrid::xbt::string_printf(
845 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, SD_RUNNABLE, or SD_RUNNING",
848 if (dst->get_state() == SD_DONE || dst->get_state() == SD_FAILED || dst->get_state() == SD_RUNNING)
849 throw std::invalid_argument(simgrid::xbt::string_printf(
850 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, or SD_RUNNABLE", dst->get_cname()));
852 if (dst->is_child_of(src) || src->is_parent_of(dst))
853 throw std::invalid_argument(simgrid::xbt::string_printf(
854 "A dependency already exists between task '%s' and task '%s'", src->get_cname(), dst->get_cname()));
856 XBT_DEBUG("SD_task_dependency_add: src = %s, dst = %s", src->get_cname(), dst->get_cname());
858 if (src->get_kind() == SD_TASK_COMM_E2E || src->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK) {
859 if (dst->get_kind() == SD_TASK_COMP_SEQ || dst->get_kind() == SD_TASK_COMP_PAR_AMDAHL)
862 dst->add_predecessor(src);
863 src->add_successor(dst);
865 if (dst->get_kind() == SD_TASK_COMM_E2E || dst->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK)
866 src->add_output(dst);
868 src->add_successor(dst);
869 dst->add_predecessor(src);
872 /* if the task was runnable, the task goes back to SD_SCHEDULED because of the new dependency*/
873 if (dst->get_state() == SD_RUNNABLE) {
874 XBT_DEBUG("SD_task_dependency_add: %s was runnable and becomes scheduled!", dst->get_cname());
875 dst->set_state(SD_SCHEDULED);
880 * @brief Indicates whether there is a dependency between two tasks.
883 * @param dst a task depending on @a src
885 * If src is nullptr, checks whether dst has any pre-dependency.
886 * If dst is nullptr, checks whether src has any post-dependency.
888 int SD_task_dependency_exists(const_SD_task_t src, SD_task_t dst)
890 xbt_assert(src != nullptr || dst != nullptr, "Invalid parameter: both src and dst are nullptr");
894 return src->is_parent_of(dst);
896 return static_cast<int>(src->is_waited_by());
898 return static_cast<int>(dst->has_unsolved_dependencies());
902 * @brief Remove a dependency between two tasks
905 * @param dst a task depending on @a src
906 * @see SD_task_dependency_add()
908 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst)
910 XBT_DEBUG("SD_task_dependency_remove: src = %s, dst = %s", src->get_cname(), dst->get_cname());
912 if (not src->is_parent_of(dst))
913 throw std::invalid_argument(simgrid::xbt::string_printf(
914 "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'", src->get_cname(),
915 dst->get_cname(), dst->get_cname(), src->get_cname()));
917 if (src->get_kind() == SD_TASK_COMM_E2E || src->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK) {
918 if (dst->get_kind() == SD_TASK_COMP_SEQ || dst->get_kind() == SD_TASK_COMP_PAR_AMDAHL)
921 dst->rm_predecessor(src);
922 src->rm_successor(dst);
924 if (dst->get_kind() == SD_TASK_COMM_E2E || dst->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK)
927 src->rm_successor(dst);
928 dst->rm_predecessor(src);
931 /* if the task was scheduled and dependencies are satisfied, we can make it runnable */
932 if (dst->has_unsolved_dependencies() == 0 && dst->get_state() == SD_SCHEDULED)
933 dst->set_state(SD_RUNNABLE);
937 * @brief Adds a watch point to a task
939 * SD_simulate() will stop as soon as the @ref e_SD_task_state_t "state" of this task becomes the one given in argument.
940 * The watch point is then automatically removed.
943 * @param state the @ref e_SD_task_state_t "state" you want to watch (cannot be #SD_NOT_SCHEDULED)
944 * @see SD_task_unwatch()
946 void SD_task_watch(SD_task_t task, e_SD_task_state_t state)
952 * @brief Removes a watch point from a task
955 * @param state the @ref e_SD_task_state_t "state" you no longer want to watch
956 * @see SD_task_watch()
958 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state)
960 task->unwatch(state);
964 * @brief Returns an approximative estimation of the execution time of a task.
966 * The estimation is very approximative because the value returned is the time the task would take if it was executed
967 * now and if it was the only task.
969 * @param host_count number of hosts on which the task would be executed
970 * @param host_list the hosts on which the task would be executed
971 * @param flops_amount computation amount for each host(i.e., an array of host_count doubles)
972 * @param bytes_amount communication amount between each pair of hosts (i.e., a matrix of host_count*host_count doubles)
975 double SD_task_get_execution_time(const_SD_task_t /*task*/, int host_count, const sg_host_t* host_list,
976 const double* flops_amount, const double* bytes_amount)
978 xbt_assert(host_count > 0, "Invalid parameter");
979 double max_time = 0.0;
981 /* the task execution time is the maximum execution time of the parallel tasks */
982 for (int i = 0; i < host_count; i++) {
984 if (flops_amount != nullptr)
985 time = flops_amount[i] / host_list[i]->get_speed();
987 if (bytes_amount != nullptr)
988 for (int j = 0; j < host_count; j++)
989 if (bytes_amount[i * host_count + j] != 0)
990 time += (sg_host_get_route_latency(host_list[i], host_list[j]) +
991 bytes_amount[i * host_count + j] / sg_host_get_route_bandwidth(host_list[i], host_list[j]));
1000 * @brief Schedules a task
1002 * The task state must be #SD_NOT_SCHEDULED.
1003 * Once scheduled, a task is executed as soon as possible in @see SD_simulate, i.e. when its dependencies are satisfied.
1005 * @param task the task you want to schedule
1006 * @param host_count number of hosts on which the task will be executed
1007 * @param host_list the hosts on which the task will be executed
1008 * @param flops_amount computation amount for each hosts (i.e., an array of host_count doubles)
1009 * @param bytes_amount communication amount between each pair of hosts (i.e., a matrix of host_count*host_count doubles)
1010 * @param rate task execution speed rate
1011 * @see SD_task_unschedule()
1013 void SD_task_schedule(SD_task_t task, int host_count, const sg_host_t* host_list, const double* flops_amount,
1014 const double* bytes_amount, double rate)
1016 xbt_assert(host_count > 0, "host_count must be positive");
1017 std::vector<sg_host_t> hosts(host_count);
1019 for (int i = 0; i < host_count; i++)
1020 hosts[i] = host_list[i];
1022 task->schedule(hosts, flops_amount, bytes_amount, rate);
1026 * @brief Unschedules a task
1028 * The task state must be #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING or #SD_FAILED.
1029 * If you call this function, the task state becomes #SD_NOT_SCHEDULED.
1030 * Call SD_task_schedule() to schedule it again.
1032 * @param task the task you want to unschedule
1033 * @see SD_task_schedule()
1035 void SD_task_unschedule(SD_task_t task)
1041 * @brief Returns the start time of a task
1043 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
1045 * @param task: a task
1046 * @return the start time of this task
1048 double SD_task_get_start_time(const_SD_task_t task)
1050 return task->get_start_time();
1054 * @brief Returns the finish time of a task
1056 * The task state must be SD_RUNNING, SD_DONE or SD_FAILED.
1057 * If the state is not completed yet, the returned value is an estimation of the task finish time. This value can
1058 * vary until the task is completed.
1060 * @param task: a task
1061 * @return the start time of this task
1063 double SD_task_get_finish_time(const_SD_task_t task)
1065 return task->get_finish_time();
1068 void SD_task_distribute_comp_amdahl(SD_task_t task, int count)
1070 task->distribute_comp_amdahl(count);
1073 void SD_task_build_MxN_1D_block_matrix(SD_task_t task, int src_nb, int dst_nb)
1075 task->build_MxN_1D_block_matrix(src_nb, dst_nb);
1078 /** @brief Auto-schedules a task.
1080 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows one to specify the task costs at
1081 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
1084 * To be auto-schedulable, a task must be a typed computation SD_TASK_COMP_SEQ or SD_TASK_COMP_PAR_AMDAHL.
1086 void SD_task_schedulev(SD_task_t task, int count, const sg_host_t* host_list)
1088 std::vector<sg_host_t> list(count);
1089 for (int i = 0; i < count; i++)
1090 list[i] = host_list[i];
1091 task->schedulev(list);
1094 /** @brief autoschedule a task on a list of hosts
1096 * This function is similar to SD_task_schedulev(), but takes the list of hosts to schedule onto as separate parameters.
1097 * It builds a proper vector of hosts and then call SD_task_schedulev()
1099 void SD_task_schedulel(SD_task_t task, int count, ...)
1102 std::vector<sg_host_t> list(count);
1103 va_start(ap, count);
1104 for (int i = 0; i < count; i++)
1105 list[i] = va_arg(ap, sg_host_t);
1108 task->schedulev(list);