Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill useless empty function.
[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   if (end_func_)
109     end_func_(this);
110   Operation::on_end(this);
111   for (auto const& op : successors_)
112     op->receive(this);
113   if (ready_to_run())
114     execute();
115 }
116
117 /** @ingroup plugin_operation
118  *  @brief Init the Operation plugin.
119  *  @note Add a completion callback to all Activities to call Operation::complete().
120  */
121 void Operation::init()
122 {
123   if (Operation::inited_)
124     return;
125   Operation::inited_                      = true;
126   ExtendedAttributeActivity::EXTENSION_ID = simgrid::s4u::Activity::extension_create<ExtendedAttributeActivity>();
127   simgrid::s4u::Activity::on_completion_cb([&](simgrid::s4u::Activity const& activity) {
128     activity.extension<ExtendedAttributeActivity>()->operation_->complete();
129   });
130 }
131
132 /** @ingroup plugin_operation
133  *  @param n The number of executions to enqueue.
134  *  @brief Enqueue executions.
135  *  @note Immediatly starts an execution if possible.
136  */
137 void Operation::enqueue_execs(int n)
138 {
139   simgrid::kernel::actor::simcall_answered([this, n] {
140     queued_execs_ += n;
141     if (ready_to_run())
142       execute();
143   });
144 }
145
146 /** @ingroup plugin_operation
147  *  @param amount The amount to set.
148  *  @brief Set the amout of work to do.
149  *  @note Amount in flop for ExecOp and in bytes for CommOp.
150  */
151 void Operation::set_amount(double amount)
152 {
153   simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
154 }
155
156 /** @ingroup plugin_operation
157  *  @param successor The Operation to add.
158  *  @brief Add a successor to this Operation.
159  *  @note It also adds this as a predecessor of successor.
160  */
161 void Operation::add_successor(OperationPtr successor)
162 {
163   simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); });
164   successor->add_predecessor(this);
165 }
166
167 /** @ingroup plugin_operation
168  *  @param successor The Operation to remove.
169  *  @brief Remove a successor from this Operation.
170  *  @note It also remove this from the predecessors of successor.
171  */
172 void Operation::remove_successor(OperationPtr successor)
173 {
174   simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); });
175   successor->remove_predecessor(this);
176 }
177
178 /** @ingroup plugin_operation
179  *  @param func The function to set.
180  *  @brief Set a function to be called before each execution.
181  *  @note The function is called before the underlying Activity starts.
182  */
183 void Operation::on_this_start(std::function<void(Operation*)> func)
184 {
185   simgrid::kernel::actor::simcall_answered([this, func] { start_func_ = func; });
186 }
187
188 /** @ingroup plugin_operation
189  *  @param func The function to set.
190  *  @brief Set a function to be called after each execution.
191  *  @note The function is called after the underlying Activity ends, but before sending tokens to successors.
192  */
193 void Operation::on_this_end(std::function<void(Operation*)> func)
194 {
195   simgrid::kernel::actor::simcall_answered([this, func] { end_func_ = func; });
196 }
197
198 /** @ingroup plugin_operation
199  *  @brief Return the number of completed executions.
200  */
201 int Operation::get_count()
202 {
203   return count_;
204 }
205
206 /**
207  *  @brief Default constructor.
208  */
209 ExecOp::ExecOp(const std::string& name) : Operation(name) {}
210
211 /** @ingroup plugin_operation
212  *  @brief Smart Constructor.
213  */
214 ExecOpPtr ExecOp::init(const std::string& name)
215 {
216   auto op = ExecOpPtr(new ExecOp(name));
217   return op;
218 }
219
220 /** @ingroup plugin_operation
221  *  @brief Smart Constructor.
222  */
223 ExecOpPtr ExecOp::init(const std::string& name, double flops, simgrid::s4u::Host* host)
224 {
225   auto op = ExecOpPtr(new ExecOp(name));
226   op->set_flops(flops);
227   op->set_host(host);
228   return op;
229 }
230
231 /**
232  *  @brief Do one execution of the Operation.
233  *  @note Call the on_this_start() func. Set its working status as true.
234  *  Init and start the underlying Activity.
235  */
236 void ExecOp::execute()
237 {
238   if (start_func_)
239     start_func_(this);
240   Operation::on_start(this);
241   simgrid::kernel::actor::simcall_answered([this] {
242     working_      = true;
243     queued_execs_ = std::max(queued_execs_ - 1, 0);
244   });
245   simgrid::s4u::ExecPtr exec = simgrid::s4u::Exec::init();
246   exec->set_name(name_);
247   exec->set_flops_amount(amount_);
248   exec->set_host(host_);
249   exec->start();
250   exec->extension_set(new ExtendedAttributeActivity());
251   exec->extension<ExtendedAttributeActivity>()->operation_ = this;
252   simgrid::kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
253 }
254
255 /** @ingroup plugin_operation
256  *  @param host The host to set.
257  *  @brief Set a new host.
258  */
259 void ExecOp::set_host(simgrid::s4u::Host* host)
260 {
261   simgrid::kernel::actor::simcall_answered([this, host] { host_ = host; });
262 }
263
264 /** @ingroup plugin_operation
265  *  @param flops The amount of flops to set.
266  */
267 void ExecOp::set_flops(double flops)
268 {
269   simgrid::kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
270 }
271
272 /**
273  *  @brief Default constructor.
274  */
275 CommOp::CommOp(const std::string& name) : Operation(name) {}
276
277 /** @ingroup plugin_operation
278  *  @brief Smart constructor.
279  */
280 CommOpPtr CommOp::init(const std::string& name)
281 {
282   auto op = CommOpPtr(new CommOp(name));
283   return op;
284 }
285
286 /** @ingroup plugin_operation
287  *  @brief Smart constructor.
288  */
289 CommOpPtr CommOp::init(const std::string& name, double bytes, simgrid::s4u::Host* source,
290                        simgrid::s4u::Host* destination)
291 {
292   auto op = CommOpPtr(new CommOp(name));
293   op->set_bytes(bytes);
294   op->set_source(source);
295   op->set_destination(destination);
296   return op;
297 }
298
299 /**
300  *  @brief Do one execution of the Operation.
301  *  @note Call the on_this_start() func. Set its working status as true.
302  *  Init and start the underlying Activity.
303  */
304 void CommOp::execute()
305 {
306   if (start_func_)
307     start_func_(this);
308   Operation::on_start(this);
309   simgrid::kernel::actor::simcall_answered([this] {
310     working_      = true;
311     queued_execs_ = std::max(queued_execs_ - 1, 0);
312   });
313   simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::sendto_init(source_, destination_);
314   comm->set_name(name_);
315   comm->set_payload_size(amount_);
316   comm->start();
317   comm->extension_set(new ExtendedAttributeActivity());
318   comm->extension<ExtendedAttributeActivity>()->operation_ = this;
319   simgrid::kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
320 }
321
322 /** @ingroup plugin_operation
323  *  @param source The host to set.
324  *  @brief Set a new source host.
325  */
326 void CommOp::set_source(simgrid::s4u::Host* source)
327 {
328   simgrid::kernel::actor::simcall_answered([this, source] { source_ = source; });
329 }
330
331 /** @ingroup plugin_operation
332  *  @param destination The host to set.
333  *  @brief Set a new destination host.
334  */
335 void CommOp::set_destination(simgrid::s4u::Host* destination)
336 {
337   simgrid::kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
338 }
339
340 /** @ingroup plugin_operation
341  *  @param bytes The amount of bytes to set.
342  */
343 void CommOp::set_bytes(double bytes)
344 {
345   simgrid::kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
346 }
347
348 } // namespace simgrid::plugins
349
350 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
351     simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
352 bool simgrid::plugins::Operation::inited_ = false;