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(unsigned long 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 (unsigned long i = 0; i < count; i++)
80 flops_amount_[i] = (alpha_ + (1 - alpha_) / count) * amount_;
83 void Task::build_MxN_1D_block_matrix(unsigned long src_nb, unsigned long 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 (unsigned long 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 (unsigned long 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("(%lu->%lu): (%.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 void Task::dependency_add(Task* task)
111 throw std::invalid_argument(
112 simgrid::xbt::string_printf("Cannot add a dependency between task '%s' and itself", get_cname()));
114 if (state_ == SD_DONE || state_ == SD_FAILED)
115 throw std::invalid_argument(simgrid::xbt::string_printf(
116 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, SD_RUNNABLE, or SD_RUNNING", get_cname()));
118 if (task->get_state() == SD_DONE || task->get_state() == SD_FAILED || task->get_state() == SD_RUNNING)
119 throw std::invalid_argument(simgrid::xbt::string_printf(
120 "Task '%s' must be SD_NOT_SCHEDULED, SD_SCHEDULABLE, SD_SCHEDULED, or SD_RUNNABLE", task->get_cname()));
122 if (dependency_exist(task))
123 throw std::invalid_argument(simgrid::xbt::string_printf(
124 "A dependency already exists between task '%s' and task '%s'", get_cname(), task->get_cname()));
126 successors_.push_back(task);
127 task->dependencies_.insert({this});
129 /* if 'task' was runnable, it goes back to the SD_SCHEDULED state because of the new dependency*/
130 if (task->get_state() == SD_RUNNABLE) {
131 XBT_DEBUG("SD_task_dependency_add: %s was runnable and becomes scheduled!", task->get_cname());
132 task->set_state(SD_SCHEDULED);
136 bool Task::dependency_exist(Task* task) const
138 return (std::find(successors_.begin(), successors_.end(), task) != successors_.end() ||
139 dependencies_.find(task) != dependencies_.end());
142 void Task::dependency_remove(Task* task)
145 throw std::invalid_argument("Cannot ask to remove itself from successors");
147 auto p = std::find(successors_.begin(), successors_.end(), task);
148 if (p != successors_.end()) {
149 successors_.erase(p);
150 task->dependencies_.erase({this});
152 throw std::invalid_argument(simgrid::xbt::string_printf(
153 "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'", get_cname(),
154 task->get_cname(), task->get_cname(), get_cname()));
156 /* if 'task' was scheduled and dependencies are satisfied, we can make it runnable */
157 if (task->has_unsolved_dependencies() == 0 && task->get_state() == SD_SCHEDULED)
158 task->set_state(SD_RUNNABLE);
161 std::set<Task*> Task::get_predecessors() const
164 for (const auto& d : dependencies_)
165 if (d->get_kind() == SD_TASK_COMP_SEQ || d->get_kind() == SD_TASK_COMP_PAR_AMDAHL)
170 std::set<Task*> Task::get_inputs() const
173 for (const auto& d : dependencies_)
174 if (d->get_kind() == SD_TASK_COMM_E2E || d->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK)
179 std::vector<Task*> Task::get_outputs() const
181 std::vector<Task*> res;
182 for (const auto& d : successors_)
183 if (d->get_kind() == SD_TASK_COMM_E2E || d->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK)
188 void Task::set_amount(double amount)
191 if (kind_ == SD_TASK_COMP_SEQ)
192 flops_amount_[0] = amount;
193 if (kind_ == SD_TASK_COMM_E2E) {
194 bytes_amount_[2] = amount;
198 void Task::set_rate(double rate)
200 xbt_assert(kind_ == SD_TASK_COMM_E2E, "The rate can be modified for end-to-end communications only.");
201 if (state_ < SD_RUNNING) {
204 XBT_WARN("Task %p has started. Changing rate is ineffective.", this);
208 void Task::set_state(e_SD_task_state_t new_state)
210 std::set<Task*>::iterator idx;
211 XBT_DEBUG("Set state of '%s' to %d", get_cname(), new_state);
212 if ((new_state == SD_NOT_SCHEDULED || new_state == SD_SCHEDULABLE) && state_ == SD_FAILED) {
213 sd_global->completed_tasks.erase(this);
214 sd_global->initial_tasks.insert(this);
217 if (new_state == SD_SCHEDULED && state_ == SD_RUNNABLE) {
218 sd_global->initial_tasks.insert(this);
219 sd_global->runnable_tasks.erase(this);
222 if (new_state == SD_RUNNABLE) {
223 idx = sd_global->initial_tasks.find(this);
224 if (idx != sd_global->initial_tasks.end()) {
225 sd_global->runnable_tasks.insert(*idx);
226 sd_global->initial_tasks.erase(idx);
230 if (new_state == SD_RUNNING)
231 sd_global->runnable_tasks.erase(this);
233 if (new_state == SD_DONE || new_state == SD_FAILED) {
234 sd_global->completed_tasks.insert(this);
235 start_time_ = surf_action_->get_start_time();
236 if (new_state == SD_DONE) {
237 finish_time_ = surf_action_->get_finish_time();
238 #if SIMGRID_HAVE_JEDULE
239 jedule_log_sd_event(this);
242 finish_time_ = simgrid_get_clock();
243 surf_action_->unref();
244 surf_action_ = nullptr;
245 allocation_->clear();
250 if (watch_points_ & new_state) {
251 XBT_VERB("Watch point reached with task '%s'!", get_cname());
252 sd_global->watch_point_reached = true;
253 unwatch(new_state); /* remove the watch point */
257 double Task::get_remaining_amount() const
260 return surf_action_->get_remains();
262 return (state_ == SD_DONE) ? 0 : amount_;
265 double Task::get_start_time() const
267 return surf_action_ ? surf_action_->get_start_time() : start_time_;
270 double Task::get_finish_time() const
272 if (surf_action_) /* should never happen as actions are destroyed right after their completion */
273 return surf_action_->get_finish_time();
278 void Task::set_sender_side_allocation(unsigned long count, const std::vector<s4u::Host*>* sender)
280 for (unsigned long i = 0; i < count; i++)
281 allocation_->push_back(sender->at(i));
284 void Task::set_receiver_side_allocation(unsigned long count, const std::vector<s4u::Host*>* receiver)
286 for (unsigned long i = 0; i < count; i++)
287 allocation_->insert(allocation_->begin() + i, receiver->at(i));
290 void Task::watch(e_SD_task_state_t state)
292 if (state & SD_NOT_SCHEDULED)
293 throw std::invalid_argument("Cannot add a watch point for state SD_NOT_SCHEDULED");
295 watch_points_ = watch_points_ | state;
298 void Task::unwatch(e_SD_task_state_t state)
300 xbt_assert(state != SD_NOT_SCHEDULED, "SimDag error: Cannot have a watch point for state SD_NOT_SCHEDULED");
301 watch_points_ = watch_points_ & ~state;
304 void Task::dump() const
306 XBT_INFO("Displaying task %s", get_cname());
307 if (state_ == SD_RUNNABLE)
308 XBT_INFO(" - state: runnable");
309 else if (state_ < SD_RUNNABLE)
310 XBT_INFO(" - state: %s not runnable", __get_state_name(state_));
312 XBT_INFO(" - state: not runnable %s", __get_state_name(state_));
316 case SD_TASK_COMM_E2E:
317 XBT_INFO(" - kind: end-to-end communication");
319 case SD_TASK_COMP_SEQ:
320 XBT_INFO(" - kind: sequential computation");
322 case SD_TASK_COMP_PAR_AMDAHL:
323 XBT_INFO(" - kind: parallel computation following Amdahl's law");
325 case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
326 XBT_INFO(" - kind: MxN data redistribution assuming 1D block distribution");
329 XBT_INFO(" - (unknown kind %d)", kind_);
333 XBT_INFO(" - amount: %.0f", amount_);
334 if (kind_ == SD_TASK_COMP_PAR_AMDAHL)
335 XBT_INFO(" - alpha: %.2f", alpha_);
336 XBT_INFO(" - Dependencies to satisfy: %lu", has_unsolved_dependencies());
337 if (has_unsolved_dependencies() > 0) {
338 XBT_INFO(" - pre-dependencies:");
339 for (auto const& it : dependencies_)
340 XBT_INFO(" %s", it->get_cname());
342 if (is_waited_by() > 0) {
343 XBT_INFO(" - post-dependencies:");
345 for (auto const& it : successors_)
346 XBT_INFO(" %s", it->get_cname());
350 void Task::released_by(Task* pred)
352 dependencies_.erase(pred);
353 XBT_DEBUG("Release dependency on %s: %lu remain(s). Becomes schedulable if %zu=0", get_cname(),
354 has_unsolved_dependencies(), get_predecessors().size());
356 if (state_ == SD_NOT_SCHEDULED && get_predecessors().empty())
357 set_state(SD_SCHEDULABLE);
359 if (state_ == SD_SCHEDULED && has_unsolved_dependencies() == 0)
360 set_state(SD_RUNNABLE);
362 if (state_ == SD_RUNNABLE && not sd_global->watch_point_reached)
366 void Task::produced_by(Task* pred)
368 if (state_ == SD_RUNNABLE)
371 start_time_ = pred->get_finish_time();
372 dependencies_.erase(pred);
373 if (state_ == SD_SCHEDULED)
374 set_state(SD_RUNNABLE);
376 set_state(SD_SCHEDULABLE);
378 Task* comm_dst = *(successors_.begin());
379 if (comm_dst->get_state() == SD_NOT_SCHEDULED && comm_dst->get_predecessors().empty()) {
380 XBT_DEBUG("%s is a transfer, %s may be ready now if %zu=0", get_cname(), comm_dst->get_cname(),
381 comm_dst->get_predecessors().size());
382 comm_dst->set_state(SD_SCHEDULABLE);
384 if (state_ == SD_RUNNABLE && not sd_global->watch_point_reached)
388 void Task::do_schedule()
390 if (state_ > SD_SCHEDULABLE)
391 throw std::invalid_argument(simgrid::xbt::string_printf("Task '%s' has already been scheduled", get_cname()));
393 if (has_unsolved_dependencies() == 0)
394 set_state(SD_RUNNABLE);
396 set_state(SD_SCHEDULED);
399 void Task::schedule(const std::vector<s4u::Host*>& hosts, const double* flops_amount, const double* bytes_amount,
402 unsigned long host_count = hosts.size();
406 flops_amount_ = static_cast<double*>(xbt_realloc(flops_amount_, sizeof(double) * host_count));
407 memcpy(flops_amount_, flops_amount, sizeof(double) * host_count);
409 xbt_free(flops_amount_);
410 flops_amount_ = nullptr;
413 unsigned long communication_nb = host_count * host_count;
415 bytes_amount_ = static_cast<double*>(xbt_realloc(bytes_amount_, sizeof(double) * communication_nb));
416 memcpy(bytes_amount_, bytes_amount, sizeof(double) * communication_nb);
418 xbt_free(bytes_amount_);
419 bytes_amount_ = nullptr;
422 for (const auto& h : hosts)
423 allocation_->push_back(h);
428 void Task::schedulev(const std::vector<s4u::Host*>& hosts)
430 xbt_assert(kind_ == SD_TASK_COMP_SEQ || kind_ == SD_TASK_COMP_PAR_AMDAHL,
431 "Task %s is not typed. Cannot automatically schedule it.", get_cname());
433 for (unsigned long i = 0; i < hosts.size(); i++)
434 allocation_->push_back(hosts[i]);
436 XBT_VERB("Schedule computation task %s on %zu host(s)", get_cname(), allocation_->size());
438 if (kind_ == SD_TASK_COMP_SEQ) {
439 if (not flops_amount_) { /*This task has failed and is rescheduled. Reset the flops_amount*/
440 flops_amount_ = xbt_new0(double, 1);
441 flops_amount_[0] = amount_;
443 XBT_VERB("It costs %.f flops", flops_amount_[0]);
446 if (kind_ == SD_TASK_COMP_PAR_AMDAHL) {
447 distribute_comp_amdahl(hosts.size());
448 XBT_VERB("%.f flops will be distributed following Amdahl's Law", flops_amount_[0]);
453 /* Iterate over all inputs and outputs to say where I am located (and start them if runnable) */
454 for (auto const& input : get_inputs()) {
455 unsigned long src_nb = input->get_allocation_size();
456 unsigned long dst_nb = hosts.size();
458 XBT_VERB("Sender side of '%s' not scheduled. Set receiver side to '%s''s allocation", input->get_cname(),
460 input->set_sender_side_allocation(dst_nb, allocation_);
462 if (input->get_allocation_size() > allocation_->size()) {
463 if (kind_ == SD_TASK_COMP_PAR_AMDAHL)
464 input->build_MxN_1D_block_matrix(src_nb, dst_nb);
466 input->do_schedule();
467 XBT_VERB("Auto-Schedule Communication task '%s'. Send %.f bytes from %lu hosts to %lu hosts.", input->get_cname(),
468 input->get_amount(), src_nb, dst_nb);
472 for (auto const& output : get_outputs()) {
473 unsigned long src_nb = hosts.size();
474 unsigned long dst_nb = output->get_allocation_size();
476 XBT_VERB("Receiver side of '%s' not scheduled. Set sender side to '%s''s allocation", output->get_cname(),
478 output->set_receiver_side_allocation(src_nb, allocation_);
480 if (output->get_allocation_size() > allocation_->size()) {
481 if (kind_ == SD_TASK_COMP_PAR_AMDAHL)
482 output->build_MxN_1D_block_matrix(src_nb, dst_nb);
484 output->do_schedule();
485 XBT_VERB("Auto-Schedule Communication task %s. Send %.f bytes from %lu hosts to %lu hosts.", output->get_cname(),
486 output->get_amount(), src_nb, dst_nb);
491 void Task::unschedule()
493 if (state_ == SD_NOT_SCHEDULED || state_ == SD_SCHEDULABLE)
494 throw std::invalid_argument(xbt::string_printf(
495 "Task %s: the state must be SD_SCHEDULED, SD_RUNNABLE, SD_RUNNING or SD_FAILED", get_cname()));
497 if (state_ == SD_SCHEDULED || state_ == SD_RUNNABLE) /* if the task is scheduled or runnable */ {
498 allocation_->clear();
499 if (kind_ == SD_TASK_COMP_PAR_AMDAHL || kind_ == SD_TASK_COMM_PAR_MXN_1D_BLOCK) {
500 /* Don't free scheduling data for typed tasks */
501 xbt_free(flops_amount_);
502 xbt_free(bytes_amount_);
503 bytes_amount_ = nullptr;
504 flops_amount_ = nullptr;
508 if (state_ == SD_RUNNING)
509 /* the task should become SD_FAILED */
510 surf_action_->cancel();
512 if (has_unsolved_dependencies() == 0)
513 set_state(SD_SCHEDULABLE);
515 set_state(SD_NOT_SCHEDULED);
522 xbt_assert(state_ == SD_RUNNABLE, "Task '%s' is not runnable! Task state: %d", get_cname(), (int)state_);
523 xbt_assert(not allocation_->empty(), "Task '%s': host_list is empty!", get_cname());
525 XBT_VERB("Executing task '%s'", get_cname());
527 /* Beware! The scheduling data are now used by the surf action directly! no copy was done */
528 auto host_model = allocation_->front()->get_netpoint()->get_englobing_zone()->get_host_model();
529 surf_action_ = host_model->execute_parallel(*allocation_, flops_amount_, bytes_amount_, rate_);
531 surf_action_->set_data(this);
533 XBT_DEBUG("surf_action = %p", surf_action_);
535 set_state(SD_RUNNING);
536 sd_global->return_set.insert(this);
541 XBT_DEBUG("Destroying task %s...", get_cname());
543 /* First Remove all dependencies associated with the task. */
544 while (not dependencies_.empty())
545 (*(dependencies_.begin()))->dependency_remove(this);
546 while (not successors_.empty())
547 this->dependency_remove(successors_.front());
549 if (state_ == SD_SCHEDULED || state_ == SD_RUNNABLE) {
550 xbt_free(flops_amount_);
551 xbt_free(bytes_amount_);
552 bytes_amount_ = nullptr;
553 flops_amount_ = nullptr;
556 xbt_free(flops_amount_);
557 xbt_free(bytes_amount_);
561 if (surf_action_ != nullptr)
562 surf_action_->unref();
564 XBT_DEBUG("Task destroyed.");
568 } // namespace simgrid
570 /* **************************** Public C interface *************************** */
573 * @brief Creates a new task.
575 * @param name the name of the task (can be @c nullptr)
576 * @param data the user data you want to associate with the task (can be @c nullptr)
577 * @param amount amount of the task
578 * @return the new task
579 * @see SD_task_destroy()
581 SD_task_t SD_task_create(const char* name, void* data, double amount)
583 return simgrid::sd::Task::create(name, amount, data);
586 /** @brief create an end-to-end communication task that can then be auto-scheduled
588 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows one to specify the task costs at
589 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
592 * A end-to-end communication must be scheduled on 2 hosts, and the amount specified at creation is sent from hosts[0]
595 SD_task_t SD_task_create_comm_e2e(const char* name, void* data, double amount)
597 return simgrid::sd::Task::create_comm_e2e(name, amount, data);
600 /** @brief create a sequential computation task that can then be auto-scheduled
602 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows one to specify the task costs at
603 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
606 * A sequential computation must be scheduled on 1 host, and the amount specified at creation to be run on hosts[0].
608 * @param name the name of the task (can be @c nullptr)
609 * @param data the user data you want to associate with the task (can be @c nullptr)
610 * @param flops_amount amount of compute work to be done by the task
611 * @return the new SD_TASK_COMP_SEQ typed task
613 SD_task_t SD_task_create_comp_seq(const char* name, void* data, double flops_amount)
615 return simgrid::sd::Task::create_comp_seq(name, flops_amount, data);
618 /** @brief create a parallel computation task that can then be auto-scheduled
620 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows one to specify the task costs at
621 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
624 * A parallel computation can be scheduled on any number of host.
625 * The underlying speedup model is Amdahl's law.
626 * To be auto-scheduled, @see SD_task_distribute_comp_amdahl has to be called first.
627 * @param name the name of the task (can be @c nullptr)
628 * @param data the user data you want to associate with the task (can be @c nullptr)
629 * @param flops_amount amount of compute work to be done by the task
630 * @param alpha purely serial fraction of the work to be done (in [0.;1.[)
631 * @return the new task
633 SD_task_t SD_task_create_comp_par_amdahl(const char* name, void* data, double flops_amount, double alpha)
635 return simgrid::sd::Task::create_comp_par_amdahl(name, flops_amount, data, alpha);
638 /** @brief create a complex data redistribution task that can then be auto-scheduled
640 * Auto-scheduling mean that the task can be used with SD_task_schedulev().
641 * This allows one to specify the task costs at creation, and decouple them from the scheduling process where you just
642 * specify which resource should communicate.
644 * A data redistribution can be scheduled on any number of host.
645 * The assumed distribution is a 1D block distribution. Each host owns the same share of the @see amount.
646 * To be auto-scheduled, @see SD_task_distribute_comm_mxn_1d_block has to be called first.
647 * @param name the name of the task (can be @c nullptr)
648 * @param data the user data you want to associate with the task (can be @c nullptr)
649 * @param amount amount of data to redistribute by the task
650 * @return the new task
652 SD_task_t SD_task_create_comm_par_mxn_1d_block(const char* name, void* data, double amount)
654 return simgrid::sd::Task::create_comm_par_mxn_1d_block(name, amount, data);
658 * @brief Destroys a task.
660 * The user data (if any) should have been destroyed first.
662 * @param task the task you want to destroy
663 * @see SD_task_create()
665 void SD_task_destroy(SD_task_t task)
670 /** @brief Returns the user data of a task */
671 void* SD_task_get_data(const_SD_task_t task)
673 return task->get_data();
676 /** @brief Sets the user data of a task
677 * The new data can be @c nullptr. The old data should have been freed first, if it was not @c nullptr.
679 void SD_task_set_data(SD_task_t task, void* data)
681 task->set_data(data);
684 void SD_task_set_rate(SD_task_t task, double rate)
686 task->set_rate(rate);
690 * @brief Returns the state of a task
693 * @return the current @ref e_SD_task_state_t "state" of this task:
694 * #SD_NOT_SCHEDULED, #SD_SCHEDULED, #SD_RUNNABLE, #SD_RUNNING, #SD_DONE or #SD_FAILED
695 * @see e_SD_task_state_t
697 e_SD_task_state_t SD_task_get_state(const_SD_task_t task)
699 return task->get_state();
702 const char* SD_task_get_name(const_SD_task_t task)
704 return task->get_cname();
707 void SD_task_set_name(SD_task_t task, const char* name)
709 task->set_name(name);
712 /** @brief Returns the parents of a task ina dynar */
713 xbt_dynar_t SD_task_get_parents(const_SD_task_t task)
715 xbt_dynar_t parents = xbt_dynar_new(sizeof(SD_task_t), nullptr);
717 for (auto const& it : task->get_dependencies())
718 xbt_dynar_push(parents, &it);
723 /** @brief Returns the children of a task in a dynar */
724 xbt_dynar_t SD_task_get_children(const_SD_task_t task)
726 xbt_dynar_t children = xbt_dynar_new(sizeof(SD_task_t), nullptr);
728 for (auto const& it : task->get_successors())
729 xbt_dynar_push(children, &it);
734 double SD_task_get_start_time(const_SD_task_t task)
736 return task->get_start_time();
739 double SD_task_get_finish_time(const_SD_task_t task)
741 return task->get_finish_time();
744 void SD_task_distribute_comp_amdahl(SD_task_t task, int count)
746 task->distribute_comp_amdahl(count);
749 void SD_task_build_MxN_1D_block_matrix(SD_task_t task, int src_nb, int dst_nb)
751 task->build_MxN_1D_block_matrix(src_nb, dst_nb);
755 * @brief Returns the number of workstations involved in a task
757 * Only call this on already scheduled tasks!
760 int SD_task_get_workstation_count(const_SD_task_t task)
762 return static_cast<int>(task->get_allocation_size());
766 * @brief Returns the list of workstations involved in a task
768 * Only call this on already scheduled tasks!
771 sg_host_t* SD_task_get_workstation_list(const_SD_task_t task)
773 return task->get_allocation()->data();
777 * @brief Returns the total amount of work contained in a task
780 * @return the total amount of work (computation or data transfer) for this task
781 * @see SD_task_get_remaining_amount()
783 double SD_task_get_amount(const_SD_task_t task)
785 return task->get_amount();
788 void SD_task_set_amount(SD_task_t task, double amount)
790 task->set_amount(amount);
793 double SD_task_get_remaining_amount(const_SD_task_t task)
795 return task->get_remaining_amount();
798 e_SD_task_kind_t SD_task_get_kind(const_SD_task_t task)
800 return task->get_kind();
803 void SD_task_dump(const_SD_task_t task)
808 void SD_task_dependency_add(SD_task_t src, SD_task_t dst)
810 XBT_DEBUG("SD_task_dependency_add: src = %s, dst = %s", src->get_cname(), dst->get_cname());
811 src->dependency_add(dst);
813 void SD_task_dependency_remove(SD_task_t src, SD_task_t dst)
815 XBT_DEBUG("SD_task_dependency_remove: src = %s, dst = %s", src->get_cname(), dst->get_cname());
816 src->dependency_remove(dst);
820 * @brief Indicates whether there is a dependency between two tasks.
821 * If src is nullptr, checks whether dst has any pre-dependency.
822 * If dst is nullptr, checks whether src has any post-dependency.
824 int SD_task_dependency_exists(const_SD_task_t src, SD_task_t dst)
826 xbt_assert(src != nullptr || dst != nullptr, "Invalid parameter: both src and dst are nullptr");
830 return src->dependency_exist(dst);
832 return static_cast<int>(src->is_waited_by());
834 return static_cast<int>(dst->has_unsolved_dependencies());
837 void SD_task_watch(SD_task_t task, e_SD_task_state_t state)
842 void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state)
844 task->unwatch(state);
847 /** @brief Dumps the task in dotty formalism into the FILE* passed as second argument */
848 void SD_task_dotty(const_SD_task_t task, void* out)
850 auto* fout = static_cast<FILE*>(out);
851 fprintf(fout, " T%p [label=\"%.20s\"", task, task->get_cname());
852 switch (task->get_kind()) {
853 case SD_TASK_COMM_E2E:
854 case SD_TASK_COMM_PAR_MXN_1D_BLOCK:
855 fprintf(fout, ", shape=box");
857 case SD_TASK_COMP_SEQ:
858 case SD_TASK_COMP_PAR_AMDAHL:
859 fprintf(fout, ", shape=circle");
862 xbt_die("Unknown task type!");
864 fprintf(fout, "];\n");
865 for (auto const& it : task->get_dependencies())
866 fprintf(fout, " T%p -> T%p;\n", it, task);
870 * @brief Returns an approximative estimation of the execution time of a task.
872 * The estimation is very approximative because the value returned is the time the task would take if it was executed
873 * now and if it was the only task.
875 * @param host_count number of hosts on which the task would be executed
876 * @param host_list the hosts on which the task would be executed
877 * @param flops_amount computation amount for each host(i.e., an array of host_count doubles)
878 * @param bytes_amount communication amount between each pair of hosts (i.e., a matrix of host_count*host_count doubles)
881 double SD_task_get_execution_time(const_SD_task_t /*task*/, int host_count, const sg_host_t* host_list,
882 const double* flops_amount, const double* bytes_amount)
884 xbt_assert(host_count > 0, "Invalid parameter");
885 double max_time = 0.0;
887 /* the task execution time is the maximum execution time of the parallel tasks */
888 for (int i = 0; i < host_count; i++) {
890 if (flops_amount != nullptr)
891 time = flops_amount[i] / host_list[i]->get_speed();
893 if (bytes_amount != nullptr)
894 for (int j = 0; j < host_count; j++)
895 if (bytes_amount[i * host_count + j] != 0)
896 time += (sg_host_get_route_latency(host_list[i], host_list[j]) +
897 bytes_amount[i * host_count + j] / sg_host_get_route_bandwidth(host_list[i], host_list[j]));
906 * @brief Schedules a task
908 * The task state must be #SD_NOT_SCHEDULED.
909 * Once scheduled, a task is executed as soon as possible in @see SD_simulate, i.e. when its dependencies are satisfied.
911 * @param task the task you want to schedule
912 * @param host_count number of hosts on which the task will be executed
913 * @param host_list the hosts on which the task will be executed
914 * @param flops_amount computation amount for each hosts (i.e., an array of host_count doubles)
915 * @param bytes_amount communication amount between each pair of hosts (i.e., a matrix of host_count*host_count doubles)
916 * @param rate task execution speed rate
917 * @see SD_task_unschedule()
919 void SD_task_schedule(SD_task_t task, int host_count, const sg_host_t* host_list, const double* flops_amount,
920 const double* bytes_amount, double rate)
922 xbt_assert(host_count > 0, "host_count must be positive");
923 std::vector<sg_host_t> hosts(host_count);
925 for (int i = 0; i < host_count; i++)
926 hosts[i] = host_list[i];
928 task->schedule(hosts, flops_amount, bytes_amount, rate);
931 void SD_task_unschedule(SD_task_t task)
936 /** @brief Auto-schedules a task.
938 * Auto-scheduling mean that the task can be used with SD_task_schedulev(). This allows one to specify the task costs at
939 * creation, and decouple them from the scheduling process where you just specify which resource should deliver the
942 * To be auto-schedulable, a task must be a typed computation SD_TASK_COMP_SEQ or SD_TASK_COMP_PAR_AMDAHL.
944 void SD_task_schedulev(SD_task_t task, int count, const sg_host_t* host_list)
946 std::vector<sg_host_t> list(count);
947 for (int i = 0; i < count; i++)
948 list[i] = host_list[i];
949 task->schedulev(list);
952 /** @brief autoschedule a task on a list of hosts
954 * This function is similar to SD_task_schedulev(), but takes the list of hosts to schedule onto as separate parameters.
955 * It builds a proper vector of hosts and then call SD_task_schedulev()
957 void SD_task_schedulel(SD_task_t task, int count, ...)
960 std::vector<sg_host_t> list(count);
962 for (int i = 0; i < count; i++)
963 list[i] = va_arg(ap, sg_host_t);
966 task->schedulev(list);