Logo AND Algorithmique Numérique Distribuée

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