Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill a few globals.
[simgrid.git] / examples / smpi / replay_multiple_manual_deploy / replay_multiple_manual.cpp
index c82a7c7..7707303 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2020. The SimGrid Team.
+/* Copyright (c) 2009-2023. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -44,17 +44,6 @@ struct Job {
   int unique_job_number;                     //!< The job unique number in [0, n[.
 };
 
-// ugly globals to avoid creating structures for giving args to processes
-static std::vector<simgrid::s4u::Host*> hosts;
-static int noise_between_jobs;
-
-static bool job_comparator(const std::unique_ptr<Job>& j1, const std::unique_ptr<Job>& j2)
-{
-  if (j1->starting_time == j2->starting_time)
-    return j1->smpi_app_name < j2->smpi_app_name;
-  return j1->starting_time < j2->starting_time;
-}
-
 static void smpi_replay_process(Job* job, simgrid::s4u::BarrierPtr barrier, int rank)
 {
   XBT_INFO("Replaying rank %d of job %d (smpi_app '%s')", rank, job->unique_job_number, job->smpi_app_name.c_str());
@@ -82,7 +71,7 @@ static void pop_some_processes(int nb_processes, simgrid::s4u::Host* host)
   }
 }
 
-static int job_executor_process(Job* job)
+static int job_executor_process(const std::vector<simgrid::s4u::Host*>& hosts, Job* job)
 {
   XBT_INFO("Executing job %d (smpi_app '%s')", job->unique_job_number, job->smpi_app_name.c_str());
 
@@ -103,12 +92,12 @@ static int job_executor_process(Job* job)
 }
 
 // Executes a workload of SMPI processes
-static int workload_executor_process(const std::vector<std::unique_ptr<Job>>* workload)
+static int workload_executor_process(const std::vector<simgrid::s4u::Host*>& hosts,
+                                     const std::vector<std::unique_ptr<Job>>& workload, int noise_between_jobs)
 {
-  for (auto const& job : *workload) {
+  for (auto const& job : workload) {
     // Let's wait until the job's waiting time if needed
-    double curr_time = simgrid::s4u::Engine::get_clock();
-    if (job->starting_time > curr_time) {
+    if (double curr_time = simgrid::s4u::Engine::get_clock(); job->starting_time > curr_time) {
       double time_to_sleep = (double)job->starting_time - curr_time;
       XBT_INFO("Sleeping %g seconds (waiting for job %d, app '%s')", time_to_sleep, job->starting_time,
                job->smpi_app_name.c_str());
@@ -125,7 +114,8 @@ static int workload_executor_process(const std::vector<std::unique_ptr<Job>>* wo
     // Let's finally run the job executor
     char* str_pname = bprintf("job_%04d", job->unique_job_number);
     XBT_INFO("Launching the job executor of job %d (app '%s')", job->unique_job_number, job->smpi_app_name.c_str());
-    simgrid::s4u::Actor::create(str_pname, hosts[job->allocation[0]], job_executor_process, job.get());
+    simgrid::s4u::Actor::create(str_pname, hosts[job->allocation[0]], job_executor_process, std::cref(hosts),
+                                job.get());
     xbt_free(str_pname);
   }
 
@@ -171,7 +161,7 @@ static std::vector<std::unique_ptr<Job>> all_jobs(const std::string& workload_fi
 
         // Let's read the filename
         std::ifstream traces_file(job->filename);
-        if (!traces_file.is_open())
+        if (not traces_file.is_open())
           throw std::invalid_argument("Cannot open file " + job->filename);
 
         std::string traces_line;
@@ -197,8 +187,11 @@ static std::vector<std::unique_ptr<Job>> all_jobs(const std::string& workload_fi
 
   // Jobs are sorted by ascending date, then by lexicographical order of their
   // application names
-  sort(jobs.begin(), jobs.end(), job_comparator);
-
+  sort(jobs.begin(), jobs.end(), [](auto const& j1, auto const& j2) {
+    if (j1->starting_time == j2->starting_time)
+      return j1->smpi_app_name < j2->smpi_app_name;
+    return j1->starting_time < j2->starting_time;
+  });
   for (unsigned int i = 0; i < jobs.size(); ++i)
     jobs[i]->unique_job_number = i;
 
@@ -215,7 +208,7 @@ int main(int argc, char* argv[])
   //  Simulation setting
   simgrid::s4u::Engine e(&argc, argv);
   e.load_platform(argv[1]);
-  hosts = e.get_all_hosts();
+  const auto hosts = e.get_all_hosts();
   xbt_assert(hosts.size() >= 4, "The given platform should contain at least 4 hosts (found %zu).", hosts.size());
 
   // Let's retrieve all SMPI jobs
@@ -231,7 +224,7 @@ int main(int argc, char* argv[])
   int initial_noise = std::stoi(argv[3]);
   xbt_assert(initial_noise >= 0, "Invalid initial_noise argument");
 
-  noise_between_jobs = std::stoi(argv[4]);
+  int noise_between_jobs = std::stoi(argv[4]);
   xbt_assert(noise_between_jobs >= 0, "Invalid noise_between_jobs argument");
 
   if (initial_noise > 0) {
@@ -240,10 +233,11 @@ int main(int argc, char* argv[])
   }
 
   // Let's execute the workload
-  simgrid::s4u::Actor::create("workload", hosts[0], workload_executor_process, &jobs);
+  simgrid::s4u::Actor::create("workload", hosts[0], workload_executor_process, std::cref(hosts), std::cref(jobs),
+                              noise_between_jobs);
 
   e.run();
-  XBT_INFO("Simulation finished! Final time: %g", e.get_clock());
+  XBT_INFO("Simulation finished! Final time: %g", simgrid::s4u::Engine::get_clock());
 
   SMPI_finalize();