Logo AND Algorithmique Numérique Distribuée

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