Logo AND Algorithmique Numérique Distribuée

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