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::Activity::on_completion_cb([&](simgrid::s4u::Activity const& activity) {
123 activity.extension<ExtendedAttributeActivity>()->operation_->complete();
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.
132 void Operation::enqueue_execs(int n)
134 simgrid::kernel::actor::simcall_answered([this, n] {
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.
146 void Operation::set_amount(double amount)
148 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
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.
156 void Operation::add_successor(OperationPtr successor)
158 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); });
159 successor->add_predecessor(this);
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.
167 void Operation::remove_successor(OperationPtr successor)
169 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); });
170 successor->remove_predecessor(this);
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.
178 void Operation::on_this_start(std::function<void(Operation*)> func)
180 simgrid::kernel::actor::simcall_answered([this, func] { start_func_ = func; });
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.
188 void Operation::on_this_end(std::function<void(Operation*)> func)
190 simgrid::kernel::actor::simcall_answered([this, func] { end_func_ = func; });
193 /** @ingroup plugin_operation
194 * @brief Return the number of completed executions.
196 int Operation::get_count()
202 * @brief Default constructor.
204 ExecOp::ExecOp(const std::string& name) : Operation(name) {}
206 /** @ingroup plugin_operation
207 * @brief Smart Constructor.
209 ExecOpPtr ExecOp::init(const std::string& name)
211 auto op = ExecOpPtr(new ExecOp(name));
215 /** @ingroup plugin_operation
216 * @brief Smart Constructor.
218 ExecOpPtr ExecOp::init(const std::string& name, double flops, s4u::Host* host)
220 return ExecOpPtr(new ExecOp(name))->set_flops(flops)->set_host(host);
224 * @brief Do one execution of the Operation.
225 * @note Call the on_this_start() func. Set its working status as true.
226 * Init and start the underlying Activity.
228 void ExecOp::execute()
232 Operation::on_start(this);
233 kernel::actor::simcall_answered([this] {
235 queued_execs_ = std::max(queued_execs_ - 1, 0);
237 s4u::ExecPtr exec = s4u::Exec::init();
238 exec->set_name(name_);
239 exec->set_flops_amount(amount_);
240 exec->set_host(host_);
242 exec->extension_set(new ExtendedAttributeActivity());
243 exec->extension<ExtendedAttributeActivity>()->operation_ = this;
244 kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
247 /** @ingroup plugin_operation
248 * @param host The host to set.
249 * @brief Set a new host.
251 ExecOpPtr ExecOp::set_host(s4u::Host* host)
253 kernel::actor::simcall_answered([this, host] { host_ = host; });
257 /** @ingroup plugin_operation
258 * @param flops The amount of flops to set.
260 ExecOpPtr ExecOp::set_flops(double flops)
262 kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
267 * @brief Default constructor.
269 CommOp::CommOp(const std::string& name) : Operation(name) {}
271 /** @ingroup plugin_operation
272 * @brief Smart constructor.
274 CommOpPtr CommOp::init(const std::string& name)
276 auto op = CommOpPtr(new CommOp(name));
280 /** @ingroup plugin_operation
281 * @brief Smart constructor.
283 CommOpPtr CommOp::init(const std::string& name, double bytes, s4u::Host* source,
284 s4u::Host* destination)
286 auto op = CommOpPtr(new CommOp(name));
287 op->set_bytes(bytes);
288 op->set_source(source);
289 op->set_destination(destination);
294 * @brief Do one execution of the Operation.
295 * @note Call the on_this_start() func. Set its working status as true.
296 * Init and start the underlying Activity.
298 void CommOp::execute()
302 Operation::on_start(this);
303 kernel::actor::simcall_answered([this] {
305 queued_execs_ = std::max(queued_execs_ - 1, 0);
307 s4u::CommPtr comm = s4u::Comm::sendto_init(source_, destination_);
308 comm->set_name(name_);
309 comm->set_payload_size(amount_);
311 comm->extension_set(new ExtendedAttributeActivity());
312 comm->extension<ExtendedAttributeActivity>()->operation_ = this;
313 kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
316 /** @ingroup plugin_operation
317 * @param source The host to set.
318 * @brief Set a new source host.
320 CommOpPtr CommOp::set_source(s4u::Host* source)
322 kernel::actor::simcall_answered([this, source] { source_ = source; });
326 /** @ingroup plugin_operation
327 * @param destination The host to set.
328 * @brief Set a new destination host.
330 CommOpPtr CommOp::set_destination(s4u::Host* destination)
332 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
336 /** @ingroup plugin_operation
337 * @param bytes The amount of bytes to set.
339 CommOpPtr CommOp::set_bytes(double bytes)
341 kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
345 } // namespace simgrid::plugins
347 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
348 simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
349 bool simgrid::plugins::Operation::inited_ = false;