Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
De-obfuscation.
[simgrid.git] / examples / cpp / network-factors / s4u-network-factors.cpp
1 /* Copyright (c) 2010-2022. 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 /* This example shows how to build set customized communication factors
7  *
8  * It uses the interface provided by NetworkModelIntf to register 2 callbacks that
9  * are called everytime a communication occurs.
10  *
11  * These factors are used to change the communication time depending on the message size
12  * and destination.
13  *
14  * This example uses factors obtained by some experiments on dahu cluster in Grid'5000.
15  * You must change the values according to the calibration of your enviroment.
16  */
17
18 #include <map>
19 #include <simgrid/kernel/resource/NetworkModelIntf.hpp>
20 #include <simgrid/s4u.hpp>
21 namespace sg4 = simgrid::s4u;
22
23 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_network_factors, "Messages specific for this s4u example");
24
25 /* Factors used in this platform, for remote and local communications
26  * Obtained from dahu cluster. Obs.: just an example, change the values according
27  * to the calibration on your environment */
28 static const std::map<double, double> REMOTE_BW_FACTOR = {
29     {0, 1.0000000000000002},         {8000, 1.0000000000000002},     {15798, 0.07435006650635523},
30     {64000, 0.3163352696348148},     {6000000, 0.13003278960133288}, {42672591, 0.10354740223279707},
31     {160097505, 0.40258935729656503}};
32 static const std::map<double, double> LOCAL_BW_FACTOR = {{0, 0.17591906192813994},
33                                                          {16000, 0.12119203247138953},
34                                                          {6000000, 0.07551057012803415},
35                                                          {36900419, 0.04281516758309203},
36                                                          {160097505, 0.17440518795992602}};
37
38 static const std::map<double, double> REMOTE_LAT_FACTOR = {{0, 0.0},
39                                                            {8000, 1731.7102918851567},
40                                                            {15798, 1441.073993161278},
41                                                            {64000, 1761.4784830658123},
42                                                            {6000000, 0.0},
43                                                            {42672591, 0.0},
44                                                            {160097505, 970913.4558162984}};
45 static const std::map<double, double> LOCAL_LAT_FACTOR  = {
46     {0, 0.0}, {16000, 650.2212383180362}, {6000000, 0.0}, {36900419, 0.0}, {160097505, 1017885.3518765072}};
47
48 /* bandwidth and latency used on the platform */
49 constexpr static double BW_REMOTE = 12.5e9;
50 constexpr static double BW_LOCAL  = 25e9;
51 constexpr static double LATENCY   = .1e-6;
52
53 /*************************************************************************************************/
54 /** @brief Create a simple platform based on Dahu cluster */
55 static void load_platform()
56 {
57   /**
58    * Inspired on dahu cluster on Grenoble
59    *     ________________
60    *     |               |
61    *     |     dahu      |
62    *     |_______________|
63    *     / /   | |    \ \
64    *    / /    | |     \ \     <-- 12.5GBps links
65    *   / /     | |      \ \
66    * host1     ...      hostN
67    */
68
69   auto* root         = sg4::create_star_zone("dahu");
70   std::string prefix = "dahu-";
71   std::string suffix = ".grid5000.fr";
72
73   for (int id = 0; id < 32; id++) {
74     std::string hostname = prefix + std::to_string(id) + suffix;
75     /* create host */
76     const sg4::Host* host = root->create_host(hostname, 1)->set_core_count(32)->seal();
77     /* create UP/DOWN link */
78     const sg4::Link* l = root->create_split_duplex_link(hostname, BW_REMOTE)->set_latency(LATENCY)->seal();
79
80     /* add link UP/DOWN for communications from the host */
81     root->add_route(host->get_netpoint(), nullptr, nullptr, nullptr, {{l, sg4::LinkInRoute::Direction::UP}}, true);
82
83     const sg4::Link* loopback = root->create_link(hostname + "_loopback", BW_LOCAL)->set_latency(LATENCY)->seal();
84     root->add_route(host->get_netpoint(), host->get_netpoint(), nullptr, nullptr, {sg4::LinkInRoute(loopback)});
85   }
86
87   root->seal();
88 }
89
90 /*************************************************************************************************/
91 /** @brief Auxiliary method to get factor for a message size */
92 static double get_factor_from_map(const std::map<double, double>& factors, double size)
93 {
94   double factor = 1.0;
95   for (auto const& fact : factors) {
96     if (size < fact.first) {
97       break;
98     } else {
99       factor = fact.second;
100     }
101   }
102   return factor;
103 }
104
105 /**
106  * @brief Callback to set latency factor for a communication
107  *
108  * Set different factors for local (loopback) and remote communications.
109  * Function signature is defined by API
110  *
111  * @param size Message size
112  * @param src Host origin
113  * @param dst Host destination
114  */
115 static double latency_factor_cb(double size, const sg4::Host* src, const sg4::Host* dst,
116                                 const std::vector<sg4::Link*>& /*links*/,
117                                 const std::unordered_set<sg4::NetZone*>& /*netzones*/)
118 {
119   if (src->get_name() == dst->get_name()) {
120     /* local communication factors */
121     return get_factor_from_map(LOCAL_LAT_FACTOR, size);
122   } else {
123     return get_factor_from_map(REMOTE_LAT_FACTOR, size);
124   }
125 }
126
127 /**
128  * @brief Callback to set bandwidth factor for a communication
129  *
130  * Set different factors for local (loopback) and remote communications.
131  * Function signature is defined by API
132  *
133  * @param size Message size
134  * @param src Host origin
135  * @param dst Host destination
136  */
137 static double bandwidth_factor_cb(double size, const sg4::Host* src, const sg4::Host* dst,
138                                   const std::vector<sg4::Link*>& /*links*/,
139                                   const std::unordered_set<sg4::NetZone*>& /*netzones*/)
140 {
141   if (src->get_name() == dst->get_name()) {
142     /* local communication factors */
143     return get_factor_from_map(LOCAL_BW_FACTOR, size);
144   } else {
145     return get_factor_from_map(REMOTE_BW_FACTOR, size);
146   }
147 }
148
149 /*************************************************************************************************/
150 class Sender {
151   std::vector<sg4::Host*> hosts_;
152   double crosstraffic_ = 1.0;
153
154 public:
155   explicit Sender(const std::vector<sg4::Host*>& hosts, bool crosstraffic) : hosts_{hosts}
156   {
157     if (crosstraffic)
158       crosstraffic_ = 1.05; // add crosstraffic load if it is enabled
159   }
160   void operator()() const
161   {
162     const std::vector<double> msg_sizes = {64e3, 64e6, 64e9}; // 64KB, 64MB, 64GB
163
164     for (double size : msg_sizes) {
165       for (const auto* host : hosts_) {
166         std::string msg;
167         /* calculating the estimated communication time depending of message size and destination */
168         if (host->get_name() == sg4::this_actor::get_host()->get_name()) {
169           double lat_factor = get_factor_from_map(LOCAL_LAT_FACTOR, size);
170           double bw_factor  = get_factor_from_map(LOCAL_BW_FACTOR, size);
171           /* Account for crosstraffic on local communications
172            * local communications use only a single link and crosstraffic impact on resource sharing
173            * on remote communications, we don't see this effect since we have split-duplex links */
174           double est_time =
175               sg4::Engine::get_clock() + size / (BW_LOCAL * bw_factor / crosstraffic_) + LATENCY * lat_factor;
176
177           msg = "Local communication: size=" + std::to_string(size) + ". Use bw_factor=" + std::to_string(bw_factor) +
178                 " lat_factor=" + std::to_string(lat_factor) + ". Estimated finished time=" + std::to_string(est_time);
179         } else {
180           double lat_factor = get_factor_from_map(REMOTE_LAT_FACTOR, size);
181           double bw_factor  = get_factor_from_map(REMOTE_BW_FACTOR, size);
182           double est_time   = sg4::Engine::get_clock() + (size / (BW_REMOTE * bw_factor)) + LATENCY * lat_factor * 2;
183           msg = "Remote communication: size=" + std::to_string(size) + ". Use bw_factor=" + std::to_string(bw_factor) +
184                 " lat_factor=" + std::to_string(lat_factor) + ". Estimated finished time=" + std::to_string(est_time);
185         }
186
187         /* Create a communication representing the ongoing communication */
188         auto mbox     = sg4::Mailbox::by_name(host->get_name());
189         auto* payload = new std::string(msg);
190         mbox->put(payload, static_cast<uint64_t>(size));
191       }
192     }
193
194     XBT_INFO("Done dispatching all messages");
195     /* sending message to stop receivers */
196     for (const auto* host : hosts_) {
197       auto mbox = sg4::Mailbox::by_name(host->get_name());
198       mbox->put(new std::string("finalize"), 0);
199     }
200   }
201 };
202
203 /* Receiver actor: wait for messages on the mailbox identified by the hostname */
204 class Receiver {
205 public:
206   void operator()() const
207   {
208     auto mbox = sg4::Mailbox::by_name(sg4::this_actor::get_host()->get_name());
209     // Receiving the message was all we were supposed to do
210     for (bool cont = true; cont;) {
211       auto received = mbox->get_unique<std::string>();
212       XBT_INFO("I got a '%s'.", received->c_str());
213       cont = (*received != "finalize"); // If it's a finalize message, we're done
214     }
215   }
216 };
217
218 /*************************************************************************************************/
219 int main(int argc, char* argv[])
220 {
221   bool crosstraffic = true;
222   sg4::Engine e(&argc, argv);
223   /* setting network model to default one */
224   sg4::Engine::set_config("network/model:CM02");
225
226   /* test with crosstraffic disabled */
227   if (argc == 2 && std::string(argv[1]) == "disable_crosstraffic") {
228     sg4::Engine::set_config("network/crosstraffic:0");
229     crosstraffic = false;
230   }
231
232   /* create platform */
233   load_platform();
234   /* setting network factors callbacks */
235   simgrid::kernel::resource::NetworkModelIntf* model = e.get_netzone_root()->get_network_model();
236   model->set_lat_factor_cb(latency_factor_cb);
237   model->set_bw_factor_cb(bandwidth_factor_cb);
238
239   sg4::Host* host        = e.host_by_name("dahu-1.grid5000.fr");
240   sg4::Host* host_remote = e.host_by_name("dahu-10.grid5000.fr");
241   sg4::Actor::create(std::string("receiver-local"), host, Receiver());
242   sg4::Actor::create(std::string("receiver-remote"), host_remote, Receiver());
243   sg4::Actor::create(std::string("sender") + std::string(host->get_name()), host,
244                      Sender({host, host_remote}, crosstraffic));
245
246   /* runs the simulation */
247   e.run();
248
249   return 0;
250 }