Logo AND Algorithmique Numérique Distribuée

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