Logo AND Algorithmique Numérique Distribuée

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