Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the README.rst in examples/ as it also describes python
[simgrid.git] / docs / source / app_s4u.rst
1 .. _S4U_doc:
2
3 The S4U Interface
4 #################
5
6 .. raw:: html
7
8    <object id="TOC" data="graphical-toc.svg" width="100%" type="image/svg+xml"></object>
9    <script>
10    window.onload=function() { // Wait for the SVG to be loaded before changing it
11      var elem=document.querySelector("#TOC").contentDocument.getElementById("ActorBox")
12      elem.style="opacity:0.93999999;fill:#ff0000;fill-opacity:0.1;stroke:#000000;stroke-width:0.35277778;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1";
13    }
14    </script>
15    <br/>
16    <br/>
17
18 The S4U interface (SimGrid for you) mixes the full power of SimGrid
19 with the full power of C++. This is the preferred interface to describe
20 abstract algorithms in the domains of Cloud, P2P, HPC, IoT, and similar
21 settings.
22
23 Since v3.20 (June 2018), S4U is definitely the way to go for long-term
24 projects. It is feature complete, but may still evolve slightly in the
25 future releases. It can already be used to do everything that can be
26 done in SimGrid, but you may have to adapt your code in future
27 releases. When this happens, compiling your code will produce
28 deprecation warnings for 4 releases (one year) before the removal of
29 the old symbols. 
30 If you want an API that will never ever evolve in the future, you
31 should use the deprecated MSG API instead. 
32
33 Main Concepts
34 *************
35
36 A typical SimGrid simulation is composed of several |API_s4u_Actors|_, that
37 execute user-provided functions. The actors have to explicitly use the
38 S4U interface to express their :ref:`computation <API_s4u_Exec>`,
39 :ref:`communication <API_s4u_Comm>`, :ref:`disk usage <API_s4u_Io>`,
40 and other |API_s4u_Activities|_, so that they get reflected within the
41 simulator. These activities take place on resources such as |API_s4u_Hosts|_,
42 |API_s4u_Links|_ and |API_s4u_Disks|_. SimGrid predicts the time taken by each
43 activity and orchestrates the actors accordingly, waiting for the
44 completion of these activities.
45
46
47 When **communicating**, data is not directly sent to other actors but
48 posted onto a |API_s4u_Mailbox|_ that serves as a rendez-vous point between
49 communicating actors. This means that you don't need to know who you
50 are talking to, you just put your communication `Put` request in a
51 mailbox, and it will be matched with a complementary `Get`
52 request.  Alternatively, actors can interact through **classical
53 synchronization mechanisms** such as |API_s4u_Barrier|_, |API_s4u_Semaphore|_,
54 |API_s4u_Mutex|_ and |API_s4u_ConditionVariable|_.
55
56 Each actor is located on a simulated |API_s4u_Host|_. Each host is located
57 itself in a |API_s4u_NetZone|_, that knows the networking path between one
58 resource to another. Each NetZone is included in another one, forming
59 a tree of NetZones which root zone contains the whole platform. The
60 actors can also be located on a |API_s4U_VirtualMachine|_ that may
61 restrict the activities it contains to a limited amount of cores.
62 Virtual machines can also be migrated between hosts.
63
64 The :ref:`simgrid::s4u::this_actor <API_s4u_this_actor>` namespace
65 provides many helper functions to simplify the code of actors.
66
67 - **Global Classes**
68
69   - :ref:`class s4u::Actor <API_s4u_Actor>`:
70     Active entities executing your application.
71   - :ref:`class s4u::Engine <API_s4u_Engine>`
72     Simulation engine (singleton).
73   - :ref:`class s4u::Mailbox <API_s4u_Mailbox>`
74     Communication rendez-vous.
75
76 - **Platform Elements**
77
78   - :ref:`class s4u::Disk <API_s4u_Disk>`
79     Resource on which actors can write and read data.
80   - :ref:`class s4u::Host <API_s4u_Host>`:
81     Actor location, providing computational power.
82   - :ref:`class s4u::Link <API_s4u_Link>`
83     Interconnecting hosts.
84   - :ref:`class s4u::NetZone <API_s4u_NetZone>`:
85     Sub-region of the platform, containing resources (Hosts, Links, etc).
86   - :ref:`class s4u::VirtualMachine <API_s4u_VirtualMachine>`:
87     Execution containers that can be moved between Hosts.
88
89 - **Activities** (:ref:`class s4u::Activity <API_s4u_Activity>`):
90   The things that actors can do on resources
91
92   - :ref:`class s4u::Comm <API_s4u_Comm>`
93     Communication activity, started on Mailboxes and consuming links.
94   - :ref:`class s4u::Exec <API_s4u_Exec>`
95     Computation activity, started on Host and consuming CPU resources.
96   - :ref:`class s4u::Io <API_s4u_Io>`
97     I/O activity, started on and consumming disks.
98
99 - **Synchronization Mechanisms**: Classical IPC that actors can use
100
101   - :ref:`class s4u::Barrier <API_s4u_Barrier>`
102   - :ref:`class s4u::ConditionVariable <API_s4u_ConditionVariable>`
103   - :ref:`class s4u::Mutex <API_s4u_Mutex>`
104   - :ref:`class s4u::Semaphore <API_s4u_Semaphore>`
105
106
107 .. |API_s4u_Actors| replace:: **Actors**
108 .. _API_s4u_Actors: #s4u-actor
109
110 .. |API_s4u_Activities| replace:: **Activities**
111 .. _API_s4u_Activities: #s4u-activity
112
113 .. |API_s4u_Hosts| replace:: **Hosts**
114 .. _API_s4u_Hosts: #s4u-host
115
116 .. |API_s4u_Links| replace:: **Links**
117 .. _API_s4u_Links: #s4u-link
118
119 .. |API_s4u_Disks| replace:: **Disks**
120 .. _API_s4u_Disks: #s4u-disk
121
122 .. |API_s4u_VirtualMachine| replace:: **VirtualMachines**
123
124 .. |API_s4u_Host| replace:: **Host**
125
126 .. |API_s4u_Mailbox| replace:: **Mailbox**
127
128 .. |API_s4u_Mailboxes| replace:: **Mailboxes**
129 .. _API_s4u_Mailboxes: #s4u-mailbox
130
131 .. |API_s4u_NetZone| replace:: **NetZone**
132
133 .. |API_s4u_Barrier| replace:: **Barrier**
134
135 .. |API_s4u_Semaphore| replace:: **Semaphore**
136
137 .. |API_s4u_ConditionVariable| replace:: **ConditionVariable**
138
139 .. |API_s4u_Mutex| replace:: **Mutex**
140
141 Activities
142 **********
143
144 Activities represent the actions that consume a resource, such as a
145 :ref:`s4u::Comm <API_s4u_Comm>` that consumes the *transmitting power* of
146 :ref:`s4u::Link <API_s4u_Link>` resources.
147
148 =======================
149 Asynchronous Activities
150 =======================
151
152 Every activity can be either **blocking** or **asynchronous**. For
153 example, :cpp:func:`s4u::Mailbox::put() <simgrid::s4u::Mailbox::put>`
154 and :cpp:func:`s4u::Mailbox::get() <simgrid::s4u::Mailbox::get>`
155 create blocking communications: the actor is blocked until the
156 completion of that communication. Asynchronous communications do not
157 block the actor during their execution but progress on their own.
158
159 Once your asynchronous activity is started, you can test for its
160 completion using :cpp:func:`s4u::Activity::test() <simgrid::s4u::Activity::test>`.
161 This function returns ``true`` if the activity completed already.
162 You can also use :cpp:func:`s4u::Activity::wait() <simgrid::s4u::Activity::wait>`
163 to block until the completion of the activity. To wait for at most a given amount of time,
164 use  :cpp:func:`s4u::Activity::wait_for() <simgrid::s4u::Activity::wait_for>`.
165 Finally, to wait at most until a specified time limit, use
166 :cpp:func:`s4u::Activity::wait_until() <simgrid::s4u::Activity::wait_until>`.
167
168 .. todo::
169
170    wait_for and wait_until are currently not implemented for Exec and Io activities.
171
172 Every kind of activity can be asynchronous:
173
174   - :ref:`s4u::CommPtr <API_s4u_Comm>` are created with 
175     :cpp:func:`s4u::Mailbox::put_async() <simgrid::s4u::Mailbox::put_async>` and
176     :cpp:func:`s4u::Mailbox::get_async() <simgrid::s4u::Mailbox::get_async>`.
177   - :ref:`s4u::IoPtr <API_s4u_Io>` are created with 
178     :cpp:func:`s4u::Disk::read_async() <simgrid::s4u::Disk::read_async>` and
179     :cpp:func:`s4u::Disk::write_async() <simgrid::s4u::Disk::write_async>`.
180   - :ref:`s4u::ExecPtr <API_s4u_Exec>` are created with
181     :cpp:func:`s4u::Host::exec_async() <simgrid::s4u::Host::exec_async>`.
182   - In the future, it will become possible to have asynchronous IPC
183     such as asynchronous mutex lock requests.
184
185 The following example shows how to have several concurrent
186 communications ongoing.  First, you have to declare a vector in which
187 we will store the ongoing communications. It is also useful to have a
188 vector of mailboxes.
189
190 .. literalinclude:: ../../examples/s4u/async-waitall/s4u-async-waitall.cpp
191    :language: c++
192    :start-after: init-begin
193    :end-before: init-end
194    :dedent: 4
195
196 Then, you start all the communications that should occur concurrently with
197 :cpp:func:`s4u::Mailbox::put_async() <simgrid::s4u::Mailbox::put_async>`.  
198 Finally, the actor waits for the completion of all of them at once
199 with 
200 :cpp:func:`s4u::Comm::wait_all() <simgrid::s4u::Comm::wait_all>`.  
201      
202 .. literalinclude:: ../../examples/s4u/async-waitall/s4u-async-waitall.cpp
203    :language: c++
204    :start-after: put-begin
205    :end-before: put-end
206    :dedent: 4
207
208
209 =====================
210 Activities Life cycle
211 =====================
212
213 Sometimes, you want to change the setting of an activity before it even starts. 
214
215 .. todo:: write this section
216
217 .. _s4u_mailbox:
218           
219 Mailboxes
220 *********
221
222 Please also refer to the :ref:`API reference for s4u::Mailbox
223 <API_s4u_Mailbox>`.
224
225 ===================
226 What are Mailboxes?
227 ===================
228
229 |API_s4u_Mailboxes|_ are rendez-vous points for network communications,
230 similar to URLs on which you could post and retrieve data. Actually,
231 the mailboxes are not involved in the communication once it starts,
232 but only to find the contact with which you want to communicate.
233
234 They are similar to many common things: The phone number, which allows
235 the caller to find the receiver. The twitter hashtag, which help
236 senders and receivers to find each others. In TCP, the pair 
237 ``{host name, host port}`` to which you can connect to find your peer.
238 In HTTP, URLs through which the clients can connect to the servers. 
239 In ZeroMQ, the queues are used to match senders and receivers.
240
241 One big difference with most of these systems is that no actor is the
242 exclusive owner of a mailbox, neither in sending nor in receiving.
243 Many actors can send into and/or receive from the same mailbox.  TCP
244 socket ports for example are shared on the sender side but exclusive
245 on the receiver side (only one process can receive from a given socket
246 at a given point of time).
247
248 A big difference with TCP sockets or MPI communications is that
249 communications do not start right away after a
250 :cpp:func:`Mailbox::put() <simgrid::s4u::Mailbox::put()>`, but wait
251 for the corresponding :cpp:func:`Mailbox::get() <simgrid::s4u::Mailbox::get()>`.
252 You can change this by :ref:`declaring a receiving actor <s4u_receiving_actor>`.
253
254 A big difference with twitter hashtags is that SimGrid does not
255 offer easy support to broadcast a given message to many
256 receivers. So that would be like a twitter tag where each message
257 is consumed by the first receiver.
258
259 A big difference with the ZeroMQ queues is that you cannot filter
260 on the data you want to get from the mailbox. To model such settings
261 in SimGrid, you'd have one mailbox per potential topic, and subscribe
262 to each topic individually with a 
263 :cpp:func:`get_async() <simgrid::s4u::Mailbox::get_async()>` on each mailbox.
264 Then, use :cpp:func:`Comm::wait_any() <simgrid::s4u::Comm::wait_any()>` 
265 to get the first message on any of the mailbox you are subscribed onto.
266
267 The mailboxes are not located on the network, and you can access
268 them without any latency. The network delay are only related to the
269 location of the sender and receiver once the match between them is
270 done on the mailbox. This is just like the phone number that you
271 can use locally, and the geographical distance only comes into play
272 once you start the communication by dialing this number.
273
274 =====================
275 How to use Mailboxes?
276 =====================
277
278 You can retrieve any existing mailbox from its name (which is a
279 unique string, just like a twitter tag). This results in a
280 versatile mechanism that can be used to build many different
281 situations.
282
283 To model classical socket communications, use "hostname:port" as
284 mailbox names, and make sure that only one actor reads into a given
285 mailbox. This does not make it easy to build a perfectly realistic
286 model of the TCP sockets, but in most cases, this system is too
287 cumbersome for your simulations anyway. You probably want something
288 simpler, that turns our to be easy to build with the mailboxes.
289
290 Many SimGrid examples use a sort of yellow page system where the
291 mailbox names are the name of the service (such as "worker",
292 "master" or "reducer"). That way, you don't have to know where your
293 peer is located to contact it. You don't even need its name. Its
294 function is enough for that. This also gives you some sort of load
295 balancing for free if more than one actor pulls from the mailbox:
296 the first actor that can deal with the request will handle it.
297
298 =========================================
299 How are put() and get() requests matched?
300 =========================================
301
302 The matching algorithm simple: first come, first serve. When a new
303 send arrives, it matches the oldest enqueued receive. If no receive is
304 currently enqueued, then the incoming send is enqueued. As you can
305 see, the mailbox cannot contain both send and receive requests: all
306 enqueued requests must be of the same sort.
307
308 .. _s4u_receiving_actor:
309
310 ===========================
311 Declaring a Receiving Actor
312 ===========================
313
314 The last twist is that by default in the simulator, the data starts
315 to be exchanged only when both the sender and the receiver are
316 announced (it waits until both :cpp:func:`put() <simgrid::s4u::Mailbox::put()>`
317 and :cpp:func:`get() <simgrid::s4u::Mailbox::get()>` are posted). 
318 In TCP, since you establish connections beforehand, the data starts to
319 flow as soon as the sender posts it, even if the receiver did not post
320 its :cpp:func:`recv() <simgrid::s4u::Mailbox::recv()>` yet. 
321
322 To model this in SimGrid, you can declare a specific receiver to a
323 given mailbox (with the function 
324 :cpp:func:`set_receiver() <simgrid::s4u::Mailbox::set_receiver()>`). 
325 That way, any :cpp:func:`put() <simgrid::s4u::Mailbox::put()>`
326 posted to that mailbox will start as soon as possible, and the data
327 will already be there on the receiver host when the receiver actor
328 posts its :cpp:func:`get() <simgrid::s4u::Mailbox::get()>`
329
330 Note that being permanent receivers of a mailbox prevents actors to be
331 garbage-collected. If your simulation creates many short-lived actors
332 that marked as permanent receiver, you should call
333 ``mailbox->set_receiver(nullptr)`` by the end of the actors so that their
334 memory gets properly reclaimed. This call should be at the end of the
335 actor's function, not in a on_exit callback.
336
337 Memory Management
338 *****************
339
340 For sake of simplicity, we use `RAII
341 <https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization>`_
342 for many classes in S4U. This is an idiom where resources are automatically
343 managed through the context. Provided that you never manipulate
344 objects of type Foo directly but always FooPtr references (which are
345 defined as `boost::intrusive_ptr
346 <http://www.boost.org/doc/libs/1_61_0/libs/smart_ptr/intrusive_ptr.html>`_
347 <Foo>), you will never have to explicitly release the resource that
348 you use nor to free the memory of unused objects.
349 Here is a little example:
350
351 .. code-block:: cpp
352
353    void myFunc() 
354    {
355      simgrid::s4u::MutexPtr mutex = simgrid::s4u::Mutex::create(); // Too bad we cannot use `new`
356
357      mutex->lock();   // use the mutex as a simple reference
358      //  bla bla
359      mutex->unlock(); 
360   
361    } // The mutex gets automatically freed because the only existing reference gets out of scope
362
363 Note that Mailboxes, Hosts and Links are not handled thought smart
364 pointers (yet?). This means that it is currently impossible to destroy a
365 mailbox or a link. You can still destroy an host (but probably
366 shouldn't), using :cpp:func:`simgrid::s4u::Host::destroy`.
367
368 .. THE EXAMPLES
369
370 .. include:: ../../examples/README.rst
371
372 C++ API Reference
373 *****************
374
375 .. _API_s4u_this_actor:
376
377 =========================
378 namespace s4u::this_actor
379 =========================
380
381 .. doxygennamespace:: simgrid::s4u::this_actor
382 .. _API_s4u_Activity:
383
384 =============
385 s4u::Activity
386 =============
387
388 .. doxygenclass:: simgrid::s4u::Activity
389    :members:
390    :protected-members:
391    :undoc-members:
392
393 .. _API_s4u_Actor:
394
395 ==========
396 s4u::Actor
397 ==========
398
399 .. doxygentypedef:: ActorPtr
400
401 .. doxygentypedef:: aid_t
402
403 .. doxygenclass:: simgrid::s4u::Actor
404    :members:
405    :protected-members:
406    :undoc-members:
407
408 .. _API_s4u_Barrier:
409
410 ============
411 s4u::Barrier
412 ============
413
414 .. doxygentypedef:: BarrierPtr
415
416 .. doxygenclass:: simgrid::s4u::Barrier
417    :members:
418    :protected-members:
419    :undoc-members:
420
421 .. _API_s4u_Comm:
422
423 =========
424 s4u::Comm
425 =========
426
427 .. doxygentypedef:: CommPtr
428
429 .. doxygenclass:: simgrid::s4u::Comm
430    :members:
431    :protected-members:
432    :undoc-members:
433
434 .. _API_s4u_ConditionVariable:
435
436 ======================
437 s4u::ConditionVariable
438 ======================
439
440 .. doxygentypedef:: ConditionVariablePtr
441
442 .. doxygenclass:: simgrid::s4u::ConditionVariable
443    :members:
444    :protected-members:
445    :undoc-members:
446
447 .. _API_s4u_Disk:
448
449 ============
450 s4u::Disk
451 ============
452
453 .. doxygenclass:: simgrid::s4u::Disk
454    :members:
455    :protected-members:
456    :undoc-members:
457
458 .. _API_s4u_Engine:
459
460 ===========
461 s4u::Engine
462 ===========
463
464 .. doxygenclass:: simgrid::s4u::Engine
465    :members:
466    :protected-members:
467    :undoc-members:
468
469 .. _API_s4u_Exec:
470
471 =========
472 s4u::Exec
473 =========
474
475 .. doxygentypedef:: ExecPtr
476
477 .. doxygenclass:: simgrid::s4u::Exec
478    :members:
479    :protected-members:
480    :undoc-members:
481
482 .. _API_s4u_ExecSeq:
483
484 ============
485 s4u::ExecSeq
486 ============
487
488 .. doxygentypedef:: ExecSeqPtr
489
490 .. doxygenclass:: simgrid::s4u::ExecSeq
491    :members:
492    :protected-members:
493    :undoc-members:
494
495 .. _API_s4u_ExecPar:
496
497 ============
498 s4u::ExecPar
499 ============
500
501 .. doxygentypedef:: ExecParPtr
502
503 .. doxygenclass:: simgrid::s4u::ExecPar
504    :members:
505    :protected-members:
506    :undoc-members:
507
508 .. _API_s4u_Host:
509
510 =========
511 s4u::Host
512 =========
513
514 .. doxygenclass:: simgrid::s4u::Host
515    :members:
516    :protected-members:
517    :undoc-members:
518
519 .. _API_s4u_Io:
520
521 =======
522 s4u::Io
523 =======
524
525 .. doxygentypedef:: IoPtr
526
527 .. doxygenclass:: simgrid::s4u::Io
528    :members:
529    :protected-members:
530    :undoc-members:
531
532 .. _API_s4u_Link:
533
534 =========
535 s4u::Link
536 =========
537
538 .. doxygenclass:: simgrid::s4u::Link
539    :members:
540    :protected-members:
541    :undoc-members:
542
543 .. _API_s4u_Mailbox:
544
545 ============
546 s4u::Mailbox
547 ============
548
549 Please also refer to the :ref:`full doc on s4u::Mailbox <s4u_mailbox>`.
550
551 .. doxygenclass:: simgrid::s4u::Mailbox
552    :members:
553    :protected-members:
554    :undoc-members:
555
556 .. _API_s4u_Mutex:
557
558 ==========
559 s4u::Mutex
560 ==========
561
562 .. doxygentypedef:: MutexPtr
563
564 .. doxygenclass:: simgrid::s4u::Mutex
565    :members:
566    :protected-members:
567    :undoc-members:
568
569 .. _API_s4u_NetZone:
570
571 ============
572 s4u::NetZone
573 ============
574
575 .. doxygenclass:: simgrid::s4u::NetZone
576    :members:
577    :protected-members:
578    :undoc-members:
579
580 .. _API_s4u_Semaphore:
581
582 ==============
583 s4u::Semaphore
584 ==============
585
586 .. doxygentypedef:: SemaphorePtr
587
588 .. doxygenclass:: simgrid::s4u::Semaphore
589    :members:
590    :protected-members:
591    :undoc-members:
592
593 .. _API_s4u_VirtualMachine:
594
595 ===================
596 s4u::VirtualMachine
597 ===================
598
599 .. doxygenclass:: simgrid::s4u::VirtualMachine
600    :members:
601    :protected-members:
602    :undoc-members:
603
604 C API Reference
605 ***************
606
607 ==============
608 Main functions
609 ==============
610
611 .. doxygenfunction:: simgrid_init
612 .. doxygenfunction:: simgrid_get_clock
613 .. doxygenfunction:: simgrid_load_deployment
614 .. doxygenfunction:: simgrid_load_platform
615 .. doxygenfunction:: simgrid_register_default
616 .. doxygenfunction:: simgrid_register_function
617 .. doxygenfunction:: simgrid_run
618
619 ==================
620 Condition Variable
621 ==================
622
623 See also the :ref:`C++ API <API_s4u_ConditionVariable>`.
624
625 .. doxygenfunction:: sg_cond_init
626 .. doxygenfunction:: sg_cond_notify_all
627 .. doxygenfunction:: sg_cond_notify_one
628 .. doxygenfunction:: sg_cond_wait
629 .. doxygenfunction:: sg_cond_wait_for
630
631 Python API Reference
632 ********************
633
634 The Python API is automatically generated with pybind11. It closely mimicks the C++
635 API, to which you should refer for more information.
636
637 ==========
638 this_actor
639 ==========
640
641 .. automodule:: simgrid.this_actor
642    :members:
643
644 ===========
645 Class Actor
646 ===========
647
648 .. autoclass:: simgrid.Actor
649    :members:
650
651 ==========
652 Class Comm
653 ==========
654
655 .. autoclass:: simgrid.Comm
656    :members:
657
658 ============
659 Class Engine
660 ============
661
662 .. autoclass:: simgrid.Engine
663    :members:
664
665 ==========
666 Class Exec
667 ==========
668
669 .. autoclass:: simgrid.Exec
670    :members:
671
672 ==========
673 Class Host
674 ==========
675
676 .. autoclass:: simgrid.Host
677    :members:
678
679 =============
680 Class Mailbox
681 =============
682
683 .. autoclass:: simgrid.Mailbox
684    :members: