Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / src / s4u / s4u_Task.cpp
1 #include <memory>
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>
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 {
130   on_this_start(this);
131   on_start(this);
132   working_        = true;
133   queued_firings_ = std::max(queued_firings_ - 1, 0);
134   if (tokens_received_.size() > 0)
135     tokens_received_.pop_front();
136 }
137
138 /** @param successor The Task to add.
139  *  @brief Add a successor to this Task.
140  *  @note It also adds this as a predecessor of successor.
141  */
142 void Task::add_successor(TaskPtr successor)
143 {
144   simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
145     successors_.insert(successor_p);
146     successor_p->predecessors_.try_emplace(this, 0);
147   });
148 }
149
150 /** @param successor The Task to remove.
151  *  @brief Remove a successor from this Task.
152  *  @note It also remove this from the predecessors of successor.
153  */
154 void Task::remove_successor(TaskPtr successor)
155 {
156   simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
157     successor_p->predecessors_.erase(this);
158     successors_.erase(successor_p);
159   });
160 }
161
162 void Task::remove_all_successors()
163 {
164   simgrid::kernel::actor::simcall_answered([this] {
165     while (not successors_.empty()) {
166       auto* successor = *(successors_.begin());
167       successor->predecessors_.erase(this);
168       successors_.erase(successor);
169     }
170   });
171 }
172
173 /**
174  *  @brief Default constructor.
175  */
176 ExecTask::ExecTask(const std::string& name) : Task(name) {}
177
178 /** @ingroup plugin_task
179  *  @brief Smart Constructor.
180  */
181 ExecTaskPtr ExecTask::init(const std::string& name)
182 {
183   return ExecTaskPtr(new ExecTask(name));
184 }
185
186 /** @ingroup plugin_task
187  *  @brief Smart Constructor.
188  */
189 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
190 {
191   return init(name)->set_flops(flops)->set_host(host);
192 }
193
194 /**
195  *  @brief Do one execution of the Task.
196  *  @note Call the on_this_start() func. Set its working status as true.
197  *  Init and start the underlying Activity.
198  */
199 void ExecTask::fire()
200 {
201   Task::fire();
202   auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
203   exec->start();
204   exec->on_this_completion_cb([this](Exec const&) { this->complete(); });
205   set_current_activity(exec);
206 }
207
208 /** @ingroup plugin_task
209  *  @param host The host to set.
210  *  @brief Set a new host.
211  */
212 ExecTaskPtr ExecTask::set_host(Host* host)
213 {
214   kernel::actor::simcall_answered([this, host] { host_ = host; });
215   return this;
216 }
217
218 /** @ingroup plugin_task
219  *  @param flops The amount of flops to set.
220  */
221 ExecTaskPtr ExecTask::set_flops(double flops)
222 {
223   kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
224   return this;
225 }
226
227 /**
228  *  @brief Default constructor.
229  */
230 CommTask::CommTask(const std::string& name) : Task(name) {}
231
232 /** @ingroup plugin_task
233  *  @brief Smart constructor.
234  */
235 CommTaskPtr CommTask::init(const std::string& name)
236 {
237   return CommTaskPtr(new CommTask(name));
238 }
239
240 /** @ingroup plugin_task
241  *  @brief Smart constructor.
242  */
243 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
244 {
245   return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
246 }
247
248 /**
249  *  @brief Do one execution of the Task.
250  *  @note Call the on_this_start() func. Set its working status as true.
251  *  Init and start the underlying Activity.
252  */
253 void CommTask::fire()
254 {
255   Task::fire();
256   auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
257   comm->start();
258   comm->on_this_completion_cb([this](Comm const&) { this->complete(); });
259   set_current_activity(comm);
260 }
261
262 /** @ingroup plugin_task
263  *  @param source The host to set.
264  *  @brief Set a new source host.
265  */
266 CommTaskPtr CommTask::set_source(Host* source)
267 {
268   kernel::actor::simcall_answered([this, source] { source_ = source; });
269   return this;
270 }
271
272 /** @ingroup plugin_task
273  *  @param destination The host to set.
274  *  @brief Set a new destination host.
275  */
276 CommTaskPtr CommTask::set_destination(Host* destination)
277 {
278   kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
279   return this;
280 }
281
282 /** @ingroup plugin_task
283  *  @param bytes The amount of bytes to set.
284  */
285 CommTaskPtr CommTask::set_bytes(double bytes)
286 {
287   kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
288   return this;
289 }
290
291 /**
292  *  @brief Default constructor.
293  */
294 IoTask::IoTask(const std::string& name) : Task(name) {}
295
296 /** @ingroup plugin_task
297  *  @brief Smart Constructor.
298  */
299 IoTaskPtr IoTask::init(const std::string& name)
300 {
301   return IoTaskPtr(new IoTask(name));
302 }
303
304 /** @ingroup plugin_task
305  *  @brief Smart Constructor.
306  */
307 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
308 {
309   return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
310 }
311
312 /** @ingroup plugin_task
313  *  @param disk The disk to set.
314  *  @brief Set a new disk.
315  */
316 IoTaskPtr IoTask::set_disk(Disk* disk)
317 {
318   kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
319   return this;
320 }
321
322 /** @ingroup plugin_task
323  *  @param bytes The amount of bytes to set.
324  */
325 IoTaskPtr IoTask::set_bytes(double bytes)
326 {
327   kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
328   return this;
329 }
330
331 /** @ingroup plugin_task */
332 IoTaskPtr IoTask::set_op_type(Io::OpType type)
333 {
334   kernel::actor::simcall_answered([this, type] { type_ = type; });
335   return this;
336 }
337
338 void IoTask::fire()
339 {
340   Task::fire();
341   auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
342   io->start();
343   io->on_this_completion_cb([this](Io const&) { this->complete(); });
344   set_current_activity(io);
345 }
346
347 } // namespace simgrid::s4u