1 #include <simgrid/Exception.hpp>
2 #include <simgrid/plugins/task.hpp>
3 #include <simgrid/s4u/Comm.hpp>
4 #include <simgrid/s4u/Exec.hpp>
5 #include <simgrid/s4u/Io.hpp>
6 #include <simgrid/simix.hpp>
8 #include "src/simgrid/module.hpp"
10 SIMGRID_REGISTER_PLUGIN(task, "Battery management", nullptr)
11 /** @defgroup plugin_task plugin_task Plugin Task
15 This is the task plugin, enabling management of Tasks.
16 To activate this plugin, first call :cpp:func:`Task::init`.
18 Tasks are designed to represent dataflows, i.e, graphs of Tasks.
19 Tasks can only be instancied using either
20 :cpp:func:`simgrid::plugins::ExecTask::init` or :cpp:func:`simgrid::plugins::CommTask::init`
21 An ExecTask is an Execution Task. Its underlying Activity is an :ref:`Exec <API_s4u_Exec>`.
22 A CommTask is a Communication Task. Its underlying Activity is a :ref:`Comm <API_s4u_Comm>`.
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Task, kernel, "Logging specific to the task plugin");
28 namespace simgrid::plugins {
30 xbt::signal<void(Task*)> Task::on_start;
31 xbt::signal<void(Task*)> Task::on_end;
33 Task::Task(const std::string& name) : name_(name) {}
36 * @param predecessor The Task to add.
37 * @brief Add a predecessor to this Task.
39 void Task::add_predecessor(Task* predecessor)
41 if (predecessors_.find(predecessor) == predecessors_.end())
42 simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_[predecessor] = 0; });
46 * @param predecessor The Task to remove.
47 * @brief Remove a predecessor from this Task.
49 void Task::remove_predecessor(Task* predecessor)
51 simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_.erase(predecessor); });
55 * @brief Return True if the Task can start a new Activity.
56 * @note The Task is ready if not already doing something and there is at least one execution waiting in queue.
58 bool Task::ready_to_run() const
60 return not working_ && queued_execs_ > 0;
64 * @param source The sender.
65 * @brief Receive a token from another Task.
66 * @note Check upon reception if the Task has received a token from each of its predecessors,
67 * and in this case consumes those tokens and enqueue an execution.
69 void Task::receive(Task* source)
71 XBT_DEBUG("Task %s received a token from %s", name_.c_str(), source->name_.c_str());
72 auto it = predecessors_.find(source);
73 simgrid::kernel::actor::simcall_answered([this, it] {
75 bool enough_tokens = true;
76 for (auto const& [key, val] : predecessors_)
78 enough_tokens = false;
82 for (auto& [key, val] : predecessors_)
90 * @brief Task routine when finishing an execution.
91 * @note Set its working status as false.
92 * Add 1 to its count of finished executions.
93 * Call the on_this_end func.
94 * Fire on_end callback.
95 * Send a token to each of its successors.
96 * Start a new execution if possible.
100 simgrid::kernel::actor::simcall_answered([this] {
104 for (auto const& end_func : end_func_handlers_)
107 for (auto const& t : successors_)
113 /** @ingroup plugin_task
114 * @brief Init the Task plugin.
115 * @note Add a completion callback to all Activities to call Task::complete().
121 Task::inited_ = true;
122 ExtendedAttributeActivity::EXTENSION_ID = simgrid::s4u::Activity::extension_create<ExtendedAttributeActivity>();
123 simgrid::s4u::Exec::on_completion_cb(
124 [](simgrid::s4u::Exec const& exec) { exec.extension<ExtendedAttributeActivity>()->task_->complete(); });
125 simgrid::s4u::Comm::on_completion_cb(
126 [](simgrid::s4u::Comm const& comm) { comm.extension<ExtendedAttributeActivity>()->task_->complete(); });
127 simgrid::s4u::Io::on_completion_cb(
128 [](simgrid::s4u::Io const& io) { io.extension<ExtendedAttributeActivity>()->task_->complete(); });
131 /** @ingroup plugin_task
132 * @param n The number of executions to enqueue.
133 * @brief Enqueue executions.
134 * @note Immediatly starts an execution if possible.
136 void Task::enqueue_execs(int n)
138 simgrid::kernel::actor::simcall_answered([this, n] {
145 /** @ingroup plugin_task
146 * @param amount The amount to set.
147 * @brief Set the amout of work to do.
148 * @note Amount in flop for ExecTask and in bytes for CommTask.
150 void Task::set_amount(double amount)
152 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
155 /** @ingroup plugin_task
156 * @param successor The Task to add.
157 * @brief Add a successor to this Task.
158 * @note It also adds this as a predecessor of successor.
160 void Task::add_successor(TaskPtr successor)
162 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); });
163 successor->add_predecessor(this);
166 /** @ingroup plugin_task
167 * @param successor The Task to remove.
168 * @brief Remove a successor from this Task.
169 * @note It also remove this from the predecessors of successor.
171 void Task::remove_successor(TaskPtr successor)
173 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); });
174 successor->remove_predecessor(this);
177 void Task::remove_all_successors()
179 simgrid::kernel::actor::simcall_answered([this] {
180 while (not successors_.empty()) {
181 auto* successor = *(successors_.begin());
182 successor->predecessors_.erase(this);
183 successors_.erase(successor);
188 /** @ingroup plugin_task
189 * @param func The function to set.
190 * @brief Set a function to be called before each execution.
191 * @note The function is called before the underlying Activity starts.
193 void Task::on_this_start(const std::function<void(Task*)>& func)
195 simgrid::kernel::actor::simcall_answered([this, &func] { start_func_handlers_.push_back(func); });
198 /** @ingroup plugin_task
199 * @param func The function to set.
200 * @brief Set a function to be called after each execution.
201 * @note The function is called after the underlying Activity ends, but before sending tokens to successors.
203 void Task::on_this_end(const std::function<void(Task*)>& func)
205 simgrid::kernel::actor::simcall_answered([this, &func] { end_func_handlers_.push_back(func); });
208 /** @ingroup plugin_task
209 * @brief Return the number of completed executions.
211 int Task::get_count() const
217 * @brief Default constructor.
219 ExecTask::ExecTask(const std::string& name) : Task(name) {}
221 /** @ingroup plugin_task
222 * @brief Smart Constructor.
224 ExecTaskPtr ExecTask::init(const std::string& name)
226 return ExecTaskPtr(new ExecTask(name));
229 /** @ingroup plugin_task
230 * @brief Smart Constructor.
232 ExecTaskPtr ExecTask::init(const std::string& name, double flops, s4u::Host* host)
234 return init(name)->set_flops(flops)->set_host(host);
238 * @brief Do one execution of the Task.
239 * @note Call the on_this_start() func. Set its working status as true.
240 * Init and start the underlying Activity.
242 void ExecTask::fire()
244 for (auto const& start_func : start_func_handlers_)
246 Task::on_start(this);
247 kernel::actor::simcall_answered([this] {
249 queued_execs_ = std::max(queued_execs_ - 1, 0);
251 s4u::ExecPtr exec = s4u::Exec::init();
252 exec->set_name(name_);
253 exec->set_flops_amount(amount_);
254 exec->set_host(host_);
256 exec->extension_set(new ExtendedAttributeActivity());
257 exec->extension<ExtendedAttributeActivity>()->task_ = this;
258 kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
261 /** @ingroup plugin_task
262 * @param host The host to set.
263 * @brief Set a new host.
265 ExecTaskPtr ExecTask::set_host(s4u::Host* host)
267 kernel::actor::simcall_answered([this, host] { host_ = host; });
271 /** @ingroup plugin_task
272 * @param flops The amount of flops to set.
274 ExecTaskPtr ExecTask::set_flops(double flops)
276 kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
281 * @brief Default constructor.
283 CommTask::CommTask(const std::string& name) : Task(name) {}
285 /** @ingroup plugin_task
286 * @brief Smart constructor.
288 CommTaskPtr CommTask::init(const std::string& name)
290 return CommTaskPtr(new CommTask(name));
293 /** @ingroup plugin_task
294 * @brief Smart constructor.
296 CommTaskPtr CommTask::init(const std::string& name, double bytes, s4u::Host* source, s4u::Host* destination)
298 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
302 * @brief Do one execution of the Task.
303 * @note Call the on_this_start() func. Set its working status as true.
304 * Init and start the underlying Activity.
306 void CommTask::fire()
308 for (auto const& start_func : start_func_handlers_)
310 Task::on_start(this);
311 kernel::actor::simcall_answered([this] {
313 queued_execs_ = std::max(queued_execs_ - 1, 0);
315 s4u::CommPtr comm = s4u::Comm::sendto_init(source_, destination_);
316 comm->set_name(name_);
317 comm->set_payload_size(amount_);
319 comm->extension_set(new ExtendedAttributeActivity());
320 comm->extension<ExtendedAttributeActivity>()->task_ = this;
321 kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
324 /** @ingroup plugin_task
325 * @param source The host to set.
326 * @brief Set a new source host.
328 CommTaskPtr CommTask::set_source(s4u::Host* source)
330 kernel::actor::simcall_answered([this, source] { source_ = source; });
334 /** @ingroup plugin_task
335 * @param destination The host to set.
336 * @brief Set a new destination host.
338 CommTaskPtr CommTask::set_destination(s4u::Host* destination)
340 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
344 /** @ingroup plugin_task
345 * @param bytes The amount of bytes to set.
347 CommTaskPtr CommTask::set_bytes(double bytes)
349 kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
354 * @brief Default constructor.
356 IoTask::IoTask(const std::string& name) : Task(name) {}
358 /** @ingroup plugin_task
359 * @brief Smart Constructor.
361 IoTaskPtr IoTask::init(const std::string& name)
363 return IoTaskPtr(new IoTask(name));
366 /** @ingroup plugin_task
367 * @brief Smart Constructor.
369 IoTaskPtr IoTask::init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type)
371 return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
374 /** @ingroup plugin_task
375 * @param disk The disk to set.
376 * @brief Set a new disk.
378 IoTaskPtr IoTask::set_disk(s4u::Disk* disk)
380 kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
384 /** @ingroup plugin_task
385 * @param bytes The amount of bytes to set.
387 IoTaskPtr IoTask::set_bytes(double bytes)
389 kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
393 /** @ingroup plugin_task */
394 IoTaskPtr IoTask::set_op_type(s4u::Io::OpType type)
396 kernel::actor::simcall_answered([this, type] { type_ = type; });
402 for (auto const& start_func : start_func_handlers_)
404 Task::on_start(this);
405 kernel::actor::simcall_answered([this] {
407 queued_execs_ = std::max(queued_execs_ - 1, 0);
409 s4u::IoPtr io = s4u::Io::init();
411 io->set_size(amount_);
413 io->set_op_type(type_);
415 io->extension_set(new ExtendedAttributeActivity());
416 io->extension<ExtendedAttributeActivity>()->task_ = this;
417 kernel::actor::simcall_answered([this, io] { current_activity_ = io; });
420 } // namespace simgrid::plugins
422 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
423 simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
424 bool simgrid::plugins::Task::inited_ = false;