Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d899e1accaf6c92931cd61f4e87bbe45f006450f
[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 running_instances_ < parallelism_degree_ && 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   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_)
55     if (val < 1) {
56       enough_tokens = false;
57       break;
58     }
59   if (enough_tokens) {
60     for (auto& [key, val] : predecessors_)
61       val--;
62     enqueue_firings(1);
63   }
64 }
65
66 /**
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.
74  */
75 void Task::complete()
76 {
77   xbt_assert(Actor::is_maestro());
78   running_instances_--;
79   count_++;
80   on_this_completion(this);
81   on_completion(this);
82   for (auto const& t : successors_)
83     t->receive(this);
84   if (ready_to_run())
85     fire();
86 }
87
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.
92  */
93 void Task::set_parallelism_degree(int n)
94 {
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())
99       fire();
100   });
101 }
102
103 /** @param n The number of firings to enqueue.
104  *  @brief Enqueue firing.
105  *  @note Immediatly fire an activity if possible.
106  */
107 void Task::enqueue_firings(int n)
108 {
109   simgrid::kernel::actor::simcall_answered([this, n] {
110     queued_firings_ += n;
111     while (ready_to_run())
112       fire();
113   });
114 }
115
116 /** @param name The new name to set.
117  *  @brief Set the name of the Task.
118  */
119 void Task::set_name(std::string name)
120 {
121   name_ = name;
122 }
123
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.
127  */
128 void Task::set_amount(double amount)
129 {
130   simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
131 }
132
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.
136  */
137 void Task::set_token(std::shared_ptr<Token> token)
138 {
139   simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
140 }
141
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.
144  */
145 std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
146 {
147   return tokens_received_.front()[t];
148 }
149
150 void Task::fire()
151 {
152   if ((int)current_activities_.size() > parallelism_degree_) {
153     current_activities_.pop_front();
154   }
155   on_this_start(this);
156   on_start(this);
157   running_instances_++;
158   queued_firings_ = std::max(queued_firings_ - 1, 0);
159   if (not tokens_received_.empty())
160     tokens_received_.pop_front();
161 }
162
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.
166  */
167 void Task::add_successor(TaskPtr successor)
168 {
169   simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
170     successors_.insert(successor_p);
171     successor_p->predecessors_.try_emplace(this, 0);
172   });
173 }
174
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.
178  */
179 void Task::remove_successor(TaskPtr successor)
180 {
181   simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
182     successor_p->predecessors_.erase(this);
183     successors_.erase(successor_p);
184   });
185 }
186
187 void Task::remove_all_successors()
188 {
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);
194     }
195   });
196 }
197
198 /**
199  *  @brief Default constructor.
200  */
201 ExecTask::ExecTask(const std::string& name) : Task(name) {}
202
203 /** @ingroup plugin_task
204  *  @brief Smart Constructor.
205  */
206 ExecTaskPtr ExecTask::init(const std::string& name)
207 {
208   return ExecTaskPtr(new ExecTask(name));
209 }
210
211 /** @ingroup plugin_task
212  *  @brief Smart Constructor.
213  */
214 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
215 {
216   return init(name)->set_flops(flops)->set_host(host);
217 }
218
219 /**
220  *  @brief Do one execution of the Task.
221  *  @note Call the on_this_start() func.
222  *  Init and start the underlying Activity.
223  */
224 void ExecTask::fire()
225 {
226   Task::fire();
227   auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
228   exec->start();
229   exec->on_this_completion_cb([this](Exec const&) { complete(); });
230   store_activity(exec);
231 }
232
233 /** @ingroup plugin_task
234  *  @param host The host to set.
235  *  @brief Set a new host.
236  */
237 ExecTaskPtr ExecTask::set_host(Host* host)
238 {
239   kernel::actor::simcall_answered([this, host] { host_ = host; });
240   return this;
241 }
242
243 /** @ingroup plugin_task
244  *  @param flops The amount of flops to set.
245  */
246 ExecTaskPtr ExecTask::set_flops(double flops)
247 {
248   kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
249   return this;
250 }
251
252 /**
253  *  @brief Default constructor.
254  */
255 CommTask::CommTask(const std::string& name) : Task(name) {}
256
257 /** @ingroup plugin_task
258  *  @brief Smart constructor.
259  */
260 CommTaskPtr CommTask::init(const std::string& name)
261 {
262   return CommTaskPtr(new CommTask(name));
263 }
264
265 /** @ingroup plugin_task
266  *  @brief Smart constructor.
267  */
268 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
269 {
270   return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
271 }
272
273 /**
274  *  @brief Do one execution of the Task.
275  *  @note Call the on_this_start() func.
276  *  Init and start the underlying Activity.
277  */
278 void CommTask::fire()
279 {
280   Task::fire();
281   auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
282   comm->start();
283   comm->on_this_completion_cb([this](Comm const&) { complete(); });
284   store_activity(comm);
285 }
286
287 /** @ingroup plugin_task
288  *  @param source The host to set.
289  *  @brief Set a new source host.
290  */
291 CommTaskPtr CommTask::set_source(Host* source)
292 {
293   kernel::actor::simcall_answered([this, source] { source_ = source; });
294   return this;
295 }
296
297 /** @ingroup plugin_task
298  *  @param destination The host to set.
299  *  @brief Set a new destination host.
300  */
301 CommTaskPtr CommTask::set_destination(Host* destination)
302 {
303   kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
304   return this;
305 }
306
307 /** @ingroup plugin_task
308  *  @param bytes The amount of bytes to set.
309  */
310 CommTaskPtr CommTask::set_bytes(double bytes)
311 {
312   kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
313   return this;
314 }
315
316 /**
317  *  @brief Default constructor.
318  */
319 IoTask::IoTask(const std::string& name) : Task(name) {}
320
321 /** @ingroup plugin_task
322  *  @brief Smart Constructor.
323  */
324 IoTaskPtr IoTask::init(const std::string& name)
325 {
326   return IoTaskPtr(new IoTask(name));
327 }
328
329 /** @ingroup plugin_task
330  *  @brief Smart Constructor.
331  */
332 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
333 {
334   return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
335 }
336
337 /** @ingroup plugin_task
338  *  @param disk The disk to set.
339  *  @brief Set a new disk.
340  */
341 IoTaskPtr IoTask::set_disk(Disk* disk)
342 {
343   kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
344   return this;
345 }
346
347 /** @ingroup plugin_task
348  *  @param bytes The amount of bytes to set.
349  */
350 IoTaskPtr IoTask::set_bytes(double bytes)
351 {
352   kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
353   return this;
354 }
355
356 /** @ingroup plugin_task */
357 IoTaskPtr IoTask::set_op_type(Io::OpType type)
358 {
359   kernel::actor::simcall_answered([this, type] { type_ = type; });
360   return this;
361 }
362
363 void IoTask::fire()
364 {
365   Task::fire();
366   auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
367   io->start();
368   io->on_this_completion_cb([this](Io const&) { complete(); });
369   store_activity(io);
370 }
371
372 } // namespace simgrid::s4u