Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add python bindings for plugin host load
[simgrid.git] / examples / cpp / network-factors / s4u-network-factors.cpp
index 0e2d09d53625adc52b001415cf1e72e9ffe9c80b..1ff67f6252c2b7e02209ead39700387b1947288d 100644 (file)
@@ -1,22 +1,20 @@
-/* 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. */
 
-/* This example shows how to build set customized communication factors
+/* This example shows how to build set custom communication factors
  *
- * It uses the interface provided by NetworkModelIntf to register 2 callbacks that
- * are called everytime a communication occurs.
+ * It uses the netzone interface to register 2 callbacks that are called for every communications.
  *
  * These factors are used to change the communication time depending on the message size
  * and destination.
  *
  * This example uses factors obtained by some experiments on dahu cluster in Grid'5000.
- * You must change the values according to the calibration of your enviroment.
+ * You should change the values according to the calibration of your enviroment.
  */
 
 #include <map>
-#include <simgrid/kernel/resource/NetworkModelIntf.hpp>
 #include <simgrid/s4u.hpp>
 namespace sg4 = simgrid::s4u;
 
@@ -75,15 +73,13 @@ static void load_platform()
     /* create host */
     const sg4::Host* host = root->create_host(hostname, 1)->set_core_count(32)->seal();
     /* create UP/DOWN link */
-    sg4::Link* l_up   = root->create_link(hostname + "_up", BW_REMOTE)->set_latency(LATENCY)->seal();
-    sg4::Link* l_down = root->create_link(hostname + "_down", BW_REMOTE)->set_latency(LATENCY)->seal();
+    const sg4::Link* l = root->create_split_duplex_link(hostname, BW_REMOTE)->set_latency(LATENCY)->seal();
 
     /* add link UP/DOWN for communications from the host */
-    root->add_route(host->get_netpoint(), nullptr, nullptr, nullptr, std::vector<sg4::Link*>{l_up}, false);
-    root->add_route(nullptr, host->get_netpoint(), nullptr, nullptr, std::vector<sg4::Link*>{l_down}, false);
+    root->add_route(host->get_netpoint(), nullptr, nullptr, nullptr, {{l, sg4::LinkInRoute::Direction::UP}}, true);
 
-    sg4::Link* loopback = root->create_link(hostname + "_loopback", BW_LOCAL)->set_latency(LATENCY)->seal();
-    root->add_route(host->get_netpoint(), host->get_netpoint(), nullptr, nullptr, std::vector<sg4::Link*>{loopback});
+    const sg4::Link* loopback = root->create_link(hostname + "_loopback", BW_LOCAL)->set_latency(LATENCY)->seal();
+    root->add_route(host->get_netpoint(), host->get_netpoint(), nullptr, nullptr, {sg4::LinkInRoute(loopback)});
   }
 
   root->seal();
@@ -94,11 +90,11 @@ static void load_platform()
 static double get_factor_from_map(const std::map<double, double>& factors, double size)
 {
   double factor = 1.0;
-  for (auto const& fact : factors) {
-    if (size < fact.first) {
+  for (auto const& [factor_size, factor_value] : factors) {
+    if (size < factor_size) {
       break;
     } else {
-      factor = fact.second;
+      factor = factor_value;
     }
   }
   return factor;
@@ -187,7 +183,7 @@ public:
         }
 
         /* Create a communication representing the ongoing communication */
-        auto mbox     = sg4::Mailbox::by_name(host->get_name());
+        auto* mbox    = sg4::Mailbox::by_name(host->get_name());
         auto* payload = new std::string(msg);
         mbox->put(payload, static_cast<uint64_t>(size));
       }
@@ -196,7 +192,7 @@ public:
     XBT_INFO("Done dispatching all messages");
     /* sending message to stop receivers */
     for (const auto* host : hosts_) {
-      auto mbox = sg4::Mailbox::by_name(host->get_name());
+      auto* mbox = sg4::Mailbox::by_name(host->get_name());
       mbox->put(new std::string("finalize"), 0);
     }
   }
@@ -207,7 +203,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());
     // Receiving the message was all we were supposed to do
     for (bool cont = true; cont;) {
       auto received = mbox->get_unique<std::string>();
@@ -234,16 +230,14 @@ int main(int argc, char* argv[])
   /* create platform */
   load_platform();
   /* setting network factors callbacks */
-  simgrid::kernel::resource::NetworkModelIntf* model = e.get_netzone_root()->get_network_model();
-  model->set_lat_factor_cb(latency_factor_cb);
-  model->set_bw_factor_cb(bandwidth_factor_cb);
+  e.get_netzone_root()->set_latency_factor_cb(latency_factor_cb);
+  e.get_netzone_root()->set_bandwidth_factor_cb(bandwidth_factor_cb);
 
   sg4::Host* host        = e.host_by_name("dahu-1.grid5000.fr");
   sg4::Host* host_remote = e.host_by_name("dahu-10.grid5000.fr");
-  sg4::Actor::create(std::string("receiver-local"), host, Receiver());
-  sg4::Actor::create(std::string("receiver-remote"), host_remote, Receiver());
-  sg4::Actor::create(std::string("sender") + std::string(host->get_name()), host,
-                     Sender({host, host_remote}, crosstraffic));
+  sg4::Actor::create("receiver-local", host, Receiver());
+  sg4::Actor::create("receiver-remote", host_remote, Receiver());
+  sg4::Actor::create("sender" + host->get_name(), host, Sender({host, host_remote}, crosstraffic));
 
   /* runs the simulation */
   e.run();