Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove unused variable
[simgrid.git] / src / s4u / s4u_Task.cpp
index c0a6e38e90d30d692d522955919d9fcaced45a80..80756d6cfa87645a0a73c832933ab7b27f53f144 100644 (file)
@@ -1,10 +1,14 @@
 #include <memory>
 #include <simgrid/Exception.hpp>
-#include <simgrid/s4u/Task.hpp>
+#include <simgrid/s4u/Activity.hpp>
 #include <simgrid/s4u/Comm.hpp>
+#include <simgrid/s4u/Disk.hpp>
 #include <simgrid/s4u/Exec.hpp>
 #include <simgrid/s4u/Io.hpp>
+#include <simgrid/s4u/Task.hpp>
 #include <simgrid/simix.hpp>
+#include <string>
+#include <xbt/asserts.h>
 
 #include "src/simgrid/module.hpp"
 
@@ -25,18 +29,15 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Task, kernel, "Logging specific to the task plug
 
 namespace simgrid::s4u {
 
-xbt::signal<void(Task*)> Task::on_start;
-xbt::signal<void(Task*)> Task::on_end;
-
 Task::Task(const std::string& name) : name_(name) {}
 
 /**
  *  @brief Return True if the Task can start a new Activity.
  *  @note The Task is ready if not already doing something and there is at least one execution waiting in queue.
  */
-bool Task::ready_to_run() const
+bool Task::ready_to_run(std::string instance)
 {
-  return not working_ && queued_execs_ > 0;
+  return running_instances_[instance] < parallelism_degree_[instance] && queued_firings_[instance] > 0;
 }
 
 /**
@@ -48,10 +49,8 @@ bool Task::ready_to_run() const
 void Task::receive(Task* source)
 {
   XBT_DEBUG("Task %s received a token from %s", name_.c_str(), source->name_.c_str());
-  auto source_count = predecessors_[source]++;
-  if (tokens_received_.size() <= queued_execs_ + source_count)
-    tokens_received_.push_back({});
-  tokens_received_[queued_execs_ + source_count][source] = source->token_;
+  predecessors_[source]++;
+  tokens_received_[source].push_back(source->token_);
   bool enough_tokens = true;
   for (auto const& [key, val] : predecessors_)
     if (val < 1) {
@@ -61,7 +60,7 @@ void Task::receive(Task* source)
   if (enough_tokens) {
     for (auto& [key, val] : predecessors_)
       val--;
-    enqueue_execs(1);
+    enqueue_firings(1);
   }
 }
 
@@ -74,58 +73,121 @@ void Task::receive(Task* source)
  * Send a token to each of its successors.
  * Start a new execution if possible.
  */
-void Task::complete()
+void Task::complete(std::string instance)
+{
+  xbt_assert(Actor::is_maestro());
+  running_instances_[instance] = running_instances_[instance] - 1;
+  count_[instance]             = count_[instance] + 1;
+  if (instance == "collector") {
+    // XBT_INFO("Trigger on completion: %s - %s", get_cname(), instance.c_str());
+    on_this_completion(this);
+    on_completion(this);
+    for (auto const& t : successors_)
+      t->receive(this);
+  } else if (instance == "dispatcher") {
+    auto next_instance = load_balancing_function_();
+    xbt_assert(next_instance != "dispatcher" and next_instance != "collector", "Invalid instance selected: %s",
+               next_instance.c_str());
+    queued_firings_[next_instance] = queued_firings_.at(next_instance) + 1;
+    while (ready_to_run(next_instance))
+      fire(next_instance);
+  } else {
+    queued_firings_["collector"] = queued_firings_["collector"] + 1;
+    while (ready_to_run("collector"))
+      fire("collector");
+  }
+  if (ready_to_run(instance))
+    fire(instance);
+}
+
+/** @param n The new parallelism degree of the Task.
+ *  @brief Set the parallelism degree of the Task to inscrease or decrease horizontal scaling.
+ *  @note When increasing the degree the function starts new instances if there is queued firings.
+ *        When decreasing the degree the function does NOT stop running instances.
+ */
+void Task::set_parallelism_degree(int n, std::string instance)
+{
+  xbt_assert(n > 0, "Parallelism degree must be above 0.");
+  simgrid::kernel::actor::simcall_answered([this, n, &instance] {
+    if (instance == "all") {
+      for (auto& [key, value] : parallelism_degree_) {
+        parallelism_degree_[key] = n;
+        while (ready_to_run(key))
+          fire(key);
+      }
+    } else {
+      parallelism_degree_[instance] = n;
+      while (ready_to_run(instance))
+        fire(instance);
+    }
+  });
+}
+
+void Task::set_internal_bytes(int bytes, std::string instance)
 {
-  xbt_assert(s4u::Actor::is_maestro());
-  working_ = false;
-  count_++;
-  on_this_end_(this);
-  Task::on_end(this);
-  if (current_activity_)
-    previous_activity_ = std::move(current_activity_);
-  for (auto const& t : successors_)
-    t->receive(this);
-  if (ready_to_run())
-    fire();
+  simgrid::kernel::actor::simcall_answered([this, bytes, &instance] { internal_bytes_to_send_[instance] = bytes; });
 }
 
-/** @param n The number of executions to enqueue.
- *  @brief Enqueue executions.
- *  @note Immediatly starts an execution if possible.
+void Task::set_load_balancing_function(std::function<std::string()> func)
+{
+  simgrid::kernel::actor::simcall_answered([this, func] { load_balancing_function_ = func; });
+}
+
+/** @param n The number of firings to enqueue.
+ *  @brief Enqueue firing.
+ *  @note Immediatly fire an activity if possible.
  */
-void Task::enqueue_execs(int n)
+void Task::enqueue_firings(int n)
 {
   simgrid::kernel::actor::simcall_answered([this, n] {
-    queued_execs_ += n;
-    if (ready_to_run())
-      fire();
+    queued_firings_["dispatcher"] += n;
+    while (ready_to_run("dispatcher"))
+      fire("dispatcher");
   });
 }
 
+/** @param name The new name to set.
+ *  @brief Set the name of the Task.
+ */
+void Task::set_name(std::string name)
+{
+  name_ = name;
+}
+
 /** @param amount The amount to set.
  *  @brief Set the amout of work to do.
  *  @note Amount in flop for ExecTask and in bytes for CommTask.
  */
-void Task::set_amount(double amount)
+void Task::set_amount(double amount, std::string instance)
 {
-  simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
+  simgrid::kernel::actor::simcall_answered([this, amount, &instance] { amount_[instance] = amount; });
 }
 
 /** @param token The token to set.
  *  @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.
+ *  @note The token is passed to each successor after the task end, i.e., after the on_completion callback.
  */
 void Task::set_token(std::shared_ptr<Token> token)
 {
   simgrid::kernel::actor::simcall_answered([this, token] { token_ = 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::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
+void Task::deque_token_from(TaskPtr t)
 {
-  return tokens_received_.front()[t];
+  simgrid::kernel::actor::simcall_answered([this, &t] { tokens_received_.at(t).pop_front(); });
+}
+
+void Task::fire(std::string instance)
+{
+  if ((int)current_activities_[instance].size() > parallelism_degree_[instance]) {
+    current_activities_[instance].pop_front();
+  }
+  if (instance != "dispatcher" and instance != "collector") {
+    on_this_start(this);
+    on_start(this);
+  }
+  running_instances_[instance]++;
+  queued_firings_[instance] = std::max(queued_firings_[instance] - 1, 0);
 }
 
 /** @param successor The Task to add.
@@ -152,6 +214,9 @@ void Task::remove_successor(TaskPtr successor)
   });
 }
 
+/**
+ *  @brief TODO
+ */
 void Task::remove_all_successors()
 {
   simgrid::kernel::actor::simcall_answered([this] {
@@ -163,40 +228,56 @@ void Task::remove_all_successors()
   });
 }
 
-/** @ingroup plugin_task
- *  @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.
+/**
+ *  @brief TODO
  */
-void Task::on_this_start_cb(const std::function<void(Task*)>& func)
+void Task::add_instances(int n)
 {
-  simgrid::kernel::actor::simcall_answered([this, &func] { on_this_start_.connect(func); });
+  xbt_assert(n >= 0, "Cannot add a negative number of instances (provided: %d)", n);
+  int instance_count = (int)amount_.size() - 2;
+  for (int i = instance_count; i < n + instance_count; i++) {
+    amount_["instance_" + std::to_string(i)]                 = amount_.at("instance_0");
+    queued_firings_["instance_" + std::to_string(i)]         = 0;
+    running_instances_["instance_" + std::to_string(i)]      = 0;
+    count_["instance_" + std::to_string(i)]                  = 0;
+    parallelism_degree_["instance_" + std::to_string(i)]     = parallelism_degree_.at("instance_0");
+    current_activities_["instance_" + std::to_string(i)]     = {};
+    internal_bytes_to_send_["instance_" + std::to_string(i)] = internal_bytes_to_send_.at("instance_0");
+    ;
+  }
 }
 
-/** @ingroup plugin_task
- *  @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.
+/**
+ *  @brief TODO
  */
-void Task::on_this_end_cb(const std::function<void(Task*)>& func)
+void Task::remove_instances(int n)
 {
-  simgrid::kernel::actor::simcall_answered([this, &func] { on_this_end_.connect(func); });
+  int instance_count = (int)amount_.size() - 2;
+  xbt_assert(n >= 0, "Cannot remove a negative number of instances (provided: %d)", n);
+  xbt_assert(instance_count - n > 0, "The number of instances must be above 0 (instances: %d, provided: %d)",
+             instance_count, n);
+  for (int i = instance_count - 1; i >= instance_count - n; i--)
+    xbt_assert(running_instances_.at("instance_" + std::to_string(i)) == 0,
+               "Cannot remove a running instance (instances: %d)", i);
+  for (int i = instance_count - 1; i >= instance_count - n; i--) {
+    amount_.erase("instance_" + std::to_string(i));
+    queued_firings_.erase("instance_" + std::to_string(i));
+    running_instances_.erase("instance_" + std::to_string(i));
+    count_.erase("instance_" + std::to_string(i));
+    parallelism_degree_.erase("instance_" + std::to_string(i));
+    current_activities_.erase("instance_" + std::to_string(i));
+  }
 }
 
-/** @ingroup plugin_task
- *  @brief Return the number of completed executions.
+/**
+ *  @brief Default constructor.
  */
-int Task::get_count() const
+ExecTask::ExecTask(const std::string& name) : Task(name)
 {
-  return count_;
+  set_load_balancing_function([]() { return "instance_0"; });
 }
 
 /**
- *  @brief Default constructor.
- */
-ExecTask::ExecTask(const std::string& name) : Task(name) {}
-
-/** @ingroup plugin_task
  *  @brief Smart Constructor.
  */
 ExecTaskPtr ExecTask::init(const std::string& name)
@@ -204,61 +285,114 @@ ExecTaskPtr ExecTask::init(const std::string& name)
   return ExecTaskPtr(new ExecTask(name));
 }
 
-/** @ingroup plugin_task
+/**
  *  @brief Smart Constructor.
  */
-ExecTaskPtr ExecTask::init(const std::string& name, double flops, s4u::Host* host)
+ExecTaskPtr ExecTask::init(const std::string& name, double flops, Host* host)
 {
   return init(name)->set_flops(flops)->set_host(host);
 }
 
 /**
  *  @brief Do one execution of the Task.
- *  @note Call the on_this_start() func. Set its working status as true.
+ *  @note Call the on_this_start() func.
  *  Init and start the underlying Activity.
  */
-void ExecTask::fire()
+void ExecTask::fire(std::string instance)
 {
-  on_this_start_(this);
-  Task::on_start(this);
-  working_          = true;
-  queued_execs_     = std::max(queued_execs_ - 1, 0);
-  if (tokens_received_.size() > 0)
-      tokens_received_.pop_front();
-  s4u::ExecPtr exec = s4u::Exec::init();
-  exec->set_name(name_);
-  exec->set_flops_amount(amount_);
-  exec->set_host(host_);
-  exec->start();
-  exec->on_this_completion_cb([this](Exec const& exec) { this->complete(); });
-  current_activity_                                   = exec;
+  Task::fire(instance);
+  if (instance == "dispatcher" or instance == "collector") {
+    auto exec = Exec::init()
+                    ->set_name(get_name() + "_" + instance)
+                    ->set_flops_amount(get_amount(instance))
+                    ->set_host(host_[instance]);
+    exec->start();
+    exec->on_this_completion_cb([this, instance](Exec const&) { complete(instance); });
+    store_activity(exec, instance);
+  } else {
+    auto exec = Exec::init()->set_name(get_name())->set_flops_amount(get_amount())->set_host(host_[instance]);
+    if (host_["dispatcher"] == host_[instance]) {
+      exec->start();
+      store_activity(exec, instance);
+    } else {
+      auto comm = Comm::sendto_init(host_["dispatcher"], host_[instance])
+                      ->set_name(get_name() + "_dispatcher_to_" + instance)
+                      ->set_payload_size(get_internal_bytes("dispatcher"));
+      comm->add_successor(exec);
+      comm->start();
+      store_activity(comm, instance);
+    }
+    if (host_[instance] == host_["collector"]) {
+      exec->on_this_completion_cb([this, instance](Exec const&) { complete(instance); });
+      if (host_["dispatcher"] != host_[instance])
+        store_activity(exec, instance);
+    } else {
+      auto comm = Comm::sendto_init(host_[instance], host_["collector"])
+                      ->set_name(get_name() + instance + "_to_collector")
+                      ->set_payload_size(get_internal_bytes(instance));
+      exec->add_successor(comm);
+      comm->on_this_completion_cb([this, instance](Comm const&) { complete(instance); });
+      comm.detach();
+    }
+  }
 }
 
-/** @ingroup plugin_task
+/**
  *  @param host The host to set.
  *  @brief Set a new host.
  */
-ExecTaskPtr ExecTask::set_host(s4u::Host* host)
+ExecTaskPtr ExecTask::set_host(Host* host, std::string instance)
 {
-  kernel::actor::simcall_answered([this, host] { host_ = host; });
+  kernel::actor::simcall_answered([this, host, &instance] {
+    if (instance == "all")
+      for (auto& [key, value] : host_)
+        host_[key] = host;
+    else
+      host_[instance] = host;
+  });
   return this;
 }
 
-/** @ingroup plugin_task
+/**
  *  @param flops The amount of flops to set.
  */
-ExecTaskPtr ExecTask::set_flops(double flops)
+ExecTaskPtr ExecTask::set_flops(double flops, std::string instance)
 {
-  kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
+  kernel::actor::simcall_answered([this, flops, &instance] { set_amount(flops, instance); });
   return this;
 }
 
+/**
+ *  @brief TODO
+ */
+void ExecTask::add_instances(int n)
+{
+  Task::add_instances(n);
+  int instance_count = (int)host_.size() - 2;
+  for (int i = instance_count; i < n + instance_count; i++)
+    host_["instance_" + std::to_string(i)] = host_.at("instance_0");
+}
+
+/**
+ *  @brief TODO
+ */
+void ExecTask::remove_instances(int n)
+{
+  Task::remove_instances(n);
+  int instance_count = (int)host_.size() - 2;
+  for (int i = instance_count - 1; i >= instance_count - n; i--)
+    host_.erase("instance_" + std::to_string(i));
+}
+
 /**
  *  @brief Default constructor.
  */
-CommTask::CommTask(const std::string& name) : Task(name) {}
+CommTask::CommTask(const std::string& name) : Task(name)
+{
+  set_load_balancing_function([]() { return "instance_0"; });
+}
 
-/** @ingroup plugin_task
+/**
  *  @brief Smart constructor.
  */
 CommTaskPtr CommTask::init(const std::string& name)
@@ -266,70 +400,76 @@ CommTaskPtr CommTask::init(const std::string& name)
   return CommTaskPtr(new CommTask(name));
 }
 
-/** @ingroup plugin_task
+/**
  *  @brief Smart constructor.
  */
-CommTaskPtr CommTask::init(const std::string& name, double bytes, s4u::Host* source, s4u::Host* destination)
+CommTaskPtr CommTask::init(const std::string& name, double bytes, Host* source, Host* destination)
 {
   return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
 }
 
 /**
  *  @brief Do one execution of the Task.
- *  @note Call the on_this_start() func. Set its working status as true.
+ *  @note Call the on_this_start() func.
  *  Init and start the underlying Activity.
  */
-void CommTask::fire()
+void CommTask::fire(std::string instance)
 {
-  on_this_start_(this);
-  Task::on_start(this);
-  working_          = true;
-  queued_execs_     = std::max(queued_execs_ - 1, 0);
-  if (tokens_received_.size() > 0)
-      tokens_received_.pop_front();
-  s4u::CommPtr comm = s4u::Comm::sendto_init(source_, destination_);
-  comm->set_name(name_);
-  comm->set_payload_size(amount_);
-  comm->start();
-  comm->on_this_completion_cb([this](Comm const& comm) { this->complete(); });
-  current_activity_                                   = comm;
+  Task::fire(instance);
+  if (instance == "dispatcher" or instance == "collector") {
+    auto exec = Exec::init()
+                    ->set_name(get_name() + "_" + instance)
+                    ->set_flops_amount(get_amount(instance))
+                    ->set_host(instance == "dispatcher" ? source_ : destination_);
+    exec->start();
+    exec->on_this_completion_cb([this, instance](Exec const&) { complete(instance); });
+    store_activity(exec, instance);
+  } else {
+    auto comm = Comm::sendto_init(source_, destination_)->set_name(get_name())->set_payload_size(get_amount());
+    comm->start();
+    comm->on_this_completion_cb([this, instance](Comm const&) { complete(instance); });
+    store_activity(comm, instance);
+  }
 }
 
-/** @ingroup plugin_task
+/**
  *  @param source The host to set.
  *  @brief Set a new source host.
  */
-CommTaskPtr CommTask::set_source(s4u::Host* source)
+CommTaskPtr CommTask::set_source(Host* source)
 {
   kernel::actor::simcall_answered([this, source] { source_ = source; });
   return this;
 }
 
-/** @ingroup plugin_task
+/**
  *  @param destination The host to set.
  *  @brief Set a new destination host.
  */
-CommTaskPtr CommTask::set_destination(s4u::Host* destination)
+CommTaskPtr CommTask::set_destination(Host* destination)
 {
   kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
   return this;
 }
 
-/** @ingroup plugin_task
+/**
  *  @param bytes The amount of bytes to set.
  */
 CommTaskPtr CommTask::set_bytes(double bytes)
 {
-  kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
+  kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
   return this;
 }
 
 /**
  *  @brief Default constructor.
  */
-IoTask::IoTask(const std::string& name) : Task(name) {}
+IoTask::IoTask(const std::string& name) : Task(name)
+{
+  set_load_balancing_function([]() { return "instance_0"; });
+}
 
-/** @ingroup plugin_task
+/**
  *  @brief Smart Constructor.
  */
 IoTaskPtr IoTask::init(const std::string& name)
@@ -337,56 +477,57 @@ IoTaskPtr IoTask::init(const std::string& name)
   return IoTaskPtr(new IoTask(name));
 }
 
-/** @ingroup plugin_task
+/**
  *  @brief Smart Constructor.
  */
-IoTaskPtr IoTask::init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type)
+IoTaskPtr IoTask::init(const std::string& name, double bytes, Disk* disk, Io::OpType type)
 {
   return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
 }
 
-/** @ingroup plugin_task
+/**
  *  @param disk The disk to set.
  *  @brief Set a new disk.
  */
-IoTaskPtr IoTask::set_disk(s4u::Disk* disk)
+IoTaskPtr IoTask::set_disk(Disk* disk)
 {
   kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
   return this;
 }
 
-/** @ingroup plugin_task
+/**
  *  @param bytes The amount of bytes to set.
  */
 IoTaskPtr IoTask::set_bytes(double bytes)
 {
-  kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
+  kernel::actor::simcall_answered([this, bytes] { set_amount(bytes); });
   return this;
 }
 
-/** @ingroup plugin_task */
-IoTaskPtr IoTask::set_op_type(s4u::Io::OpType type)
+/**  */
+IoTaskPtr IoTask::set_op_type(Io::OpType type)
 {
   kernel::actor::simcall_answered([this, type] { type_ = type; });
   return this;
 }
 
-void IoTask::fire()
-{
-  on_this_start_(this);
-  Task::on_start(this);
-  working_      = true;
-  queued_execs_ = std::max(queued_execs_ - 1, 0);
-  if (tokens_received_.size() > 0)
-      tokens_received_.pop_front();
-  s4u::IoPtr io = s4u::Io::init();
-  io->set_name(name_);
-  io->set_size(amount_);
-  io->set_disk(disk_);
-  io->set_op_type(type_);
-  io->start();
-  io->on_this_completion_cb([this](Io const& io) { this->complete(); });
-  current_activity_ = io;
+void IoTask::fire(std::string instance)
+{
+  Task::fire(instance);
+  if (instance == "dispatcher" or instance == "collector") {
+    auto exec = Exec::init()
+                    ->set_name(get_name() + "_" + instance)
+                    ->set_flops_amount(get_amount(instance))
+                    ->set_host(disk_->get_host());
+    exec->start();
+    exec->on_this_completion_cb([this, instance](Exec const&) { complete(instance); });
+    store_activity(exec, instance);
+  } else {
+    auto io = Io::init()->set_name(get_name())->set_size(get_amount())->set_disk(disk_)->set_op_type(type_);
+    io->start();
+    io->on_this_completion_cb([this, instance](Io const&) { complete(instance); });
+    store_activity(io, instance);
+  }
 }
 
 } // namespace simgrid::s4u