Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add possibility for a battery to be a simple connector and an example. add power...
[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
23
24   @endrst
25  */
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Task, kernel, "Logging specific to the task plugin");
27
28 namespace simgrid::s4u {
29
30 Task::Task(const std::string& name) : name_(name) {}
31
32 /**
33  *  @brief Return True if the Task can start a new Activity.
34  *  @note The Task is ready if not already doing something and there is at least one execution waiting in queue.
35  */
36 bool Task::ready_to_run() const
37 {
38   return running_instances_ < parallelism_degree_ && queued_firings_ > 0;
39 }
40
41 /**
42  *  @param source The sender.
43  *  @brief Receive a token from another Task.
44  *  @note Check upon reception if the Task has received a token from each of its predecessors,
45  * and in this case consumes those tokens and enqueue an execution.
46  */
47 void Task::receive(Task* source)
48 {
49   XBT_DEBUG("Task %s received a token from %s", name_.c_str(), source->name_.c_str());
50   auto source_count = predecessors_[source];
51   predecessors_[source]++;
52   if (tokens_received_.size() <= queued_firings_ + source_count)
53     tokens_received_.emplace_back();
54   tokens_received_[queued_firings_ + source_count][source] = source->token_;
55   bool enough_tokens                                       = true;
56   for (auto const& [key, val] : predecessors_)
57     if (val < 1) {
58       enough_tokens = false;
59       break;
60     }
61   if (enough_tokens) {
62     for (auto& [key, val] : predecessors_)
63       val--;
64     enqueue_firings(1);
65   }
66 }
67
68 /**
69  *  @brief Task routine when finishing an execution.
70  *  @note Set its working status as false.
71  * Add 1 to its count of finished executions.
72  * Call the on_this_end func.
73  * Fire on_end callback.
74  * Send a token to each of its successors.
75  * Start a new execution if possible.
76  */
77 void Task::complete()
78 {
79   xbt_assert(Actor::is_maestro());
80   running_instances_--;
81   count_++;
82   on_this_completion(this);
83   on_completion(this);
84   for (auto const& t : successors_)
85     t->receive(this);
86   if (ready_to_run())
87     fire();
88 }
89
90 /** @param n The new parallelism degree of the Task.
91  *  @brief Set the parallelism degree of the Task to inscrease or decrease horizontal scaling.
92  *  @note When increasing the degree the function starts new instances if there is queued firings.
93  *        When decreasing the degree the function does NOT stop running instances.
94
95  */
96 void Task::set_parallelism_degree(int n)
97 {
98   xbt_assert(n > 0, "Parallelism degree of Tasks must be above 0.");
99   simgrid::kernel::actor::simcall_answered([this, n] {
100     parallelism_degree_ = n;
101     while (ready_to_run())
102       fire();
103   });
104 }
105
106 /** @param n The number of firings to enqueue.
107  *  @brief Enqueue firing.
108  *  @note Immediatly fire an activity if possible.
109  */
110 void Task::enqueue_firings(int n)
111 {
112   simgrid::kernel::actor::simcall_answered([this, n] {
113     queued_firings_ += n;
114     while (ready_to_run())
115       fire();
116   });
117 }
118
119 /** @param name The new name to set.
120  *  @brief Set the name of the Task.
121  */
122 void Task::set_name(std::string name)
123 {
124   name_ = name;
125 }
126
127 /** @param amount The amount to set.
128  *  @brief Set the amout of work to do.
129  *  @note Amount in flop for ExecTask and in bytes for CommTask.
130  */
131 void Task::set_amount(double amount)
132 {
133   simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
134 }
135
136 /** @param token The token to set.
137  *  @brief Set the token to send to successors.
138  *  @note The token is passed to each successor after the task end, i.e., after the on_end callback.
139  */
140 void Task::set_token(std::shared_ptr<Token> token)
141 {
142   simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
143 }
144
145 /** @return Map of tokens received for the next execution.
146  *  @note If there is no queued execution for this task the map might not exist or be partially empty.
147  */
148 std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
149 {
150   return tokens_received_.front()[t];
151 }
152
153 void Task::fire()
154 {
155   if ((int)current_activities_.size() > parallelism_degree_) {
156     current_activities_.pop_front();
157   }
158   on_this_start(this);
159   on_start(this);
160   running_instances_++;
161   queued_firings_ = std::max(queued_firings_ - 1, 0);
162   if (not tokens_received_.empty())
163     tokens_received_.pop_front();
164 }
165
166 /** @param successor The Task to add.
167  *  @brief Add a successor to this Task.
168  *  @note It also adds this as a predecessor of successor.
169  */
170 void Task::add_successor(TaskPtr successor)
171 {
172   simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
173     successors_.insert(successor_p);
174     successor_p->predecessors_.try_emplace(this, 0);
175   });
176 }
177
178 /** @param successor The Task to remove.
179  *  @brief Remove a successor from this Task.
180  *  @note It also remove this from the predecessors of successor.
181  */
182 void Task::remove_successor(TaskPtr successor)
183 {
184   simgrid::kernel::actor::simcall_answered([this, successor_p = successor.get()] {
185     successor_p->predecessors_.erase(this);
186     successors_.erase(successor_p);
187   });
188 }
189
190 void Task::remove_all_successors()
191 {
192   simgrid::kernel::actor::simcall_answered([this] {
193     while (not successors_.empty()) {
194       auto* successor = *(successors_.begin());
195       successor->predecessors_.erase(this);
196       successors_.erase(successor);
197     }
198   });
199 }
200
201 /**
202  *  @brief Default constructor.
203  */
204 ExecTask::ExecTask(const std::string& name) : Task(name) {}
205
206 /** @ingroup plugin_task
207  *  @brief Smart Constructor.
208  */
209 ExecTaskPtr ExecTask::init(const std::string& name)
210 {
211   return ExecTaskPtr(new ExecTask(name));
212 }
213
214 /** @ingroup plugin_task
215  *  @brief Smart Constructor.
216  */
217 ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
218 {
219   return init(name)->set_flops(flops)->set_host(host);
220 }
221
222 /**
223  *  @brief Do one execution of the Task.
224  *  @note Call the on_this_start() func.
225  *  Init and start the underlying Activity.
226  */
227 void ExecTask::fire()
228 {
229   Task::fire();
230   auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_);
231   exec->start();
232   exec->on_this_completion_cb([this](Exec const&) { complete(); });
233   store_activity(exec);
234 }
235
236 /** @ingroup plugin_task
237  *  @param host The host to set.
238  *  @brief Set a new host.
239  */
240 ExecTaskPtr ExecTask::set_host(Host* host)
241 {
242   kernel::actor::simcall_answered([this, host] { host_ = host; });
243   return this;
244 }
245
246 /** @ingroup plugin_task
247  *  @param flops The amount of flops to set.
248  */
249 ExecTaskPtr ExecTask::set_flops(double flops)
250 {
251   kernel::actor::simcall_answered([this, flops] { set_amount(flops); });
252   return this;
253 }
254
255 /**
256  *  @brief Default constructor.
257  */
258 CommTask::CommTask(const std::string& name) : Task(name) {}
259
260 /** @ingroup plugin_task
261  *  @brief Smart constructor.
262  */
263 CommTaskPtr CommTask::init(const std::string& name)
264 {
265   return CommTaskPtr(new CommTask(name));
266 }
267
268 /** @ingroup plugin_task
269  *  @brief Smart constructor.
270  */
271 CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
272 {
273   return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
274 }
275
276 /**
277  *  @brief Do one execution of the Task.
278  *  @note Call the on_this_start() func.
279  *  Init and start the underlying Activity.
280  */
281 void CommTask::fire()
282 {
283   Task::fire();
284   auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
285   comm->start();
286   comm->on_this_completion_cb([this](Comm const&) { complete(); });
287   store_activity(comm);
288 }
289
290 /** @ingroup plugin_task
291  *  @param source The host to set.
292  *  @brief Set a new source host.
293  */
294 CommTaskPtr CommTask::set_source(Host* source)
295 {
296   kernel::actor::simcall_answered([this, source] { source_ = source; });
297   return this;
298 }
299
300 /** @ingroup plugin_task
301  *  @param destination The host to set.
302  *  @brief Set a new destination host.
303  */
304 CommTaskPtr CommTask::set_destination(Host* destination)
305 {
306   kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
307   return this;
308 }
309
310 /** @ingroup plugin_task
311  *  @param bytes The amount of bytes to set.
312  */
313 CommTaskPtr CommTask::set_bytes(double bytes)
314 {
315   kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
316   return this;
317 }
318
319 /**
320  *  @brief Default constructor.
321  */
322 IoTask::IoTask(const std::string& name) : Task(name) {}
323
324 /** @ingroup plugin_task
325  *  @brief Smart Constructor.
326  */
327 IoTaskPtr IoTask::init(const std::string& name)
328 {
329   return IoTaskPtr(new IoTask(name));
330 }
331
332 /** @ingroup plugin_task
333  *  @brief Smart Constructor.
334  */
335 IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
336 {
337   return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
338 }
339
340 /** @ingroup plugin_task
341  *  @param disk The disk to set.
342  *  @brief Set a new disk.
343  */
344 IoTaskPtr IoTask::set_disk(Disk* disk)
345 {
346   kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
347   return this;
348 }
349
350 /** @ingroup plugin_task
351  *  @param bytes The amount of bytes to set.
352  */
353 IoTaskPtr IoTask::set_bytes(double bytes)
354 {
355   kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
356   return this;
357 }
358
359 /** @ingroup plugin_task */
360 IoTaskPtr IoTask::set_op_type(Io::OpType type)
361 {
362   kernel::actor::simcall_answered([this, type] { type_ = type; });
363   return this;
364 }
365
366 void IoTask::fire()
367 {
368   Task::fire();
369   auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
370   io->start();
371   io->on_this_completion_cb([this](Io const&) { complete(); });
372   store_activity(io);
373 }
374
375 } // namespace simgrid::s4u