Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
399fe998125494d7d28ad82787d49e5040a2a342
[simgrid.git] / teshsuite / models / cm02-set-lat-bw / cm02-set-lat-bw.cpp
1 /* Copyright (c) 2017-2021. 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 /**
7  * Test NetworkCm02Link::set_bandwidth and set_latency.
8  *
9  * Math behind: https://hal.inria.fr/inria-00361031/document
10  * - Fig. 1 and Eq. 7 in the paper
11  *
12  *
13  * Platform: dogbone
14  *
15  *   S1 ___[ 1 ]___                 ___[ 2 ]___ C1
16  *                 \               /
17  *                  R1 __[ 0 ]__ R2
18  *                 /               \
19  *   S2 ___[ 3 ]__/                 \__[ 4 ]___ C2
20  *
21  * 2 communications:
22  * - Flow A: S1 -> C1
23  * - Flow B: S2 -> C2
24  *
25  * Links: [1], [2], [3]: 1Gb/s, 10ms
26  * Link: [0] bottleneck: 1Mb/s, 20ms
27  * Link: [4]: We'll change the bandwidth and latency to see the impact on the
28  * sharing of the bottleneck link between the 2 flows.
29  */
30
31 #include <simgrid/s4u.hpp>
32
33 namespace sg4 = simgrid::s4u;
34
35 XBT_LOG_NEW_DEFAULT_CATEGORY(cm02_set_lat_bw, "Messages specific for this simulation");
36
37 static void sender(const std::string& recv_name, sg4::Link* l4)
38 {
39   sg4::Mailbox* mbox = sg4::Mailbox::by_name(recv_name);
40   XBT_INFO("Comm to %s, same weight/penalty (w_a == w_b, ~20) for everybody, each comm should take 1s and finish at "
41            "the same time",
42            recv_name.c_str());
43   auto* payload = new double(sg4::Engine::get_clock());
44   auto comm     = mbox->put_async(payload, 1e3);
45   comm->wait();
46   sg4::this_actor::sleep_until(10); // synchronize senders
47
48   if (recv_name == "C2") {
49     XBT_INFO("Comm Flow B to C2: after 1s, change latency of L4 to increase penalty for flow B (w_b = 2* w_a)");
50     XBT_INFO("rho_a = 2*rho_b, flow A receives twice the bandwidth than flow B");
51   } else {
52     XBT_INFO("Comm Flow A to C1");
53   }
54   payload = new double(sg4::Engine::get_clock());
55   comm    = mbox->put_async(payload, 1e3);
56   sg4::this_actor::sleep_for(1);
57   if (l4)
58     l4->set_latency(20);
59   comm->wait();
60   sg4::this_actor::sleep_until(20); // synchronize senders
61   if (l4)
62     l4->set_latency(1e-9);
63
64   if (recv_name == "C2") {
65     XBT_INFO("Comm Flow B to C2: after 1s, change bandwidth of L4 to increase penalty for flow B (w_b = 2* w_a)");
66     XBT_INFO("rho_a = 2*rho_b, flow A receives twice the bandwidth than flow B");
67   } else {
68     XBT_INFO("Comm Flow A to C1");
69   }
70   payload = new double(sg4::Engine::get_clock());
71   comm    = mbox->put_async(payload, 1e3);
72   sg4::this_actor::sleep_for(1);
73   if (l4)
74     l4->set_bandwidth(1e3);
75   comm->wait();
76   sg4::this_actor::sleep_until(30);
77
78   payload = new double(-1.0);
79   mbox->put(payload, 0);
80 }
81
82 static void receiver()
83 {
84   sg4::Mailbox* mbox = sg4::Mailbox::by_name(sg4::this_actor::get_host()->get_name());
85   while (true) {
86     auto payload = mbox->get_unique<double>();
87     if (*payload < 0)
88       break;
89     XBT_INFO("Received data. Elapsed %lf", sg4::Engine::get_clock() - *payload);
90   }
91   XBT_INFO("Bye");
92 }
93
94 /*************************************************************************************************/
95 int main(int argc, char** argv)
96 {
97   sg4::Engine e(&argc, argv);
98   /* keep it simple, no network factors nor crosstrafic */
99   simgrid::s4u::Engine::set_config("network/model:CM02");
100   simgrid::s4u::Engine::set_config("network/weight-S:20537");
101   simgrid::s4u::Engine::set_config("network/crosstraffic:0");
102
103   /* dog-bone platform */
104   std::unordered_map<std::string, sg4::Host*> hosts;
105   std::unordered_map<std::string, sg4::Link*> links;
106   auto* zone = sg4::create_full_zone("dog_zone");
107   for (const auto& name : {"S1", "S2", "C1", "C2"}) {
108     hosts[name] = zone->create_host(name, 1e6)->seal();
109   }
110
111   for (const auto& name : {"L1", "L2", "L3", "L4"}) {
112     links[name] = zone->create_link(name, 1e9)->set_latency(1e-9)->seal();
113   }
114   links["L0"] = zone->create_link("L0", 1e3)->seal();
115   zone->add_route(hosts["S1"]->get_netpoint(), hosts["C1"]->get_netpoint(), nullptr, nullptr,
116                   {sg4::LinkInRoute(links["L1"]), sg4::LinkInRoute(links["L0"]), sg4::LinkInRoute(links["L2"])});
117   zone->add_route(hosts["S2"]->get_netpoint(), hosts["C2"]->get_netpoint(), nullptr, nullptr,
118                   {sg4::LinkInRoute(links["L3"]), sg4::LinkInRoute(links["L0"]), sg4::LinkInRoute(links["L4"])});
119
120   zone->seal();
121
122   sg4::Actor::create("", hosts["S1"], sender, "C1", nullptr);
123   sg4::Actor::create("", hosts["C1"], receiver);
124   sg4::Actor::create("", hosts["S2"], sender, "C2", links["L4"]);
125   sg4::Actor::create("", hosts["C2"], receiver);
126
127   e.run();
128
129   return 0;
130 }