Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[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(const 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(const 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() const
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   return ExecOpPtr(new ExecOp(name));
212 }
213
214 /** @ingroup plugin_operation
215  *  @brief Smart Constructor.
216  */
217 ExecOpPtr ExecOp::init(const std::string& name, double flops, s4u::Host* host)
218 {
219   return init(name)->set_flops(flops)->set_host(host);
220 }
221
222 /**
223  *  @brief Do one execution of the Operation.
224  *  @note Call the on_this_start() func. Set its working status as true.
225  *  Init and start the underlying Activity.
226  */
227 void ExecOp::execute()
228 {
229   if (start_func_)
230     start_func_(this);
231   Operation::on_start(this);
232   kernel::actor::simcall_answered([this] {
233     working_      = true;
234     queued_execs_ = std::max(queued_execs_ - 1, 0);
235   });
236   s4u::ExecPtr exec = s4u::Exec::init();
237   exec->set_name(name_);
238   exec->set_flops_amount(amount_);
239   exec->set_host(host_);
240   exec->start();
241   exec->extension_set(new ExtendedAttributeActivity());
242   exec->extension<ExtendedAttributeActivity>()->operation_ = this;
243   kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
244 }
245
246 /** @ingroup plugin_operation
247  *  @param host The host to set.
248  *  @brief Set a new host.
249  */
250 ExecOpPtr ExecOp::set_host(s4u::Host* host)
251 {
252   kernel::actor::simcall_answered([this, host] { host_ = host; });
253   return this;
254 }
255
256 /** @ingroup plugin_operation
257  *  @param flops The amount of flops to set.
258  */
259 ExecOpPtr ExecOp::set_flops(double flops)
260 {
261   kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
262   return this;
263 }
264
265 /**
266  *  @brief Default constructor.
267  */
268 CommOp::CommOp(const std::string& name) : Operation(name) {}
269
270 /** @ingroup plugin_operation
271  *  @brief Smart constructor.
272  */
273 CommOpPtr CommOp::init(const std::string& name)
274 {
275   return CommOpPtr(new CommOp(name));
276 }
277
278 /** @ingroup plugin_operation
279  *  @brief Smart constructor.
280  */
281 CommOpPtr CommOp::init(const std::string& name, double bytes, s4u::Host* source,
282                        s4u::Host* destination)
283 {
284   return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
285 }
286
287 /**
288  *  @brief Do one execution of the Operation.
289  *  @note Call the on_this_start() func. Set its working status as true.
290  *  Init and start the underlying Activity.
291  */
292 void CommOp::execute()
293 {
294   if (start_func_)
295     start_func_(this);
296   Operation::on_start(this);
297   kernel::actor::simcall_answered([this] {
298     working_      = true;
299     queued_execs_ = std::max(queued_execs_ - 1, 0);
300   });
301   s4u::CommPtr comm = s4u::Comm::sendto_init(source_, destination_);
302   comm->set_name(name_);
303   comm->set_payload_size(amount_);
304   comm->start();
305   comm->extension_set(new ExtendedAttributeActivity());
306   comm->extension<ExtendedAttributeActivity>()->operation_ = this;
307   kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
308 }
309
310 /** @ingroup plugin_operation
311  *  @param source The host to set.
312  *  @brief Set a new source host.
313  */
314 CommOpPtr CommOp::set_source(s4u::Host* source)
315 {
316   kernel::actor::simcall_answered([this, source] { source_ = source; });
317   return this;
318 }
319
320 /** @ingroup plugin_operation
321  *  @param destination The host to set.
322  *  @brief Set a new destination host.
323  */
324 CommOpPtr CommOp::set_destination(s4u::Host* destination)
325 {
326   kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
327   return this;
328 }
329
330 /** @ingroup plugin_operation
331  *  @param bytes The amount of bytes to set.
332  */
333 CommOpPtr CommOp::set_bytes(double bytes)
334 {
335   kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
336   return this;
337 }
338
339 } // namespace simgrid::plugins
340
341 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
342     simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
343 bool simgrid::plugins::Operation::inited_ = false;