Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename an example to stick to the function name
[simgrid.git] / examples / s4u / README.rst
1 .. S4U (Simgrid for you) is the next interface of SimGrid, expected to be released with SimGrid 4.0.
2 ..
3 .. Even if it is not completely rock stable yet, it may well already fit
4 .. your needs. You are welcome to try it and report any interface
5 .. glitches that you see. Be however warned that the interface may change
6 .. until the final release.  You will have to adapt your code on the way.
7 .. 
8 .. This file follows the ReStructured syntax to be included in the
9 .. documentation, but it should remain readable directly.
10
11
12 S4U Examples
13 ************
14
15 SimGrid comes with an extensive set of examples, documented on this
16 page. Most of them only demonstrate one single feature, with some
17 larger examplars listed below. 
18
19 The C++ examples can be found under examples/s4u while python examples
20 are in examples/python. Each such directory contains the source code (also listed
21 from this page), and the so-called tesh file containing how to call
22 the binary obtained by compiling this example and also the expected
23 output. Tesh files are used to turn each of our examples into an
24 integration test. Some examples also contain other files, on need.
25
26 A good way to bootstrap your own project is to copy and combine some
27 of the provided examples to constitute the skeleton of what you plan
28 to simulate.
29
30 ===========================
31 Actors: the Active Entities
32 ===========================
33
34
35 Starting and Stoping Actors
36 ---------------------------
37
38   - **Creating actors:**
39     Most actors are started from the deployment XML file, but there is other methods.
40     This example show them all.
41     |br| |cpp| `examples/s4u/actor-create/s4u-actor-create.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-create/s4u-actor-create.cpp>`_
42     |br|  |py| `examples/python/actor-create/actor-create.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-create/actor-create.py>`_
43     
44   - **Kill actors:**
45     Actors can forcefully stop other actors with the 
46     :cpp:func:`void simgrid::s4u::Actor::kill(void)` or the 
47     :cpp:func:`void simgrid::s4u::Actor::kill(aid_t)` methods.
48     |br| `examples/s4u/actor-kill/s4u-actor-kill.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-kill/s4u-actor-kill.cpp>`_
49
50   - **Controling the actor life cycle from the XML:**
51     You can specify a start time and a kill time in the deployment
52     file.
53     |br| `examples/s4u/actor-lifetime/s4u-actor-lifetime.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-lifetime/s4u-actor-lifetime.cpp>`_
54     |br| `examples/s4u/actor-lifetime/s4u-actor-lifetime_d.xml <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-lifetime/s4u-actor-lifetime_d.xml>`_
55
56   - **Daemonize actors:**
57     Some actors may be intended to simulate daemons that run in background. This example show how to transform a regular
58     actor into a daemon that will be automatically killed once the simulation is over.
59     |br| `examples/s4u/actor-daemon/s4u-actor-daemon.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-daemon/s4u-actor-daemon.cpp>`_
60     
61 Inter-Actors Interactions
62 -------------------------
63
64   - **Suspend and Resume actors:**    
65     Actors can be suspended and resumed during their executions thanks
66     to :cpp:func:`simgrid::s4u::Actor::suspend()` and
67     :cpp:func:`simgrid::s4u::Actor::resume()`.
68     |br| `examples/s4u/actor-suspend/s4u-actor-suspend.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-suspend/s4u-actor-suspend.cpp>`_
69
70   - **Migrating Actors:**
71     Actors can move or be moved from a host to another very easily.
72     |br| |cpp| `examples/s4u/actor-migrate/s4u-actor-migrate.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-migrate/s4u-actor-migrate.cpp>`_
73     :cpp:func:`simgrid::s4u::this_actor::migrate()` |cpp|
74     |br|  |py| `examples/python/actor-migrate/actor-migrate.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-migrate/actor-migrate.py>`_
75     :py:func:`simgrid.this_actor.migrate()` |py|
76
77   - **Waiting for the termination of an actor:** (joining on it)
78     You can block the current actor until the end of another actor.
79     |br| |cpp| `examples/s4u/actor-join/s4u-actor-join.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-join/s4u-actor-join.cpp>`_
80     :cpp:func:`simgrid::s4u::Actor::join()` |cpp|
81     |br| |py| `examples/python/actor-join/actor-join.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-join/actor-join.py>`_
82     :py:func:`simgrid.Actor.join()` |py|
83
84   - **Yielding to other actors**.
85     The ```yield()``` function interrupts the execution of the current
86     actor, leaving a chance to the other actors that are ready to run
87     at this timestamp. 
88     |br| |cpp| `examples/s4u/actor-yield/s4u-actor-yield.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-yield/s4u-actor-yield.cpp>`_
89     :cpp:func:`simgrid::s4u::this_actor::yield()` |cpp|
90     |br|  |py| `examples/python/actor-yield/actor-yield.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-yield/actor-yield.py>`_
91     :py:func:`simgrid.this_actor.yield_()` |py|
92
93 Traces Replay as a Workload
94 ---------------------------
95
96 This section details how to run trace-driven simulations. It is very
97 handy when you want to test an algorithm or protocol that only react
98 to external events. For example, many P2P protocols react to user
99 requests, but do nothing if there is no such event.
100
101 In such situations, you should write your protocol in C++, and separate
102 the workload that you want to play onto your protocol in a separate
103 text file. Declare a function handling each type of the events in your
104 trace, register them using :cpp:func:`xbt_replay_action_register()` in
105 your main, and then run the simulation.
106
107 Then, you can either have one trace file containing all your events,
108 or a file per simulated process: the former may be easier to work
109 with, but the second is more efficient on very large traces. Check
110 also the tesh files in the example directories for details.
111
112   - **Communication replay:**
113     Presents a set of event handlers reproducing classical communication
114     primitives (asynchronous send/receive at the moment).
115     |br| `examples/s4u/replay-comm/s4u-replay-comm.cpp  <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/replay-comm/s4u-replay-comm.cpp>`_
116
117   - **I/O replay:**
118     Presents a set of event handlers reproducing classical I/O
119     primitives (open, read, close).
120     |br| `examples/s4u/replay-storage/s4u-replay-storage.cpp  <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/replay-storage/s4u-replay-storage.cpp>`_
121
122 ==========================
123 Activities: what Actors do
124 ==========================
125
126 Communications on the Network
127 -----------------------------
128
129  - **Basic asynchronous communications:**
130    Illustrates how to have non-blocking communications, that are
131    communications running in the background leaving the process free
132    to do something else during their completion. The main functions
133    involved are :cpp:func:`simgrid::s4u::Mailbox::put_async()` and 
134    :cpp:func:`simgrid::s4u::Comm::wait()`.
135    |br| `examples/s4u/async-wait/s4u-async-wait.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/async-wait/s4u-async-wait.cpp>`_
136
137  - **Waiting for all communications in a set:**
138    The :cpp:func:`simgrid::s4u::Comm::wait_all()` function is useful
139    when you want to block until all activities in a given set have
140    completed. 
141    |br| `examples/s4u/async-waitall/s4u-async-waitall.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/async-waitall/s4u-async-waitall.cpp>`_
142
143  - **Waiting for the first completed communication in a set:**
144    The :cpp:func:`simgrid::s4u::Comm::wait_any()` function is useful
145    when you want to block until one activity of the set completes, no
146    matter which terminates first.    
147    |br| `examples/s4u/async-waitany/s4u-async-waitany.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/async-waitany/s4u-async-waitany.cpp>`_
148
149 .. todo:: add the `ready` example here
150    
151 .. _s4u_ex_execution:
152
153 Executions on the CPU
154 ---------------------
155
156   - **Basic execution:**
157     The computations done in your program are not reported to the
158     simulated world, unless you explicitely request the simulator to pause
159     the actor until a given amount of flops gets computed on its simulated
160     host. Some executions can be given an higher priority so that they
161     get more resources.
162     |br| |cpp| `examples/s4u/exec-basic/s4u-exec-basic.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/exec-basic/s4u-exec-basic.cpp>`_
163     |br|  |py| `examples/python/exec-basic/exec-basic.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/exec-basic/exec-basic.py>`_
164
165   - **Asynchronous execution:**
166     You can start asynchronous executions, just like you would fire
167     background threads.
168     |br| `examples/s4u/exec-async/s4u-exec-async.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/exec-async/s4u-exec-async.cpp>`_
169     
170   - **Monitoring asynchronous executions:**
171     This example shows how to start an asynchronous execution, and
172     monitor its status.
173     |br| `examples/s4u/exec-monitor/s4u-exec-monitor.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/exec-monitor/s4u-exec-monitor.cpp>`_
174     
175   - **Remote execution:**
176     Before its start, you can change the host on which a given execution will occur.
177     |br| `examples/s4u/exec-remote/s4u-exec-remote.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/exec-remote/s4u-exec-remote.cpp>`_
178
179   - **Using Pstates on a host:**
180     Shows how define a set of pstatesfor a host in the XML, and how the current
181     pstate can be accessed/changed with :cpp:func:`simgrid::s4u::Host::get_pstate_speed` and :cpp:func:`simgrid::s4u::Host::set_pstate`.
182     |br| `examples/s4u/exec-dvfs/s4u-exec-dvfs.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/exec-dvfs/s4u-exec-dvfs.cpp>`_
183     |br| `examples/platforms/energy_platform.xml <https://framagit.org/simgrid/simgrid/tree/master/examples/platforms/energy_platform.xml>`_
184
185   - **Parallel executions:**
186     These objects are convenient abstractions of parallel
187     computational kernels that span over several machines, such as a
188     PDGEM and the other ScaLAPACK routines.
189     |br| `examples/s4u/exec-ptask/s4u-exec-ptask.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/exec-ptask/s4u-exec-ptask.cpp>`_
190
191 I/O on Disks and Files
192 ----------------------
193
194 SimGrid provides two levels of abstraction to interact with the
195 simulated storages. At the simplest level, you simply create read and
196 write actions on the storage resources.
197
198   - **Access to raw storage devices:**
199     This example illustrates how to simply read and write data on a
200     simulated storage resource.
201     |br| `examples/s4u/io-storage-raw/s4u-io-storage-raw.cpp  <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/io-storage-raw/s4u-io-storage-raw.cpp>`_
202
203 The FileSystem plugin provides a more detailed view, with the
204 classical operations over files: open, move, unlink, and of course
205 read and write. The file and disk sizes are also dealt with and can
206 result in short reads and short write, as in reality.
207
208   - **File Management:**
209     This example illustrates the use of operations on files
210     (read, write, seek, tell, unlink, etc).
211     |br| `examples/s4u/io-file-system/s4u-io-file-system.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/io-file-system/s4u-io-file-system.cpp>`_
212
213   - **Remote I/O:**
214     I/O operations on files can also be done in a remote fashion, 
215     i.e. when the accessed disk is not mounted on the caller's host.
216     |br| `examples/s4u/io-file-remote/s4u-io-file-remote.cpp  <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/io-file-remote/s4u-io-file-remote.cpp>`_
217
218 Classical synchronization objects
219 ---------------------------------
220
221  - **Mutex:**
222    Shows how to use simgrid::s4u::Mutex synchronization objects.
223    |br| `examples/s4u/synchro-mutex/s4u-synchro-mutex.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/synchro-mutex/s4u-synchro-mutex.cpp>`_
224
225  - **Barrier:**
226    Shows how to use simgrid::s4u::Barrier synchronization objects.
227    |br| `examples/s4u/synchro-barrier/s4u-synchro-barrier.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/synchro-barrier/s4u-synchro-barrier.cpp>`_
228
229  - **Semaphore:**
230    Shows how to use simgrid::s4u::Semaphore synchronization objects.
231    |br| `examples/s4u/synchro-semaphore/s4u-synchro-semaphore.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/synchro-semaphore/s4u-synchro-semaphore.cpp>`_
232
233 =============================
234 Interacting with the Platform
235 =============================
236
237  - **Retrieving the list of hosts matching a given criteria:**
238    Shows how to filter the actors that match a given criteria.
239    |br| `examples/s4u/engine-filtering/s4u-engine-filtering.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/engine-filtering/s4u-engine-filtering.cpp>`_
240
241  - **User-defined properties:**
242    You can attach arbitrary information to most platform elements from
243    the XML file, and then interact with these values from your
244    program. Note that the changes are not written permanently on disk,
245    in the XML file nor anywhere else. They only last until the end of
246    your simulation.
247    
248    - :cpp:func:`simgrid::s4u::Actor::get_property()` and :cpp:func:`simgrid::s4u::Actor::set_property()`
249    - :cpp:func:`simgrid::s4u::Host::get_property()` and :cpp:func:`simgrid::s4u::Host::set_property()`
250    - :cpp:func:`simgrid::s4u::Link::get_property()` and :cpp:func:`simgrid::s4u::Link::set_property()`
251    - :cpp:func:`simgrid::s4u::NetZone::get_property()` and :cpp:func:`simgrid::s4u::NetZone::set_property()`
252      
253    |br| `examples/s4u/platform-properties/s4u-platform-properties.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/platform-properties/s4u-platform-properties.cpp>`_
254    |br| `examples/s4u/platform-properties/s4u-platform-properties_d.xml <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/platform-properties/s4u-platform-properties_d.xml>`_
255    |br| `examples/platforms/prop.xml <https://framagit.org/simgrid/simgrid/tree/master/examples/platforms/prop.xml>`_
256
257 =================
258 Energy Simulation
259 =================
260
261   - **Describing the energy profiles in the platform:**
262     This platform file contains the energy profile of each links and
263     hosts, which is necessary to get energy consumption predictions.
264     As usual, you should not trust our example, and you should strive
265     to double-check that your instanciation matches your target platform.
266     |br| `examples/platforms/energy_platform.xml <https://framagit.org/simgrid/simgrid/tree/master/examples/platforms/energy_platform.xml>`_
267
268   - **Consumption due to the CPU:** 
269     This example shows how to retrieve the amount of energy consumed
270     by the CPU during computations, and the impact of the pstate.
271     |br| `examples/s4u/energy-exec/s4u-energy-exec.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/energy-exec/s4u-energy-exec.cpp>`_
272
273   - **Consumption due to the network:**
274     This example shows how to retrieve and display the energy consumed
275     by the network during communications.
276     |br| `examples/s4u/energy-link/s4u-energy-link.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/energy-link/s4u-energy-link.cpp>`_
277
278   - **Modeling the shutdown and boot of hosts:**
279     Simple example of model of model for the energy consumption during
280     the host boot and shutdown periods.
281     |br| `examples/s4u/energy-boot/platform_boot.xml <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/energy-boot/platform_boot.xml>`_
282     |br| `examples/s4u/energy-boot/s4u-energy-boot.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/energy-boot/s4u-energy-boot.cpp>`_
283
284 =======================
285 Tracing and Visualizing
286 =======================
287
288 Tracing can be activated by various configuration options which
289 are illustrated in these example. See also the 
290 :ref:`full list of options related to tracing <tracing_tracing_options>`.
291
292 It is interesting to run the process-create example with the following
293 options to see the task executions:
294
295   - **Platform Tracing:**
296     This program is a toy example just loading the platform, so that
297     you can play with the platform visualization. Recommanded options:
298     ``--cfg=tracing:yes --cfg=tracing/categorized:yes``
299     |br| `examples/s4u/trace-platform/s4u-trace-platform.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/trace-platform/s4u-trace-platform.cpp>`_
300
301 ========================
302 Larger SimGrid Examplars
303 ========================
304
305 This section contains application examples that are somewhat larger
306 than the previous examples.
307
308   - **Ping Pong:**
309     This simple example just sends one message back and forth.
310     The tesh file laying in the directory show how to start the simulator binary, highlighting how to pass options to 
311     the simulators (as detailed in Section :ref:`options`). 
312     |br| `examples/s4u/app-pingpong/s4u-app-pingpong.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/app-pingpong/s4u-app-pingpong.cpp>`_
313
314   - **Token ring:**
315     Shows how to implement a classical communication pattern, where a
316     token is exchanged along a ring to reach every participant.
317     |br| `examples/s4u/app-token-ring/s4u-app-token-ring.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/app-token-ring/s4u-app-token-ring.cpp>`_
318
319   - **Master Workers:**
320     Another good old example, where one Master process has a bunch of task to dispatch to a set of several Worker 
321     processes. This example comes in two equivalent variants, one
322     where the actors are specified as simple functions (which is easier to
323     understand for newcomers) and one where the actors are specified
324     as classes (which is more powerful for the users wanting to build
325     their own projects upon the example).
326     |br| `examples/s4u/app-masterworkers/s4u-app-masterworkers-class.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/app-masterworkers/s4u-app-masterworkers-class.cpp>`_
327     |br| `examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp>`_
328     
329 Data diffusion
330 --------------
331
332   - **Bit Torrent:** 
333     Classical protocol for Peer-to-Peer data diffusion.
334     |br| `examples/s4u/app-bittorrent/s4u-bittorrent.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/app-bittorrent/s4u-bittorrent.cpp>`_
335     
336   - **Chained Send:** 
337     Data broadcast over a ring of processes.
338     |br| `examples/s4u/app-chainsend/s4u-app-chainsend.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/app-chainsend/s4u-app-chainsend.cpp>`_
339
340 Distributed Hash Tables (DHT)
341 -----------------------------
342
343   - **Chord Protocol** 
344     One of the most famous DHT protocol.
345     |br| `examples/s4u/dht-chord/s4u-dht-chord.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/dht-chord/s4u-dht-chord.cpp>`_
346
347 .. TODO:: document here the examples about plugins
348
349 .. |br| raw:: html
350
351    <br />
352
353 .. |cpp| image:: /img/lang_cpp.png
354    :align: middle
355    :width: 12
356
357 .. |py| image:: /img/lang_python.png
358    :align: middle
359    :width: 12