From: Adrien Gougeon Date: Thu, 15 Jun 2023 14:14:29 +0000 (+0200) Subject: add simgrid::plugins::Token. switch from get_tokens to get_next_token_from X-Git-Tag: v3.34~14^2~7^2~13 X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/6f8347f58430e00fabef8e6cbbf3fb94e6b8a49d add simgrid::plugins::Token. switch from get_tokens to get_next_token_from --- diff --git a/examples/cpp/task-storm/s4u-task-storm.cpp b/examples/cpp/task-storm/s4u-task-storm.cpp index 2a0d1fd3a3..98575d0b97 100644 --- a/examples/cpp/task-storm/s4u-task-storm.cpp +++ b/examples/cpp/task-storm/s4u-task-storm.cpp @@ -37,11 +37,6 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(task_storm, "Messages specific for this s4u example"); -struct Token { - double data_ = 0; - Token(double data) : data_(data) {} -}; - int main(int argc, char* argv[]) { simgrid::s4u::Engine e(&argc, argv); @@ -95,30 +90,31 @@ int main(int argc, char* argv[]) } std::vector amount = {1e3,1e6,1e9}; comm->set_amount(amount[count % 3]); - auto token = std::make_shared(amount[count % 3]); + auto token = std::make_shared(); + token->set_data(new double(amount[count % 3])); t->set_token(token); }); // The token sent by SA is forwarded by both communication tasks SA_to_B1->on_this_start_cb([&](simgrid::plugins::Task* t) { - t->set_token(t->get_next_execution_tokens()[SA]); + t->set_token(t->get_next_token_from(SA)); }); SA_to_B2->on_this_start_cb([&](simgrid::plugins::Task* t) { - t->set_token(t->get_next_execution_tokens()[SA]); + t->set_token(t->get_next_token_from(SA)); }); /* B1 and B2 read the value of the token received by their predecessors and use it to adapt their amount of work to do. */ B1->on_this_start_cb([&](simgrid::plugins::Task* t) { - auto tokens_map = t->get_next_execution_tokens(); - Token* tok = (Token*)(tokens_map[SA_to_B1].get()); - t->set_amount(tok->data_ * 10); + auto data = t->get_next_token_from(SA_to_B1)->get_data(); + t->set_amount(*data * 10); + delete data; }); B2->on_this_start_cb([&](simgrid::plugins::Task* t) { - auto tokens_map = t->get_next_execution_tokens(); - Token* tok = (Token*)(tokens_map[SA_to_B2].get()); - t->set_amount(tok->data_ * 10); + auto data = t->get_next_token_from(SA_to_B2)->get_data(); + t->set_amount(*data * 10); + delete data; }); // Enqueue executions for tasks without predecessors diff --git a/include/simgrid/plugins/task.hpp b/include/simgrid/plugins/task.hpp index 67b5a68b23..87a357be43 100644 --- a/include/simgrid/plugins/task.hpp +++ b/include/simgrid/plugins/task.hpp @@ -29,6 +29,8 @@ struct ExtendedAttributeActivity { Task* task_; }; +class XBT_PUBLIC Token : public xbt::Extendable {}; + class Task { std::set successors_ = {}; std::map predecessors_ = {}; @@ -40,8 +42,8 @@ class Task { protected: std::string name_; double amount_; - std::shared_ptr token_ = NULL; - std::deque>> tokens_received_; + std::shared_ptr token_ = nullptr; + std::deque>> tokens_received_; int queued_execs_ = 0; int count_ = 0; bool working_ = false; @@ -64,8 +66,8 @@ public: void enqueue_execs(int n); void set_amount(double amount); double get_amount() const { return amount_; } - void set_token(std::shared_ptr token); - std::map> get_next_execution_tokens() const; + void set_token(std::shared_ptr token); + std::shared_ptr get_next_token_from(TaskPtr t); void add_successor(TaskPtr t); void remove_successor(TaskPtr t); void remove_all_successors(); diff --git a/src/plugins/task.cpp b/src/plugins/task.cpp index a2337dc343..835b307144 100644 --- a/src/plugins/task.cpp +++ b/src/plugins/task.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -142,7 +143,7 @@ void Task::set_amount(double amount) * @brief Set the token to send to successors. * @note The token is passed to each successor after the task end, i.e., after the on_end callback. */ -void Task::set_token(std::shared_ptr token) +void Task::set_token(std::shared_ptr token) { simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; }); } @@ -151,9 +152,9 @@ void Task::set_token(std::shared_ptr token) * @return Map of tokens received for the next execution. * @note If there is no queued execution for this task the map might not exist or be partially empty. */ -std::map> Task::get_next_execution_tokens() const +std::shared_ptr Task::get_next_token_from(TaskPtr t) { - return tokens_received_.front(); + return tokens_received_.front()[t]; } /** @ingroup plugin_task