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.
90 * @note When increasing the degree the function starts new instances.
91 * When decreasing the degree the function does NOT stop running instances.
93 void Task::set_parallelism_degree(int n)
95 xbt_assert(n > 0, "Parallelism degree of Tasks must be above 0.");
96 simgrid::kernel::actor::simcall_answered([this, n] {
97 parallelism_degree_ = n;
98 while (ready_to_run())
103 /** @param n The number of firings to enqueue.
104 * @brief Enqueue firing.
105 * @note Immediatly fire an activity if possible.
107 void Task::enqueue_firings(int n)
109 simgrid::kernel::actor::simcall_answered([this, n] {
110 queued_firings_ += n;
111 while (ready_to_run())
116 /** @param name The new name to set.
117 * @brief Set the name of the Task.
119 void Task::set_name(std::string name)
124 /** @param amount The amount to set.
125 * @brief Set the amout of work to do.
126 * @note Amount in flop for ExecTask and in bytes for CommTask.
128 void Task::set_amount(double amount)
130 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
133 /** @param token The token to set.
134 * @brief Set the token to send to successors.
135 * @note The token is passed to each successor after the task end, i.e., after the on_end callback.
137 void Task::set_token(std::shared_ptr<Token> token)
139 simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
142 /** @return Map of tokens received for the next execution.
143 * @note If there is no queued execution for this task the map might not exist or be partially empty.
145 std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
147 return tokens_received_.front()[t];
152 if ((int)current_activities_.size() > parallelism_degree_) {
153 current_activities_.pop_front();
157 running_instances_++;
158 queued_firings_ = std::max(queued_firings_ - 1, 0);
159 if (not tokens_received_.empty())
160 tokens_received_.pop_front();
163 /** @param successor The Task to add.
164 * @brief Add a successor to this Task.
165 * @note It also adds this as a predecessor of successor.
167 void Task::add_successor(TaskPtr successor)
169 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
170 successors_.insert(successor_p);
171 successor_p->predecessors_.try_emplace(this, 0);
175 /** @param successor The Task to remove.
176 * @brief Remove a successor from this Task.
177 * @note It also remove this from the predecessors of successor.
179 void Task::remove_successor(TaskPtr successor)
181 simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
182 successor_p->predecessors_.erase(this);
183 successors_.erase(successor_p);
187 void Task::remove_all_successors()
189 simgrid::kernel::actor::simcall_answered([this] {
190 while (not successors_.empty()) {
191 auto* successor = *(successors_.begin());
192 successor->predecessors_.erase(this);
193 successors_.erase(successor);
199 * @brief Default constructor.
201 ExecTask::ExecTask(const std::string& name) : Task(name) {}
203 /** @ingroup plugin_task
204 * @brief Smart Constructor.
206 ExecTaskPtr ExecTask::init(const std::string& name)
208 return ExecTaskPtr(new ExecTask(name));
211 /** @ingroup plugin_task
212 * @brief Smart Constructor.
214 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
216 return init(name)->set_flops(flops)->set_host(host);
220 * @brief Do one execution of the Task.
221 * @note Call the on_this_start() func.
222 * Init and start the underlying Activity.
224 void ExecTask::fire()
227 auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
229 exec->on_this_completion_cb([this](Exec const&) { complete(); });
230 store_activity(exec);
233 /** @ingroup plugin_task
234 * @param host The host to set.
235 * @brief Set a new host.
237 ExecTaskPtr ExecTask::set_host(Host* host)
239 kernel::actor::simcall_answered([this, host] { host_ = host; });
243 /** @ingroup plugin_task
244 * @param flops The amount of flops to set.
246 ExecTaskPtr ExecTask::set_flops(double flops)
248 kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
253 * @brief Default constructor.
255 CommTask::CommTask(const std::string& name) : Task(name) {}
257 /** @ingroup plugin_task
258 * @brief Smart constructor.
260 CommTaskPtr CommTask::init(const std::string& name)
262 return CommTaskPtr(new CommTask(name));
265 /** @ingroup plugin_task
266 * @brief Smart constructor.
268 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
270 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
274 * @brief Do one execution of the Task.
275 * @note Call the on_this_start() func.
276 * Init and start the underlying Activity.
278 void CommTask::fire()
281 auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
283 comm->on_this_completion_cb([this](Comm const&) { complete(); });
284 store_activity(comm);
287 /** @ingroup plugin_task
288 * @param source The host to set.
289 * @brief Set a new source host.
291 CommTaskPtr CommTask::set_source(Host* source)
293 kernel::actor::simcall_answered([this, source] { source_ = source; });
297 /** @ingroup plugin_task
298 * @param destination The host to set.
299 * @brief Set a new destination host.
301 CommTaskPtr CommTask::set_destination(Host* destination)
303 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
307 /** @ingroup plugin_task
308 * @param bytes The amount of bytes to set.
310 CommTaskPtr CommTask::set_bytes(double bytes)
312 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
317 * @brief Default constructor.
319 IoTask::IoTask(const std::string& name) : Task(name) {}
321 /** @ingroup plugin_task
322 * @brief Smart Constructor.
324 IoTaskPtr IoTask::init(const std::string& name)
326 return IoTaskPtr(new IoTask(name));
329 /** @ingroup plugin_task
330 * @brief Smart Constructor.
332 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
334 return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
337 /** @ingroup plugin_task
338 * @param disk The disk to set.
339 * @brief Set a new disk.
341 IoTaskPtr IoTask::set_disk(Disk* disk)
343 kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
347 /** @ingroup plugin_task
348 * @param bytes The amount of bytes to set.
350 IoTaskPtr IoTask::set_bytes(double bytes)
352 kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
356 /** @ingroup plugin_task */
357 IoTaskPtr IoTask::set_op_type(Io::OpType type)
359 kernel::actor::simcall_answered([this, type] { type_ = type; });
366 auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
368 io->on_this_completion_cb([this](Io const&) { complete(); });
372 } // namespace simgrid::s4u