Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplify expression.
[simgrid.git] / src / plugins / operation.cpp
1 #include <simgrid/Exception.hpp>
2 #include <simgrid/plugins/operation.hpp>
3 #include <simgrid/s4u/Comm.hpp>
4 #include <simgrid/s4u/Exec.hpp>
5 #include <simgrid/simix.hpp>
6
7 #include "src/simgrid/module.hpp"
8
9 SIMGRID_REGISTER_PLUGIN(operation, "Battery management", nullptr)
10 /** @defgroup plugin_operation plugin_operation Plugin Operation
11
12   @beginrst
13
14 This is the operation plugin, enabling management of Operations.
15 To activate this plugin, first call :cpp:func:`Operation::init`.
16
17 Operations are designed to represent workflows, i.e, graphs of Operations.
18 Operations can only be instancied using either
19 :cpp:func:`simgrid::plugins::ExecOp::init` or :cpp:func:`simgrid::plugins::CommOp::init`
20 An ExecOp is an Execution Operation. Its underlying Activity is an :ref:`Exec <API_s4u_Exec>`.
21 A CommOp is a Communication Operation. Its underlying Activity is a :ref:`Comm <API_s4u_Comm>`.
22
23   @endrst
24  */
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Operation, kernel, "Logging specific to the operation plugin");
26
27 namespace simgrid::plugins {
28
29 xbt::signal<void(Operation*)> Operation::on_start;
30 xbt::signal<void(Operation*)> Operation::on_end;
31
32 Operation::Operation(const std::string& name) : name_(name) {}
33
34 std::string Operation::get_name()
35 {
36   return name_;
37 }
38
39 /**
40  *  @param predecessor The Operation to add.
41  *  @brief Add a predecessor to this Operation.
42  */
43 void Operation::add_predecessor(Operation* predecessor)
44 {
45   if (predecessors_.find(predecessor) == predecessors_.end())
46     simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_[predecessor] = 0; });
47 }
48
49 /**
50  *  @param predecessor The Operation to remove.
51  *  @brief Remove a predecessor from this Operation.
52  */
53 void Operation::remove_predecessor(Operation* predecessor)
54 {
55   simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_.erase(predecessor); });
56 }
57
58 /**
59  *  @brief Return True if the Operation can start a new Activity.
60  *  @note The Operation is ready if not already doing something and there is at least one execution waiting in queue.
61  */
62 bool Operation::ready_to_run() const
63 {
64   return not working_ && queued_execs_ > 0;
65 }
66
67 /**
68  *  @param source The sender.
69  *  @brief Receive a token from another Operation.
70  *  @note Check upon reception if the Operation has received a token from each of its predecessors,
71  * and in this case consumes those tokens and enqueue an execution.
72  */
73 void Operation::receive(Operation* source)
74 {
75   XBT_DEBUG("Operation %s received a token from %s", name_.c_str(), source->name_.c_str());
76   auto it = predecessors_.find(source);
77   simgrid::kernel::actor::simcall_answered([this, it] {
78     it->second++;
79     bool enough_tokens = true;
80     for (auto const& [key, val] : predecessors_)
81       if (val < 1) {
82         enough_tokens = false;
83         break;
84       }
85     if (enough_tokens) {
86       for (auto& [key, val] : predecessors_)
87         val--;
88       enqueue_execs(1);
89     }
90   });
91 }
92
93 /**
94  *  @brief Operation routine when finishing an execution.
95  *  @note Set its working status as false.
96  * Add 1 to its count of finished executions.
97  * Call the on_this_end func.
98  * Fire on_end callback.
99  * Send a token to each of its successors.
100  * Start a new execution if possible.
101  */
102 void Operation::complete()
103 {
104   simgrid::kernel::actor::simcall_answered([this] {
105     working_ = false;
106     count_++;
107   });
108   end_func_(this);
109   Operation::on_end(this);
110   for (auto const& op : successors_)
111     op->receive(this);
112   if (ready_to_run())
113     execute();
114 }
115
116 /** @ingroup plugin_operation
117  *  @brief Init the Operation plugin.
118  *  @note Add a completion callback to all Activities to call Operation::complete().
119  */
120 void Operation::init()
121 {
122   if (Operation::inited_)
123     return;
124   Operation::inited_                      = true;
125   ExtendedAttributeActivity::EXTENSION_ID = simgrid::s4u::Activity::extension_create<ExtendedAttributeActivity>();
126   simgrid::s4u::Activity::on_completion_cb([&](simgrid::s4u::Activity const& activity) {
127     activity.extension<ExtendedAttributeActivity>()->operation_->complete();
128   });
129 }
130
131 /** @ingroup plugin_operation
132  *  @param n The number of executions to enqueue.
133  *  @brief Enqueue executions.
134  *  @note Immediatly starts an execution if possible.
135  */
136 void Operation::enqueue_execs(int n)
137 {
138   simgrid::kernel::actor::simcall_answered([this, n] {
139     queued_execs_ += n;
140     if (ready_to_run())
141       execute();
142   });
143 }
144
145 /** @ingroup plugin_operation
146  *  @param amount The amount to set.
147  *  @brief Set the amout of work to do.
148  *  @note Amount in flop for ExecOp and in bytes for CommOp.
149  */
150 void Operation::set_amount(double amount)
151 {
152   simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
153 }
154
155 /** @ingroup plugin_operation
156  *  @param successor The Operation to add.
157  *  @brief Add a successor to this Operation.
158  *  @note It also adds this as a predecessor of successor.
159  */
160 void Operation::add_successor(OperationPtr successor)
161 {
162   simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); });
163   successor->add_predecessor(this);
164 }
165
166 /** @ingroup plugin_operation
167  *  @param successor The Operation to remove.
168  *  @brief Remove a successor from this Operation.
169  *  @note It also remove this from the predecessors of successor.
170  */
171 void Operation::remove_successor(OperationPtr successor)
172 {
173   simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); });
174   successor->remove_predecessor(this);
175 }
176
177 /** @ingroup plugin_operation
178  *  @param func The function to set.
179  *  @brief Set a function to be called before each execution.
180  *  @note The function is called before the underlying Activity starts.
181  */
182 void Operation::on_this_start(std::function<void(Operation*)> func)
183 {
184   simgrid::kernel::actor::simcall_answered([this, func] { start_func_ = func; });
185 }
186
187 /** @ingroup plugin_operation
188  *  @param func The function to set.
189  *  @brief Set a function to be called after each execution.
190  *  @note The function is called after the underlying Activity ends, but before sending tokens to successors.
191  */
192 void Operation::on_this_end(std::function<void(Operation*)> func)
193 {
194   simgrid::kernel::actor::simcall_answered([this, func] { end_func_ = func; });
195 }
196
197 /** @ingroup plugin_operation
198  *  @brief Return the number of completed executions.
199  */
200 int Operation::get_count()
201 {
202   return count_;
203 }
204
205 /**
206  *  @brief Default constructor.
207  */
208 ExecOp::ExecOp(const std::string& name) : Operation(name) {}
209
210 /** @ingroup plugin_operation
211  *  @brief Smart Constructor.
212  */
213 ExecOpPtr ExecOp::init(const std::string& name)
214 {
215   auto op = ExecOpPtr(new ExecOp(name));
216   return op;
217 }
218
219 /** @ingroup plugin_operation
220  *  @brief Smart Constructor.
221  */
222 ExecOpPtr ExecOp::init(const std::string& name, double flops, simgrid::s4u::Host* host)
223 {
224   auto op = ExecOpPtr(new ExecOp(name));
225   op->set_flops(flops);
226   op->set_host(host);
227   return op;
228 }
229
230 /**
231  *  @brief Do one execution of the Operation.
232  *  @note Call the on_this_start() func. Set its working status as true.
233  *  Init and start the underlying Activity.
234  */
235 void ExecOp::execute()
236 {
237   start_func_(this);
238   Operation::on_start(this);
239   simgrid::kernel::actor::simcall_answered([this] {
240     working_      = true;
241     queued_execs_ = std::max(queued_execs_ - 1, 0);
242   });
243   simgrid::s4u::ExecPtr exec = simgrid::s4u::Exec::init();
244   exec->set_name(name_);
245   exec->set_flops_amount(amount_);
246   exec->set_host(host_);
247   exec->start();
248   exec->extension_set(new ExtendedAttributeActivity());
249   exec->extension<ExtendedAttributeActivity>()->operation_ = this;
250   simgrid::kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
251 }
252
253 /** @ingroup plugin_operation
254  *  @param host The host to set.
255  *  @brief Set a new host.
256  */
257 void ExecOp::set_host(simgrid::s4u::Host* host)
258 {
259   simgrid::kernel::actor::simcall_answered([this, host] { host_ = host; });
260 }
261
262 /** @ingroup plugin_operation
263  *  @param flops The amount of flops to set.
264  */
265 void ExecOp::set_flops(double flops)
266 {
267   simgrid::kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
268 }
269
270 /**
271  *  @brief Default constructor.
272  */
273 CommOp::CommOp(const std::string& name) : Operation(name) {}
274
275 /** @ingroup plugin_operation
276  *  @brief Smart constructor.
277  */
278 CommOpPtr CommOp::init(const std::string& name)
279 {
280   auto op = CommOpPtr(new CommOp(name));
281   return op;
282 }
283
284 /** @ingroup plugin_operation
285  *  @brief Smart constructor.
286  */
287 CommOpPtr CommOp::init(const std::string& name, double bytes, simgrid::s4u::Host* source,
288                        simgrid::s4u::Host* destination)
289 {
290   auto op = CommOpPtr(new CommOp(name));
291   op->set_bytes(bytes);
292   op->set_source(source);
293   op->set_destination(destination);
294   return op;
295 }
296
297 /**
298  *  @brief Do one execution of the Operation.
299  *  @note Call the on_this_start() func. Set its working status as true.
300  *  Init and start the underlying Activity.
301  */
302 void CommOp::execute()
303 {
304   start_func_(this);
305   Operation::on_start(this);
306   simgrid::kernel::actor::simcall_answered([this] {
307     working_      = true;
308     queued_execs_ = std::max(queued_execs_ - 1, 0);
309   });
310   simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::sendto_init(source_, destination_);
311   comm->set_name(name_);
312   comm->set_payload_size(amount_);
313   comm->start();
314   comm->extension_set(new ExtendedAttributeActivity());
315   comm->extension<ExtendedAttributeActivity>()->operation_ = this;
316   simgrid::kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
317 }
318
319 /** @ingroup plugin_operation
320  *  @param source The host to set.
321  *  @brief Set a new source host.
322  */
323 void CommOp::set_source(simgrid::s4u::Host* source)
324 {
325   simgrid::kernel::actor::simcall_answered([this, source] { source_ = source; });
326 }
327
328 /** @ingroup plugin_operation
329  *  @param destination The host to set.
330  *  @brief Set a new destination host.
331  */
332 void CommOp::set_destination(simgrid::s4u::Host* destination)
333 {
334   simgrid::kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
335 }
336
337 /** @ingroup plugin_operation
338  *  @param bytes The amount of bytes to set.
339  */
340 void CommOp::set_bytes(double bytes)
341 {
342   simgrid::kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
343 }
344
345 } // namespace simgrid::plugins
346
347 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
348     simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
349 bool simgrid::plugins::Operation::inited_ = false;