Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
FatTreeZone: Add limiters for switches
[simgrid.git] / examples / cpp / clusters-multicpu / s4u-clusters-multicpu.cpp
1 /* Copyright (c) 2010-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 /* This example shows how to build a torus cluster with multi-core hosts.
7  *
8  * However, each leaf in the torus is a StarZone, composed of several CPUs
9  *
10  * Each actor runs in a specific CPU. One sender broadcasts a message to all receivers.
11  */
12
13 #include "simgrid/s4u.hpp"
14 namespace sg4 = simgrid::s4u;
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_torus_multicpu, "Messages specific for this s4u example");
17
18 class Sender {
19   long msg_size = 1e6; /* message size in bytes */
20   std::vector<sg4::Host*> hosts_;
21
22 public:
23   explicit Sender(const std::vector<sg4::Host*> hosts) : hosts_{hosts} {}
24   void operator()() const
25   {
26     /* Vector in which we store all ongoing communications */
27     std::vector<sg4::CommPtr> pending_comms;
28
29     /* Make a vector of the mailboxes to use */
30     std::vector<sg4::Mailbox*> mboxes;
31
32     /* Start dispatching 1 message to all receivers */
33     std::string msg_content =
34         std::string("Hello, I'm alive and running on ") + std::string(sg4::this_actor::get_host()->get_name());
35     for (const auto* host : hosts_) {
36       /* Copy the data we send: the 'msg_content' variable is not a stable storage location.
37        * It will be destroyed when this actor leaves the loop, ie before the receiver gets it */
38       auto* payload = new std::string(msg_content);
39
40       /* Create a communication representing the ongoing communication, and store it in pending_comms */
41       auto mbox = sg4::Mailbox::by_name(host->get_name());
42       mboxes.push_back(mbox);
43       sg4::CommPtr comm = mbox->put_async(payload, msg_size);
44       pending_comms.push_back(comm);
45     }
46
47     XBT_INFO("Done dispatching all messages");
48
49     /* Now that all message exchanges were initiated, wait for their completion in one single call */
50     sg4::Comm::wait_all(&pending_comms);
51
52     XBT_INFO("Goodbye now!");
53   }
54 };
55
56 /* Receiver actor: wait for 1 message on the mailbox identified by the hostname */
57 class Receiver {
58 public:
59   void operator()() const
60   {
61     auto mbox     = sg4::Mailbox::by_name(sg4::this_actor::get_host()->get_name());
62     auto received = mbox->get_unique<std::string>();
63     XBT_INFO("I got a '%s'.", received->c_str());
64   }
65 };
66
67 /*************************************************************************************************/
68 /**
69  * @brief Callback to set a cluster leaf/element
70  *
71  * In our example, each leaf if a StarZone, composed of 8 CPUs.
72  * Each CPU is modeled as a host, connected to the outer world through a high-speed PCI link.
73  * Obs.: CPU0 is the gateway for this zone
74  *
75  *    (outer world)
76  *         CPU0 (gateway)
77  *    up ->|   |
78  *         |   |<-down
79  *         +star+
80  *      /   / \   \
81  *     /   /   \   \<-- 100Gbs, 10us link (1 link UP and 1 link DOWN for full-duplex)
82  *    /   /     \   \
83  *   /   /       \   \
84  *   CPU1   ...   CPU8
85  *
86  * @param zone Cluster netzone being created (usefull to create the hosts/links inside it)
87  * @param coord Coordinates in the torus (e.g. "0,0,0", "0,1,0")
88  * @param id Internal identifier in the torus (for information)
89  * @return netpoint, gateway: the netpoint to the StarZone and CPU0 as gateway
90  */
91 static std::pair<simgrid::kernel::routing::NetPoint*, simgrid::kernel::routing::NetPoint*>
92 create_hostzone(const sg4::NetZone* zone, const std::vector<unsigned int>& /*coord*/, int id)
93 {
94   constexpr int num_cpus    = 8;     //!< Number of CPUs in the zone
95   constexpr double speed    = 1e9;   //!< Speed of each CPU
96   constexpr double link_bw  = 100e9; //!< Link bw connecting the CPU
97   constexpr double link_lat = 1e-9;  //!< Link latency
98
99   std::string hostname = "host" + std::to_string(id);
100   /* create the StarZone */
101   auto* host_zone = sg4::create_star_zone(hostname);
102   /* setting my Torus parent zone */
103   host_zone->set_parent(zone);
104
105   const sg4::Host* gateway = nullptr;
106   /* create CPUs */
107   for (int i = 0; i < num_cpus; i++) {
108     std::string cpu_name  = hostname + "-cpu" + std::to_string(i);
109     const sg4::Host* host = host_zone->create_host(cpu_name, speed)->seal();
110     /* the first CPU is the gateway */
111     if (i == 0)
112       gateway = host;
113     /* create 2 links for a full-duplex communication */
114     sg4::Link* link_up   = host_zone->create_link("link-up-" + cpu_name, link_bw)->set_latency(link_lat)->seal();
115     sg4::Link* link_down = host_zone->create_link("link-down-" + cpu_name, link_bw)->set_latency(link_lat)->seal();
116     /* link UP, connection from CPU to outer world */
117     host_zone->add_route(host->get_netpoint(), nullptr, nullptr, nullptr, std::vector<sg4::Link*>{link_up}, false);
118     /* link DOWN, connection from outer to CPU */
119     host_zone->add_route(nullptr, host->get_netpoint(), nullptr, nullptr, std::vector<sg4::Link*>{link_down}, false);
120   }
121   return std::make_pair(host_zone->get_netpoint(), gateway->get_netpoint());
122 }
123
124 /*************************************************************************************************/
125 /**
126  * @brief Callback to create limiter link (1Gbs) for each netpoint
127  * @param zone Torus netzone being created (usefull to create the hosts/links inside it)
128  * @param coord Coordinates in the torus (e.g. "0,0,0", "0,1,0")
129  * @param id Internal identifier in the torus (for information)
130  * @return Limiter link
131  */
132 static sg4::Link* create_limiter(sg4::NetZone* zone, const std::vector<unsigned int>& coord, int id)
133 {
134   return zone->create_link("limiter-" + std::to_string(id), 1e9)->seal();
135 }
136
137 /**
138  * @brief Creates a TORUS cluster
139  *
140  * Creates a TORUS cluster with dimensions 2x2x2
141  *
142  * The cluster has 8 elements/leaves in total. Each element is a StarZone containing 8 Hosts.
143  * Each pair in the torus is connected through 2 links:
144  * 1) limiter: a 1Gbs limiter link (set by user through the set_limiter callback)
145  * 2) link: 10Gbs link connecting the components (created automatically)
146  *
147  * (Y-axis=2)
148  * A
149  * |
150  * |   D (Z-axis=2)
151  * +  / 10 Gbs
152  * | +
153  * |/ limiter=1Gps
154  * B-----+----C (X-axis=2)
155  *
156  * For example, a communication from A to C goes through:
157  * <tt> A->limiter(A)->link(A-B)->limiter(B)->link(B-C)->limiter(C)->C </tt>
158  *
159  * More precisely, considering that A and C are StarZones, a
160  * communication from A-CPU-3 to C-CPU-7 goes through:
161  * 1) StarZone A: A-CPU-3 -> link-up-A-CPU-3 -> A-CPU-0
162  * 2) A-CPU-0->limiter(A)->link(A-B)->limiter(B)->link(B-C)->limiter(C)->C-CPU-0
163  * 3) StarZone C: C-CPU-0-> link-down-C-CPU-7 -> C-CPU-7
164  *
165  * Note that we don't have limiter links inside the StarZones(A, B, C),
166  * but we have limiters in the Torus that are added to the links in the path (as we can see in "2)")
167  *
168  * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html?highlight=torus#torus-cluster">Torus
169  * Cluster</a>
170  */
171 static void create_torus_cluster()
172 {
173   /* create the torus cluster, 10Gbs link between elements in the cluster */
174   sg4::create_torus_zone("cluster", nullptr, {2, 2, 2}, {create_hostzone, {}, create_limiter}, 10e9, 10e-6,
175                          sg4::Link::SharingPolicy::SPLITDUPLEX)
176       ->seal();
177 }
178
179 /*************************************************************************************************/
180 /**
181  * @brief Creates a Fat-Tree cluster
182  *
183  * Creates a Fat-Tree cluster with 2 levels and 6 nodes
184  * The following parameters are used to create this cluster:
185  * - Levels: 2 - two-level cluster
186  * - Down links: 2, 3 - L1 routers is connected to 2 elements, L2 routers to 3 elements
187  * - Up links: 1, 2 - Each node (A-F) is connected to 1 L2 router, L2 routers are connected to 2 L1
188  * - Link count: 1, 1 - Use 1 link in each level
189  *
190  * The first parameter describes how many levels we have.
191  * The following ones describe the connection between the elements and must have exactly n_levels components.
192  *
193  *
194  *                         S3     S4                <-- Level 1 routers
195  *    link:limiter -      /   \  /  \
196  *                       +     ++    +
197  *    link: 10GBps -->  |     /  \    |
198  *     (full-duplex)    |    /    \   |
199  *                      +   +      +  +
200  *                      |  /        \ |
201  *                      S1           S2             <-- Level 2 routers
202  *   link:limiter ->    |             |
203  *                      +             +
204  *  link:10GBps  -->   /|\           /|\
205  *                    / | \         / | \
206  *                   +  +  +       +  +  +
207  *  link:limiter -> /   |   \     /   |   \
208  *                 A    B    C   D    E    F        <-- Nodes
209  *
210  * Each element (A to F) is a StarZone containing 8 Hosts.
211  * The connection uses 2 links:
212  * 1) limiter: a 1Gbs limiter link (set by user through the set_limiter callback)
213  * 2) link: 10Gbs link connecting the components (created automatically)
214  *
215  * For example, a communication from A to C goes through:
216  * <tt> A->limiter(A)->link(A-S1)->limiter(S1)->link(S1-C)->->limiter(C)->C</tt>
217  *
218  * More precisely, considering that A and C are StarZones, a
219  * communication from A-CPU-3 to C-CPU-7 goes through:
220  * 1) StarZone A: A-CPU-3 -> link-up-A-CPU-3 -> A-CPU-0
221  * 2) A-CPU-0->limiter(A)->link(A-S1)->limiter(S1)->link(S1-C)->limiter(C)->C-CPU-0
222  * 3) StarZone C: C-CPU-0-> link-down-C-CPU-7 -> C-CPU-7
223  *
224  * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html#fat-tree-cluster">Fat-Tree
225  * Cluster</a>
226  */
227 static void create_fatTree_cluster()
228 {
229   /* create the fat tree cluster, 10Gbs link between elements in the cluster */
230   sg4::create_fatTree_zone("cluster", nullptr, {2, {2, 3}, {1, 2}, {1, 1}}, {create_hostzone, {}, create_limiter}, 10e9,
231                            10e-6, sg4::Link::SharingPolicy::SPLITDUPLEX)
232       ->seal();
233 }
234
235 /*************************************************************************************************/
236 /**
237  * @brief Creates a Dragonfly cluster
238  *
239  * Creates a Dragonfly cluster with 2 groups and 16 nodes
240  * The following parameters are used to create this cluster:
241  * - Groups: 2 groups, connected with 2 links (blue links)
242  * - Chassis: 2 chassis, connected with a single link (black links)
243  * - Routers: 2 routers, connected with 2 links (green links)
244  * - Nodes: 2 leaves per router, single link
245  *
246  * The diagram below illustrates a group in the dragonfly cluster
247  *
248  * +------------------------------------------------+
249  * |        black link(1)                           |
250  * |     +------------------------+                 |
251  * | +---|--------------+     +---|--------------+  |
252  * | |   |  green       |     |   |  green       |  |
253  * | |   |  links (2)   |     |   |  links (2)   |  |   blue links(2)
254  * | |   R1 ====== R2   |     |   R3 -----  R4 ======================> "Group 2"
255  * | |  /  \      /  \  |     |  /  \      /  \  |  |
256  * | | A    B    C    D |     | E    F    G    H |  |
257  * | +------------------+     +------------------+  |
258  * |      Chassis 1                Chassis 2        |
259  * +------------------------------------------------+
260  *  Group 1
261  *
262  * Each element (A, B, C, etc) is a StarZone containing 8 Hosts.
263  * The connection between nodes and routers (e.g. A->R1) uses 2 links:
264  * 1) limiter: a 1Gbs limiter link (set by user through the set_limiter callback)
265  * 2) link: 10Gbs link connecting the components (created automatically)
266  *
267  * For example, a communication from A to C goes through:
268  * <tt> A->limiter(A)->link(A-R1)->link(R1-R2)->limiter(C)->C</tt>
269  *
270  * Note that limiters are only valid for leaves, not routers.
271  *
272  * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html#dragonfly-cluster">Dragonfly
273  * Cluster</a>
274  */
275 static void create_dragonfly_cluster()
276 {
277   /* create the dragonfly cluster, 10Gbs link between elements in the cluster */
278   sg4::create_dragonfly_zone("cluster", nullptr, {{2, 2}, {2, 1}, {2, 2}, 2}, {create_hostzone, {}, create_limiter},
279                              10e9, 10e-6, sg4::Link::SharingPolicy::SPLITDUPLEX)
280       ->seal();
281 }
282
283 /*************************************************************************************************/
284
285 int main(int argc, char* argv[])
286 {
287   sg4::Engine e(&argc, argv);
288   std::string platform = argv[1];
289
290   /* create platform */
291   if (platform == "torus")
292     create_torus_cluster();
293   else if (platform == "fatTree")
294     create_fatTree_cluster();
295   else if (platform == "dragonfly")
296     create_dragonfly_cluster();
297
298   std::vector<sg4::Host*> host_list = e.get_all_hosts();
299   /* create the sender actor running on first host */
300   sg4::Actor::create("sender", host_list[0], Sender(host_list));
301   /* create receiver in every host */
302   for (auto* host : host_list) {
303     sg4::Actor::create(std::string("receiver-") + std::string(host->get_name()), host, Receiver());
304   }
305
306   /* runs the simulation */
307   e.run();
308
309   return 0;
310 }