Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Typo found by lintian
[simgrid.git] / src / s4u / s4u_Task.cpp
1 #include <memory>
2 #include <simgrid/Exception.hpp>
3 #include <simgrid/s4u/Task.hpp>
4 #include <simgrid/s4u/Comm.hpp>
5 #include <simgrid/s4u/Exec.hpp>
6 #include <simgrid/s4u/Io.hpp>
7 #include <simgrid/simix.hpp>
8
9 #include "src/simgrid/module.hpp"
10
11 SIMGRID_REGISTER_PLUGIN(task, "Battery management", nullptr)
12 /**
13   @beginrst
14
15
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>`.
21
22   @endrst
23  */
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Task, kernel, "Logging specific to the task plugin");
25
26 namespace simgrid::s4u {
27
28 Task::Task(const std::string& name) : name_(name) {}
29
30 /**
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.
33  */
34 bool Task::ready_to_run() const
35 {
36   return not working_ && queued_firings_ > 0;
37 }
38
39 /**
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.
44  */
45 void Task::receive(Task* source)
46 {
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_)
54     if (val < 1) {
55       enough_tokens = false;
56       break;
57     }
58   if (enough_tokens) {
59     for (auto& [key, val] : predecessors_)
60       val--;
61     enqueue_firings(1);
62   }
63 }
64
65 /**
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.
73  */
74 void Task::complete()
75 {
76   xbt_assert(Actor::is_maestro());
77   working_ = false;
78   count_++;
79   on_this_completion(this);
80   on_completion(this);
81   if (current_activity_)
82     previous_activity_ = std::move(current_activity_);
83   for (auto const& t : successors_)
84     t->receive(this);
85   if (ready_to_run())
86     fire();
87 }
88
89 /** @param n The number of firings to enqueue.
90  *  @brief Enqueue firing.
91  *  @note Immediatly fire an activity if possible.
92  */
93 void Task::enqueue_firings(int n)
94 {
95   simgrid::kernel::actor::simcall_answered([this, n] {
96     queued_firings_ += n;
97     if (ready_to_run())
98       fire();
99   });
100 }
101
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.
105  */
106 void Task::set_amount(double amount)
107 {
108   simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
109 }
110
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.
114  */
115 void Task::set_token(std::shared_ptr<Token> token)
116 {
117   simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
118 }
119
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.
122  */
123 std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
124 {
125   return tokens_received_.front()[t];
126 }
127
128 void Task::fire() {
129   on_this_start(this);
130   on_start(this);
131   working_ = true;
132   queued_firings_ = std::max(queued_firings_ - 1, 0);
133   if (tokens_received_.size() > 0)
134     tokens_received_.pop_front();
135 }
136
137 /** @param successor The Task to add.
138  *  @brief Add a successor to this Task.
139  *  @note It also adds this as a predecessor of successor.
140  */
141 void Task::add_successor(TaskPtr successor)
142 {
143   simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
144     successors_.insert(successor_p);
145     successor_p->predecessors_.try_emplace(this, 0);
146   });
147 }
148
149 /** @param successor The Task to remove.
150  *  @brief Remove a successor from this Task.
151  *  @note It also remove this from the predecessors of successor.
152  */
153 void Task::remove_successor(TaskPtr successor)
154 {
155   simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
156     successor_p->predecessors_.erase(this);
157     successors_.erase(successor_p);
158   });
159 }
160
161 void Task::remove_all_successors()
162 {
163   simgrid::kernel::actor::simcall_answered([this] {
164     while (not successors_.empty()) {
165       auto* successor = *(successors_.begin());
166       successor->predecessors_.erase(this);
167       successors_.erase(successor);
168     }
169   });
170 }
171
172 /**
173  *  @brief Default constructor.
174  */
175 ExecTask::ExecTask(const std::string& name) : Task(name) {}
176
177 /** @ingroup plugin_task
178  *  @brief Smart Constructor.
179  */
180 ExecTaskPtr ExecTask::init(const std::string& name)
181 {
182   return ExecTaskPtr(new ExecTask(name));
183 }
184
185 /** @ingroup plugin_task
186  *  @brief Smart Constructor.
187  */
188 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
189 {
190   return init(name)->set_flops(flops)->set_host(host);
191 }
192
193 /**
194  *  @brief Do one execution of the Task.
195  *  @note Call the on_this_start() func. Set its working status as true.
196  *  Init and start the underlying Activity.
197  */
198 void ExecTask::fire()
199 {
200   Task::fire();
201   auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
202   exec->start();
203   exec->on_this_completion_cb([this](Exec const&) { this->complete(); });
204   set_current_activity(exec);
205 }
206
207 /** @ingroup plugin_task
208  *  @param host The host to set.
209  *  @brief Set a new host.
210  */
211 ExecTaskPtr ExecTask::set_host(Host* host)
212 {
213   kernel::actor::simcall_answered([this, host] { host_ = host; });
214   return this;
215 }
216
217 /** @ingroup plugin_task
218  *  @param flops The amount of flops to set.
219  */
220 ExecTaskPtr ExecTask::set_flops(double flops)
221 {
222   kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
223   return this;
224 }
225
226 /**
227  *  @brief Default constructor.
228  */
229 CommTask::CommTask(const std::string& name) : Task(name) {}
230
231 /** @ingroup plugin_task
232  *  @brief Smart constructor.
233  */
234 CommTaskPtr CommTask::init(const std::string& name)
235 {
236   return CommTaskPtr(new CommTask(name));
237 }
238
239 /** @ingroup plugin_task
240  *  @brief Smart constructor.
241  */
242 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
243 {
244   return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
245 }
246
247 /**
248  *  @brief Do one execution of the Task.
249  *  @note Call the on_this_start() func. Set its working status as true.
250  *  Init and start the underlying Activity.
251  */
252 void CommTask::fire()
253 {
254   Task::fire();
255   auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
256   comm->start();
257   comm->on_this_completion_cb([this](Comm const&) { this->complete(); });
258   set_current_activity(comm);
259 }
260
261 /** @ingroup plugin_task
262  *  @param source The host to set.
263  *  @brief Set a new source host.
264  */
265 CommTaskPtr CommTask::set_source(Host* source)
266 {
267   kernel::actor::simcall_answered([this, source] { source_ = source; });
268   return this;
269 }
270
271 /** @ingroup plugin_task
272  *  @param destination The host to set.
273  *  @brief Set a new destination host.
274  */
275 CommTaskPtr CommTask::set_destination(Host* destination)
276 {
277   kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
278   return this;
279 }
280
281 /** @ingroup plugin_task
282  *  @param bytes The amount of bytes to set.
283  */
284 CommTaskPtr CommTask::set_bytes(double bytes)
285 {
286   kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
287   return this;
288 }
289
290 /**
291  *  @brief Default constructor.
292  */
293 IoTask::IoTask(const std::string& name) : Task(name) {}
294
295 /** @ingroup plugin_task
296  *  @brief Smart Constructor.
297  */
298 IoTaskPtr IoTask::init(const std::string& name)
299 {
300   return IoTaskPtr(new IoTask(name));
301 }
302
303 /** @ingroup plugin_task
304  *  @brief Smart Constructor.
305  */
306 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
307 {
308   return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
309 }
310
311 /** @ingroup plugin_task
312  *  @param disk The disk to set.
313  *  @brief Set a new disk.
314  */
315 IoTaskPtr IoTask::set_disk(Disk* disk)
316 {
317   kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
318   return this;
319 }
320
321 /** @ingroup plugin_task
322  *  @param bytes The amount of bytes to set.
323  */
324 IoTaskPtr IoTask::set_bytes(double bytes)
325 {
326   kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
327   return this;
328 }
329
330 /** @ingroup plugin_task */
331 IoTaskPtr IoTask::set_op_type(Io::OpType type)
332 {
333   kernel::actor::simcall_answered([this, type] { type_ = type; });
334   return this;
335 }
336
337 void IoTask::fire()
338 {
339   Task::fire();
340   auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
341   io->start();
342   io->on_this_completion_cb([this](Io const&) { this->complete(); });
343   set_current_activity(io);
344 }
345
346 } // namespace simgrid::s4u