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::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>`.
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Operation, kernel, "Logging specific to the operation plugin");
27 namespace simgrid::plugins {
28 Operation::Operation(const std::string& name, double amount) : name_(name), amount_(amount) {}
30 std::string Operation::get_name()
36 * @param predecessor The Operation to add.
37 * @brief Add a predecessor to this Operation.
39 void Operation::add_predecessor(Operation* predecessor)
41 if (predecessors_.find(predecessor) == predecessors_.end())
42 simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_[predecessor] = 0; });
46 * @param predecessor The Operation to remove.
47 * @brief Remove a predecessor from this Operation.
49 void Operation::remove_predecessor(Operation* predecessor)
51 simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_.erase(predecessor); });
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.
58 bool Operation::ready_to_run() const
60 if (working_ or queued_execs_ <= 0)
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.
72 void Operation::receive(Operation* source)
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] {
78 bool enough_tokens = true;
79 for (auto const& [key, val] : predecessors_)
81 enough_tokens = false;
85 for (auto [key, val] : predecessors_)
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.
98 void Operation::complete()
100 simgrid::kernel::actor::simcall_answered([this] {
105 for (auto const& op : successors_)
111 /** @ingroup plugin_operation
112 * @brief Init the Operation plugin.
113 * @note Add a completion callback to all Activities to call Operation::complete().
115 void Operation::init()
117 if (Operation::inited_)
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();
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.
131 void Operation::enqueue_execs(int n)
133 simgrid::kernel::actor::simcall_answered([this, n] {
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.
145 void Operation::set_amount(double amount)
147 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
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.
155 void Operation::add_successor(OperationPtr successor)
157 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); });
158 successor->add_predecessor(this);
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.
166 void Operation::remove_successor(OperationPtr successor)
168 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); });
169 successor->remove_predecessor(this);
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.
177 void Operation::on_start(std::function<void(Operation*)> func)
179 simgrid::kernel::actor::simcall_answered([this, func] { start_func_ = func; });
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.
187 void Operation::on_end(std::function<void(Operation*)> func)
189 simgrid::kernel::actor::simcall_answered([this, func] { end_func_ = func; });
192 /** @ingroup plugin_operation
193 * @brief Return the number of completed executions.
195 int Operation::get_count()
201 * @brief Default constructor.
203 ExecOp::ExecOp(const std::string& name, double flops, simgrid::s4u::Host* host) : Operation(name, flops), host_(host) {}
205 /** @ingroup plugin_operation
206 * @brief Smart Constructor.
208 ExecOpPtr ExecOp::create(const std::string& name, double flops, simgrid::s4u::Host* host)
210 auto op = ExecOpPtr(new ExecOp(name, flops, host));
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.
219 void ExecOp::execute()
222 simgrid::kernel::actor::simcall_answered([this] {
224 queued_execs_ = std::max(queued_execs_ - 1, 0);
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_);
231 exec->extension_set(new ExtendedAttributeActivity());
232 exec->extension<ExtendedAttributeActivity>()->operation_ = this;
233 simgrid::kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
236 /** @ingroup plugin_operation
237 * @param host The host to set.
238 * @brief Set a new host.
240 void ExecOp::set_host(simgrid::s4u::Host* host)
242 simgrid::kernel::actor::simcall_answered([this, host] { host_ = host; });
246 * @brief Default constructor.
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)
253 /** @ingroup plugin_operation
254 * @brief Smart constructor.
256 CommOpPtr CommOp::create(const std::string& name, double bytes, simgrid::s4u::Host* source,
257 simgrid::s4u::Host* destination)
259 auto op = CommOpPtr(new CommOp(name, bytes, source, destination));
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.
268 void CommOp::execute()
271 simgrid::kernel::actor::simcall_answered([this] {
273 queued_execs_ = std::max(queued_execs_ - 1, 0);
275 simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::sendto_init(source_, destination_);
276 comm->set_name(name_);
277 comm->set_payload_size(amount_);
279 comm->extension_set(new ExtendedAttributeActivity());
280 comm->extension<ExtendedAttributeActivity>()->operation_ = this;
281 simgrid::kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
284 /** @ingroup plugin_operation
285 * @param source The host to set.
286 * @brief Set a new source host.
288 void CommOp::set_source(simgrid::s4u::Host* source)
290 simgrid::kernel::actor::simcall_answered([this, source] { source_ = source; });
293 /** @ingroup plugin_operation
294 * @param destination The host to set.
295 * @brief Set a new destination host.
297 void CommOp::set_destination(simgrid::s4u::Host* destination)
299 simgrid::kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
302 } // namespace simgrid::plugins
304 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
305 simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
306 bool simgrid::plugins::Operation::inited_ = false;