]> AND Public Git Repository - simgrid.git/blobdiff - examples/cpp/network-ns3/s4u-network-ns3.cpp
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove MSG. Its EOL was scheduled for 2020
[simgrid.git] / examples / cpp / network-ns3 / s4u-network-ns3.cpp
index f523fe9c1d9048bce682923ab42cd94f52263753..ebc50c3f097a6bf7b02201dd2d186ba90598e0fb 100644 (file)
@@ -1,87 +1,60 @@
-/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "simgrid/s4u.hpp"
+#include <string>
+#include <unordered_map>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
+namespace sg4 = simgrid::s4u;
 
-int timer_start; // set as 1 in the master actor
-
-#define NTASKS 1500
 double start_time;
-const char* workernames[NTASKS];
-const char* masternames[NTASKS];
-int count_finished = 0;
+std::unordered_map<int, std::string> workernames;
+std::unordered_map<int, std::string> masternames;
 
-static void master(int argc, char* argv[])
+static void master(std::vector<std::string> args)
 {
-  xbt_assert(argc == 4, "Strange number of arguments expected 3 got %d", argc - 1);
+  xbt_assert(args.size() == 4, "Strange number of arguments expected 3 got %zu", args.size() - 1);
 
   XBT_DEBUG("Master started");
 
   /* data size */
-  double msg_size = std::stod(argv[1]);
-  int id          = std::stoi(argv[3]); // unique id to control statistics
+  double msg_size = std::stod(args[1]);
+  int id          = std::stoi(args[3]); // unique id to control statistics
 
   /* worker name */
-  workernames[id] = xbt_strdup(argv[2]);
+  workernames[id] = args[2];
 
-  simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(argv[3]);
+  sg4::Mailbox* mbox = sg4::Mailbox::by_name(args[3]);
 
-  masternames[id] = simgrid::s4u::Host::current()->get_cname();
+  masternames[id] = sg4::Host::current()->get_name();
 
   auto* payload = new double(msg_size);
 
-  count_finished++;
-  timer_start = 1;
-
   /* time measurement */
-  start_time = simgrid::s4u::Engine::get_clock();
-  mbox->put(payload, msg_size);
+  start_time = sg4::Engine::get_clock();
+  mbox->put(payload, static_cast<uint64_t>(msg_size));
 
   XBT_DEBUG("Finished");
 }
 
-static void timer(int argc, char* argv[])
+static void worker(std::vector<std::string> args)
 {
-  xbt_assert(argc == 3, "Strange number of arguments expected 2 got %d", argc - 1);
-  double first_sleep = std::stod(argv[1]);
-  double sleep_time  = std::stod(argv[2]);
-
-  XBT_DEBUG("Timer started");
+  xbt_assert(args.size() == 2, "Strange number of arguments expected 1 got %zu", args.size() - 1);
 
-  if (first_sleep)
-    simgrid::s4u::this_actor::sleep_for(first_sleep);
-
-  do {
-    XBT_DEBUG("Get sleep");
-    simgrid::s4u::this_actor::sleep_for(sleep_time);
-  } while (timer_start);
-
-  XBT_DEBUG("Finished");
-}
-
-static void worker(int argc, char* argv[])
-{
-  xbt_assert(argc == 2, "Strange number of arguments expected 1 got %d", argc - 1);
-
-  int id                      = std::stoi(argv[1]);
-  simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(argv[1]);
+  int id                      = std::stoi(args[1]);
+  sg4::Mailbox* mbox          = sg4::Mailbox::by_name(args[1]);
 
   XBT_DEBUG("Worker started");
 
   auto payload = mbox->get_unique<double>();
 
-  count_finished--;
-  if (count_finished == 0) {
-    timer_start = 0;
-  }
-
-  double elapsed_time = simgrid::s4u::Engine::get_clock() - start_time;
+  double elapsed_time = sg4::Engine::get_clock() - start_time;
 
-  XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, *payload, masternames[id], workernames[id]);
+  XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, *payload, masternames.at(id).c_str(),
+           workernames.at(id).c_str());
   XBT_DEBUG("FLOW[%d] : transferred in  %f seconds", id, elapsed_time);
 
   XBT_DEBUG("Finished");
@@ -89,7 +62,7 @@ static void worker(int argc, char* argv[])
 
 int main(int argc, char* argv[])
 {
-  simgrid::s4u::Engine e(&argc, argv);
+  sg4::Engine e(&argc, argv);
   xbt_assert(argc > 2,
              "Usage: %s platform_file deployment_file\n"
              "\tExample: %s platform.xml deployment.xml\n",
@@ -99,7 +72,6 @@ int main(int argc, char* argv[])
 
   e.register_function("master", master);
   e.register_function("worker", worker);
-  e.register_function("timer", timer);
 
   e.load_deployment(argv[2]);