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>`.
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Task, kernel, "Logging specific to the task plugin");
28 namespace simgrid::s4u {
30 Task::Task(const std::string& name) : name_(name) {}
33 * @brief Return True if the Task can start a new Activity.
34 * @note The Task is ready if not already doing something and there is at least one execution waiting in queue.
36 bool Task::ready_to_run() const
38 return running_instances_ < parallelism_degree_ && queued_firings_ > 0;
42 * @param source The sender.
43 * @brief Receive a token from another Task.
44 * @note Check upon reception if the Task has received a token from each of its predecessors,
45 * and in this case consumes those tokens and enqueue an execution.
47 void Task::receive(Task* source)
49 XBT_DEBUG("Task %s received a token from %s", name_.c_str(), source->name_.c_str());
50 auto source_count = predecessors_[source];
51 predecessors_[source]++;
52 if (tokens_received_.size() <= queued_firings_ + source_count)
53 tokens_received_.emplace_back();
54 tokens_received_[queued_firings_ + source_count][source] = source->token_;
55 bool enough_tokens = true;
56 for (auto const& [key, val] : predecessors_)
58 enough_tokens = false;
62 for (auto& [key, val] : predecessors_)
69 * @brief Task routine when finishing an execution.
70 * @note Set its working status as false.
71 * Add 1 to its count of finished executions.
72 * Call the on_this_end func.
73 * Fire on_end callback.
74 * Send a token to each of its successors.
75 * Start a new execution if possible.
79 xbt_assert(Actor::is_maestro());
82 on_this_completion(this);
84 for (auto const& t : successors_)
90 /** @param n The new parallelism degree of the Task.
91 * @brief Set the parallelism degree of the Task to inscrease or decrease horizontal scaling.
92 * @note When increasing the degree the function starts new instances if there is queued firings.
93 * When decreasing the degree the function does NOT stop running instances.
96 void Task::set_parallelism_degree(int n)
98 xbt_assert(n > 0, "Parallelism degree of Tasks must be above 0.");
99 simgrid::kernel::actor::simcall_answered([this, n] {
100 parallelism_degree_ = n;
101 while (ready_to_run())
106 /** @param n The number of firings to enqueue.
107 * @brief Enqueue firing.
108 * @note Immediatly fire an activity if possible.
110 void Task::enqueue_firings(int n)
112 simgrid::kernel::actor::simcall_answered([this, n] {
113 queued_firings_ += n;
114 while (ready_to_run())
119 /** @param name The new name to set.
120 * @brief Set the name of the Task.
122 void Task::set_name(std::string name)
127 /** @param amount The amount to set.
128 * @brief Set the amout of work to do.
129 * @note Amount in flop for ExecTask and in bytes for CommTask.
131 void Task::set_amount(double amount)
133 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
136 /** @param token The token to set.
137 * @brief Set the token to send to successors.
138 * @note The token is passed to each successor after the task end, i.e., after the on_end callback.
140 void Task::set_token(std::shared_ptr<Token> token)
142 simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
145 /** @return Map of tokens received for the next execution.
146 * @note If there is no queued execution for this task the map might not exist or be partially empty.
148 std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
150 return tokens_received_.front()[t];
155 if ((int)current_activities_.size() > parallelism_degree_) {
156 current_activities_.pop_front();
160 running_instances_++;
161 queued_firings_ = std::max(queued_firings_ - 1, 0);
162 if (not tokens_received_.empty())
163 tokens_received_.pop_front();
166 /** @param successor The Task to add.
167 * @brief Add a successor to this Task.
168 * @note It also adds this as a predecessor of successor.
170 void Task::add_successor(TaskPtr successor)
172 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
173 successors_.insert(successor_p);
174 successor_p->predecessors_.try_emplace(this, 0);
178 /** @param successor The Task to remove.
179 * @brief Remove a successor from this Task.
180 * @note It also remove this from the predecessors of successor.
182 void Task::remove_successor(TaskPtr successor)
184 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
185 successor_p->predecessors_.erase(this);
186 successors_.erase(successor_p);
190 void Task::remove_all_successors()
192 simgrid::kernel::actor::simcall_answered([this] {
193 while (not successors_.empty()) {
194 auto* successor = *(successors_.begin());
195 successor->predecessors_.erase(this);
196 successors_.erase(successor);
202 * @brief Default constructor.
204 ExecTask::ExecTask(const std::string& name) : Task(name) {}
206 /** @ingroup plugin_task
207 * @brief Smart Constructor.
209 ExecTaskPtr ExecTask::init(const std::string& name)
211 return ExecTaskPtr(new ExecTask(name));
214 /** @ingroup plugin_task
215 * @brief Smart Constructor.
217 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
219 return init(name)->set_flops(flops)->set_host(host);
223 * @brief Do one execution of the Task.
224 * @note Call the on_this_start() func.
225 * Init and start the underlying Activity.
227 void ExecTask::fire()
230 auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
232 exec->on_this_completion_cb([this](Exec const&) { complete(); });
233 store_activity(exec);
236 /** @ingroup plugin_task
237 * @param host The host to set.
238 * @brief Set a new host.
240 ExecTaskPtr ExecTask::set_host(Host* host)
242 kernel::actor::simcall_answered([this, host] { host_ = host; });
246 /** @ingroup plugin_task
247 * @param flops The amount of flops to set.
249 ExecTaskPtr ExecTask::set_flops(double flops)
251 kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
256 * @brief Default constructor.
258 CommTask::CommTask(const std::string& name) : Task(name) {}
260 /** @ingroup plugin_task
261 * @brief Smart constructor.
263 CommTaskPtr CommTask::init(const std::string& name)
265 return CommTaskPtr(new CommTask(name));
268 /** @ingroup plugin_task
269 * @brief Smart constructor.
271 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
273 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
277 * @brief Do one execution of the Task.
278 * @note Call the on_this_start() func.
279 * Init and start the underlying Activity.
281 void CommTask::fire()
284 auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
286 comm->on_this_completion_cb([this](Comm const&) { complete(); });
287 store_activity(comm);
290 /** @ingroup plugin_task
291 * @param source The host to set.
292 * @brief Set a new source host.
294 CommTaskPtr CommTask::set_source(Host* source)
296 kernel::actor::simcall_answered([this, source] { source_ = source; });
300 /** @ingroup plugin_task
301 * @param destination The host to set.
302 * @brief Set a new destination host.
304 CommTaskPtr CommTask::set_destination(Host* destination)
306 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
310 /** @ingroup plugin_task
311 * @param bytes The amount of bytes to set.
313 CommTaskPtr CommTask::set_bytes(double bytes)
315 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
320 * @brief Default constructor.
322 IoTask::IoTask(const std::string& name) : Task(name) {}
324 /** @ingroup plugin_task
325 * @brief Smart Constructor.
327 IoTaskPtr IoTask::init(const std::string& name)
329 return IoTaskPtr(new IoTask(name));
332 /** @ingroup plugin_task
333 * @brief Smart Constructor.
335 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
337 return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
340 /** @ingroup plugin_task
341 * @param disk The disk to set.
342 * @brief Set a new disk.
344 IoTaskPtr IoTask::set_disk(Disk* disk)
346 kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
350 /** @ingroup plugin_task
351 * @param bytes The amount of bytes to set.
353 IoTaskPtr IoTask::set_bytes(double bytes)
355 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
359 /** @ingroup plugin_task */
360 IoTaskPtr IoTask::set_op_type(Io::OpType type)
362 kernel::actor::simcall_answered([this, type] { type_ = type; });
369 auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
371 io->on_this_completion_cb([this](Io const&) { complete(); });
375 } // namespace simgrid::s4u