Logo AND Algorithmique Numérique Distribuée

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