Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
82a0785bdef4ed5c63e9606f9a17e8b0be21dfa6
[simgrid.git] / docs / source / app_s4u.rst
1 .. _S4U_doc:
2
3 =================
4 The S4U Interface
5 =================
6
7 .. raw:: html
8
9    <object id="TOC" data="graphical-toc.svg" width="100%" type="image/svg+xml"></object>
10    <script>
11    window.onload=function() { // Wait for the SVG to be loaded before changing it
12      var elem=document.querySelector("#TOC").contentDocument.getElementById("S4UBox")
13      elem.style="opacity:0.93999999;fill:#ff0000;fill-opacity:0.1";
14    }
15    </script>
16    <br/>
17    <br/>
18
19 The S4U interface (SimGrid for you) mixes the full power of SimGrid
20 with the full power of C++. This is the prefered interface to describe
21 abstract algorithms in the domains of Cloud, P2P, HPC, IoT and similar
22 settings.
23
24 -------------
25 Main Concepts
26 -------------
27
28 A typical SimGrid simulation is composed of several |Actors|_, that
29 execute user-provided functions. The actors have to explicitly use the
30 S4U interface to express their
31 :ref:`computation <exhale_class_classsimgrid_1_1s4u_1_1Exec>`,
32 :ref:`communication <exhale_class_classsimgrid_1_1s4u_1_1Comm>`,
33 :ref:`disk usage <exhale_class_classsimgrid_1_1s4u_1_1Io>`,
34 and other |Activities|_, so that they get reflected within the
35 simulator. These activities take place on resources such as |Hosts|_,
36 |Links|_ and |Storages|_. SimGrid predicts the time taken by each
37 activity and orchestrates accordingly the actors waiting for the
38 completion of these activities.
39
40
41 When **communicating**, data is not directly sent to other actors but
42 posted onto a |Mailbox|_ that serve as rendez-vous point between
43 communicating actors. This means that you don't need to know who you
44 are talking to, you just put your communication `Send` request in a
45 mailbox, and it will be matched with a complementary `Receive`
46 request.  Alternatively, actors can interact through **classical
47 synchronization mechanisms** such as |Barrier|_, |Semaphore|_,
48 |Mutex|_ and |ConditionVariable|_.
49
50 Each actor is located on a simulated |Host|_. Each host is located
51 itself in a |NetZone|_, that knows the networking path between one
52 resource to another. Each NetZone is included in another one, forming
53 a tree of NetZones which root zone contains the whole platform.
54
55 The :ref:`simgrid::s4u::this_actor
56 <namespace_simgrid__s4u__this_actor>` namespace provides many helper
57 functions to simplify the code of actors.
58
59 - **Global Classes**
60
61   - :ref:`class s4u::Actor <exhale_class_classsimgrid_1_1s4u_1_1Actor>`:
62     Active entities executing your application.
63   - :ref:`class s4u::Engine <exhale_class_classsimgrid_1_1s4u_1_1Engine>`
64     Simulation engine (singleton).
65   - :ref:`class s4u::Mailbox <exhale_class_classsimgrid_1_1s4u_1_1Mailbox>`
66     Communication rendez-vous.
67
68 - **Platform Elements**
69
70   - :ref:`class s4u::Host <exhale_class_classsimgrid_1_1s4u_1_1Host>`:
71     Actor location, providing computational power.
72   - :ref:`class s4u::Link <exhale_class_classsimgrid_1_1s4u_1_1Link>`
73     Interconnecting hosts.
74   - :ref:`class s4u::NetZone <exhale_class_classsimgrid_1_1s4u_1_1NetZone>`:
75     Sub-region of the platform, containing resources (Hosts, Link, etc).
76   - :ref:`class s4u::Storage <exhale_class_classsimgrid_1_1s4u_1_1Storage>`
77     Resource on which actors can write and read data.
78   - :ref:`class s4u::VirtualMachine <exhale_class_classsimgrid_1_1s4u_1_1VirtualMachine>`:
79     Execution containers that can be moved between Hosts.
80
81 - **Activities** (:ref:`class s4u::Activity <exhale_class_classsimgrid_1_1s4u_1_1Activity>`):
82   The things that actors can do on resources
83
84   - :ref:`class s4u::Comm <exhale_class_classsimgrid_1_1s4u_1_1Comm>`
85     Communication activity, started on Mailboxes and consuming links.
86   - :ref:`class s4u::Exec <exhale_class_classsimgrid_1_1s4u_1_1Exec>`
87     Computation activity, started on Host and consuming CPU resources.
88   - :ref:`class s4u::Io <exhale_class_classsimgrid_1_1s4u_1_1Io>`
89     I/O activity, started on and consumming Storages.
90
91 - **Synchronization Mechanisms**: Classical IPC that actors can use
92
93   - :ref:`class s4u::Barrier <exhale_class_classsimgrid_1_1s4u_1_1Barrier>`
94   - :ref:`class s4u::ConditionVariable <exhale_class_classsimgrid_1_1s4u_1_1ConditionVariable>`
95   - :ref:`class s4u::Mutex <exhale_class_classsimgrid_1_1s4u_1_1Mutex>`
96   - :ref:`class s4u::Semaphore <exhale_class_classsimgrid_1_1s4u_1_1Semaphore>`
97
98
99 .. |Actors| replace:: **Actors**
100 .. _Actors: api/classsimgrid_1_1s4u_1_1Actor.html
101
102 .. |Activities| replace:: **Activities**
103 .. _Activities: api/classsimgrid_1_1s4u_1_1Activity.html
104
105 .. |Hosts| replace:: **Hosts**
106 .. _Hosts: api/classsimgrid_1_1s4u_1_1Host.html
107
108 .. |Links| replace:: **Links**
109 .. _Links: api/classsimgrid_1_1s4u_1_1Link.html
110
111 .. |Storages| replace:: **Storages**
112 .. _Storages: api/classsimgrid_1_1s4u_1_1Storage.html
113
114 .. |VirtualMachines| replace:: **VirtualMachines**
115 .. _VirtualMachines: api/classsimgrid_1_1s4u_1_1VirtualMachine.html
116
117 .. |Host| replace:: **Host**
118 .. _Host: api/classsimgrid_1_1s4u_1_1Host.html
119
120 .. |Mailbox| replace:: **Mailbox**
121 .. _Mailbox: api/classsimgrid_1_1s4u_1_1Mailbox.html
122
123 .. |NetZone| replace:: **NetZone**
124 .. _NetZone: api/classsimgrid_1_1s4u_1_1NetZone.html
125
126 .. |Barrier| replace:: **Barrier**
127 .. _Barrier: api/classsimgrid_1_1s4u_1_1Barrier.html
128
129 .. |ConditionVariable| replace:: **ConditionVariable**
130 .. _ConditionVariable: api/classsimgrid_1_1s4u_1_1ConditionVariable.html
131
132 .. |Mutex| replace:: **Mutex**
133 .. _Mutex: api/classsimgrid_1_1s4u_1_1Mutex.html
134
135 -----------------------
136 Asynchronous Activities
137 -----------------------
138
139 Activities represent the actions that consumes a resource, such as
140 :ref:`s4u::Comm <exhale_class_classsimgrid_1_1s4u_1_1Comm>` that
141 consumes the *transmiting power* of :ref:`s4u::Link
142 <exhale_class_classsimgrid_1_1s4u_1_1Link>` resources. Every activity
143 can be either **blocking** or **asynchronous**. For example,
144 :cpp:func:`s4u::Mailbox::put() <simgrid::s4u::Mailbox::put>` and
145 :cpp:func:`s4u::Mailbox::get() <simgrid::s4u::Mailbox::get>` create
146 blocking communications: the actor is blocked until the completion of
147 that communication. Asynchronous communications do not block the actor
148 during their execution. The following example shows how to have
149 several concurrent communications ongoing.  First, you have to declare
150 a vector in which we will store the ongoing communications. It is also
151 useful to have a vector of mailboxes.
152
153 .. literalinclude:: ../../examples/s4u/async-waitall/s4u-async-waitall.cpp
154    :language: c++
155    :start-after: init-begin
156    :end-before: init-end
157    :dedent: 4
158
159 You then start all communications that should occur concurrently with
160 :cpp:func:`s4u::Mailbox::put_async() <simgrid::s4u::Mailbox::put_async>`.  
161 Finally, the actor waits for the completion of all of them at once
162 with 
163 :cpp:func:`s4u::Comm::wait_all() <simgrid::s4u::Comm::wait_all>`.  
164      
165 .. literalinclude:: ../../examples/s4u/async-waitall/s4u-async-waitall.cpp
166    :language: c++
167    :start-after: send-begin
168    :end-before: send-end
169    :dedent: 4
170
171 Every kind of activities can be asynchronous:
172
173   - :ref:`s4u::CommPtr <exhale_class_classsimgrid_1_1s4u_1_1Comm>` are created with 
174     :cpp:func:`s4u::Mailbox::put_async() <simgrid::s4u::Mailbox::put_async>` and
175     :cpp:func:`s4u::Mailbox::get_async() <simgrid::s4u::Mailbox::get_async>`.
176   - :ref:`s4u::IoPtr <exhale_class_classsimgrid_1_1s4u_1_1Io>` are created with 
177     :cpp:func:`s4u::Storage::read_async() <simgrid::s4u::Storage::read_async>` and
178     :cpp:func:`s4u::Storage::write_async() <simgrid::s4u::Storage::write_async>`.    
179   - :ref:`s4u::ExecPtr <exhale_class_classsimgrid_1_1s4u_1_1Exec>` are created with
180     :cpp:func:`s4u::Host::exec_async() <simgrid::s4u::Host::exec_async>`.
181   - In the future, it will become possible to have asynchronous IPC
182     such as asynchronous mutex lock requests.