Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 `Put` request in a
45 mailbox, and it will be matched with a complementary `Get`
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 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.
143
144 .......................
145 Asynchronous Activities
146 .......................
147
148 Every activity can be either **blocking** or **asynchronous**. For
149 example, :cpp:func:`s4u::Mailbox::put() <simgrid::s4u::Mailbox::put>`
150 and :cpp:func:`s4u::Mailbox::get() <simgrid::s4u::Mailbox::get>`
151 create blocking communications: the actor is blocked until the
152 completion of that communication. Asynchronous communications do not
153 block the actor during their execution but progress on their own.
154
155 Once your asynchronous activity is started, you can test for
156 completion using :cpp:func:`s4u::Activity::test() <simgrid::s4u::Activity::test>`.
157 This function returns ``true`` if the activity completed already.
158 You can also use :cpp:func:`s4u::Activity::wait() <simgrid::s4u::Activity::wait>`
159 to block until the completion of the activity. To wait at most for a given period,
160 use  :cpp:func:`s4u::Activity::wait_for() <simgrid::s4u::Activity::wait_for>`.
161 Finally, to wait at most until a specified time limit, use
162 :cpp:func:`s4u::Activity::wait_until() <simgrid::s4u::Activity::wait_until>`.
163
164 .. todo::
165
166    wait_for and wait_until are currently not implemented for Exec and Io activities.
167
168 Every kind of activities can be asynchronous:
169
170   - :ref:`s4u::CommPtr <exhale_class_classsimgrid_1_1s4u_1_1Comm>` are created with 
171     :cpp:func:`s4u::Mailbox::put_async() <simgrid::s4u::Mailbox::put_async>` and
172     :cpp:func:`s4u::Mailbox::get_async() <simgrid::s4u::Mailbox::get_async>`.
173   - :ref:`s4u::IoPtr <exhale_class_classsimgrid_1_1s4u_1_1Io>` are created with 
174     :cpp:func:`s4u::Storage::read_async() <simgrid::s4u::Storage::read_async>` and
175     :cpp:func:`s4u::Storage::write_async() <simgrid::s4u::Storage::write_async>`.    
176   - :ref:`s4u::ExecPtr <exhale_class_classsimgrid_1_1s4u_1_1Exec>` are created with
177     :cpp:func:`s4u::Host::exec_async() <simgrid::s4u::Host::exec_async>`.
178   - In the future, it will become possible to have asynchronous IPC
179     such as asynchronous mutex lock requests.
180
181 The following example shows how to have several concurrent
182 communications ongoing.  First, you have to declare a vector in which
183 we will store the ongoing communications. It is also useful to have a
184 vector of mailboxes.
185
186 .. literalinclude:: ../../examples/s4u/async-waitall/s4u-async-waitall.cpp
187    :language: c++
188    :start-after: init-begin
189    :end-before: init-end
190    :dedent: 4
191
192 You then start all communications that should occur concurrently with
193 :cpp:func:`s4u::Mailbox::put_async() <simgrid::s4u::Mailbox::put_async>`.  
194 Finally, the actor waits for the completion of all of them at once
195 with 
196 :cpp:func:`s4u::Comm::wait_all() <simgrid::s4u::Comm::wait_all>`.  
197      
198 .. literalinclude:: ../../examples/s4u/async-waitall/s4u-async-waitall.cpp
199    :language: c++
200    :start-after: put-begin
201    :end-before: put-end
202    :dedent: 4
203
204
205 .....................
206 Activities Life cycle
207 .....................
208
209 Sometimes, you want to change the setting of an activity before it even starts.