Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix conflict
[simgrid.git] / examples / cpp / dag-scheduling / s4u-dag-scheduling.cpp
index bdced0001cb17d6c6a885980a23f976fba52528c..8dc40601945b42ccd930236db307930153a10759 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2021. The SimGrid Team.
+/* Copyright (c) 2009-2022. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(dag_scheduling, "Logging specific to this example");
 
-typedef struct _HostAttribute* HostAttribute;
-struct _HostAttribute {
+struct HostAttribute {
   /* Earliest time at which a host is ready to execute a task */
-  double available_at;
-  simgrid::s4u::Exec* last_scheduled_task;
+  double available_at                     = 0.0;
+  simgrid::s4u::Exec* last_scheduled_task = nullptr;
 };
 
 static double sg_host_get_available_at(const simgrid::s4u::Host* host)
 {
-  return static_cast<HostAttribute>(host->get_data())->available_at;
+  return static_cast<HostAttribute*>(host->get_data())->available_at;
 }
 
 static void sg_host_set_available_at(simgrid::s4u::Host* host, double time)
 {
-  auto* attr         = static_cast<HostAttribute>(host->get_data());
+  auto* attr         = static_cast<HostAttribute*>(host->get_data());
   attr->available_at = time;
-  host->set_data(attr);
 }
 
 static simgrid::s4u::Exec* sg_host_get_last_scheduled_task(const simgrid::s4u::Host* host)
 {
-  return static_cast<HostAttribute>(host->get_data())->last_scheduled_task;
+  return static_cast<HostAttribute*>(host->get_data())->last_scheduled_task;
 }
 
 static void sg_host_set_last_scheduled_task(simgrid::s4u::Host* host, simgrid::s4u::ExecPtr task)
 {
-  auto* attr                = static_cast<HostAttribute>(host->get_data());
+  auto* attr                = static_cast<HostAttribute*>(host->get_data());
   attr->last_scheduled_task = task.get();
-  host->set_data(attr);
 }
 
 static bool dependency_exists(const simgrid::s4u::Exec* src, simgrid::s4u::Exec* dst)
@@ -143,17 +140,32 @@ static simgrid::s4u::Host* get_best_host(const simgrid::s4u::ExecPtr exec)
   return best_host;
 }
 
-int main(int argc, char** argv)
+static void schedule_on(simgrid::s4u::ExecPtr exec, simgrid::s4u::Host* host)
 {
-  double min_finish_time            = -1.0;
-  simgrid::s4u::Exec* selected_task = nullptr;
-  simgrid::s4u::Host* selected_host = nullptr;
+  exec->set_host(host);
+  // we can also set the destination of all the input comms of this exec
+  for (const auto& pred : exec->get_dependencies()) {
+    auto* comm = dynamic_cast<simgrid::s4u::Comm*>(pred.get());
+    if (comm != nullptr) {
+      comm->set_destination(host);
+      delete static_cast<double*>(comm->get_data());
+    }
+  }
+  // we can also set the source of all the output comms of this exec
+  for (const auto& succ : exec->get_successors()) {
+    auto* comm = dynamic_cast<simgrid::s4u::Comm*>(succ.get());
+    if (comm != nullptr)
+      comm->set_source(host);
+  }
+}
 
+int main(int argc, char** argv)
+{
   simgrid::s4u::Engine e(&argc, argv);
   std::set<simgrid::s4u::Activity*> vetoed;
   e.track_vetoed_activities(&vetoed);
 
-  simgrid::s4u::Activity::on_completion.connect([](simgrid::s4u::Activity& activity) {
+  simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity& activity) {
     // when an Exec completes, we need to set the potential start time of all its ouput comms
     const auto* exec = dynamic_cast<simgrid::s4u::Exec*>(&activity);
     if (exec == nullptr) // Only Execs are concerned here
@@ -174,23 +186,17 @@ int main(int argc, char** argv)
   /*  Allocating the host attribute */
   unsigned long total_nhosts = e.get_host_count();
   const auto hosts          = e.get_all_hosts();
-
+  std::vector<HostAttribute> host_attributes(total_nhosts);
   for (unsigned long i = 0; i < total_nhosts; i++)
-    hosts[i]->set_data(xbt_new0(struct _HostAttribute, 1));
+    hosts[i]->set_data(&host_attributes[i]);
 
   /* load the DAX file */
-  std::vector<simgrid::s4u::ActivityPtr> dax = simgrid::s4u::create_DAG_from_DAX(argv[2]);
+  auto dax = simgrid::s4u::create_DAG_from_DAX(argv[2]);
 
   /* Schedule the root first */
   auto* root = static_cast<simgrid::s4u::Exec*>(dax.front().get());
   auto host  = get_best_host(root);
-  root->set_host(host);
-  // we can also set the source of all the output comms of the root node
-  for (const auto& succ : root->get_successors()) {
-    auto* comm = dynamic_cast<simgrid::s4u::Comm*>(succ.get());
-    if (comm != nullptr)
-      comm->set_source(host);
-  }
+  schedule_on(root, host);
 
   e.run();
 
@@ -201,7 +207,6 @@ int main(int argc, char** argv)
     vetoed.clear();
 
     if (ready_tasks.empty()) {
-      ready_tasks.clear();
       /* there is no ready task, let advance the simulation */
       e.run();
       continue;
@@ -210,6 +215,10 @@ int main(int argc, char** argv)
      * get the host that minimizes the completion time.
      * select the task that has the minimum completion time on its best host.
      */
+    double min_finish_time            = -1.0;
+    simgrid::s4u::Exec* selected_task = nullptr;
+    simgrid::s4u::Host* selected_host = nullptr;
+
     for (auto task : ready_tasks) {
       XBT_DEBUG("%s is ready", task->get_cname());
       host               = get_best_host(task);
@@ -222,21 +231,7 @@ int main(int argc, char** argv)
     }
 
     XBT_INFO("Schedule %s on %s", selected_task->get_cname(), selected_host->get_cname());
-    selected_task->set_host(selected_host);
-    // we can also set the destination of all the input comms of the selected task
-    for (const auto& pred : selected_task->get_dependencies()) {
-      auto* comm = dynamic_cast<simgrid::s4u::Comm*>(pred.get());
-      if (comm != nullptr) {
-        comm->set_destination(selected_host);
-        delete static_cast<double*>(comm->get_data());
-      }
-    }
-    // we can also set the source of all the output comms of the selected task
-    for (const auto& succ : selected_task->get_successors()) {
-      auto* comm = dynamic_cast<simgrid::s4u::Comm*>(succ.get());
-      if (comm != nullptr)
-        comm->set_source(selected_host);
-    }
+    schedule_on(selected_task, selected_host);
 
     /*
      * tasks can be executed concurrently when they can by default.
@@ -257,17 +252,10 @@ int main(int argc, char** argv)
     sg_host_set_available_at(selected_host, min_finish_time);
 
     ready_tasks.clear();
-    /* reset the min_finish_time for the next set of ready tasks */
-    min_finish_time = -1.;
     e.run();
   }
 
   XBT_INFO("Simulation Time: %f", simgrid_get_clock());
 
-  for (auto h : hosts) {
-    xbt_free(h->get_data());
-    h->set_data(nullptr);
-  }
-
   return 0;
 }