Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
68977c4093861e49aec2c7852d6bcd3441019409
[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::create` or :cpp:func:`simgrid::plugins::CommOp::create`
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, double amount) : name_(name), amount_(amount) {}
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   if (working_ or queued_execs_ <= 0)
65     return false;
66   else
67     return true;
68 }
69
70 /**
71  *  @param source The sender.
72  *  @brief Receive a token from another Operation.
73  *  @note Check upon reception if the Operation has received a token from each of its predecessors,
74  * and in this case consumes those tokens and enqueue an execution.
75  */
76 void Operation::receive(Operation* source)
77 {
78   XBT_DEBUG("Operation %s received a token from %s", name_.c_str(), source->name_.c_str());
79   auto it = predecessors_.find(source);
80   simgrid::kernel::actor::simcall_answered([this, it] {
81     it->second++;
82     bool enough_tokens = true;
83     for (auto const& [key, val] : predecessors_)
84       if (val < 1) {
85         enough_tokens = false;
86         break;
87       }
88     if (enough_tokens) {
89       for (auto& [key, val] : predecessors_)
90         val--;
91       enqueue_execs(1);
92     }
93   });
94 }
95
96 /**
97  *  @brief Operation routine when finishing an execution.
98  *  @note Set its working status as false.
99  * Add 1 to its count of finished executions.
100  * Call the on_this_end func.
101  * Fire on_end callback.
102  * Send a token to each of its successors.
103  * Start a new execution if possible.
104  */
105 void Operation::complete()
106 {
107   simgrid::kernel::actor::simcall_answered([this] {
108     working_ = false;
109     count_++;
110   });
111   end_func_(this);
112   Operation::on_end(this);
113   for (auto const& op : successors_)
114     op->receive(this);
115   if (ready_to_run())
116     execute();
117 }
118
119 /** @ingroup plugin_operation
120  *  @brief Init the Operation plugin.
121  *  @note Add a completion callback to all Activities to call Operation::complete().
122  */
123 void Operation::init()
124 {
125   if (Operation::inited_)
126     return;
127   Operation::inited_                      = true;
128   ExtendedAttributeActivity::EXTENSION_ID = simgrid::s4u::Activity::extension_create<ExtendedAttributeActivity>();
129   simgrid::s4u::Activity::on_completion_cb([&](simgrid::s4u::Activity const& activity) {
130     activity.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 /** @ingroup plugin_operation
181  *  @param func The function to set.
182  *  @brief Set a function to be called before each execution.
183  *  @note The function is called before the underlying Activity starts.
184  */
185 void Operation::on_this_start(std::function<void(Operation*)> func)
186 {
187   simgrid::kernel::actor::simcall_answered([this, func] { start_func_ = func; });
188 }
189
190 /** @ingroup plugin_operation
191  *  @param func The function to set.
192  *  @brief Set a function to be called after each execution.
193  *  @note The function is called after the underlying Activity ends, but before sending tokens to successors.
194  */
195 void Operation::on_this_end(std::function<void(Operation*)> func)
196 {
197   simgrid::kernel::actor::simcall_answered([this, func] { end_func_ = func; });
198 }
199
200 /** @ingroup plugin_operation
201  *  @brief Return the number of completed executions.
202  */
203 int Operation::get_count()
204 {
205   return count_;
206 }
207
208 /**
209  *  @brief Default constructor.
210  */
211 ExecOp::ExecOp(const std::string& name, double flops, simgrid::s4u::Host* host) : Operation(name, flops), host_(host) {}
212
213 /** @ingroup plugin_operation
214  *  @brief Smart Constructor.
215  */
216 ExecOpPtr ExecOp::create(const std::string& name, double flops, simgrid::s4u::Host* host)
217 {
218   auto op = ExecOpPtr(new ExecOp(name, flops, host));
219   return op;
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  *  Create and start the underlying Activity.
226  */
227 void ExecOp::execute()
228 {
229   start_func_(this);
230   Operation::on_start(this);
231   simgrid::kernel::actor::simcall_answered([this] {
232     working_      = true;
233     queued_execs_ = std::max(queued_execs_ - 1, 0);
234   });
235   simgrid::s4u::ExecPtr exec = simgrid::s4u::Exec::init();
236   exec->set_name(name_);
237   exec->set_flops_amount(amount_);
238   exec->set_host(host_);
239   exec->start();
240   exec->extension_set(new ExtendedAttributeActivity());
241   exec->extension<ExtendedAttributeActivity>()->operation_ = this;
242   simgrid::kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
243 }
244
245 /** @ingroup plugin_operation
246  *  @param host The host to set.
247  *  @brief Set a new host.
248  */
249 void ExecOp::set_host(simgrid::s4u::Host* host)
250 {
251   simgrid::kernel::actor::simcall_answered([this, host] { host_ = host; });
252 }
253
254 /**
255  *  @brief Default constructor.
256  */
257 CommOp::CommOp(const std::string& name, double bytes, simgrid::s4u::Host* source, simgrid::s4u::Host* destination)
258     : Operation(name, bytes), source_(source), destination_(destination)
259 {
260 }
261
262 /** @ingroup plugin_operation
263  *  @brief Smart constructor.
264  */
265 CommOpPtr CommOp::create(const std::string& name, double bytes, simgrid::s4u::Host* source,
266                          simgrid::s4u::Host* destination)
267 {
268   auto op = CommOpPtr(new CommOp(name, bytes, source, destination));
269   return op;
270 }
271
272 /**
273  *  @brief Do one execution of the Operation.
274  *  @note Call the on_this_start() func. Set its working status as true.
275  *  Create and start the underlying Activity.
276  */
277 void CommOp::execute()
278 {
279   start_func_(this);
280   Operation::on_start(this);
281   simgrid::kernel::actor::simcall_answered([this] {
282     working_      = true;
283     queued_execs_ = std::max(queued_execs_ - 1, 0);
284   });
285   simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::sendto_init(source_, destination_);
286   comm->set_name(name_);
287   comm->set_payload_size(amount_);
288   comm->start();
289   comm->extension_set(new ExtendedAttributeActivity());
290   comm->extension<ExtendedAttributeActivity>()->operation_ = this;
291   simgrid::kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
292 }
293
294 /** @ingroup plugin_operation
295  *  @param source The host to set.
296  *  @brief Set a new source host.
297  */
298 void CommOp::set_source(simgrid::s4u::Host* source)
299 {
300   simgrid::kernel::actor::simcall_answered([this, source] { source_ = source; });
301 }
302
303 /** @ingroup plugin_operation
304  *  @param destination The host to set.
305  *  @brief Set a new destination host.
306  */
307 void CommOp::set_destination(simgrid::s4u::Host* destination)
308 {
309   simgrid::kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
310 }
311
312 } // namespace simgrid::plugins
313
314 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
315     simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
316 bool simgrid::plugins::Operation::inited_ = false;