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 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];
133 queued_firings_ = std::max(queued_firings_ - 1, 0);
134 if (tokens_received_.size() > 0)
135 tokens_received_.pop_front();
138 /** @param successor The Task to add.
139 * @brief Add a successor to this Task.
140 * @note It also adds this as a predecessor of successor.
142 void Task::add_successor(TaskPtr successor)
144 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
145 successors_.insert(successor_p);
146 successor_p->predecessors_.try_emplace(this, 0);
150 /** @param successor The Task to remove.
151 * @brief Remove a successor from this Task.
152 * @note It also remove this from the predecessors of successor.
154 void Task::remove_successor(TaskPtr successor)
156 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
157 successor_p->predecessors_.erase(this);
158 successors_.erase(successor_p);
162 void Task::remove_all_successors()
164 simgrid::kernel::actor::simcall_answered([this] {
165 while (not successors_.empty()) {
166 auto* successor = *(successors_.begin());
167 successor->predecessors_.erase(this);
168 successors_.erase(successor);
174 * @brief Default constructor.
176 ExecTask::ExecTask(const std::string& name) : Task(name) {}
178 /** @ingroup plugin_task
179 * @brief Smart Constructor.
181 ExecTaskPtr ExecTask::init(const std::string& name)
183 return ExecTaskPtr(new ExecTask(name));
186 /** @ingroup plugin_task
187 * @brief Smart Constructor.
189 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
191 return init(name)->set_flops(flops)->set_host(host);
195 * @brief Do one execution of the Task.
196 * @note Call the on_this_start() func. Set its working status as true.
197 * Init and start the underlying Activity.
199 void ExecTask::fire()
202 auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
204 exec->on_this_completion_cb([this](Exec const&) { this->complete(); });
205 set_current_activity(exec);
208 /** @ingroup plugin_task
209 * @param host The host to set.
210 * @brief Set a new host.
212 ExecTaskPtr ExecTask::set_host(Host* host)
214 kernel::actor::simcall_answered([this, host] { host_ = host; });
218 /** @ingroup plugin_task
219 * @param flops The amount of flops to set.
221 ExecTaskPtr ExecTask::set_flops(double flops)
223 kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
228 * @brief Default constructor.
230 CommTask::CommTask(const std::string& name) : Task(name) {}
232 /** @ingroup plugin_task
233 * @brief Smart constructor.
235 CommTaskPtr CommTask::init(const std::string& name)
237 return CommTaskPtr(new CommTask(name));
240 /** @ingroup plugin_task
241 * @brief Smart constructor.
243 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
245 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
249 * @brief Do one execution of the Task.
250 * @note Call the on_this_start() func. Set its working status as true.
251 * Init and start the underlying Activity.
253 void CommTask::fire()
256 auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
258 comm->on_this_completion_cb([this](Comm const&) { this->complete(); });
259 set_current_activity(comm);
262 /** @ingroup plugin_task
263 * @param source The host to set.
264 * @brief Set a new source host.
266 CommTaskPtr CommTask::set_source(Host* source)
268 kernel::actor::simcall_answered([this, source] { source_ = source; });
272 /** @ingroup plugin_task
273 * @param destination The host to set.
274 * @brief Set a new destination host.
276 CommTaskPtr CommTask::set_destination(Host* destination)
278 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
282 /** @ingroup plugin_task
283 * @param bytes The amount of bytes to set.
285 CommTaskPtr CommTask::set_bytes(double bytes)
287 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
292 * @brief Default constructor.
294 IoTask::IoTask(const std::string& name) : Task(name) {}
296 /** @ingroup plugin_task
297 * @brief Smart Constructor.
299 IoTaskPtr IoTask::init(const std::string& name)
301 return IoTaskPtr(new IoTask(name));
304 /** @ingroup plugin_task
305 * @brief Smart Constructor.
307 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
309 return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
312 /** @ingroup plugin_task
313 * @param disk The disk to set.
314 * @brief Set a new disk.
316 IoTaskPtr IoTask::set_disk(Disk* disk)
318 kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
322 /** @ingroup plugin_task
323 * @param bytes The amount of bytes to set.
325 IoTaskPtr IoTask::set_bytes(double bytes)
327 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
331 /** @ingroup plugin_task */
332 IoTaskPtr IoTask::set_op_type(Io::OpType type)
334 kernel::actor::simcall_answered([this, type] { type_ = type; });
341 auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
343 io->on_this_completion_cb([this](Io const&) { this->complete(); });
344 set_current_activity(io);
347 } // namespace simgrid::s4u