Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanup work to make is_race_reversible() a method of Transition
[simgrid.git] / examples / cpp / clusters-multicpu / s4u-clusters-multicpu.cpp
index 0cf10c47dead465b3c1dfcebd8cb5e19c9467c60..b2ca2e78f75fd72ba8b9248c0c3a4c6c8979ed96 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-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. */
@@ -20,34 +20,33 @@ class Sender {
   std::vector<sg4::Host*> hosts_;
 
 public:
-  explicit Sender(const std::vector<sg4::Host*> hosts) : hosts_{hosts} {}
+  explicit Sender(const std::vector<sg4::Host*>& hosts) : hosts_{hosts} {}
   void operator()() const
   {
     /* Vector in which we store all ongoing communications */
-    std::vector<sg4::CommPtr> pending_comms;
+    sg4::ActivitySet pending_comms;
 
     /* Make a vector of the mailboxes to use */
     std::vector<sg4::Mailbox*> mboxes;
 
     /* Start dispatching 1 message to all receivers */
-    std::string msg_content =
-        std::string("Hello, I'm alive and running on ") + std::string(sg4::this_actor::get_host()->get_name());
+    std::string msg_content = "Hello, I'm alive and running on " + sg4::this_actor::get_host()->get_name();
     for (const auto* host : hosts_) {
       /* Copy the data we send: the 'msg_content' variable is not a stable storage location.
        * It will be destroyed when this actor leaves the loop, ie before the receiver gets it */
       auto* payload = new std::string(msg_content);
 
       /* Create a communication representing the ongoing communication, and store it in pending_comms */
-      auto mbox = sg4::Mailbox::by_name(host->get_name());
+      auto* mbox = sg4::Mailbox::by_name(host->get_name());
       mboxes.push_back(mbox);
       sg4::CommPtr comm = mbox->put_async(payload, msg_size);
-      pending_comms.push_back(comm);
+      pending_comms.push(comm);
     }
 
     XBT_INFO("Done dispatching all messages");
 
     /* Now that all message exchanges were initiated, wait for their completion in one single call */
-    sg4::Comm::wait_all(&pending_comms);
+    pending_comms.wait_all();
 
     XBT_INFO("Goodbye now!");
   }
@@ -58,7 +57,7 @@ class Receiver {
 public:
   void operator()() const
   {
-    auto mbox     = sg4::Mailbox::by_name(sg4::this_actor::get_host()->get_name());
+    auto* mbox    = sg4::Mailbox::by_name(sg4::this_actor::get_host()->get_name());
     auto received = mbox->get_unique<std::string>();
     XBT_INFO("I got a '%s'.", received->c_str());
   }
@@ -88,8 +87,7 @@ public:
  * @param id Internal identifier in the torus (for information)
  * @return netpoint, gateway: the netpoint to the StarZone and CPU0 as gateway
  */
-static std::pair<simgrid::kernel::routing::NetPoint*, simgrid::kernel::routing::NetPoint*>
-create_hostzone(const sg4::NetZone* zone, const std::vector<unsigned int>& /*coord*/, int id)
+static sg4::NetZone* create_hostzone(const sg4::NetZone* zone, const std::vector<unsigned long>& /*coord*/, unsigned long id)
 {
   constexpr int num_cpus    = 8;     //!< Number of CPUs in the zone
   constexpr double speed    = 1e9;   //!< Speed of each CPU
@@ -102,23 +100,21 @@ create_hostzone(const sg4::NetZone* zone, const std::vector<unsigned int>& /*coo
   /* setting my Torus parent zone */
   host_zone->set_parent(zone);
 
-  const sg4::Host* gateway = nullptr;
   /* create CPUs */
   for (int i = 0; i < num_cpus; i++) {
     std::string cpu_name  = hostname + "-cpu" + std::to_string(i);
-    const sg4::Host* host = host_zone->create_host(cpu_name, speed)->seal();
+    const sg4::Host* host = host_zone->create_host(cpu_name, speed);
     /* the first CPU is the gateway */
     if (i == 0)
-      gateway = host;
-    /* create 2 links for a full-duplex communication */
-    sg4::Link* link_up   = host_zone->create_link("link-up-" + cpu_name, link_bw)->set_latency(link_lat)->seal();
-    sg4::Link* link_down = host_zone->create_link("link-down-" + cpu_name, link_bw)->set_latency(link_lat)->seal();
-    /* link UP, connection from CPU to outer world */
-    host_zone->add_route(host->get_netpoint(), nullptr, nullptr, nullptr, std::vector<sg4::Link*>{link_up}, false);
-    /* link DOWN, connection from outer to CPU */
-    host_zone->add_route(nullptr, host->get_netpoint(), nullptr, nullptr, std::vector<sg4::Link*>{link_down}, false);
+      host_zone->set_gateway(host->get_netpoint());
+    /* create split-duplex link */
+    auto* link = host_zone->create_split_duplex_link("link-" + cpu_name, link_bw)->set_latency(link_lat);
+    /* connecting CPU to outer world */
+    host_zone->add_route(host, nullptr, {{link, sg4::LinkInRoute::Direction::UP}}, true);
   }
-  return std::make_pair(host_zone->get_netpoint(), gateway->get_netpoint());
+  /* seal newly created netzone */
+  host_zone->seal();
+  return host_zone;
 }
 
 /*************************************************************************************************/
@@ -138,7 +134,7 @@ create_hostzone(const sg4::NetZone* zone, const std::vector<unsigned int>& /*coo
  * @param id Internal identifier in the torus (for information)
  * @return Limiter link
  */
-static sg4::Link* create_limiter(sg4::NetZone* zone, const std::vector<unsigned int>& /*coord*/, int id)
+static sg4::Link* create_limiter(sg4::NetZone* zone, const std::vector<unsigned long>& /*coord*/, unsigned long id)
 {
   return zone->create_link("limiter-" + std::to_string(id), 1e9)->seal();
 }
@@ -292,10 +288,9 @@ static void create_dragonfly_cluster()
 int main(int argc, char* argv[])
 {
   sg4::Engine e(&argc, argv);
-  std::string platform = argv[1];
 
   /* create platform */
-  if (platform == "torus")
+  if (std::string platform(argv[1]); platform == "torus")
     create_torus_cluster();
   else if (platform == "fatTree")
     create_fatTree_cluster();
@@ -307,7 +302,7 @@ int main(int argc, char* argv[])
   sg4::Actor::create("sender", host_list[0], Sender(host_list));
   /* create receiver in every host */
   for (auto* host : host_list) {
-    sg4::Actor::create(std::string("receiver-") + std::string(host->get_name()), host, Receiver());
+    sg4::Actor::create("receiver-" + host->get_name(), host, Receiver());
   }
 
   /* runs the simulation */