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>
7 #include "src/simgrid/module.hpp"
9 SIMGRID_REGISTER_PLUGIN(operation, "Battery management", nullptr)
10 /** @defgroup plugin_operation plugin_operation Plugin Operation
14 This is the operation plugin, enabling management of Operations.
15 To activate this plugin, first call :cpp:func:`Operation::init`.
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>`.
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Operation, kernel, "Logging specific to the operation plugin");
27 namespace simgrid::plugins {
29 xbt::signal<void(Operation*)> Operation::on_start;
30 xbt::signal<void(Operation*)> Operation::on_end;
32 Operation::Operation(const std::string& name) : name_(name) {}
35 * @param predecessor The Operation to add.
36 * @brief Add a predecessor to this Operation.
38 void Operation::add_predecessor(Operation* predecessor)
40 if (predecessors_.find(predecessor) == predecessors_.end())
41 simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_[predecessor] = 0; });
45 * @param predecessor The Operation to remove.
46 * @brief Remove a predecessor from this Operation.
48 void Operation::remove_predecessor(Operation* predecessor)
50 simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_.erase(predecessor); });
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.
57 bool Operation::ready_to_run() const
59 return not working_ && queued_execs_ > 0;
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.
68 void Operation::receive(Operation* source)
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] {
74 bool enough_tokens = true;
75 for (auto const& [key, val] : predecessors_)
77 enough_tokens = false;
81 for (auto& [key, val] : predecessors_)
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.
97 void Operation::complete()
99 simgrid::kernel::actor::simcall_answered([this] {
105 Operation::on_end(this);
106 for (auto const& op : successors_)
112 /** @ingroup plugin_operation
113 * @brief Init the Operation plugin.
114 * @note Add a completion callback to all Activities to call Operation::complete().
116 void Operation::init()
118 if (Operation::inited_)
120 Operation::inited_ = true;
121 ExtendedAttributeActivity::EXTENSION_ID = simgrid::s4u::Activity::extension_create<ExtendedAttributeActivity>();
122 simgrid::s4u::Exec::on_completion_cb([](simgrid::s4u::Exec const& exec) {
123 exec.extension<ExtendedAttributeActivity>()->operation_->complete();
125 simgrid::s4u::Comm::on_completion_cb([](simgrid::s4u::Comm const& comm) {
126 comm.extension<ExtendedAttributeActivity>()->operation_->complete();
130 /** @ingroup plugin_operation
131 * @param n The number of executions to enqueue.
132 * @brief Enqueue executions.
133 * @note Immediatly starts an execution if possible.
135 void Operation::enqueue_execs(int n)
137 simgrid::kernel::actor::simcall_answered([this, n] {
144 /** @ingroup plugin_operation
145 * @param amount The amount to set.
146 * @brief Set the amout of work to do.
147 * @note Amount in flop for ExecOp and in bytes for CommOp.
149 void Operation::set_amount(double amount)
151 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
154 /** @ingroup plugin_operation
155 * @param successor The Operation to add.
156 * @brief Add a successor to this Operation.
157 * @note It also adds this as a predecessor of successor.
159 void Operation::add_successor(OperationPtr successor)
161 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); });
162 successor->add_predecessor(this);
165 /** @ingroup plugin_operation
166 * @param successor The Operation to remove.
167 * @brief Remove a successor from this Operation.
168 * @note It also remove this from the predecessors of successor.
170 void Operation::remove_successor(OperationPtr successor)
172 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); });
173 successor->remove_predecessor(this);
176 void Operation::remove_all_successors()
178 simgrid::kernel::actor::simcall_answered([this] {
179 while (not successors_.empty()) {
180 auto* successor = *(successors_.begin());
181 successor->predecessors_.erase(this);
182 successors_.erase(successor);
187 /** @ingroup plugin_operation
188 * @param func The function to set.
189 * @brief Set a function to be called before each execution.
190 * @note The function is called before the underlying Activity starts.
192 void Operation::on_this_start(const std::function<void(Operation*)>& func)
194 simgrid::kernel::actor::simcall_answered([this, &func] { start_func_ = func; });
197 /** @ingroup plugin_operation
198 * @param func The function to set.
199 * @brief Set a function to be called after each execution.
200 * @note The function is called after the underlying Activity ends, but before sending tokens to successors.
202 void Operation::on_this_end(const std::function<void(Operation*)>& func)
204 simgrid::kernel::actor::simcall_answered([this, &func] { end_func_ = func; });
207 /** @ingroup plugin_operation
208 * @brief Return the number of completed executions.
210 int Operation::get_count() const
216 * @brief Default constructor.
218 ExecOp::ExecOp(const std::string& name) : Operation(name) {}
220 /** @ingroup plugin_operation
221 * @brief Smart Constructor.
223 ExecOpPtr ExecOp::init(const std::string& name)
225 return ExecOpPtr(new ExecOp(name));
228 /** @ingroup plugin_operation
229 * @brief Smart Constructor.
231 ExecOpPtr ExecOp::init(const std::string& name, double flops, s4u::Host* host)
233 return init(name)->set_flops(flops)->set_host(host);
237 * @brief Do one execution of the Operation.
238 * @note Call the on_this_start() func. Set its working status as true.
239 * Init and start the underlying Activity.
241 void ExecOp::execute()
245 Operation::on_start(this);
246 kernel::actor::simcall_answered([this] {
248 queued_execs_ = std::max(queued_execs_ - 1, 0);
250 s4u::ExecPtr exec = s4u::Exec::init();
251 exec->set_name(name_);
252 exec->set_flops_amount(amount_);
253 exec->set_host(host_);
255 exec->extension_set(new ExtendedAttributeActivity());
256 exec->extension<ExtendedAttributeActivity>()->operation_ = this;
257 kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
260 /** @ingroup plugin_operation
261 * @param host The host to set.
262 * @brief Set a new host.
264 ExecOpPtr ExecOp::set_host(s4u::Host* host)
266 kernel::actor::simcall_answered([this, host] { host_ = host; });
270 /** @ingroup plugin_operation
271 * @param flops The amount of flops to set.
273 ExecOpPtr ExecOp::set_flops(double flops)
275 kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
280 * @brief Default constructor.
282 CommOp::CommOp(const std::string& name) : Operation(name) {}
284 /** @ingroup plugin_operation
285 * @brief Smart constructor.
287 CommOpPtr CommOp::init(const std::string& name)
289 return CommOpPtr(new CommOp(name));
292 /** @ingroup plugin_operation
293 * @brief Smart constructor.
295 CommOpPtr CommOp::init(const std::string& name, double bytes, s4u::Host* source,
296 s4u::Host* destination)
298 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
302 * @brief Do one execution of the Operation.
303 * @note Call the on_this_start() func. Set its working status as true.
304 * Init and start the underlying Activity.
306 void CommOp::execute()
310 Operation::on_start(this);
311 kernel::actor::simcall_answered([this] {
313 queued_execs_ = std::max(queued_execs_ - 1, 0);
315 s4u::CommPtr comm = s4u::Comm::sendto_init(source_, destination_);
316 comm->set_name(name_);
317 comm->set_payload_size(amount_);
319 comm->extension_set(new ExtendedAttributeActivity());
320 comm->extension<ExtendedAttributeActivity>()->operation_ = this;
321 kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
324 /** @ingroup plugin_operation
325 * @param source The host to set.
326 * @brief Set a new source host.
328 CommOpPtr CommOp::set_source(s4u::Host* source)
330 kernel::actor::simcall_answered([this, source] { source_ = source; });
334 /** @ingroup plugin_operation
335 * @param destination The host to set.
336 * @brief Set a new destination host.
338 CommOpPtr CommOp::set_destination(s4u::Host* destination)
340 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
344 /** @ingroup plugin_operation
345 * @param bytes The amount of bytes to set.
347 CommOpPtr CommOp::set_bytes(double bytes)
349 kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
353 } // namespace simgrid::plugins
355 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
356 simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
357 bool simgrid::plugins::Operation::inited_ = false;