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 running_instances_ < parallelism_degree_ && 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 for (auto const& t : successors_)
88 /** @param n The new parallelism degree of the Task.
89 * @brief Set the parallelism degree of the Task to inscrease or decrease horizontal scaling.
90 * @note When increasing the degree the function starts new instances if there is queued firings.
91 * When decreasing the degree the function does NOT stop running instances.
94 void Task::set_parallelism_degree(int n)
96 xbt_assert(n > 0, "Parallelism degree of Tasks must be above 0.");
97 simgrid::kernel::actor::simcall_answered([this, n] {
98 parallelism_degree_ = n;
99 while (ready_to_run())
104 /** @param n The number of firings to enqueue.
105 * @brief Enqueue firing.
106 * @note Immediatly fire an activity if possible.
108 void Task::enqueue_firings(int n)
110 simgrid::kernel::actor::simcall_answered([this, n] {
111 queued_firings_ += n;
112 while (ready_to_run())
117 /** @param name The new name to set.
118 * @brief Set the name of the Task.
120 void Task::set_name(std::string name)
125 /** @param amount The amount to set.
126 * @brief Set the amout of work to do.
127 * @note Amount in flop for ExecTask and in bytes for CommTask.
129 void Task::set_amount(double amount)
131 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
134 /** @param token The token to set.
135 * @brief Set the token to send to successors.
136 * @note The token is passed to each successor after the task end, i.e., after the on_end callback.
138 void Task::set_token(std::shared_ptr<Token> token)
140 simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
143 /** @return Map of tokens received for the next execution.
144 * @note If there is no queued execution for this task the map might not exist or be partially empty.
146 std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
148 return tokens_received_.front()[t];
153 if ((int)current_activities_.size() > parallelism_degree_) {
154 current_activities_.pop_front();
158 running_instances_++;
159 queued_firings_ = std::max(queued_firings_ - 1, 0);
160 if (not tokens_received_.empty())
161 tokens_received_.pop_front();
164 /** @param successor The Task to add.
165 * @brief Add a successor to this Task.
166 * @note It also adds this as a predecessor of successor.
168 void Task::add_successor(TaskPtr successor)
170 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
171 successors_.insert(successor_p);
172 successor_p->predecessors_.try_emplace(this, 0);
176 /** @param successor The Task to remove.
177 * @brief Remove a successor from this Task.
178 * @note It also remove this from the predecessors of successor.
180 void Task::remove_successor(TaskPtr successor)
182 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
183 successor_p->predecessors_.erase(this);
184 successors_.erase(successor_p);
188 void Task::remove_all_successors()
190 simgrid::kernel::actor::simcall_answered([this] {
191 while (not successors_.empty()) {
192 auto* successor = *(successors_.begin());
193 successor->predecessors_.erase(this);
194 successors_.erase(successor);
200 * @brief Default constructor.
202 ExecTask::ExecTask(const std::string& name) : Task(name) {}
204 /** @ingroup plugin_task
205 * @brief Smart Constructor.
207 ExecTaskPtr ExecTask::init(const std::string& name)
209 return ExecTaskPtr(new ExecTask(name));
212 /** @ingroup plugin_task
213 * @brief Smart Constructor.
215 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
217 return init(name)->set_flops(flops)->set_host(host);
221 * @brief Do one execution of the Task.
222 * @note Call the on_this_start() func.
223 * Init and start the underlying Activity.
225 void ExecTask::fire()
228 auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
230 exec->on_this_completion_cb([this](Exec const&) { complete(); });
231 store_activity(exec);
234 /** @ingroup plugin_task
235 * @param host The host to set.
236 * @brief Set a new host.
238 ExecTaskPtr ExecTask::set_host(Host* host)
240 kernel::actor::simcall_answered([this, host] { host_ = host; });
244 /** @ingroup plugin_task
245 * @param flops The amount of flops to set.
247 ExecTaskPtr ExecTask::set_flops(double flops)
249 kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
254 * @brief Default constructor.
256 CommTask::CommTask(const std::string& name) : Task(name) {}
258 /** @ingroup plugin_task
259 * @brief Smart constructor.
261 CommTaskPtr CommTask::init(const std::string& name)
263 return CommTaskPtr(new CommTask(name));
266 /** @ingroup plugin_task
267 * @brief Smart constructor.
269 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
271 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
275 * @brief Do one execution of the Task.
276 * @note Call the on_this_start() func.
277 * Init and start the underlying Activity.
279 void CommTask::fire()
282 auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
284 comm->on_this_completion_cb([this](Comm const&) { complete(); });
285 store_activity(comm);
288 /** @ingroup plugin_task
289 * @param source The host to set.
290 * @brief Set a new source host.
292 CommTaskPtr CommTask::set_source(Host* source)
294 kernel::actor::simcall_answered([this, source] { source_ = source; });
298 /** @ingroup plugin_task
299 * @param destination The host to set.
300 * @brief Set a new destination host.
302 CommTaskPtr CommTask::set_destination(Host* destination)
304 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
308 /** @ingroup plugin_task
309 * @param bytes The amount of bytes to set.
311 CommTaskPtr CommTask::set_bytes(double bytes)
313 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
318 * @brief Default constructor.
320 IoTask::IoTask(const std::string& name) : Task(name) {}
322 /** @ingroup plugin_task
323 * @brief Smart Constructor.
325 IoTaskPtr IoTask::init(const std::string& name)
327 return IoTaskPtr(new IoTask(name));
330 /** @ingroup plugin_task
331 * @brief Smart Constructor.
333 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
335 return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
338 /** @ingroup plugin_task
339 * @param disk The disk to set.
340 * @brief Set a new disk.
342 IoTaskPtr IoTask::set_disk(Disk* disk)
344 kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
348 /** @ingroup plugin_task
349 * @param bytes The amount of bytes to set.
351 IoTaskPtr IoTask::set_bytes(double bytes)
353 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
357 /** @ingroup plugin_task */
358 IoTaskPtr IoTask::set_op_type(Io::OpType type)
360 kernel::actor::simcall_answered([this, type] { type_ = type; });
367 auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
369 io->on_this_completion_cb([this](Io const&) { complete(); });
373 } // namespace simgrid::s4u