From e0f1a9dae032024c4f4043e29fa878fc2a6f7699 Mon Sep 17 00:00:00 2001 From: Adrien Gougeon Date: Tue, 11 Apr 2023 16:33:21 +0200 Subject: [PATCH] clang-format --- .../operation-simple/s4u-operation-simple.cpp | 68 +++++------ .../s4u-operation-switch-host.cpp | 102 ++++++++-------- .../s4u-operation-variable-load.cpp | 74 ++++++------ include/simgrid/plugins/operation.hpp | 12 +- src/plugins/operation.cpp | 113 +++++++----------- 5 files changed, 170 insertions(+), 199 deletions(-) diff --git a/examples/cpp/operation-simple/s4u-operation-simple.cpp b/examples/cpp/operation-simple/s4u-operation-simple.cpp index f540dd3bc6..ce60a021ff 100644 --- a/examples/cpp/operation-simple/s4u-operation-simple.cpp +++ b/examples/cpp/operation-simple/s4u-operation-simple.cpp @@ -5,49 +5,49 @@ /* This example demonstrate basic use of the operation plugin. * - * We model the following graph: - * + * We model the following graph: + * * exec1 -> comm -> exec2 - * + * * exec1 and exec2 are execution operations. * comm is a communication operation. */ -#include "simgrid/s4u.hpp" #include "simgrid/plugins/operation.hpp" +#include "simgrid/s4u.hpp" XBT_LOG_NEW_DEFAULT_CATEGORY(operation_simple, "Messages specific for this operation example"); int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - simgrid::plugins::Operation::init(); - - // Retrieve hosts - auto tremblay = e.host_by_name("Tremblay"); - auto jupiter = e.host_by_name("Jupiter"); - - // Create operations - auto exec1 = simgrid::plugins::ExecOp::create("exec1",1e9,tremblay); - auto exec2 = simgrid::plugins::ExecOp::create("exec2",1e9,jupiter); - auto comm = simgrid::plugins::CommOp::create("comm",1e7,tremblay,jupiter); - - // Create the graph by defining dependencies between operations - exec1->add_successor(comm); - comm->add_successor(exec2); - - // Add a function to be called when operations end for log purpose - std::vector ops{exec1,exec2,comm}; - for (auto op: ops) - op->on_end([](simgrid::plugins::Operation* op) { - XBT_INFO("Operation %s finished (%d)",op->get_name().c_str(), op->get_count()); - }); - - // Enqueue two executions for operation exec1 - exec1->enqueue_execs(2); - - // Start the simulation - e.run(); - return 0; + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + simgrid::plugins::Operation::init(); + + // Retrieve hosts + auto tremblay = e.host_by_name("Tremblay"); + auto jupiter = e.host_by_name("Jupiter"); + + // Create operations + auto exec1 = simgrid::plugins::ExecOp::create("exec1", 1e9, tremblay); + auto exec2 = simgrid::plugins::ExecOp::create("exec2", 1e9, jupiter); + auto comm = simgrid::plugins::CommOp::create("comm", 1e7, tremblay, jupiter); + + // Create the graph by defining dependencies between operations + exec1->add_successor(comm); + comm->add_successor(exec2); + + // Add a function to be called when operations end for log purpose + std::vector ops{exec1, exec2, comm}; + for (auto op : ops) + op->on_end([](simgrid::plugins::Operation* op) { + XBT_INFO("Operation %s finished (%d)", op->get_name().c_str(), op->get_count()); + }); + + // Enqueue two executions for operation exec1 + exec1->enqueue_execs(2); + + // Start the simulation + e.run(); + return 0; } diff --git a/examples/cpp/operation-switch-host/s4u-operation-switch-host.cpp b/examples/cpp/operation-switch-host/s4u-operation-switch-host.cpp index 5803181e92..72d52dcf6f 100644 --- a/examples/cpp/operation-switch-host/s4u-operation-switch-host.cpp +++ b/examples/cpp/operation-switch-host/s4u-operation-switch-host.cpp @@ -4,76 +4,74 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ /* This example demonstrates how to dynamically modify a graph of operations. - * - * Assuming we have two instances of a service placed on different hosts, + * + * Assuming we have two instances of a service placed on different hosts, * we want to send data alternatively to thoses instances. * * We consider the following graph: - * + * * comm0 -> exec1 -> comm1 - * ↳-> exec2 ->comm2 - * + * ↳-> exec2 ->comm2 + * * With exec1 and exec2 on different hosts. */ -#include "simgrid/s4u.hpp" #include "simgrid/plugins/operation.hpp" +#include "simgrid/s4u.hpp" XBT_LOG_NEW_DEFAULT_CATEGORY(operation_switch_host, "Messages specific for this operation example"); int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - simgrid::plugins::Operation::init(); - - // Retrieve hosts - auto tremblay = e.host_by_name("Tremblay"); - auto jupiter = e.host_by_name("Jupiter"); - auto fafard = e.host_by_name("Fafard"); + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + simgrid::plugins::Operation::init(); - // Create operations - auto comm0 = simgrid::plugins::CommOp::create("comm0",1e7,tremblay,jupiter); - auto exec1 = simgrid::plugins::ExecOp::create("exec1",1e9,jupiter); - auto exec2 = simgrid::plugins::ExecOp::create("exec2",1e9,fafard); - auto comm1 = simgrid::plugins::CommOp::create("comm1",1e7,jupiter,tremblay); - auto comm2 = simgrid::plugins::CommOp::create("comm2",1e7,fafard,tremblay); + // Retrieve hosts + auto tremblay = e.host_by_name("Tremblay"); + auto jupiter = e.host_by_name("Jupiter"); + auto fafard = e.host_by_name("Fafard"); - // Create the initial graph by defining dependencies between operations - comm0->add_successor(exec2); - exec1->add_successor(comm1); - exec2->add_successor(comm2); + // Create operations + auto comm0 = simgrid::plugins::CommOp::create("comm0", 1e7, tremblay, jupiter); + auto exec1 = simgrid::plugins::ExecOp::create("exec1", 1e9, jupiter); + auto exec2 = simgrid::plugins::ExecOp::create("exec2", 1e9, fafard); + auto comm1 = simgrid::plugins::CommOp::create("comm1", 1e7, jupiter, tremblay); + auto comm2 = simgrid::plugins::CommOp::create("comm2", 1e7, fafard, tremblay); - // Add a function to be called when operations end for log purpose - std::vector v = - {comm0,exec1,exec2,comm1,comm2}; - for (auto op : v) - op->on_end([](simgrid::plugins::Operation* op) { - XBT_INFO("Operation %s finished (%d)",op->get_name().c_str(), op->get_count()); - }); + // Create the initial graph by defining dependencies between operations + comm0->add_successor(exec2); + exec1->add_successor(comm1); + exec2->add_successor(comm2); - // Add a function to be called before each executions of comm0 - // This function modifies the graph of operations by adding or removing - // successors to comm0 - int count = 0; - comm0->on_start([&](simgrid::plugins::Operation* op) { - if (count % 2 == 0) { - comm0->set_destination(jupiter); - comm0->add_successor(exec1); - comm0->remove_successor(exec2); - } - else { - comm0->set_destination(fafard); - comm0->add_successor(exec2); - comm0->remove_successor(exec1); - } - count++; + // Add a function to be called when operations end for log purpose + std::vector v = {comm0, exec1, exec2, comm1, comm2}; + for (auto op : v) + op->on_end([](simgrid::plugins::Operation* op) { + XBT_INFO("Operation %s finished (%d)", op->get_name().c_str(), op->get_count()); }); - // Enqueue four executions for operation comm0 - comm0->enqueue_execs(4); + // Add a function to be called before each executions of comm0 + // This function modifies the graph of operations by adding or removing + // successors to comm0 + int count = 0; + comm0->on_start([&](simgrid::plugins::Operation* op) { + if (count % 2 == 0) { + comm0->set_destination(jupiter); + comm0->add_successor(exec1); + comm0->remove_successor(exec2); + } else { + comm0->set_destination(fafard); + comm0->add_successor(exec2); + comm0->remove_successor(exec1); + } + count++; + }); + + // Enqueue four executions for operation comm0 + comm0->enqueue_execs(4); - // Start the simulation - e.run(); - return 0; + // Start the simulation + e.run(); + return 0; } diff --git a/examples/cpp/operation-variable-load/s4u-operation-variable-load.cpp b/examples/cpp/operation-variable-load/s4u-operation-variable-load.cpp index 3aa32fd948..a7d4553cf5 100644 --- a/examples/cpp/operation-variable-load/s4u-operation-variable-load.cpp +++ b/examples/cpp/operation-variable-load/s4u-operation-variable-load.cpp @@ -4,63 +4,63 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ /* This example demonstrates how to create a variable load for operations. - * + * * We consider the following graph: - * + * * comm -> exec - * + * * With a small load each comm operation is followed by an exec operation. * With a heavy load there is a burst of comm before the exec operation can even finish once. */ -#include "simgrid/s4u.hpp" #include "simgrid/plugins/operation.hpp" +#include "simgrid/s4u.hpp" XBT_LOG_NEW_DEFAULT_CATEGORY(operation_variable_load, "Messages specific for this s4u example"); static void variable_load(simgrid::plugins::OperationPtr op) { - XBT_INFO("--- Small load ---"); - for (int i=0; i < 3; i++) { - op->enqueue_execs(1); - simgrid::s4u::this_actor::sleep_for(100); - } - simgrid::s4u::this_actor::sleep_until(1000); - XBT_INFO("--- Heavy load ---"); - for (int i=0; i < 3; i++) { - op->enqueue_execs(1); - simgrid::s4u::this_actor::sleep_for(1); - } + XBT_INFO("--- Small load ---"); + for (int i = 0; i < 3; i++) { + op->enqueue_execs(1); + simgrid::s4u::this_actor::sleep_for(100); + } + simgrid::s4u::this_actor::sleep_until(1000); + XBT_INFO("--- Heavy load ---"); + for (int i = 0; i < 3; i++) { + op->enqueue_execs(1); + simgrid::s4u::this_actor::sleep_for(1); + } } int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - simgrid::plugins::Operation::init(); + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + simgrid::plugins::Operation::init(); - // Retreive hosts - auto tremblay = e.host_by_name("Tremblay"); - auto jupiter = e.host_by_name("Jupiter"); + // Retreive hosts + auto tremblay = e.host_by_name("Tremblay"); + auto jupiter = e.host_by_name("Jupiter"); - // Create operations - auto comm = simgrid::plugins::CommOp::create("comm",1e7,tremblay,jupiter); - auto exec = simgrid::plugins::ExecOp::create("exec",1e9,jupiter); + // Create operations + auto comm = simgrid::plugins::CommOp::create("comm", 1e7, tremblay, jupiter); + auto exec = simgrid::plugins::ExecOp::create("exec", 1e9, jupiter); - // Create the graph by defining dependencies between operations - comm->add_successor(exec); + // Create the graph by defining dependencies between operations + comm->add_successor(exec); - // Add a function to be called when operations end for log purpose - std::vector ops{exec,comm}; - for (auto op: ops) - op->on_end([](simgrid::plugins::Operation* op) { - XBT_INFO("Operation %s finished (%d)",op->get_name().c_str(), op->get_count()); - }); + // Add a function to be called when operations end for log purpose + std::vector ops{exec, comm}; + for (auto op : ops) + op->on_end([](simgrid::plugins::Operation* op) { + XBT_INFO("Operation %s finished (%d)", op->get_name().c_str(), op->get_count()); + }); - // Create the actor that will inject load during the simulation - simgrid::s4u::Actor::create("input", tremblay, variable_load, comm); + // Create the actor that will inject load during the simulation + simgrid::s4u::Actor::create("input", tremblay, variable_load, comm); - // Start the simulation - e.run(); - return 0; + // Start the simulation + e.run(); + return 0; } diff --git a/include/simgrid/plugins/operation.hpp b/include/simgrid/plugins/operation.hpp index 0ff852785b..acf723d7fa 100644 --- a/include/simgrid/plugins/operation.hpp +++ b/include/simgrid/plugins/operation.hpp @@ -40,13 +40,13 @@ protected: std::string name_; double amount_; int queued_execs_ = 0; - int count_ = 0; - bool working_ = false; + int count_ = 0; + bool working_ = false; simgrid::s4u::ActivityPtr current_activity_; - std::function end_func_ = [](Operation*) {}; - std::function start_func_ = [](Operation*) {}; + std::function end_func_ = [](Operation*) {}; + std::function start_func_ = [](Operation*) {}; Operation(const std::string& name, double amount); - ~Operation() = default; + ~Operation() = default; virtual void execute() = 0; public: @@ -59,7 +59,6 @@ public: void on_start(std::function func); void on_end(std::function func); int get_count(); - }; class ExecOp : public Operation { @@ -87,7 +86,6 @@ public: simgrid::s4u::Host* destination); void set_source(simgrid::s4u::Host* source); void set_destination(simgrid::s4u::Host* destination); - }; } // namespace simgrid::plugins #endif \ No newline at end of file diff --git a/src/plugins/operation.cpp b/src/plugins/operation.cpp index c3996bb632..27bb193f82 100644 --- a/src/plugins/operation.cpp +++ b/src/plugins/operation.cpp @@ -6,7 +6,6 @@ #include "src/simgrid/module.hpp" - SIMGRID_REGISTER_PLUGIN(operation, "Battery management", nullptr) /** @defgroup plugin_operation plugin_operation Plugin Operation @@ -15,8 +14,8 @@ SIMGRID_REGISTER_PLUGIN(operation, "Battery management", nullptr) This is the operation plugin, enabling management of Operations. To activate this plugin, first call :cpp:func:`Operation::init`. -Operations are designed to represent workflows, i.e, graphs of Operations. -Operations can only be instancied using either +Operations are designed to represent workflows, i.e, graphs of Operations. +Operations can only be instancied using either :cpp:func:`simgrid::plugins::ExecOp::create` or :cpp:func:`simgrid::plugins::CommOp::create` An ExecOp is an Execution Operation. Its underlying Activity is an :ref:`Exec `. A CommOp is a Communication Operation. Its underlying Activity is a :ref:`Comm `. @@ -33,30 +32,26 @@ std::string Operation::get_name() return name_; } -/** +/** * @param predecessor The Operation to add. * @brief Add a predecessor to this Operation. */ void Operation::add_predecessor(Operation* predecessor) { if (predecessors_.find(predecessor) == predecessors_.end()) - simgrid::kernel::actor::simcall_answered([this, predecessor] { - predecessors_[predecessor] = 0; - }); + simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_[predecessor] = 0; }); } -/** +/** * @param predecessor The Operation to remove. * @brief Remove a predecessor from this Operation. */ void Operation::remove_predecessor(Operation* predecessor) { - simgrid::kernel::actor::simcall_answered([this, predecessor] { - predecessors_.erase(predecessor); - }); + simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_.erase(predecessor); }); } -/** +/** * @brief Return True if the Operation can start a new Activity. * @note The Operation is ready if not already doing something and there is at least one execution waiting in queue. */ @@ -68,10 +63,10 @@ bool Operation::ready_to_run() const return true; } -/** +/** * @param source The sender. * @brief Receive a token from another Operation. - * @note Check upon reception if the Operation has received a token from each of its predecessors, + * @note Check upon reception if the Operation has received a token from each of its predecessors, * and in this case consumes those tokens and enqueue an execution. */ void Operation::receive(Operation* source) @@ -79,25 +74,25 @@ void Operation::receive(Operation* source) XBT_DEBUG("Operation %s received a token from %s", name_.c_str(), source->name_.c_str()); auto it = predecessors_.find(source); simgrid::kernel::actor::simcall_answered([this, it] { - it->second++; - bool enough_tokens = true; - for (auto const& [key, val] : predecessors_) - if (val < 1) { - enough_tokens = false; - break; + it->second++; + bool enough_tokens = true; + for (auto const& [key, val] : predecessors_) + if (val < 1) { + enough_tokens = false; + break; + } + if (enough_tokens) { + for (auto [key, val] : predecessors_) + val--; + enqueue_execs(1); } - if (enough_tokens) { - for (auto [key, val] : predecessors_) - val--; - enqueue_execs(1); - } }); } -/** +/** * @brief Operation routine when finishing an execution. - * @note Set its working status as false. Add 1 to its count of finished executions. - * Call the on_end() func. Send a token to each of its successors. + * @note Set its working status as false. Add 1 to its count of finished executions. + * Call the on_end() func. Send a token to each of its successors. * Start a new execution if possible. */ void Operation::complete() @@ -136,9 +131,9 @@ void Operation::init() void Operation::enqueue_execs(int n) { simgrid::kernel::actor::simcall_answered([this, n] { - queued_execs_ += n; - if (ready_to_run()) - execute(); + queued_execs_ += n; + if (ready_to_run()) + execute(); }); } @@ -149,9 +144,7 @@ void Operation::enqueue_execs(int n) */ void Operation::set_amount(double amount) { - simgrid::kernel::actor::simcall_answered([this, amount] { - amount_ = amount; - }); + simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; }); } /** @ingroup plugin_operation @@ -161,9 +154,7 @@ void Operation::set_amount(double amount) */ void Operation::add_successor(OperationPtr successor) { - simgrid::kernel::actor::simcall_answered([this, successor] { - successors_.insert(successor.get()); - }); + simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); }); successor->add_predecessor(this); } @@ -174,34 +165,28 @@ void Operation::add_successor(OperationPtr successor) */ void Operation::remove_successor(OperationPtr successor) { - simgrid::kernel::actor::simcall_answered([this, successor] { - successors_.erase(successor.get()); - }); + simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); }); successor->remove_predecessor(this); } /** @ingroup plugin_operation * @param func The function to set. * @brief Set a function to be called before each execution. - * @note The function is called before the underlying Activity starts. + * @note The function is called before the underlying Activity starts. */ void Operation::on_start(std::function func) { - simgrid::kernel::actor::simcall_answered([this, func] { - start_func_ = func; - }); + simgrid::kernel::actor::simcall_answered([this, func] { start_func_ = func; }); } /** @ingroup plugin_operation * @param func The function to set. * @brief Set a function to be called after each execution. - * @note The function is called after the underlying Activity ends, but before sending tokens to successors. + * @note The function is called after the underlying Activity ends, but before sending tokens to successors. */ void Operation::on_end(std::function func) { - simgrid::kernel::actor::simcall_answered([this,func] { - end_func_ = func; - }); + simgrid::kernel::actor::simcall_answered([this, func] { end_func_ = func; }); } /** @ingroup plugin_operation @@ -212,7 +197,7 @@ int Operation::get_count() return count_; } -/** +/** * @brief Default constructor. */ ExecOp::ExecOp(const std::string& name, double flops, simgrid::s4u::Host* host) : Operation(name, flops), host_(host) {} @@ -226,7 +211,7 @@ ExecOpPtr ExecOp::create(const std::string& name, double flops, simgrid::s4u::Ho return op; } -/** +/** * @brief Do one execution of the Operation. * @note Call the on_start() func. Set its working status as true. * Create and start the underlying Activity. @@ -235,7 +220,7 @@ void ExecOp::execute() { start_func_(this); simgrid::kernel::actor::simcall_answered([this] { - working_ = true; + working_ = true; queued_execs_ = std::max(queued_execs_ - 1, 0); }); simgrid::s4u::ExecPtr exec = simgrid::s4u::Exec::init(); @@ -245,9 +230,7 @@ void ExecOp::execute() exec->start(); exec->extension_set(new ExtendedAttributeActivity()); exec->extension()->operation_ = this; - simgrid::kernel::actor::simcall_answered([this, exec] { - current_activity_ = exec; - }); + simgrid::kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; }); } /** @ingroup plugin_operation @@ -256,12 +239,10 @@ void ExecOp::execute() */ void ExecOp::set_host(simgrid::s4u::Host* host) { - simgrid::kernel::actor::simcall_answered([this, host] { - host_ = host; - }); + simgrid::kernel::actor::simcall_answered([this, host] { host_ = host; }); } -/** +/** * @brief Default constructor. */ CommOp::CommOp(const std::string& name, double bytes, simgrid::s4u::Host* source, simgrid::s4u::Host* destination) @@ -279,7 +260,7 @@ CommOpPtr CommOp::create(const std::string& name, double bytes, simgrid::s4u::Ho return op; } -/** +/** * @brief Do one execution of the Operation. * @note Call the on_start() func. Set its working status as true. * Create and start the underlying Activity. @@ -288,7 +269,7 @@ void CommOp::execute() { start_func_(this); simgrid::kernel::actor::simcall_answered([this] { - working_ = true; + working_ = true; queued_execs_ = std::max(queued_execs_ - 1, 0); }); simgrid::s4u::CommPtr comm = simgrid::s4u::Comm::sendto_init(source_, destination_); @@ -297,9 +278,7 @@ void CommOp::execute() comm->start(); comm->extension_set(new ExtendedAttributeActivity()); comm->extension()->operation_ = this; - simgrid::kernel::actor::simcall_answered([this, comm] { - current_activity_ = comm; - }); + simgrid::kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; }); } /** @ingroup plugin_operation @@ -308,9 +287,7 @@ void CommOp::execute() */ void CommOp::set_source(simgrid::s4u::Host* source) { - simgrid::kernel::actor::simcall_answered([this, source] { - source_ = source; - }); + simgrid::kernel::actor::simcall_answered([this, source] { source_ = source; }); } /** @ingroup plugin_operation @@ -319,9 +296,7 @@ void CommOp::set_source(simgrid::s4u::Host* source) */ void CommOp::set_destination(simgrid::s4u::Host* destination) { - simgrid::kernel::actor::simcall_answered([this, destination] { - destination_ = destination; - }); + simgrid::kernel::actor::simcall_answered([this, destination] { destination_ = destination; }); } } // namespace simgrid::plugins -- 2.20.1