Logo AND Algorithmique Numérique Distribuée

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