Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / cpp / platform-profile / s4u-platform-profile.cpp
1 /* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/kernel/ProfileBuilder.hpp"
7 #include "simgrid/s4u.hpp"
8
9 /* This example demonstrates how to attach a profile to a host or a link, to specify external changes to the resource
10  * speed. The first way to do so is to use a file in the XML, while the second is to use the programmatic interface. */
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_platform_profile, "Messages specific for this s4u example");
13 namespace sg4 = simgrid::s4u;
14
15 static void watcher()
16 {
17   const auto* jupiter  = sg4::Host::by_name("Jupiter");
18   const auto* fafard   = sg4::Host::by_name("Fafard");
19   const auto* lilibeth = sg4::Host::by_name("Lilibeth");
20   const auto* link1    = sg4::Link::by_name("1");
21   const auto* link2    = sg4::Link::by_name("2");
22
23   std::vector<sg4::Link*> links;
24   double lat = 0;
25   jupiter->route_to(fafard, links, &lat);
26
27   std::string path;
28   for (const auto* l : links)
29     path += std::string(path.empty() ? "" : ", ") + "link '" + l->get_name() + "'";
30   XBT_INFO("Path from Jupiter to Fafard: %s (latency: %fs).", path.c_str(), lat);
31
32   for (int i = 0; i < 10; i++) {
33     XBT_INFO("Fafard: %.0fMflops, Jupiter: %4.0fMflops, Lilibeth: %3.1fMflops, Link1: (%.2fMB/s %.0fms), Link2: "
34              "(%.2fMB/s %.0fms)",
35              fafard->get_speed() * fafard->get_available_speed() / 1000000,
36              jupiter->get_speed() * jupiter->get_available_speed() / 1000000,
37              lilibeth->get_speed() * lilibeth->get_available_speed() / 1000000, link1->get_bandwidth() / 1000,
38              link1->get_latency() * 1000, link2->get_bandwidth() / 1000, link2->get_latency() * 1000);
39     sg4::this_actor::sleep_for(1);
40   }
41 }
42
43 int main(int argc, char* argv[])
44 {
45   sg4::Engine e(&argc, argv);
46
47   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s platform.xml\n", argv[0], argv[0]);
48
49   e.load_platform(argv[1]);
50
51   // Add a new host programmatically, and attach a simple speed profile to it (alternate between full and half speed
52   // every two seconds
53   e.get_netzone_root()
54       ->create_host("Lilibeth", 25e6)
55       ->set_speed_profile(simgrid::kernel::profile::ProfileBuilder::from_string("lilibeth_profile", R"(
56 0 1.0
57 2 0.5
58 )",
59                                                                                 4))
60       ->seal();
61
62   // Add a watcher of the changes
63   sg4::Actor::create("watcher", e.host_by_name("Fafard"), watcher);
64
65   e.run();
66
67   return 0;
68 }