2 #include <simgrid/Exception.hpp>
3 #include <simgrid/s4u/Task.hpp>
4 #include <simgrid/s4u/Comm.hpp>
5 #include <simgrid/s4u/Exec.hpp>
6 #include <simgrid/s4u/Io.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 if (tokens_received_.size() <= queued_firings_ + source_count)
50 tokens_received_.push_back({});
51 tokens_received_[queued_firings_ + source_count][source] = source->token_;
52 bool enough_tokens = true;
53 for (auto const& [key, val] : predecessors_)
55 enough_tokens = false;
59 for (auto& [key, val] : predecessors_)
66 * @brief Task routine when finishing an execution.
67 * @note Set its working status as false.
68 * Add 1 to its count of finished executions.
69 * Call the on_this_end func.
70 * Fire on_end callback.
71 * Send a token to each of its successors.
72 * Start a new execution if possible.
76 xbt_assert(Actor::is_maestro());
79 on_this_completion(this);
81 if (current_activity_)
82 previous_activity_ = std::move(current_activity_);
83 for (auto const& t : successors_)
89 /** @param n The number of firings to enqueue.
90 * @brief Enqueue firing.
91 * @note Immediatly fire an activity if possible.
93 void Task::enqueue_firings(int n)
95 simgrid::kernel::actor::simcall_answered([this, n] {
102 /** @param amount The amount to set.
103 * @brief Set the amout of work to do.
104 * @note Amount in flop for ExecTask and in bytes for CommTask.
106 void Task::set_amount(double amount)
108 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
111 /** @param token The token to set.
112 * @brief Set the token to send to successors.
113 * @note The token is passed to each successor after the task end, i.e., after the on_end callback.
115 void Task::set_token(std::shared_ptr<Token> token)
117 simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
120 /** @return Map of tokens received for the next execution.
121 * @note If there is no queued execution for this task the map might not exist or be partially empty.
123 std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
125 return tokens_received_.front()[t];
132 queued_firings_ = std::max(queued_firings_ - 1, 0);
133 if (tokens_received_.size() > 0)
134 tokens_received_.pop_front();
137 /** @param successor The Task to add.
138 * @brief Add a successor to this Task.
139 * @note It also adds this as a predecessor of successor.
141 void Task::add_successor(TaskPtr successor)
143 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
144 successors_.insert(successor_p);
145 successor_p->predecessors_.try_emplace(this, 0);
149 /** @param successor The Task to remove.
150 * @brief Remove a successor from this Task.
151 * @note It also remove this from the predecessors of successor.
153 void Task::remove_successor(TaskPtr successor)
155 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
156 successor_p->predecessors_.erase(this);
157 successors_.erase(successor_p);
161 void Task::remove_all_successors()
163 simgrid::kernel::actor::simcall_answered([this] {
164 while (not successors_.empty()) {
165 auto* successor = *(successors_.begin());
166 successor->predecessors_.erase(this);
167 successors_.erase(successor);
173 * @brief Default constructor.
175 ExecTask::ExecTask(const std::string& name) : Task(name) {}
177 /** @ingroup plugin_task
178 * @brief Smart Constructor.
180 ExecTaskPtr ExecTask::init(const std::string& name)
182 return ExecTaskPtr(new ExecTask(name));
185 /** @ingroup plugin_task
186 * @brief Smart Constructor.
188 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
190 return init(name)->set_flops(flops)->set_host(host);
194 * @brief Do one execution of the Task.
195 * @note Call the on_this_start() func. Set its working status as true.
196 * Init and start the underlying Activity.
198 void ExecTask::fire()
201 auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
203 exec->on_this_completion_cb([this](Exec const&) { this->complete(); });
204 set_current_activity(exec);
207 /** @ingroup plugin_task
208 * @param host The host to set.
209 * @brief Set a new host.
211 ExecTaskPtr ExecTask::set_host(Host* host)
213 kernel::actor::simcall_answered([this, host] { host_ = host; });
217 /** @ingroup plugin_task
218 * @param flops The amount of flops to set.
220 ExecTaskPtr ExecTask::set_flops(double flops)
222 kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
227 * @brief Default constructor.
229 CommTask::CommTask(const std::string& name) : Task(name) {}
231 /** @ingroup plugin_task
232 * @brief Smart constructor.
234 CommTaskPtr CommTask::init(const std::string& name)
236 return CommTaskPtr(new CommTask(name));
239 /** @ingroup plugin_task
240 * @brief Smart constructor.
242 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
244 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
248 * @brief Do one execution of the Task.
249 * @note Call the on_this_start() func. Set its working status as true.
250 * Init and start the underlying Activity.
252 void CommTask::fire()
255 auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
257 comm->on_this_completion_cb([this](Comm const&) { this->complete(); });
258 set_current_activity(comm);
261 /** @ingroup plugin_task
262 * @param source The host to set.
263 * @brief Set a new source host.
265 CommTaskPtr CommTask::set_source(Host* source)
267 kernel::actor::simcall_answered([this, source] { source_ = source; });
271 /** @ingroup plugin_task
272 * @param destination The host to set.
273 * @brief Set a new destination host.
275 CommTaskPtr CommTask::set_destination(Host* destination)
277 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
281 /** @ingroup plugin_task
282 * @param bytes The amount of bytes to set.
284 CommTaskPtr CommTask::set_bytes(double bytes)
286 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
291 * @brief Default constructor.
293 IoTask::IoTask(const std::string& name) : Task(name) {}
295 /** @ingroup plugin_task
296 * @brief Smart Constructor.
298 IoTaskPtr IoTask::init(const std::string& name)
300 return IoTaskPtr(new IoTask(name));
303 /** @ingroup plugin_task
304 * @brief Smart Constructor.
306 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
308 return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
311 /** @ingroup plugin_task
312 * @param disk The disk to set.
313 * @brief Set a new disk.
315 IoTaskPtr IoTask::set_disk(Disk* disk)
317 kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
321 /** @ingroup plugin_task
322 * @param bytes The amount of bytes to set.
324 IoTaskPtr IoTask::set_bytes(double bytes)
326 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
330 /** @ingroup plugin_task */
331 IoTaskPtr IoTask::set_op_type(Io::OpType type)
333 kernel::actor::simcall_answered([this, type] { type_ = type; });
340 auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
342 io->on_this_completion_cb([this](Io const&) { this->complete(); });
343 set_current_activity(io);
346 } // namespace simgrid::s4u