2 #include <simgrid/Exception.hpp>
3 #include <simgrid/s4u/Comm.hpp>
4 #include <simgrid/s4u/Exec.hpp>
5 #include <simgrid/s4u/Io.hpp>
6 #include <simgrid/s4u/Task.hpp>
7 #include <simgrid/simix.hpp>
9 #include "src/simgrid/module.hpp"
11 SIMGRID_REGISTER_PLUGIN(task, "Battery management", nullptr)
16 Tasks are designed to represent dataflows, i.e, graphs of Tasks.
17 Tasks can only be instancied using either
18 :cpp:func:`simgrid::s4u::ExecTask::init` or :cpp:func:`simgrid::s4u::CommTask::init`
19 An ExecTask is an Execution Task. Its underlying Activity is an :ref:`Exec <API_s4u_Exec>`.
20 A CommTask is a Communication Task. Its underlying Activity is a :ref:`Comm <API_s4u_Comm>`.
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Task, kernel, "Logging specific to the task plugin");
26 namespace simgrid::s4u {
28 Task::Task(const std::string& name) : name_(name) {}
31 * @brief Return True if the Task can start a new Activity.
32 * @note The Task is ready if not already doing something and there is at least one execution waiting in queue.
34 bool Task::ready_to_run() const
36 return not working_ && queued_firings_ > 0;
40 * @param source The sender.
41 * @brief Receive a token from another Task.
42 * @note Check upon reception if the Task has received a token from each of its predecessors,
43 * and in this case consumes those tokens and enqueue an execution.
45 void Task::receive(Task* source)
47 XBT_DEBUG("Task %s received a token from %s", name_.c_str(), source->name_.c_str());
48 auto source_count = predecessors_[source];
49 predecessors_[source]++;
50 if (tokens_received_.size() <= queued_firings_ + source_count)
51 tokens_received_.emplace_back();
52 tokens_received_[queued_firings_ + source_count][source] = source->token_;
53 bool enough_tokens = true;
54 for (auto const& [key, val] : predecessors_)
56 enough_tokens = false;
60 for (auto& [key, val] : predecessors_)
67 * @brief Task routine when finishing an execution.
68 * @note Set its working status as false.
69 * Add 1 to its count of finished executions.
70 * Call the on_this_end func.
71 * Fire on_end callback.
72 * Send a token to each of its successors.
73 * Start a new execution if possible.
77 xbt_assert(Actor::is_maestro());
80 on_this_completion(this);
82 if (current_activity_)
83 previous_activity_ = std::move(current_activity_);
84 for (auto const& t : successors_)
90 /** @param n The number of firings to enqueue.
91 * @brief Enqueue firing.
92 * @note Immediatly fire an activity if possible.
94 void Task::enqueue_firings(int n)
96 simgrid::kernel::actor::simcall_answered([this, n] {
103 /** @param amount The amount to set.
104 * @brief Set the amout of work to do.
105 * @note Amount in flop for ExecTask and in bytes for CommTask.
107 void Task::set_amount(double amount)
109 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
112 /** @param token The token to set.
113 * @brief Set the token to send to successors.
114 * @note The token is passed to each successor after the task end, i.e., after the on_end callback.
116 void Task::set_token(std::shared_ptr<Token> token)
118 simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
121 /** @return Map of tokens received for the next execution.
122 * @note If there is no queued execution for this task the map might not exist or be partially empty.
124 std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
126 return tokens_received_.front()[t];
134 queued_firings_ = std::max(queued_firings_ - 1, 0);
135 if (not tokens_received_.empty())
136 tokens_received_.pop_front();
139 /** @param successor The Task to add.
140 * @brief Add a successor to this Task.
141 * @note It also adds this as a predecessor of successor.
143 void Task::add_successor(TaskPtr successor)
145 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
146 successors_.insert(successor_p);
147 successor_p->predecessors_.try_emplace(this, 0);
151 /** @param successor The Task to remove.
152 * @brief Remove a successor from this Task.
153 * @note It also remove this from the predecessors of successor.
155 void Task::remove_successor(TaskPtr successor)
157 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
158 successor_p->predecessors_.erase(this);
159 successors_.erase(successor_p);
163 void Task::remove_all_successors()
165 simgrid::kernel::actor::simcall_answered([this] {
166 while (not successors_.empty()) {
167 auto* successor = *(successors_.begin());
168 successor->predecessors_.erase(this);
169 successors_.erase(successor);
175 * @brief Default constructor.
177 ExecTask::ExecTask(const std::string& name) : Task(name) {}
179 /** @ingroup plugin_task
180 * @brief Smart Constructor.
182 ExecTaskPtr ExecTask::init(const std::string& name)
184 return ExecTaskPtr(new ExecTask(name));
187 /** @ingroup plugin_task
188 * @brief Smart Constructor.
190 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
192 return init(name)->set_flops(flops)->set_host(host);
196 * @brief Do one execution of the Task.
197 * @note Call the on_this_start() func. Set its working status as true.
198 * Init and start the underlying Activity.
200 void ExecTask::fire()
203 auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
205 exec->on_this_completion_cb([this](Exec const&) { this->complete(); });
206 set_current_activity(exec);
209 /** @ingroup plugin_task
210 * @param host The host to set.
211 * @brief Set a new host.
213 ExecTaskPtr ExecTask::set_host(Host* host)
215 kernel::actor::simcall_answered([this, host] { host_ = host; });
219 /** @ingroup plugin_task
220 * @param flops The amount of flops to set.
222 ExecTaskPtr ExecTask::set_flops(double flops)
224 kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
229 * @brief Default constructor.
231 CommTask::CommTask(const std::string& name) : Task(name) {}
233 /** @ingroup plugin_task
234 * @brief Smart constructor.
236 CommTaskPtr CommTask::init(const std::string& name)
238 return CommTaskPtr(new CommTask(name));
241 /** @ingroup plugin_task
242 * @brief Smart constructor.
244 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
246 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
250 * @brief Do one execution of the Task.
251 * @note Call the on_this_start() func. Set its working status as true.
252 * Init and start the underlying Activity.
254 void CommTask::fire()
257 auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
259 comm->on_this_completion_cb([this](Comm const&) { this->complete(); });
260 set_current_activity(comm);
263 /** @ingroup plugin_task
264 * @param source The host to set.
265 * @brief Set a new source host.
267 CommTaskPtr CommTask::set_source(Host* source)
269 kernel::actor::simcall_answered([this, source] { source_ = source; });
273 /** @ingroup plugin_task
274 * @param destination The host to set.
275 * @brief Set a new destination host.
277 CommTaskPtr CommTask::set_destination(Host* destination)
279 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
283 /** @ingroup plugin_task
284 * @param bytes The amount of bytes to set.
286 CommTaskPtr CommTask::set_bytes(double bytes)
288 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
293 * @brief Default constructor.
295 IoTask::IoTask(const std::string& name) : Task(name) {}
297 /** @ingroup plugin_task
298 * @brief Smart Constructor.
300 IoTaskPtr IoTask::init(const std::string& name)
302 return IoTaskPtr(new IoTask(name));
305 /** @ingroup plugin_task
306 * @brief Smart Constructor.
308 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
310 return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
313 /** @ingroup plugin_task
314 * @param disk The disk to set.
315 * @brief Set a new disk.
317 IoTaskPtr IoTask::set_disk(Disk* disk)
319 kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
323 /** @ingroup plugin_task
324 * @param bytes The amount of bytes to set.
326 IoTaskPtr IoTask::set_bytes(double bytes)
328 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
332 /** @ingroup plugin_task */
333 IoTaskPtr IoTask::set_op_type(Io::OpType type)
335 kernel::actor::simcall_answered([this, type] { type_ = type; });
342 auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
344 io->on_this_completion_cb([this](Io const&) { this->complete(); });
345 set_current_activity(io);
348 } // namespace simgrid::s4u