Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill a few globals.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 9 Feb 2023 15:51:01 +0000 (16:51 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 9 Feb 2023 16:14:03 +0000 (17:14 +0100)
examples/cpp/synchro-condition-variable/s4u-synchro-condition-variable.cpp
examples/smpi/replay/replay.cpp
examples/smpi/replay_multiple_manual_deploy/replay_multiple_manual.cpp
include/simgrid/plugins/file_system.h
src/kernel/resource/NetworkModelFactors.cpp
src/kernel/resource/NetworkModelFactors.hpp
src/plugins/file_system/s4u_FileSystem.cpp

index a5b0b44..cf1a1db 100644 (file)
@@ -9,10 +9,7 @@
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
 namespace sg4 = simgrid::s4u;
 
-std::string data;
-bool done = false;
-
-static void worker_fun(sg4::ConditionVariablePtr cv, sg4::MutexPtr mutex)
+static void worker_fun(sg4::ConditionVariablePtr cv, sg4::MutexPtr mutex, std::string& data, bool& done)
 {
   std::unique_lock lock(*mutex);
 
@@ -30,11 +27,14 @@ static void master_fun()
 {
   auto mutex  = sg4::Mutex::create();
   auto cv     = sg4::ConditionVariable::create();
-  data        = "Example data";
-  auto worker = sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), worker_fun, cv, mutex);
+  std::string data = "Example data";
+  bool done        = false;
+
+  auto worker = sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), worker_fun, cv, mutex, std::ref(data),
+                                   std::ref(done));
 
   // wait for the worker
-  cv->wait(std::unique_lock<sg4::Mutex>(*mutex), []() { return done; });
+  cv->wait(std::unique_lock<sg4::Mutex>(*mutex), [&done]() { return done; });
   XBT_INFO("data is now '%s'.", data.c_str());
 
   worker->join();
index 3b62cab..d6fff83 100644 (file)
@@ -20,12 +20,6 @@ static void action_blah(const simgrid::xbt::ReplayAction& /*args*/)
      args is a strings array containing the blank-separated parameters found in the trace for this event instance. */
 }
 
-action_fun previous_send;
-static void overriding_send(simgrid::xbt::ReplayAction& args)
-{
-  previous_send(args); // Just call the overridden symbol. That's a toy example.
-}
-
 int main(int argc, char* argv[])
 {
   auto properties = simgrid::s4u::Actor::self()->get_properties();
@@ -47,14 +41,17 @@ int main(int argc, char* argv[])
   /* Connect your callback function to the "blah" event in the trace files */
   xbt_replay_action_register("blah", action_blah);
 
-  /* The send action is an override, so we have to first save its previous value in a global */
+  /* The send action is an override, so we could have saved its previous value in a global, or use a lambda capture like
+   * in the following */
   int new_rank;
   MPI_Comm_rank(MPI_COMM_WORLD, &new_rank);
   if (new_rank != rank)
     XBT_WARN("Rank inconsistency. Got %d, expected %d", new_rank, rank);
   if (rank == 0) {
-    previous_send = xbt_replay_action_get("send");
-    xbt_replay_action_register("send", overriding_send);
+    auto previous_send = xbt_replay_action_get("send");
+    xbt_replay_action_register("send", [previous_send](simgrid::xbt::ReplayAction& args) {
+      previous_send(args); // Just call the overridden symbol. That's a toy example.
+    });
   }
   /* The regular run of the replayer */
   if (shared_trace != nullptr)
index fe5a0e0..7707303 100644 (file)
@@ -44,9 +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 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());
@@ -74,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());
 
@@ -95,7 +92,8 @@ 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, int noise_between_jobs)
+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) {
     // Let's wait until the job's waiting time if needed
@@ -116,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);
   }
 
@@ -209,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
@@ -234,7 +233,8 @@ int main(int argc, char* argv[])
   }
 
   // Let's execute the workload
-  simgrid::s4u::Actor::create("workload", hosts[0], workload_executor_process, std::cref(jobs), noise_between_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", simgrid::s4u::Engine::get_clock());
index af744b0..82632b4 100644 (file)
@@ -139,6 +139,7 @@ public:
 class XBT_PUBLIC FileDescriptorHostExt {
 public:
   static simgrid::xbt::Extension<Host, FileDescriptorHostExt> EXTENSION_ID;
+  static int max_file_descriptors;
   FileDescriptorHostExt() = default;
   FileDescriptorHostExt(const FileDescriptorHostExt&) = delete;
   FileDescriptorHostExt& operator=(const FileDescriptorHostExt&) = delete;
index ffd1b69..06ea79c 100644 (file)
@@ -5,7 +5,6 @@
 
 #include "src/kernel/resource/NetworkModelFactors.hpp"
 #include "simgrid/sg_config.hpp"
-#include "src/kernel/resource/FactorSet.hpp"
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
 
@@ -14,9 +13,6 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
  *********/
 
 namespace simgrid::kernel::resource {
-static FactorSet cfg_latency_factor("network/latency-factor");
-static FactorSet cfg_bandwidth_factor("network/bandwidth-factor");
-
 config::Flag<std::string> cfg_latency_factor_str(
     "network/latency-factor", std::initializer_list<const char*>{"smpi/lat-factor"},
     "Correction factor to apply to the provided latency (default value overridden by network model)", "1.0");
@@ -24,6 +20,9 @@ static config::Flag<std::string> cfg_bandwidth_factor_str(
     "network/bandwidth-factor", std::initializer_list<const char*>{"smpi/bw-factor"},
     "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0");
 
+FactorSet NetworkModelFactors::cfg_latency_factor("network/latency-factor");
+FactorSet NetworkModelFactors::cfg_bandwidth_factor("network/bandwidth-factor");
+
 double NetworkModelFactors::get_bandwidth_factor() const
 {
   xbt_assert(not bw_factor_cb_,
index e9e4ee6..40a4fae 100644 (file)
@@ -7,6 +7,7 @@
 #define SIMGRID_KERNEL_RESOURCE_NETWORKMODELFACTORS_HPP
 
 #include "simgrid/sg_config.hpp"
+#include "src/kernel/resource/FactorSet.hpp"
 #include "xbt/asserts.h"
 #include <simgrid/forward.h>
 
@@ -18,6 +19,9 @@ namespace simgrid::kernel::resource {
 /** This Trait of NetworkModel is in charge of handling the network factors (bw and lat) */
 
 class XBT_PUBLIC NetworkModelFactors {
+  static FactorSet cfg_latency_factor;
+  static FactorSet cfg_bandwidth_factor;
+
   using NetworkFactorCb = double(double size, const s4u::Host* src, const s4u::Host* dst,
                                  const std::vector<s4u::Link*>& links,
                                  const std::unordered_set<s4u::NetZone*>& netzones);
index 4d6db1a..ec92efb 100644 (file)
@@ -23,7 +23,6 @@
 #include <numeric>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_file, s4u, "S4U files");
-int sg_storage_max_file_descriptors = 1024;
 
 /** @defgroup plugin_filesystem Plugin FileSystem
  *
@@ -38,6 +37,7 @@ template class xbt::Extendable<s4u::File>;
 namespace s4u {
 simgrid::xbt::Extension<Disk, FileSystemDiskExt> FileSystemDiskExt::EXTENSION_ID;
 simgrid::xbt::Extension<Host, FileDescriptorHostExt> FileDescriptorHostExt::EXTENSION_ID;
+int FileDescriptorHostExt::max_file_descriptors;
 
 const Disk* File::find_local_disk_on(const Host* host)
 {
@@ -78,9 +78,9 @@ File::File(const std::string& fullpath, const_sg_host_t host, void* userdata) :
     local_disk_ = find_local_disk_on(host);
 
     // assign a file descriptor id to the newly opened File
-    auto* ext = host->extension<simgrid::s4u::FileDescriptorHostExt>();
+    auto* ext = host->extension<FileDescriptorHostExt>();
     if (ext->file_descriptor_table == nullptr) {
-      ext->file_descriptor_table = std::make_unique<std::vector<int>>(sg_storage_max_file_descriptors);
+      ext->file_descriptor_table = std::make_unique<std::vector<int>>(FileDescriptorHostExt::max_file_descriptors);
       std::iota(ext->file_descriptor_table->rbegin(), ext->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
     }
     xbt_assert(not ext->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
@@ -119,8 +119,7 @@ File* File::open(const std::string& fullpath, const_sg_host_t host, void* userda
 
 void File::close()
 {
-  std::vector<int>* desc_table =
-      Host::current()->extension<simgrid::s4u::FileDescriptorHostExt>()->file_descriptor_table.get();
+  std::vector<int>* desc_table = Host::current()->extension<FileDescriptorHostExt>()->file_descriptor_table.get();
   kernel::actor::simcall_answered([this, desc_table] { desc_table->push_back(this->desc_id); });
   delete this;
 }
@@ -470,8 +469,8 @@ static void on_simulation_end()
  */
 void sg_storage_file_system_init()
 {
-  sg_storage_max_file_descriptors = 1024;
-  simgrid::config::bind_flag(sg_storage_max_file_descriptors, "storage/max_file_descriptors",
+  FileDescriptorHostExt::max_file_descriptors = 1024;
+  simgrid::config::bind_flag(FileDescriptorHostExt::max_file_descriptors, "storage/max_file_descriptors",
                              "Maximum number of concurrently opened files per host. Default is 1024");
 
   if (not FileSystemDiskExt::EXTENSION_ID.valid()) {