Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add simgrid::plugins::Token. switch from get_tokens to get_next_token_from
authorAdrien Gougeon <adrien.gougeon@ens-rennes.fr>
Thu, 15 Jun 2023 14:14:29 +0000 (16:14 +0200)
committerAdrien Gougeon <adrien.gougeon@ens-rennes.fr>
Thu, 15 Jun 2023 14:14:29 +0000 (16:14 +0200)
examples/cpp/task-storm/s4u-task-storm.cpp
include/simgrid/plugins/task.hpp
src/plugins/task.cpp

index 2a0d1fd..98575d0 100644 (file)
 
 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<double> amount = {1e3,1e6,1e9};
     comm->set_amount(amount[count % 3]);
-    auto token = std::make_shared<Token>(amount[count % 3]);
+    auto token = std::make_shared<simgrid::plugins::Token>();
+    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<double>();
+    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<double>();
+    t->set_amount(*data * 10);
+    delete data;
   });
 
   // Enqueue executions for tasks without predecessors
index 67b5a68..87a357b 100644 (file)
@@ -29,6 +29,8 @@ struct ExtendedAttributeActivity {
   Task* task_;
 };
 
+class XBT_PUBLIC Token : public xbt::Extendable<Token> {};
+
 class Task {
   std::set<Task*> successors_                 = {};
   std::map<Task*, unsigned int> predecessors_ = {};
@@ -40,8 +42,8 @@ class Task {
 protected:
   std::string name_;
   double amount_;
-  std::shared_ptr<void> token_ = NULL;
-  std::deque<std::map<TaskPtr, std::shared_ptr<void>>> tokens_received_;
+  std::shared_ptr<Token> token_ = nullptr;
+  std::deque<std::map<TaskPtr, std::shared_ptr<Token>>> 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<void> token);
-  std::map<TaskPtr, std::shared_ptr<void>> get_next_execution_tokens() const;
+  void set_token(std::shared_ptr<Token> token);
+  std::shared_ptr<Token> get_next_token_from(TaskPtr t);
   void add_successor(TaskPtr t);
   void remove_successor(TaskPtr t);
   void remove_all_successors();
index a2337dc..835b307 100644 (file)
@@ -1,3 +1,4 @@
+#include <memory>
 #include <simgrid/Exception.hpp>
 #include <simgrid/plugins/task.hpp>
 #include <simgrid/s4u/Comm.hpp>
@@ -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<void> token)
+void Task::set_token(std::shared_ptr<Token> token)
 {
   simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
 }
@@ -151,9 +152,9 @@ void Task::set_token(std::shared_ptr<void> 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<TaskPtr, std::shared_ptr<void>> Task::get_next_execution_tokens() const
+std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
 {
-  return tokens_received_.front();
+  return tokens_received_.front()[t];
 }
 
 /** @ingroup plugin_task