Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e2bb116a44e09471671784a01fa0d1271ab6a1a5
[simgrid.git] / docs / source / Tutorial_Algorithms.rst
1 .. _usecase_simalgo:
2
3 Simulating Algorithms
4 =====================
5
6 SimGrid was conceived as a tool to study distributed algorithms. Its
7 :ref:`S4U interface <S4U_doc>` makes it easy to assess Cloud,
8 P2P, HPC, IoT, and other similar settings (:ref:`more info <index>`).
9
10 A typical SimGrid simulation is composed of several |Actors|_, that
11 execute user-provided functions. The actors have to explicitly use the
12 S4U interface to express their computation, communication, disk usage,
13 and other |Activities|_ so that they get reflected within the
14 simulator. These activities take place on **Resources** (|Hosts|_,
15 |Links|_, |Disks|_). SimGrid predicts the time taken by each
16 activity and orchestrates accordingly the actors waiting for the
17 completion of these activities.
18
19 Each actor executes a user-provided function on a simulated |Host|_
20 with which it can interact. Communications are not directly sent to
21 actors, but posted onto a |Mailbox|_ that serves as a rendez-vous point
22 between communicating actors.
23
24 .. |Actors| replace:: **Actors**
25 .. _Actors: app_s4u.html#s4u-actor
26
27 .. |Activities| replace:: **Activities**
28 .. _Activities: app_s4u.html#s4u-activity
29
30 .. |Hosts| replace:: **Hosts**
31 .. _Hosts: app_s4u.html#s4u-host
32
33 .. |Links| replace:: **Links**
34 .. _Links: app_s4u.html#s4u-link
35
36 .. |Disks| replace:: **Disks**
37 .. _Disks: app_s4u.html#s4u-disk
38
39 .. |VirtualMachines| replace:: **VirtualMachines**
40 .. _VirtualMachines: app_s4u.html#s4u-virtualmachine
41
42 .. |Host| replace:: **Host**
43 .. _Host: app_s4u.html#s4u-host
44
45 .. |Link| replace:: **Link**
46 .. _Link: app_s4u.html#s4u-link
47
48 .. |Mailbox| replace:: **Mailbox**
49 .. _Mailbox: app_s4u.html#s4u-mailbox
50
51 .. |Barrier| replace:: **Barrier**
52 .. _Barrier: app_s4u.html#s4u-barrier
53
54 .. |ConditionVariable| replace:: **ConditionVariable**
55 .. _ConditionVariable: app_s4u.html#s4u-conditionvariable
56
57 .. |Mutex| replace:: **Mutex**
58 .. _Mutex: app_s4u.html#s4u-mutex
59
60 **In the remainder of this tutorial**, you will discover a simple yet
61 fully-functioning example of SimGrid simulation: the Master/Workers
62 application. We will detail each part of the code and the necessary
63 configuration to make it work.  After this tour, several exercises
64 are proposed to let you discover some of the SimGrid features, hands
65 on the keyboard. This practical session will be given in C++ or Python, 
66 which you are supposed to know beforehand.
67
68
69 Discover the Master/Workers
70 ---------------------------
71
72 This section introduces an example of SimGrid simulation. This
73 simple application is composed of two kinds of actors: the **master**
74 is in charge of distributing some computational tasks to a set of
75 **workers** that execute them.
76
77 .. image:: /tuto_s4u/img/intro.svg
78    :align: center
79
80 The provided code dispatches these tasks in `round-robin scheduling <https://en.wikipedia.org/wiki/Round-robin_scheduling>`_, 
81 i.e. in circular order: tasks are dispatched to each worker one after the other, until all tasks are dispatched.
82 You will improve this scheme later in this tutorial.
83
84 The Actors
85 ..........
86
87 Let's start with the code of the master. It is represented by the
88 *master* function below. This simple function takes at least 3
89 parameters (the number of tasks to dispatch, their computational size
90 in flops to compute, and their communication size in bytes to
91 exchange). Every parameter after the third one must be the name of a
92 host on which a worker is waiting for something to compute.
93
94 Then, the tasks are sent one after the other, each on a mailbox named
95 after the worker's hosts. On the other side, a given worker (which
96 code is given below) waits for incoming tasks on its mailbox.
97
98
99 In the end, once all tasks are dispatched, the master dispatches
100 another task per worker, but this time with a negative amount of flops
101 to compute. Indeed, this application decided by convention, that the
102 workers should stop when encountering such a negative compute_size.
103
104    .. tabs::
105
106       .. group-tab:: C++
107
108          At the end of the day, the only SimGrid specific functions used in
109          this example are :cpp:func:`simgrid::s4u::Mailbox::by_name` (to retrieve or create a mailbox) and
110          :cpp:func:`simgrid::s4u::Mailbox::put` (so send something over a mailbox). Also, :c:macro:`XBT_INFO` is used
111          as a replacement to ``printf()`` or ``std::cout`` to ensure that the messages
112          are nicely logged along with the simulated time and actor name.
113
114          .. literalinclude:: ../../examples/cpp/app-masterworkers/s4u-app-masterworkers-fun.cpp
115             :language: c++
116             :start-after: master-begin
117             :end-before: master-end
118
119       .. group-tab:: Python
120
121          At the end of the day, the only SimGrid specific functions used in
122          this example are :py:func:`simgrid.Mailbox.by_name` (to retrieve or create a mailbox) and
123          :py:func:`simgrid.Mailbox.put` (so send something over a mailbox). Also, :py:func:`simgrid.this_actor.info` is used
124          as a replacement to `print` to ensure that the messages
125          are nicely logged along with the simulated time and actor name.
126
127          .. literalinclude:: ../../examples/python/app-masterworkers/app-masterworkers.py
128             :language: python
129             :start-after: master-begin
130             :end-before: master-end
131
132 Then comes the code of the worker actors. This function expects no
133 parameter from its vector of strings. Its code is very simple: it
134 expects messages on the mailbox that is named after its host. As long as it gets valid
135 computation requests (whose compute_amount is positive), it computes
136 this task and waits for the next one.
137
138 .. tabs::
139
140    .. group-tab:: C++
141
142       The worker retrieves its own host with
143       :cpp:func:`simgrid::s4u::this_actor::get_host`. The
144       :ref:`simgrid::s4u::this_actor <API_s4u_this_actor>`
145       namespace contains many such helping functions.
146
147       .. literalinclude:: ../../examples/cpp/app-masterworkers/s4u-app-masterworkers-fun.cpp
148          :language: c++
149          :start-after: worker-begin
150          :end-before: worker-end
151
152    .. group-tab:: Python
153
154       The worker retrieves its own host with :py:func:`simgrid.this_actor.get_host`. The
155       :ref:`this_actor <API_s4u_this_actor>` object contains many such helping functions.
156
157       .. literalinclude:: ../../examples/python/app-masterworkers/app-masterworkers.py
158          :language: python
159          :start-after: worker-begin
160          :end-before: worker-end
161
162 Starting the Simulation
163 .......................
164
165 And this is it. In only a few lines, we defined the algorithm of our master/workers example.
166
167 .. tabs::
168
169    .. group-tab:: C++
170
171       That being said, an algorithm alone is not enough to define a
172       simulation: SimGrid is a library, not a program. So you need to define
173       your own ``main()`` function as follows. This function is in charge of
174       creating a SimGrid simulation engine (on line 3), register the actor
175       functions to the engine (on lines 7 and 8), load the simulated platform
176       from its description file (on line 11), map actors onto that platform
177       (on line 12) and run the simulation until its completion on line 15.
178
179       .. literalinclude:: ../../examples/cpp/app-masterworkers/s4u-app-masterworkers-fun.cpp
180          :language: c++
181          :start-after: main-begin
182          :end-before: main-end
183          :linenos:
184
185    .. group-tab:: Python
186
187       That being said, an algorithm alone is not enough to define a simulation: 
188       you need a main block to setup the simulation and its components as follows.
189       This code creates a SimGrid simulation engine (on line 4), registers the actor
190       functions to the engine (on lines 7 and 8), loads the simulated platform
191       from its description file (on line 11), map actors onto that platform
192       (on line 12) and run the simulation until its completion on line 15.
193
194       .. literalinclude:: ../../examples/python/app-masterworkers/app-masterworkers.py
195          :language: python
196          :start-after: main-begin
197          :end-before: main-end
198          :linenos:
199
200 Finally, this example requires a platform file and a deployment file.
201
202 Platform File
203 .............
204
205 Platform files define the simulated platform on which the provided
206 application will take place. It contains one or several **Network
207 Zone** |api_s4u_NetZone|_ that contains both |Host|_ and |Link|_
208 Resources, as well as routing information.
209
210 Such files can get rather long and boring, so the example below is
211 only an excerpt of the full ``examples/platforms/small_platform.xml``
212 file. For example, most routing information is missing, and only the
213 route between the hosts Tremblay and Fafard is given. This path
214 traverses 6 links (named 4, 3, 2, 0, 1, and 8). There are several
215 examples of platforms in the archive under ``examples/platforms``.
216
217 .. |api_s4u_NetZone| image:: /img/extlink.png
218    :align: middle
219    :width: 12
220 .. _api_s4u_NetZone: app_s4u.html#s4u-netzone
221
222 .. |api_s4u_Link| image:: /img/extlink.png
223    :align: middle
224    :width: 12
225 .. _api_s4u_Link: app_s4u.html#s4u-link
226
227 .. literalinclude:: ../../examples/platforms/small_platform.xml
228    :language: xml
229    :lines: 1-10,12-20,56-62,192-
230    :caption: (excerpts of the small_platform.xml file)
231
232 Deployment File
233 ...............
234
235 Deployment files specify the execution scenario: it lists the actors
236 that should be started, along with their parameters. In the following
237 example, we start 6 actors: one master and 5 workers.
238
239 .. literalinclude:: ../../examples/cpp/app-masterworkers/s4u-app-masterworkers_d.xml
240    :language: xml
241
242 Execution Example
243 .................
244
245 This time, we have all parts: once the program is compiled, we can execute it as follows.
246
247 .. tabs::
248
249    .. group-tab:: C++
250
251       Note how the :c:macro:`XBT_INFO` requests turned into informative messages.
252
253       .. "WARNING: non-whitespace stripped by dedent" is expected here as we remove the $ marker this way
254
255       .. literalinclude:: ../../examples/cpp/app-masterworkers/s4u-app-masterworkers.tesh
256          :language: shell
257          :start-after: s4u-app-masterworkers-fun
258          :prepend: $$$ ./masterworkers platform.xml deploy.xml
259          :append: $$$
260          :dedent: 2
261
262    .. group-tab:: Python
263
264       Note how the :py:func:`simgrid.this_actor.info` calls turned into informative messages.
265
266       .. literalinclude:: ../../examples/python/app-masterworkers/app-masterworkers.tesh
267          :language: shell
268          :start-after: app-masterworkers_d.xml
269          :prepend: $$$ python ./app-masterworkers.py platform.xml deploy.xml
270          :append: $$$
271          :dedent: 2
272
273 Each example included in the SimGrid distribution comes with a `tesh`
274 file that presents how to start the example once compiled, along with
275 the expected output. These files are used for the automatic testing of
276 the framework but can be used to see the examples' output without
277 compiling them. See e.g. the file
278 `examples/cpp/app-masterworkers/s4u-app-masterworkers.tesh <https://framagit.org/simgrid/simgrid/-/blob/master/examples/cpp/app-masterworkers/s4u-app-masterworkers.tesh>`_.
279 Lines starting with `$` are the commands to execute;
280 lines starting with `>` are the expected output of each command, while
281 lines starting with `!` are configuration items for the test runner.
282
283
284 Improve it Yourself
285 -------------------
286
287 In this section, you will modify the example presented earlier to
288 explore the quality of the proposed algorithm. It already works, and
289 the simulation prints things, but the truth is that we have no idea of
290 whether this is a good algorithm to dispatch tasks to the workers.
291 This very simple setting raises many interesting questions:
292
293 .. image:: /tuto_s4u/img/question.svg
294    :align: center
295
296 - Which algorithm should the master use? Or should the worker decide
297   by themselves?
298
299     Round Robin is not an efficient algorithm when all tasks are not
300     processed at the same speed.  It would probably be more efficient
301     if the workers were asking for tasks when ready.
302
303 - Should tasks be grouped in batches or sent separately?
304
305     The workers will starve if they don't get the tasks fast
306     enough. One possibility to reduce latency would be to send tasks
307     in pools instead of one by one. But if the pools are too big, the
308     load balancing will likely get uneven, in particular when
309     distributing the last tasks.
310
311 - How does the quality of such an algorithm dependent on the platform
312   characteristics and on the task characteristics?
313
314     Whenever the input communication time is very small compared to
315     processing time and workers are homogeneous, it is likely that the
316     round-robin algorithm performs very well. Would it still hold true
317     when transfer time is not negligible? What if some tasks are
318     performed faster on some specific nodes?
319
320 - The network topology interconnecting the master and the workers
321   may be quite complicated. How does such a topology impact the
322   previous result?
323
324     When data transfers are the bottleneck, it is likely that good
325     modeling of the platform becomes essential. The SimGrid platform
326     models are particularly handy to account for complex platform
327     topologies.
328
329 - What is the best applicative topology?
330
331     Is a flat master-worker deployment sufficient? Should we go for a
332     hierarchical algorithm, with some forwarders taking large pools of
333     tasks from the master, each of them distributing their tasks to a
334     sub-pool of workers? Or should we introduce super-peers,
335     duplicating the master's role in a peer-to-peer manner?  Do the
336     algorithms require a perfect knowledge of the network?
337
338 - How is such an algorithm sensitive to external workload variation?
339
340     What if bandwidth, latency, and computing speed can vary with no
341     warning?  Shouldn't you study whether your algorithm is sensitive
342     to such load variations?
343
344 - Although an algorithm may be more efficient than another, how does
345   it interfere with unrelated applications executing on the same
346   facilities?
347
348 **SimGrid was invented to answer such questions.** Do not believe the
349 fools saying that all you need to study such settings is a simple
350 discrete event simulator. Do you really want to reinvent the wheel,
351 debug and optimize your own tool, and validate its models against real
352 settings for ages, or do you prefer to sit on the shoulders of a
353 giant? With SimGrid, you can focus on your algorithm. The whole
354 simulation mechanism is already working.
355
356 Here is the visualization of a SimGrid simulation of two master-worker
357 applications (one in light gray and the other in dark gray) running in
358 concurrence and showing resource usage over a long period of time. It
359 was obtained with the Triva software.
360
361 .. image:: /tuto_s4u/img/result.png
362    :align: center
363
364 Using Docker
365 ............
366
367 The easiest way to take the tutorial is to use the dedicated Docker image. 
368 Once you `installed Docker itself <https://docs.docker.com/install/>`_, simply do the following:
369
370 .. code-block:: console
371
372    $ docker pull simgrid/tuto-s4u
373    $ mkdir ~/simgrid-tutorial
374    $ docker run --user $UID:$GID -it --rm --name simgrid --volume ~/simgrid-tutorial:/source/tutorial simgrid/tuto-s4u bash
375
376 This will start a new container with all you need to take this
377 tutorial, and create a ``simgrid-tutorial`` directory in your home on
378 your host machine that will be visible as ``/source/tutorial`` within the
379 container.  You can then edit the files you want with your favorite
380 editor in ``~/simgrid-tutorial``, and compile them within the
381 container to enjoy the provided dependencies.
382
383 .. warning::
384
385    Any change to the container out of ``/source/tutorial`` will be lost
386    when you log out of the container, so don't edit the other files!
387
388 All needed dependencies are already installed in this container
389 (SimGrid, a C++ compiler, CMake, pajeng, and R). Vite being only
390 optional in this tutorial, it is not installed to reduce the image
391 size.
392
393 The docker does not run as root, so that the files can easily be exchanged between within the container and the outer world.
394 If you need to run a command as root within the container, simply type the following in another terminal to join the same container as root:
395
396 .. code-block:: console
397
398    $ docker container ls
399    # This lists all containers running on your machine. For example:
400    #    CONTAINER ID   IMAGE            COMMAND   CREATED         STATUS         PORTS     NAMES
401    #    7e921b1b18a7   simgrid/stable   "bash"    7 minutes ago   Up 7 minutes             simgrid
402
403    $ docker exec --user root -it simgrid bash
404
405 The code template is available under ``/source/simgrid-template-s4u.git``
406 in the image. You should copy it to your working directory and
407 recompile it when you first log in:
408
409 .. code-block:: console
410
411    $ # first connect to the running container
412    $ docker exec --user $UID:$GID -ti simgrid bash
413    $container) cp -r /source/simgrid-template-s4u.git/* /source/tutorial
414    $container) cd /source/tutorial
415    $container) cmake .
416    $container) make
417
418 Using your Computer Natively
419 ............................
420
421 .. tabs::
422
423    .. group-tab:: C++
424
425       To take the tutorial on your machine, you first need to :ref:`install
426       a recent version of SimGrid <install>`, a C++ compiler, and also
427       ``pajeng`` to visualize the traces. You may want to install `Vite
428       <http://vite.gforge.inria.fr/>`_ to get a first glance at the traces.
429       The provided code template requires CMake to compile. On Debian and
430       Ubuntu for example, you can get them as follows:
431
432       .. code-block:: console
433
434          $ sudo apt install simgrid pajeng cmake g++ vite
435
436       An initial version of the source code is provided on framagit. This
437       template compiles with CMake. If SimGrid is correctly installed, you
438       should be able to clone the `repository
439       <https://framagit.org/simgrid/simgrid-template-s4u>`_ and recompile
440       everything as follows:
441
442       .. code-block:: console
443
444          # (exporting SimGrid_PATH is only needed if SimGrid is installed in a non-standard path)
445          $ export SimGrid_PATH=/where/to/simgrid
446
447          $ git clone https://framagit.org/simgrid/simgrid-template-s4u.git
448          $ cd simgrid-template-s4u/
449          $ cmake .
450          $ make
451
452       If you struggle with the compilation, then you should double-check
453       your :ref:`SimGrid installation <install>`.  On need, please refer to
454       the :ref:`Troubleshooting your Project Setup
455       <install_yours_troubleshooting>` section.
456
457    .. group-tab:: Python
458
459       To take the tutorial on your machine, you first need to :ref:`install
460       a recent version of SimGrid <install>` and ``pajeng`` to visualize the 
461       traces. You may want to install `Vite <http://vite.gforge.inria.fr/>`_ to get a first glance at the traces.
462       On Debian and Ubuntu for example, you can get them as follows:
463
464       .. code-block:: console
465
466          $ sudo apt install simgrid pajeng vite
467
468       An initial version of the source code is provided on framagit. 
469       If SimGrid is correctly installed, you should be able to clone the `repository
470       <https://framagit.org/simgrid/simgrid-template-s4u>`_ and execute it as follows:
471
472       .. code-block:: console
473
474          $ git clone https://framagit.org/simgrid/simgrid-template-s4u.git
475          $ cd simgrid-template-s4u/
476          $ python master-workers.py small_platform.xml master-workers_d.xml
477
478       If you get some errors, then you should double-check
479       your :ref:`SimGrid installation <install>`.  On need, please refer to
480       the :ref:`Troubleshooting your Project Setup <install_yours_troubleshooting>` section.
481
482 .. warning::
483
484    If you use the stable version of Debian 11, Ubuntu 21.04 or Ubuntu 21.10, then you need the right version of this tutorial 
485    (add ``--branch simgrid-v3.25`` as below). These distributions only contain SimGrid v3.25 while the latest version of this
486    tutorial needs at least SimGrid v3.27.
487
488    .. code-block:: console
489
490       $ git clone --branch simgrid-v3.25 https://framagit.org/simgrid/simgrid-template-s4u.git
491
492 For R analysis of the produced traces, you may want to install R
493 and the `pajengr <https://github.com/schnorr/pajengr#installation/>`_ package.
494
495 .. code-block:: console
496
497    # install R and necessary packages
498    $ sudo apt install r-base r-cran-devtools r-cran-tidyverse
499    # install pajengr dependencies
500    $ sudo apt install git cmake flex bison
501    # install the pajengr R package
502    $ Rscript -e "library(devtools); install_github('schnorr/pajengr');"
503
504
505 Discovering the Provided Code
506 .............................
507
508 .. tabs::
509
510    .. group-tab:: C++
511
512       Please compile and execute the provided simulator as follows:
513
514       .. code-block:: console
515
516          $ make master-workers
517          $ ./master-workers small_platform.xml master-workers_d.xml
518
519       If you get an error message complaining that ``simgrid::s4u::Mailbox::get()`` does not exist, 
520       then your version of SimGrid is too old for the version of the tutorial that you got. Check again previous section.
521
522    .. group-tab:: Python
523
524       Please execute the provided simulator as follows:
525
526       .. code-block:: console
527
528          $ python master-workers.py small_platform.xml master-workers_d.xml
529
530       If you get an error stating that the simgrid module does not exist, you need to get a newer version of SimGrid. 
531       You may want to take the tutorial from the docker to get the newest version.
532
533 For a more "fancy" output, you can use simgrid-colorizer.
534
535 .. code-block:: console
536
537    # Run C++ code
538    $ ./master-workers small_platform.xml master-workers_d.xml 2>&1 | simgrid-colorizer
539
540    # Run Python code
541    $ python master-workers.py small_platform.xml master-workers_d.xml 2>&1 | simgrid-colorizer
542
543 If you installed SimGrid to a non-standard path, you may have to
544 specify the full path to simgrid-colorizer on the above line, such as
545 ``/opt/simgrid/bin/simgrid-colorizer``. If you did not install it at all,
546 you can find it in <simgrid_root_directory>/bin/colorize.
547
548 For a classical Gantt-Chart visualization, you can use `Vite
549 <http://vite.gforge.inria.fr/>`_ if you have it installed, as
550 follows. But do not spend too much time installing Vite, because there
551 is a better way to visualize SimGrid traces (see below).
552
553 .. code-block:: console
554
555    # Run C++ code
556    $ ./master-workers small_platform.xml master-workers_d.xml --cfg=tracing:yes --cfg=tracing/actor:yes
557    # Run Python code
558    $ python master-workers.py small_platform.xml master-workers_d.xml --cfg=tracing:yes --cfg=tracing/actor:yes
559
560    # Visualize the produced trace
561    $ vite simgrid.trace
562
563 .. image:: /tuto_s4u/img/vite-screenshot.png
564    :align: center
565
566 .. note::
567
568    If you use an older version of SimGrid (before v3.26), you should use
569    ``--cfg=tracing/msg/process:yes`` instead of ``--cfg=tracing/actor:yes``.
570
571 If you want the full power to visualize SimGrid traces, you need
572 to use R. As a start, you can download this `starter script
573 <https://framagit.org/simgrid/simgrid/raw/master/docs/source/tuto_s4u/draw_gantt.R>`_
574 and use it as follows:
575
576 .. code-block:: console
577
578    # Run C++ code
579    $ ./master-workers small_platform.xml master-workers_d.xml --cfg=tracing:yes --cfg=tracing/actor:yes
580    # Run Python code
581    $ python master-workers.py small_platform.xml master-workers_d.xml --cfg=tracing:yes --cfg=tracing/actor:yes
582
583    # Visualize the produced trace
584    $ Rscript draw_gantt.R simgrid.trace
585
586 It produces a ``Rplots.pdf`` with the following content:
587
588 .. image:: /tuto_s4u/img/Rscript-screenshot.png
589    :align: center
590
591
592 Lab 1: Simpler deployments
593 --------------------------
594
595 .. rst-class:: compact-list
596
597    **Learning goals:** 
598
599    * Get your hands on the code and change the communication pattern
600    * Discover the Mailbox mechanism
601
602 In the provided example, adding more workers quickly becomes a pain:
603 You need to start them (at the bottom of the file) and inform the
604 master of its availability with an extra parameter. This is mandatory
605 if you want to inform the master of where the workers are running. But
606 actually, the master does not need to have this information.
607
608 We could leverage the mailbox mechanism flexibility, and use a sort of
609 yellow page system: Instead of sending data to the worker running on
610 Fafard, the master could send data to the third worker. Ie, instead of
611 using the worker location (which should be filled in two locations),
612 we could use their ID (which should be filled in one location
613 only).
614
615 This could be done with the following deployment file. It's
616 not shorter than the previous one, but it's still simpler because the
617 information is only written once. It thus follows the `DRY
618 <https://en.wikipedia.org/wiki/Don't_repeat_yourself>`_ `SPOT
619 <http://wiki.c2.com/?SinglePointOfTruth>`_ design principle.
620
621 .. literalinclude:: tuto_s4u/deployment1.xml
622    :language: xml
623
624 .. tabs::
625
626    .. group-tab:: C++
627
628       Copy your ``master-workers.cpp`` into ``master-workers-lab1.cpp`` and
629       add a new executable into ``CMakeLists.txt``. Then modify your worker
630       function so that it gets its mailbox name not from the name of its
631       host, but from the string passed as ``args[1]``. The master will send
632       messages to all workers based on their number, for example as follows:
633
634       .. code-block:: cpp
635
636          for (int i = 0; i < tasks_count; i++) {
637             std::string worker_rank          = std::to_string(i % workers_count);
638             std::string mailbox_name         = std::string("worker-") + worker_rank;
639             simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
640
641             mailbox->put(...);
642
643             ...
644          }
645
646    .. group-tab:: Python
647
648       Copy your ``master-workers.py`` into ``master-workers-lab1.py`` then
649       modify your worker
650       function so that it gets its mailbox name not from the name of its
651       host, but from the string passed as ``args[0]``. The master will send
652       messages to all workers based on their number, for example as follows:
653
654       .. code-block:: cpp
655
656            for i in range(tasks_count): 
657               mailbox = Mailbox.by_name(str(i % worker_count))
658               mailbox.put(...)
659
660 Wrap up
661 .......
662
663 The mailboxes are a very powerful mechanism in SimGrid, allowing many
664 interesting application settings. They may feel unusual if you are
665 used to BSD sockets or other classical systems, but you will soon
666 appreciate their power. They are only used to match
667 communications but have no impact on the communication
668 timing. ``put()`` and ``get()`` are matched regardless of their
669 initiators' location and then the real communication occurs between
670 the involved parties.
671
672 Please refer to the full `Mailboxes' documentation <app_s4u.html#s4u-mailbox>`_ 
673 for more details.
674
675
676 Lab 2: Using the Whole Platform
677 -------------------------------
678
679 .. rst-class:: compact-list
680
681    **Learning goals:** 
682
683    * Interact with the platform (get the list of all hosts)
684    * Create actors directly from your program instead of the deployment file
685
686 It is now easier to add a new worker, but you still have to do it
687 manually. It would be much easier if the master could start the
688 workers on its own, one per available host in the platform. The new
689 deployment file should be as simple as:
690
691 .. literalinclude:: tuto_s4u/deployment2.xml
692    :language: xml
693
694
695 Creating the workers from the master
696 ....................................
697
698 .. tabs::
699
700    .. group-tab:: C++
701
702       For that, the master needs to retrieve the list of hosts declared in
703       the platform with :cpp:func:`simgrid::s4u::Engine::get_all_hosts`.
704       Then, the master should start the worker actors with
705       :cpp:func:`simgrid::s4u::Actor::create`.
706
707       ``Actor::create(name, host, func, params...)`` is a very flexible
708       function. Its third parameter is the function that the actor should
709       execute. This function can take any kind of parameter, provided that
710       you pass similar parameters to ``Actor::create()``. For example, you
711       could have something like this:
712
713       .. code-block:: cpp
714
715          void my_actor(int param1, double param2, std::string param3) {
716             ...
717          }
718          int main(int argc, char argv**) {
719             ...
720             simgrid::s4u::ActorPtr actor;
721             actor = simgrid::s4u::Actor::create("name", simgrid::s4u::Host::by_name("the_host"),
722                                                 &my_actor, 42, 3.14, "thevalue");
723             ...
724          }
725
726    .. group-tab:: Python
727
728       For that, the master needs to retrieve the list of hosts declared in
729       the platform with :py:func:`simgrid.Engine.get_all_hosts`. Since this method is not static, 
730       you may want to call it on the Engine instance, as in ``Engine.instance().get_all_hosts()``.
731       Then, the master should start the worker actors with :py:func:`simgrid.Actor.create`.
732
733       ``Actor.create(name, host, func, params...)`` is a very flexible
734       function. Its third parameter is the function that the actor should
735       execute. This function can take any kind of parameter, provided that
736       you pass similar parameters to ``Actor.create()``. For example, you
737       could have something like this:
738
739       .. code-block:: python
740
741          def my_actor(param1, param2, param3):
742             # your code comes here
743          actor = simgrid.Actor.create("name", the_host, my_actor, 42, 3.14, "thevalue")
744
745
746 Master-Workers Communication
747 ............................
748
749 Previously, the workers got from their parameter the name of the
750 mailbox they should use. We can still do so: the master should build
751 such a parameter before using it in the ``Actor::create()`` call. The
752 master could even pass directly the mailbox as a parameter to the
753 workers.
754
755 Since we want later to study concurrent applications, it is advised to
756 use a mailbox name that is unique over the simulation even if there is
757 more than one master.
758
759 .. tabs::
760
761    .. group-tab:: C++
762
763       One possibility for that is to use the actor ID (aid) of each worker
764       as a mailbox name. The master can retrieve the aid of the newly
765       created actor with :cpp:func:`simgrid::s4u::Actor::get_pid()` while the actor itself can
766       retrieve its own aid with :cpp:func:`simgrid::s4u::this_actor::get_pid()`.
767       The retrieved value is an :cpp:type:`aid_t`, which is an alias for ``long``.
768
769    .. group-tab:: Python
770
771       One possibility for that is to use the actor ID of each worker
772       as a mailbox name. The master can retrieve the aid of the newly
773       created actor with :py:func:`simgrid.Actor.pid` while the actor itself can
774       retrieve its own aid with :py:func:`simgrid.this_actor.get_pid()`.
775
776 Wrap up
777 .......
778
779 In this exercise, we reduced the amount of configuration that our
780 simulator requests. This is both a good idea and a dangerous
781 trend. This simplification is another application of the good old DRY/SPOT
782 programming principle (`Don't Repeat Yourself / Single Point Of Truth
783 <https://en.wikipedia.org/wiki/Don%27t_repeat_yourself>`_), and you
784 really want your programming artifacts to follow these software
785 engineering principles.
786
787 But at the same time, you should be careful in separating your
788 scientific contribution (the master/workers algorithm) and the
789 artifacts used to test it (platform, deployment, and workload). This is
790 why SimGrid forces you to express your platform and deployment files
791 in XML instead of using a programming interface: it forces a clear
792 separation of concerns between things of different nature.
793
794 Lab 3: Fixed Experiment Duration
795 --------------------------------
796
797 .. rst-class:: compact-list
798
799    **Learning goals:** 
800
801    * Forcefully kill actors, and stop the simulation at a given point of time
802    * Control the logging verbosity
803
804 In the current version, the number of tasks is defined through the
805 worker arguments. Hence, tasks are created at the very beginning of
806 the simulation. Instead, have the master dispatching tasks for a
807 predetermined amount of time.  The tasks must now be created on need
808 instead of beforehand.
809
810 Of course, usual time functions like ``gettimeofday`` will give you the
811 time on your real machine, which is pretty useless in the
812 simulation. Instead, retrieve the time in the simulated world with
813 :cpp:func:`simgrid::s4u::Engine::get_clock` (C++) or 
814 :py:func:`simgrid.Engine.get_clock()`) (Python).
815
816 You can still stop your workers with a specific task as previously,
817 or you may kill them forcefully with :cpp:func:`simgrid::s4u::Actor::kill` (C++)
818 :py:func:`simgrid.Actor.kill` (Python).
819
820 Anyway, the new deployment `deployment3.xml` file should thus look
821 like this:
822
823 .. literalinclude:: tuto_s4u/deployment3.xml
824    :language: xml
825
826 Controlling the message verbosity
827 .................................
828
829 Not all messages are equally informative, so you probably want to
830 change some of the *info* messages (C: :c:macro:`XBT_INFO`; Python: :py:func:`simgrid.this_actor.info`) 
831 into *debug* messages`(C: :c:macro:`XBT_DEBUG`; Python: :py:func:`simgrid.this_actor.debug`) so that they are
832 hidden by default. For example, you may want to use an *info* message once
833 every 100 tasks and *debug* when sending all the other tasks. Or
834 you could show only the total number of tasks processed by
835 default. You can still see the debug messages as follows:
836
837 .. code-block:: console
838
839    $ ./master-workers-lab3 small_platform.xml deployment3.xml --log=s4u_app_masterworker.thres:debug
840
841 Lab 4: What-if analysis
842 -----------------------
843
844 .. rst-class:: compact-list
845
846    **Learning goals:** 
847
848    * Change the platform characteristics during the simulation.
849    * Explore other communication patterns.
850
851 Computational speed
852 ...................
853
854 Attach a profile to your hosts, so that their computational speed automatically vary over time, modeling an external load on these machines.
855 This can be done with :cpp:func:`simgrid::s4u::Host::set_speed_profile` (C++) or :py:func:`simgrid.Host.set_speed_profile` (Python). 
856
857 Make it so that one of the hosts get really really slow, and observe how your whole application performance decreases.
858 This is because one slow host slows down the whole process. Instead of a round-robin dispatch push, 
859 you should completely reorganize your application in a First-Come First-Served manner (FCFS).
860 Actors should pull a task whenever they are ready, so that fast actors can overpass slow ones in the queue.
861
862 There is two ways to implement that: either the workers request a task to the master by sending their name to a specific mailbox,
863 or the master directly pushes the tasks to a centralized mailbox from which the workers pull their work. The first approach is closer
864 to what would happen with communications based on BSD sockets while the second is closer to message queues. You could also decide to
865 model your socket application in the second manner if you want to neglect these details and keep your simulator simple. It's your decision.
866
867 Changing the communication schema can be a bit hairy, but once it works, you will see that such as simple FCFS schema allows one to greatly 
868 increase the amount of tasks handled over time here. Things may be different with another platform file.
869
870 Communication speed
871 ...................
872
873 Let's now modify the communication speed between hosts.
874
875 Retrieve a link from its name with :cpp:func:`simgrid::s4u::Link::by_name()` (C++) or :py:func:`simgrid.Link.by_name()` (python).
876
877 Retrieve all links in the platform with :cpp:func:`simgrid::s4u::Engine::get_all_links()` (C++) or :py:func:`simgrid.Engine.get_all_links()` (python).
878
879 Retrieve the list of links from one host to another with :cpp:func:`simgrid::s4u::Host::route_to` (C++) or :py:func:`simgrid.Host.route_to` (python).
880
881 Modify the bandwidth of a given link with :cpp:func:`simgrid::s4u::Link::set_bandwidth` (C++) or :py:func:`simgrid.Link.set_bandwidth` (python).
882 You can even have the bandwidth automatically vary over time with :cpp:func:`simgrid::s4u::Link::set_bandwidth_profile` (C++) or :py:func:`simgrid.Link.set_bandwidth_profile` (python).
883
884 Once implemented, you will notice that slow communications may still result in situations
885 where one worker only works at a given point of time. To overcome that, your master needs 
886 to send data to several workers in parallel, using 
887 :cpp:func:`simgrid::s4u::Mailbox::put_async` (C++) or :py:func:`simgrid.Mailbox.put_async` (Python)
888 to start several communications in parallel, and
889 :cpp:func:`simgrid::s4u::Comm::wait_any` (C++) or and :py:func:`simgrid.Comm.wait_any` (Python) 
890 to react to the completion of one of these communications. Actually, since this code somewhat tricky 
891 to write, it's provided as :ref:`an example <s4u_ex_communication>` in the distribution (search for 
892 ``wait_any`` in that page). 
893
894 Dealing with failures
895 .....................
896
897 Turn a given link off with :cpp:func:`simgrid::s4u::Link::turn_off` (C++) or :py:func:`simgrid.Link.turn_off` (python). 
898 You can even implement churn where a link automatically turn off and on again over time with :cpp:func:`simgrid::s4u::Link::set_state_profile` (C++) or :py:func:`simgrid.Link.set_state_profile` (python). 
899
900 If a link fails while you try to use it, ``wait()`` will raise a ``NetworkFailureException`` that you need to catch. 
901 Again, there is a nice example demoing this feature, :ref:`under platform-failures <s4u_ex_communication>`.
902
903 Lab 5: Competing Applications
904 -----------------------------
905
906 .. rst-class:: compact-list
907
908    **Learning goals:** 
909
910    * Advanced vizualization through tracing categories
911
912
913 It is now time to start several applications at once, with the following ``deployment5.xml`` file.
914
915 .. literalinclude:: tuto_s4u/deployment5.xml
916    :language: xml
917
918 Things happen when you do so, but it remains utterly difficult to
919 understand what's happening exactly. Even Gantt visualizations
920 contain too much information to be useful: it is impossible to
921 understand which task belongs to which application. To fix this, we
922 will categorize the tasks.
923
924 Instead of starting the execution in one function call only with
925 ``this_actor::execute(cost)``, you need to
926 create the execution activity, set its tracing category, start it 
927 and wait for its completion, as follows.
928
929 .. tabs::
930
931    .. group-tab:: C++
932
933       Use :cpp:func:`simgrid::s4u::Exec::set_tracing_category` to change the category of an execution.
934
935       .. code-block:: cpp
936
937          simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(compute_cost);
938          exec->set_tracing_category(category);
939          // exec->start() is optional here as wait() starts the activity on need
940          exec->wait();
941
942       You can shorten this code as follows:
943
944       .. code-block:: cpp
945
946          simgrid::s4u::this_actor::exec_init(compute_cost)->set_tracing_category(category)->wait();
947
948    .. group-tab:: Python
949
950       Use :py:func:`simgrid.Exec.set_tracing_category` to change the category of an execution.
951
952       .. code-block:: python
953
954          exec = simgrid:.this_actor.exec_init(compute_cost)
955          exec.set_tracing_category(category)
956          # exec.start() is optional here as wait() starts the activity on need
957          exec->wait()
958
959       You can shorten this code as follows:
960
961       .. code-block:: python
962
963          simgrid.this_actor.exec_init(compute_cost).set_tracing_category(category).wait()
964
965
966 Visualizing the result
967 .......................
968
969 vite is not enough to understand the situation, because it does not
970 deal with categorization. This time, you absolutely must switch to R,
971 as explained on `this page
972 <https://simgrid.org/contrib/R_visualization.html>`_.
973
974 .. todo::
975
976    Include here the minimal setting to view something in R.
977
978 Further Improvements
979 --------------------
980
981 From this, many things can easily be added. For example, you could:
982
983 - Allow workers to have several pending requests  to overlap
984   communication and computations as much as possible. Non-blocking
985   communication will probably become handy here.
986 - Add a performance measurement mechanism, enabling the master to make smart scheduling choices.
987 - Test your code on other platforms, from the ``examples/platforms``
988   directory in your archive.
989
990   What is the largest number of tasks requiring 50e6 flops and 1e5
991   bytes that you manage to distribute and process in one hour on
992   ``g5k.xml`` ?
993 - Optimize not only for the number of tasks handled but also for the total energy dissipated.
994 - And so on. If you come up with a nice extension, please share
995   it with us so that we can extend this tutorial.
996
997 After this Tutorial
998 -------------------
999
1000 This tutorial is now terminated. You could keep reading the online documentation and
1001 tutorials, or you could head up to the :ref:`example section <s4u_examples>` to read some code.
1002
1003 .. todo::
1004
1005    Things to improve in the future:
1006
1007    - Propose equivalent exercises and skeleton in Java once we fix the Java binding.
1008
1009 .. |br| raw:: html
1010
1011    <br />
1012
1013 ..  LocalWords:  SimGrid