6 SimGrid was conceived as a tool to study distributed algorithms. Its
7 modern S4U interface makes it easy to assess Cloud, P2P, HPC, IoT and
10 A typical SimGrid simulation is composed of several **Actors**
11 |api_s4u_Actor|_ , that execute user-provided functions. The actors
12 have to explicitly use the S4U interface to express their computation,
13 communication, disk usage and other **Activities** |api_s4u_Activity|_
14 , so that they get reflected within the simulator. These activities
15 take place on **Resources** (CPUs, links, disks). SimGrid predicts the
16 time taken by each activity and orchestrates accordingly the actors
17 waiting for the completion of these activities.
19 .. |api_s4u_Actor| image:: /images/extlink.png
22 .. _api_s4u_Actor: api/classsimgrid_1_1s4u_1_1Actor.html#class-documentation
24 .. |api_s4u_Activity| image:: /images/extlink.png
27 .. _api_s4u_Activity: api/classsimgrid_1_1s4u_1_1Activity.html#class-documentation
30 Each actor executes a user-provided function on a simulated **Host**
31 |api_s4u_Host|_ with which it can interact. Communications are not
32 directly sent to actors, but posted onto **Mailboxes**
33 |api_s4u_Mailbox|_ that serve as rendez-vous points between
34 communicating processes.
36 .. |api_s4u_Host| image:: /images/extlink.png
39 .. _api_s4u_Host: api/classsimgrid_1_1s4u_1_1Host.html#class-documentation
41 .. |api_s4u_Mailbox| image:: /images/extlink.png
44 .. _api_s4u_Mailbox: api/classsimgrid_1_1s4u_1_1Mailbox.html#class-documentation
47 Discover the Master/Workers
48 ---------------------------
50 This section introduces a first example of SimGrid simulation. This
51 simple application is composed of two kind of actors: the **master**
52 is in charge of distributing some computational tasks to a set of
53 **workers** that execute them.
55 .. image:: /images/tuto-masterworkers-intro.svg
58 We first present a round-robin version of this application, where the
59 master dispatches the tasks to the workers, one after the other, until
60 all tasks are dispatched. Later in this tutorial, you will be given
61 the opportunity to improve this scheme.
66 Let's start with the code of the worker. It is represented by the
67 *master* function below. This simple function takes 4 parameters,
68 given as a vector of strings:
70 - the number of workers managed by the master.
71 - the number of tasks to dispatch
72 - the computational size (in flops to compute) of each task
73 - the communication size (in bytes to exchange) of each task
75 Then, the tasks are sent one after the other, each on a mailbox named
76 "worker-XXX" where XXX is the number of an existing worker. On the
77 other side, a given worker (which code is given below) wait for
78 incoming tasks on its own mailbox. Notice how this mailbox mechanism
79 allow the actors to find each other without having all information:
80 the master don't have to know the actors nor even where they are, it
81 simply pushes the messages on mailbox which name is predetermined.
83 At the end, once all tasks are dispatched, the master dispatches
84 another task per worker, but this time with a negative amount of flops
85 to compute. Indeed, this application decided by convention, that the
86 workers should stop when encountering such a negative compute_size.
88 At the end of the day, the only SimGrid specific functions used in
89 this example are :cpp:func:`simgrid::s4u::Mailbox::by_name` and
90 :cpp:func:`simgrid::s4u::Mailbox::put`. Also, :c:macro:`XBT_INFO` is used
91 as a replacement to printf() or to cout to ensure that the messages
92 are nicely logged along with the simulated time and actor name.
95 .. literalinclude:: ../../examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp
97 :start-after: master-begin
98 :end-before: master-end
100 Here comes the code of the worker actors. This function expects only one
101 parameter from its vector of strings: its identifier so that it knows
102 on which mailbox its incoming tasks will arrive. Its code is very
103 simple: as long as it gets valid computation requests (whose
104 compute_amount is positive), it compute this task and waits for the
107 .. literalinclude:: ../../examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp
109 :start-after: worker-begin
110 :end-before: worker-end
112 Starting the Simulation
113 .......................
115 And this is it. In only a few lines, we defined the algorithm of our
116 master/workers examples. Well, this is true, but an algorithm alone is
117 not enough to define a simulation.
119 First, SimGrid is a library, not a program. So you need to define your
120 own `main()` function, as follows. This function is in charge of
121 creating a SimGrid simulation engine (on line 3), register the actor
122 functions to the engine (on lines 7 and 8), load the virtual platform
123 from its description file (on line 11), map actors onto that platform
124 (on line 12) and run the simulation until its completion on line 15.
126 .. literalinclude:: ../../examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp
128 :start-after: main-begin
129 :end-before: main-end
132 After that, the missing pieces are the platform and deployment
138 Platform files define the virtual platform on which the provided
139 application will take place. In contains one or several **Network
140 Zone** |api_s4u_NetZone|_ that contain both **Host-** |api_s4u_Host|_
141 and **Link-** |api_s4u_Link|_ Resources, as well as routing
144 Such files can get rather long and boring, so the example below is
145 only an excerpts of the full ``examples/platforms/small_platform.xml``
146 file. For example, most routing information are missing, and only the
147 route between the hosts Tremblay and Fafard is given. This path
148 traverses 6 links (4, 3, 2, 0, 1 and 8). The full file, along with
149 other examples, can be found in the archive under
150 ``examples/platforms``.
152 .. |api_s4u_NetZone| image:: /images/extlink.png
155 .. _api_s4u_NetZone: api/classsimgrid_1_1s4u_1_1NetZone.html#class-documentation
157 .. |api_s4u_Link| image:: /images/extlink.png
160 .. _api_s4u_Link: api/classsimgrid_1_1s4u_1_1Link.html#class-documentation
162 .. literalinclude:: ../../examples/platforms/small_platform.xml
164 :lines: 1-10,12-20,56-63,192-
165 :caption: (excerpts of the small_platform.xml file)
170 Deployment files specify the execution scenario: it lists the actors
171 that should be started, along with their parameter. In the following
172 example, we start 6 actors: one master and 5 workers.
174 .. literalinclude:: ../../examples/s4u/app-masterworkers/s4u-app-masterworkers_d.xml
180 This time, we have all parts: once the program is compiled, we can
181 execute it as follows. Note how the XBT_INFO() requests turned into
182 informative messages.
184 .. literalinclude:: ../../examples/s4u/app-masterworkers/s4u-app-masterworkers.tesh
186 :start-after: s4u-app-masterworkers-fun
187 :prepend: $$$ ./masterworkers platform.xml deploy.xml
195 In this section, you will modify the example presented earlier to
196 explore the quality of the proposed algorithm. For now, it works and
197 the simulation prints things, but the truth is that we have no idea of
198 whether this is a good algorithm to dispatch tasks to the workers.
199 This very simple setting raises many interesting questions:
201 .. image:: /images/tuto-masterworkers-question.svg
204 - Which algorithm should the master use? Or should the worker decide
207 Round Robin is not an efficient algorithm when all tasks are not
208 processed at the same speed. It would probably be more efficient
209 if the workers were asking for tasks when ready.
211 - Should tasks be grouped in batches or sent separately?
213 The workers will starve if they don't get the tasks fast
214 enough. One possibility to reduce latency would be to send tasks
215 in pools instead of one by one. But if the pools are too big, the
216 load balancing will likely get uneven, in particular when
217 distributing the last tasks.
219 - How does the quality of such algorithm dependent on the platform
220 characteristics and on the task characteristics?
222 Whenever the input communication time is very small compared to
223 processing time and workers are homogeneous, it is likely that the
224 round-robin algorithm performs very well. Would it still hold true
225 when transfer time is not negligible? What if some tasks are
226 performed faster on some specific nodes?
228 - The network topology interconnecting the master and the workers
229 may be quite complicated. How does such a topology impact the
232 When data transfers are the bottleneck, it is likely that a good
233 modeling of the platform becomes essential. The SimGrid platform
234 models are particularly handy to account for complex platform
237 - What is the best applicative topology?
239 Is a flat master worker deployment sufficient? Should we go for a
240 hierarchical algorithm, with some forwarders taking large pools of
241 tasks from the master, each of them distributing their tasks to a
242 sub-pool of workers? Or should we introduce super-peers,
243 dupplicating the master's role in a peer-to-peer manner? Do the
244 algorithms require a perfect knowledge of the network?
246 - How is such an algorithm sensitive to external workload variation?
248 What if bandwidth, latency and computing speed can vary with no
249 warning? Shouldn't you study whether your algorithm is sensitive
250 to such load variations?
252 - Although an algorithm may be more efficient than another, how does
253 it interfere with unrelated applications executing on the same
256 **SimGrid was invented to answer such questions.** Do not believe the
257 fools saying that all you need to study such settings is a simple
258 discrete event simulator. Do you really want to reinvent the wheel,
259 debug your own tool, optimize it and validate its models against real
260 settings for ages, or do you prefer to sit on the shoulders of a
261 giant? With SimGrid, you can focus on your algorithm. The whole
262 simulation mechanism is already working.
264 Here is the visualization of a SimGrid simulation of two master worker
265 applications (one in light gray and the other in dark gray) running in
266 concurrence and showing resource usage over a long period of time. It
267 was obtained with the Triva software.
269 .. image:: /images/tuto-masterworkers-result.png
275 Before your proceed, you need to :ref:`install SimGrid <install>`, a
276 C++ compiler and also ``pajeng`` to visualize the traces. The provided
277 code template requires cmake to compile. On Debian and Ubuntu for
278 example, you can get them as follows:
280 .. code-block:: shell
282 sudo apt install simgrid pajeng cmake g++
284 An initial version of the source code is provided on framagit. This
285 template compiles with cmake. If SimGrid is correctly installed, you
286 should be able to clone the `repository
287 <https://framagit.org/simgrid/simgrid-template-s4u>`_ and recompile
288 everything as follows:
290 .. code-block:: shell
292 git clone git@framagit.org:simgrid/simgrid-template-s4u.git
293 cd simgrid-template-s4u/
297 If you struggle with the compilation, then you should double check
298 your :ref:`SimGrid installation <install>`. On need, please refer to
299 the :ref:`Troubleshooting your Project Setup
300 <install_yours_troubleshooting>` section.
302 Discovering the Provided Code
303 .............................
305 Please compile and execute the provided simulator as follows:
308 .. code-block:: shell
311 ./master-workers small_platform.xml master-workers_d.xml
313 For a more "fancy" output, you can use simgrid-colorizer.
315 .. code-block:: shell
317 ./master-workers small_platform.xml master-workers_d.xml 2>&1 | simgrid-colorizer
319 If you installed SimGrid to a non-standard path, you may have to
320 specify the full path to simgrid-colorizer on the above line, such as
321 ``/opt/simgrid/bin/simgrid-colorizer``. If you did not install it at all,
322 you can find it in <simgrid_root_directory>/bin/colorize.
326 Explain how to generate a Gantt-Chart with S4U and pajeng.
328 Exercise 1: Simplifying the deployment file
329 ...........................................
331 In the provided example, the deployment file is tightly connected to
332 the platform file ``small_platform.xml`` and adding more workers
333 quickly becomes a pain: You need to start them (at the bottom of the
334 file), add to inform the master that they are available by increasing
337 Instead, modify the simulator ``master-workers.c`` into
338 ``master-workers-exo1.c`` so that the master launches a worker process
339 on `all` the other machines at startup. The new deployment file should
344 <?xml version='1.0'?>
345 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
346 <platform version="4.1">
347 <actor host="Tremblay" function="master">
348 <argument value="20"/> <!-- Number of tasks -->
349 <argument value="50000000"/> <!-- Computation size of tasks -->
350 <argument value="1000000"/> <!-- Communication size of tasks -->
354 Creating the workers from the master
355 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
357 For that, the master needs to retrieve the list of hosts declared in
358 the platform with :cpp:func:`simgrid::s4u::Engine::get_all_host()`.
359 Then, the master should start the worker processes with
360 :cpp:func:`simgrid::s4u::Actor::create`.
362 ``Actor::create(name, host, func, params...)`` is a very flexible
363 function. Its third parameter is the function that the actor should
364 execute. This function can take any kind of parameter, provided that
365 you pass similar parameters to ``Actor::create()``. For example, you
366 could have something like this:
370 void my_actor(int param1, double param2, std::string param3) {
373 int main(int argc, char argv**) {
375 simgrid::s4u::ActorPtr actor;
376 actor = simgrid::s4u::Actor::create("name", simgrid::s4u::Host::by_name("the_host"),
377 &my_actor, 42, 3.14, "thevalue");
382 Master-Workers Communication
383 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
385 Previously, the workers got from their parameter the name of the
386 mailbox they should use. We can still do so: the master should build
387 such a parameter before using it in the ``Actor::create()`` call. The
388 master could even pass directly the mailbox as a parameter to the
391 Since we want later to study concurrent applications, it is advised to
392 use a mailbox name that is unique over the simulation even if there is
393 more than one master.
395 One possibility for that is to use the actor ID (aid) of each worker
396 as a mailbox name. The master can retrieve the aid of the newly
397 created actor with ``actor->get_pid()`` while the actor itself can
398 retrieve its own aid with ``simgrid::s4u::this_actor::get_pid()``.
399 The retrieved value is an ``aid_t``, which is an alias for ``long``.
401 Instead of having one mailbox per worker, you could also reorganize
402 completely your application to have only one mailbox per master. All
403 the workers of a given master would pull their work from the same
404 mailbox, which should be passed as parameter to the workers. This
405 reduces the amount of mailboxes, but prevents the master from taking
406 any scheduling decision. It really depends on how you want to organize
407 your application and what you want to study with your simulator.
412 In this exercise, we reduced the amount of configuration that our
413 simulator requests. This is both a good idea, and a dangerous
414 trend. This simplification is an application of the good old DRY/SPOT
415 programming principle (Don't Repeat Yourself / Single Point Of Truth
416 -- `more on wikipedia
417 <https://en.wikipedia.org/wiki/Don%27t_repeat_yourself>`_), and you
418 really want your programming artefacts to follow these software
419 engineering principles.
421 But at the same time, you should be careful in separating your
422 scientific contribution (the master/workers algorithm) and the
423 artefacts used to test it (platform, deployment and workload). This is
424 why SimGrid forces you to express your platform and deployment files
425 in XML instead of using a programming interface: it forces a clear
426 separation of concerns between things of very different nature.
429 .. LocalWords: SimGrid