Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
clang-format
[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 Operation::Operation(const std::string& name, double amount) : name_(name), amount_(amount) {}
29
30 std::string Operation::get_name()
31 {
32   return name_;
33 }
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   if (working_ or queued_execs_ <= 0)
61     return false;
62   else
63     return true;
64 }
65
66 /**
67  *  @param source The sender.
68  *  @brief Receive a token from another Operation.
69  *  @note Check upon reception if the Operation has received a token from each of its predecessors,
70  * and in this case consumes those tokens and enqueue an execution.
71  */
72 void Operation::receive(Operation* source)
73 {
74   XBT_DEBUG("Operation %s received a token from %s", name_.c_str(), source->name_.c_str());
75   auto it = predecessors_.find(source);
76   simgrid::kernel::actor::simcall_answered([this, it] {
77     it->second++;
78     bool enough_tokens = true;
79     for (auto const& [key, val] : predecessors_)
80       if (val < 1) {
81         enough_tokens = false;
82         break;
83       }
84     if (enough_tokens) {
85       for (auto [key, val] : predecessors_)
86         val--;
87       enqueue_execs(1);
88     }
89   });
90 }
91
92 /**
93  *  @brief Operation routine when finishing an execution.
94  *  @note Set its working status as false. Add 1 to its count of finished executions.
95  * Call the on_end() func. 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   end_func_(this);
105   for (auto const& op : successors_)
106     op->receive(this);
107   if (ready_to_run())
108     execute();
109 }
110
111 /** @ingroup plugin_operation
112  *  @brief Init the Operation plugin.
113  *  @note Add a completion callback to all Activities to call Operation::complete().
114  */
115 void Operation::init()
116 {
117   if (Operation::inited_)
118     return;
119   Operation::inited_                      = true;
120   ExtendedAttributeActivity::EXTENSION_ID = simgrid::s4u::Activity::extension_create<ExtendedAttributeActivity>();
121   simgrid::s4u::Activity::on_completion_cb([&](simgrid::s4u::Activity const& activity) {
122     activity.extension<ExtendedAttributeActivity>()->operation_->complete();
123   });
124 }
125
126 /** @ingroup plugin_operation
127  *  @param n The number of executions to enqueue.
128  *  @brief Enqueue executions.
129  *  @note Immediatly starts an execution if possible.
130  */
131 void Operation::enqueue_execs(int n)
132 {
133   simgrid::kernel::actor::simcall_answered([this, n] {
134     queued_execs_ += n;
135     if (ready_to_run())
136       execute();
137   });
138 }
139
140 /** @ingroup plugin_operation
141  *  @param amount The amount to set.
142  *  @brief Set the amout of work to do.
143  *  @note Amount in flop for ExecOp and in bytes for CommOp.
144  */
145 void Operation::set_amount(double amount)
146 {
147   simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
148 }
149
150 /** @ingroup plugin_operation
151  *  @param successor The Operation to add.
152  *  @brief Add a successor to this Operation.
153  *  @note It also adds this as a predecessor of successor.
154  */
155 void Operation::add_successor(OperationPtr successor)
156 {
157   simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); });
158   successor->add_predecessor(this);
159 }
160
161 /** @ingroup plugin_operation
162  *  @param successor The Operation to remove.
163  *  @brief Remove a successor from this Operation.
164  *  @note It also remove this from the predecessors of successor.
165  */
166 void Operation::remove_successor(OperationPtr successor)
167 {
168   simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); });
169   successor->remove_predecessor(this);
170 }
171
172 /** @ingroup plugin_operation
173  *  @param func The function to set.
174  *  @brief Set a function to be called before each execution.
175  *  @note The function is called before the underlying Activity starts.
176  */
177 void Operation::on_start(std::function<void(Operation*)> func)
178 {
179   simgrid::kernel::actor::simcall_answered([this, func] { start_func_ = func; });
180 }
181
182 /** @ingroup plugin_operation
183  *  @param func The function to set.
184  *  @brief Set a function to be called after each execution.
185  *  @note The function is called after the underlying Activity ends, but before sending tokens to successors.
186  */
187 void Operation::on_end(std::function<void(Operation*)> func)
188 {
189   simgrid::kernel::actor::simcall_answered([this, func] { end_func_ = func; });
190 }
191
192 /** @ingroup plugin_operation
193  *  @brief Return the number of completed executions.
194  */
195 int Operation::get_count()
196 {
197   return count_;
198 }
199
200 /**
201  *  @brief Default constructor.
202  */
203 ExecOp::ExecOp(const std::string& name, double flops, simgrid::s4u::Host* host) : Operation(name, flops), host_(host) {}
204
205 /** @ingroup plugin_operation
206  *  @brief Smart Constructor.
207  */
208 ExecOpPtr ExecOp::create(const std::string& name, double flops, simgrid::s4u::Host* host)
209 {
210   auto op = ExecOpPtr(new ExecOp(name, flops, host));
211   return op;
212 }
213
214 /**
215  *  @brief Do one execution of the Operation.
216  *  @note Call the on_start() func. Set its working status as true.
217  *  Create and start the underlying Activity.
218  */
219 void ExecOp::execute()
220 {
221   start_func_(this);
222   simgrid::kernel::actor::simcall_answered([this] {
223     working_      = true;
224     queued_execs_ = std::max(queued_execs_ - 1, 0);
225   });
226   simgrid::s4u::ExecPtr exec = simgrid::s4u::Exec::init();
227   exec->set_name(name_);
228   exec->set_flops_amount(amount_);
229   exec->set_host(host_);
230   exec->start();
231   exec->extension_set(new ExtendedAttributeActivity());
232   exec->extension<ExtendedAttributeActivity>()->operation_ = this;
233   simgrid::kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
234 }
235
236 /** @ingroup plugin_operation
237  *  @param host The host to set.
238  *  @brief Set a new host.
239  */
240 void ExecOp::set_host(simgrid::s4u::Host* host)
241 {
242   simgrid::kernel::actor::simcall_answered([this, host] { host_ = host; });
243 }
244
245 /**
246  *  @brief Default constructor.
247  */
248 CommOp::CommOp(const std::string& name, double bytes, simgrid::s4u::Host* source, simgrid::s4u::Host* destination)
249     : Operation(name, bytes), source_(source), destination_(destination)
250 {
251 }
252
253 /** @ingroup plugin_operation
254  *  @brief Smart constructor.
255  */
256 CommOpPtr CommOp::create(const std::string& name, double bytes, simgrid::s4u::Host* source,
257                          simgrid::s4u::Host* destination)
258 {
259   auto op = CommOpPtr(new CommOp(name, bytes, source, destination));
260   return op;
261 }
262
263 /**
264  *  @brief Do one execution of the Operation.
265  *  @note Call the on_start() func. Set its working status as true.
266  *  Create and start the underlying Activity.
267  */
268 void CommOp::execute()
269 {
270   start_func_(this);
271   simgrid::kernel::actor::simcall_answered([this] {
272     working_      = true;
273     queued_execs_ = std::max(queued_execs_ - 1, 0);
274   });
275   simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::sendto_init(source_, destination_);
276   comm->set_name(name_);
277   comm->set_payload_size(amount_);
278   comm->start();
279   comm->extension_set(new ExtendedAttributeActivity());
280   comm->extension<ExtendedAttributeActivity>()->operation_ = this;
281   simgrid::kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
282 }
283
284 /** @ingroup plugin_operation
285  *  @param source The host to set.
286  *  @brief Set a new source host.
287  */
288 void CommOp::set_source(simgrid::s4u::Host* source)
289 {
290   simgrid::kernel::actor::simcall_answered([this, source] { source_ = source; });
291 }
292
293 /** @ingroup plugin_operation
294  *  @param destination The host to set.
295  *  @brief Set a new destination host.
296  */
297 void CommOp::set_destination(simgrid::s4u::Host* destination)
298 {
299   simgrid::kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
300 }
301
302 } // namespace simgrid::plugins
303
304 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
305     simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
306 bool simgrid::plugins::Operation::inited_ = false;