Logo AND Algorithmique Numérique Distribuée

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