Logo AND Algorithmique Numérique Distribuée

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