Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Mainly cosmetics. I stop for now working on doc.
[simgrid.git] / doc / user_guide / doxygen / use.doc
1 /*! \page use Using SimGrid
2
3 SimGrid comes with many examples provided in the examples/ directory. Those examples are described in section \ref MSG_examples . Those examples are commented and should be easy to understand. for a first step into SimGird we also provide some more detailed examples in the sections below.
4
5 \section using_msg Using MSG
6
7 Here are some examples on how to use MSG, the most used API.
8
9
10 MSG comes with an extensive set of examples. It is sometimes difficult
11 to find the one you need. This list aims at helping you finding the
12 example from which you can learn what you want to.
13
14 \subsection MSG_ex_basics Basic examples and features
15
16 \subsubsection MSG_ex_asynchronous_communications Asynchronous communications
17
18
19 Simulation of asynchronous communications between a sender and a receiver using a realistic platform and
20 an external description of the deployment.
21
22  - \ref MSG_ext_icomms_code
23    - \ref MSG_ext_icomms_preliminary
24    - \ref MSG_ext_icomms_Sender
25    - \ref MSG_ext_icomms_Receiver
26    - \ref MSG_ext_icomms_core
27    - \ref MSG_ext_icomms_Main
28  - \ref MSG_ext_icomms_fct_Waitall
29  - \ref MSG_ext_icomms_fct_Waitany
30
31 <hr>
32
33 \dontinclude msg/icomms/peer.c
34
35 \paragraph MSG_ext_icomms_code Code of the application
36
37 \paragraph MSG_ext_icomms_preliminary Preliminary declarations
38 \skip include
39 \until Sender function
40
41 \paragraph MSG_ext_icomms_Sender Sender function
42
43 The sender send to a receiver an asynchronous message with the function "MSG_task_isend()". Cause this function is non-blocking
44 we have to make "MSG_comm_test()" to know   if the communication is finished for finally destroy it with function "MSG_comm_destroy()".
45 It also available to "make MSG_comm_wait()" which make both of them.
46
47   C style arguments (argc/argv) are interpreted as:
48    - the number of tasks to distribute
49    - the computation size of each task
50    - the size of the files associated to each task
51    - a list of host that will accept those tasks.
52    - the time to sleep at the beginning of the function
53    - This time defined the process sleep time
54                         if time = 0 use of MSG_comm_wait()
55                         if time > 0 use of MSG_comm_test()
56
57
58 \until Receiver function
59
60 \paragraph MSG_ext_icomms_Receiver Receiver function
61
62 This function executes tasks when it receives them. As the receiving is asynchronous we have to test the communication to know
63 if it is completed or not with "MSG_comm_test()" or wait for the completion "MSG_comm_wait()".
64
65   C style arguments (argc/argv) are interpreted as:
66    - the id to use for received the communication.
67    - the time to sleep at the beginning of the function
68    - This time defined the process sleep time
69                         if time = 0 use of MSG_comm_wait()
70                         if time > 0 use of MSG_comm_test()
71
72 \until Test function
73
74 \paragraph MSG_ext_icomms_core Simulation core
75
76   This function is the core of the simulation and is divided only into 3 parts
77   thanks to MSG_create_environment() and MSG_launch_application().
78      -# Simulation settings : MSG_create_environment() creates a realistic
79         environment
80      -# Application deployment : create the processes on the right locations with
81         MSG_launch_application()
82      -# The simulation is run with #MSG_main()
83
84   Its arguments are:
85         - <i>platform_file</i>: the name of a file containing an valid surfxml platform description.
86         - <i>application_file</i>: the name of a file containing a valid surfxml application description
87
88 \until Main function
89
90 \paragraph MSG_ext_icomms_Main Main function
91
92 This initializes MSG, runs a simulation, and free all data-structures created by MSG.
93
94 \until end_of_main
95
96 \dontinclude msg/icomms/peer2.c
97
98 \paragraph MSG_ext_icomms_fct_Waitall Waitall function for sender
99
100 The use of this function permit to send all messages and wait for the completion of all in one time.
101
102 \skipline Sender function
103 \until end_of_sender
104
105 \paragraph MSG_ext_icomms_fct_Waitany Waitany function
106
107 The MSG_comm_waitany() function return the place of the first message send or receive from a xbt_dynar_t table.
108
109 \paragraph MSG_ext_icomms_fct_Waitany_sender From a sender
110 We can use this function to wait all sent messages.
111 \dontinclude msg/icomms/peer3.c
112 \skipline Sender function
113 \until end_of_sender
114
115 \paragraph MSG_ext_icomms_fct_Waitany_receiver From a receiver
116 We can also wait for the arrival of all messages.
117 \dontinclude msg/icomms/peer3.c
118 \skipline Receiver function
119 \until end_of_receiver
120
121 \subsubsection MSG_ex_master_slave Basic Master/Slaves
122
123 Simulation of a master-slave application using a realistic platform
124 and an external description of the deployment.
125
126 \paragraph MSG_ex_ms_TOC Table of contents:
127
128    - \ref MSG_ext_ms_preliminary
129    - \ref MSG_ext_ms_master
130    - \ref MSG_ext_ms_slave
131    - \ref MSG_ext_ms_forwarder
132    - \ref MSG_ext_ms_core
133    - \ref MSG_ext_ms_main
134    - \ref MSG_ext_ms_helping
135    - \ref MSG_ext_ms_application
136    - \ref MSG_ext_ms_platform
137
138 <hr>
139
140 \dontinclude msg/masterslave/masterslave_forwarder.c
141
142
143 \paragraph MSG_ext_ms_preliminary Preliminary declarations
144
145 \skip include
146 \until printf
147 \until }
148
149 \paragraph MSG_ext_ms_master Master code
150
151 This function has to be assigned to a m_process_t that will behave as
152 the master. It should not be called directly but either given as a
153 parameter to #MSG_process_create() or registered as a public function
154 through #MSG_function_register() and then automatically assigned to a
155 process through #MSG_launch_application().
156
157 C style arguments (argc/argv) are interpreted as:
158    - the number of tasks to distribute
159    - the computation size of each task
160    - the size of the files associated to each task
161    - a list of host that will accept those tasks.
162
163 Tasks are dumbly sent in a round-robin style.
164
165 \until end_of_master
166
167 \paragraph MSG_ext_ms_slave Slave code
168
169 This function has to be assigned to a #msg_process_t that has to behave
170 as a slave. Just like the master fuction (described in \ref
171 MSG_ext_ms_master), it should not be called directly.
172
173 This function keeps waiting for tasks and executes them as it receives them.
174
175 \until end_of_slave
176
177 \paragraph MSG_ext_ms_forwarder Forwarder code
178
179 This function has to be assigned to a #msg_process_t that has to behave
180 as a forwarder. Just like the master function (described in \ref
181 MSG_ext_ms_master), it should not be called directly.
182
183 C style arguments (argc/argv) are interpreted as a list of host that
184 will accept those tasks.
185
186 This function keeps waiting for tasks and dispathes them to its slaves.
187
188 \until end_of_forwarder
189
190 \paragraph MSG_ext_ms_core Simulation core
191
192 This function is the core of the simulation and is divided only into 3 parts
193 thanks to MSG_create_environment() and MSG_launch_application().
194    -# Simulation settings : MSG_create_environment() creates a realistic
195       environment
196    -# Application deployment : create the processes on the right locations with
197       MSG_launch_application()
198    -# The simulation is run with #MSG_main()
199
200 Its arguments are:
201         - <i>platform_file</i>: the name of a file containing an valid surfxml platform description.
202         - <i>application_file</i>: the name of a file containing a valid surfxml application description
203
204 \until end_of_test_all
205
206 \paragraph MSG_ext_ms_main Main() function
207
208 This initializes MSG, runs a simulation, and free all data-structures created by MSG.
209
210 \until end_of_main
211
212 \subsubsection MSG_ext_ms_helping Helping files
213
214 \paragraph MSG_ext_ms_application Example of application file
215
216 \include msg/masterslave/deployment_masterslave.xml
217
218 \paragraph MSG_ext_ms_platform Example of platform file
219
220 \include msg/small_platform.xml
221
222 \section using_gras Using GRAS
223
224 Here are some examples on how to use GRAS.
225
226
227     There is for now rather few examples of GRAS, but it's better than
228     nothing, isn't it?
229
230        - \ref GRAS_ex_ping
231        - \ref GRAS_ex_mmrpc
232        - \ref GRAS_ex_token
233        - \ref GRAS_ex_timer
234
235
236 \subsection GRAS_ex_ping Ping-Pong
237
238     This example implements the very classical ping-pong in GRAS. It
239     involves a client (initiating the ping-pong) and a server (answering to
240     client's requests).
241
242     It works the following way:
243      - Both the client and the server register all needed messages
244      - The server registers a callback to the ping message, which sends pong
245        to the expeditor
246      - The client sends the ping message to the server, and waits for the
247        pong message as an answer.
248
249     This example resides in the <b>examples/gras/ping/ping.c</b> file. Yes, both
250     the code of the client and of the server is placed in the same file. 
251
252     \subsubsection GRAS_ex_ping_toc Table of contents of the ping example
253       - \ref GRAS_ex_ping_common
254         - \ref GRAS_ex_ping_initial
255         - \ref GRAS_ex_ping_register
256       - \ref GRAS_ex_ping_server
257         - \ref GRAS_ex_ping_serdata
258         - \ref GRAS_ex_ping_sercb
259         - \ref GRAS_ex_ping_sermain
260       - \ref GRAS_ex_ping_client
261         - \ref GRAS_ex_ping_climain
262
263     <hr>
264
265     \dontinclude gras/ping/ping_common.c
266
267     \subsubsection GRAS_ex_ping_common 1) Common code to the client and the server
268
269     \paragraph GRAS_ex_ping_initial 1.a) Initial settings
270
271     Let's first load the module header and declare a logging category (see
272     \ref XBT_log for more info on logging).
273
274     \skip include
275     \until XBT_LOG
276
277     The module header <tt>ping.h</tt> reads:
278
279     \dontinclude gras/ping/ping.h
280     \skip include
281     \until argv
282     \until argv
283
284     \paragraph GRAS_ex_ping_register 1.b) Register the messages
285
286     This function, called by both the client and the server is in charge of
287     declaring the existing messages to GRAS. Since the payload does not
288     involve any newly created types but only int, this is quite easy.
289     (to exchange more complicated types, see \ref GRAS_dd or
290     \ref GRAS_ex_mmrpc for an example).
291
292     \dontinclude gras/ping/ping_common.c
293     \skip register_messages
294     \until }
295
296     [Back to \ref GRAS_ex_ping_toc]
297
298     \subsubsection GRAS_ex_ping_server 2) Server's code
299
300     \paragraph GRAS_ex_ping_serdata 2.a) The server's globals
301
302     In order to ensure the communication between the "main" and the callback
303     of the server, we need to declare some globals. We have to put them in a
304     struct definition so that they can be handled properly in GRAS.
305
306     \dontinclude gras/ping/ping_server.c
307     \skip typedef struct
308     \until }
309
310     \paragraph GRAS_ex_ping_sercb 2.b) The callback to the ping message
311
312     Here is the callback run when the server receives any ping message (this
313     will be registered later by the server).
314
315     \skip server_cb_ping_handler
316     \until end_of_server_cb_ping_handler
317
318     \paragraph GRAS_ex_ping_sermain 2.c) The "main" of the server
319
320     This is the "main" of the server. You must not write any main()
321     function yourself. Instead, you just have to write a regular function
322     like this one which will act as a main.
323
324     \skip server
325     \until end_of_server
326
327     [Back to \ref GRAS_ex_ping_toc]
328
329     \subsubsection GRAS_ex_ping_client 3) Client's code
330
331     \paragraph GRAS_ex_ping_climain 3.a) Client's "main" function
332
333     This function is quite straightforward, and the inlined comments should
334     be enough to understand it.
335
336     \dontinclude gras/ping/ping_client.c
337     \skip client
338     \until end_of_client
339
340     [Back to \ref GRAS_ex_ping_toc]
341
342 \subsection GRAS_ex_token Token Ring example
343
344    This example implements the token ring algorithm. It involves several
345    nodes arranged in a ring (each of them have a left and a right neighbour)
346    and exchanging a "token". This algorithm is one of the solution to ensure
347    the mutual exclusion between distributed processes. There is only one
348    token at any time, so the process in its possession is ensured to be the
349    only one having it. So, if there is an action you want all processes to
350    do alternativly, but you cannot afford to have two processes doing it at
351    the same time, let the process having the token doing it.
352
353    Actually, there is a lot of different token ring algorithms in the
354    litterature, so this example implements one of them: the simplest one.
355    The ring is static (no new node can join it, and you'll get trouble if
356    one node dies or leaves), and nothing is done for the case in which the
357    token is lost.
358
359    - \ref GRAS_ex_stoken_deploy
360    - \ref GRAS_ex_stoken_global
361    - \ref GRAS_ex_stoken_callback
362    - \ref GRAS_ex_stoken_main
363
364    \subsection GRAS_ex_stoken_deploy 1) Deployment file
365
366    Here is the deployment file:
367    \include examples/gras/mutual_exclusion/simple_token/simple_token.xml
368
369    The neighbour of each node is given at startup as command line argument.
370    Moreover, one of the nodes is instructed by a specific argument (the one
371    on Tremblay here) to create the token at the begining of the algorithm.
372
373    \subsection GRAS_ex_stoken_global 2) Global definition
374
375    The token is incarned by a specific message, which circulates from node
376    to node (the payload is an integer incremented at each hop). So, the most
377    important part of the code is the message callback, which forwards the
378    message to the next node. That is why we have to store all variable in a
379    global, as explained in the \ref GRAS_globals section.
380
381    \dontinclude examples/gras/mutual_exclusion/simple_token/simple_token.c
382    \skip typedef
383    \until }
384
385    \subsection GRAS_ex_stoken_callback 3) The callback
386
387    Even if this is the core of this algorithm, this function is quite
388    straightforward.
389
390    \skip node_cb_stoken_handler
391    \until end_of_node_cb_stoken_handler
392
393    \subsection GRAS_ex_stoken_main 4) The main function
394
395    This function is splited in two parts: The first one performs all the
396    needed initialisations (points 1-7) while the end (point 8. below) calls
397    gras_msg_handle() as long as the planned amount of ring loops are not
398    performed.
399
400    \skip node
401    \until end_of_node
402
403 \subsection GRAS_ex_mmrpc A simple RPC for matrix multiplication
404
405     This example implements a remote matrix multiplication. It involves a client
406     (creating the matrices and sending the multiplications requests) and a server
407     (computing the multiplication on client's behalf).
408
409     This example also constitutes a more advanced example of data description
410     mechanisms, since the message payload type is a bit more complicated than in
411     other examples such as the ping one (\ref GRAS_ex_ping).
412
413     It works the following way (not very different from the ping example):
414      - Both the client and the server register all needed messages and datatypes
415      - The server registers a callback to the "request" message, which computes
416        what needs to be and returns the result to the expeditor.
417      - The client creates two matrices, ask for their multiplication and check
418        the server's answer.
419
420     This example resides in the <b>examples/gras/mmrpc/mmrpc.c</b> file. 
421
422     \subsubsection GRAS_ex_mmrpc_toc Table of contents of the mmrpc example
423       - \ref GRAS_ex_mmrpc_common
424         - \ref GRAS_ex_mmrpc_header
425         - \ref GRAS_ex_mmrpc_dataregister
426         - \ref GRAS_ex_mmrpc_logdef
427         - \ref GRAS_ex_mmrpc_msgregister
428       - \ref GRAS_ex_mmrpc_server
429         - \ref GRAS_ex_mmrpc_serinc
430         - \ref GRAS_ex_mmrpc_sercb
431         - \ref GRAS_ex_mmrpc_sermain
432       - \ref GRAS_ex_mmrpc_client
433         - \ref GRAS_ex_mmrpc_cliinc
434         - \ref GRAS_ex_mmrpc_climain
435
436     <hr>
437
438
439     \subsubsection GRAS_ex_mmrpc_common 1) Common code to the client and the server (mmrpc_common.c and mmrpc.h)
440
441
442     \paragraph GRAS_ex_mmrpc_header 1.a) Module header (mmrpc.h)
443
444     This loads the gras header and declare the function's prototypes as well
445     as the matrix size.
446
447     \dontinclude gras/mmrpc/mmrpc.h
448     \skip include
449     \until argv
450     \until argv
451
452     \paragraph GRAS_ex_mmrpc_dataregister 1.b) Register the data types (mmrpc.h)
453
454     The messages involved in a matrix of double. This type is automatically
455     known by the GRAS mecanism, using the gras_datadesc_matrix() function of the
456     xbt/matrix module.
457
458     \paragraph GRAS_ex_mmrpc_logdef 1.c) Logging category definition (mmrpc_common.c)
459
460     Let's first load the module header and declare a logging category (see
461     \ref XBT_log for more info on logging). This logging category does live
462     in this file (ie the required symbols are defined here and declared as
463     "extern" in any other file using them). That is why we use
464     \ref XBT_LOG_NEW_DEFAULT_CATEGORY here and
465     \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY in mmrpc_client.c and mmrpc_server.c.
466
467     \dontinclude gras/mmrpc/mmrpc_common.c
468     \skip include
469     \until XBT_LOG
470
471     \paragraph GRAS_ex_mmrpc_msgregister 1.d) Register the messages (mmrpc_common.c)
472
473     This function, called by both the client and the server is in charge of
474     declaring the existing messages to GRAS.
475
476     The datatype description builded that way can then be used to build an array datatype or
477     to declare messages.
478
479     \skip register_messages
480     \until }
481
482     [Back to \ref GRAS_ex_mmrpc_toc]
483
484     \subsubsection GRAS_ex_mmrpc_server 2) Server's code (mmrpc_server.c)
485
486     \paragraph GRAS_ex_mmrpc_serinc 2.a) Server intial settings
487
488     All module symbols live in the mmrpc_common.c file. We thus have to
489     define \ref XBT_DEFINE_TYPE_EXTERN to the preprocessor so that the
490     \ref XBT_DEFINE_TYPE symbols don't get included here. Likewise, we use
491     \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY to get the log category in here.
492
493     \dontinclude gras/mmrpc/mmrpc_server.c
494     \skip define
495     \until XBT_LOG
496
497     \paragraph GRAS_ex_mmrpc_sercb 2.b) The callback to the mmrpc message
498
499     Here is the callback run when the server receives any mmrpc message (this
500     will be registered later by the server). Note the way we get the message
501     payload. In the ping example, there was one additional level of pointer
502     indirection (see \ref GRAS_ex_ping_sercb). This is because the payload is
503     an array here (ie a pointer) whereas it is a scalar in the ping example.
504
505     \skip server_cb_request_handler
506     \until end_of_server_cb_request_handler
507
508     \paragraph GRAS_ex_mmrpc_sermain 2.c) The "main" of the server
509
510     This is the "main" of the server. You must not write any main()
511     function yourself. Instead, you just have to write a regular function
512     like this one which will act as a main.
513
514     \skip server
515     \until end_of_server
516
517     [Back to \ref GRAS_ex_mmrpc_toc]
518
519     \subsubsection GRAS_ex_mmrpc_client 3) Client's code (mmrpc_client.c)
520
521     \paragraph GRAS_ex_mmrpc_cliinc 2.a) Server intial settings
522
523     As for the server, some extra love is needed to make sure that automatic
524     datatype parsing and log categories do work even if we are using several
525     files.
526
527     \dontinclude gras/mmrpc/mmrpc_client.c
528     \skip define
529     \until XBT_LOG
530
531     \paragraph GRAS_ex_mmrpc_climain 3.b) Client's "main" function
532
533     This function is quite straightforward, and the inlined comments should
534     be enough to understand it.
535
536     \dontinclude gras/mmrpc/mmrpc_client.c
537     \skip argv
538     \until end_of_client
539
540     [Back to \ref GRAS_ex_mmrpc_toc]
541
542 \subsection GRAS_ex_timer Some timer games
543
544     This example fools around with the GRAS timers (\ref GRAS_timer). It is
545     mainly a regression test, since it uses almost all timer features.
546
547     The main program registers a repetititive task and a delayed one, and
548     then loops until the <tt>still_to_do</tt> variables of its globals reach
549     0. The delayed task set it to 5, and the repetititive one decrease it
550     each time. Here is an example of output:
551 \verbatim Initialize GRAS
552  Initialize XBT
553  [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
554  [1108335471] Programming the delayed_action for after 2.000000 sec
555  [1108335471] Have a rest
556  [1108335472] Canceling the delayed_action.
557  [1108335472] Re-programming the delayed_action for after 2.000000 sec
558  [1108335472] Repetitive_action has nothing to do yet
559  [1108335473] Repetitive_action has nothing to do yet
560  [1108335473] delayed_action setting globals->still_to_do to 5
561  [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
562  [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
563  [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
564  [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
565  [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
566  Exiting GRAS\endverbatim
567
568     Source code:
569      - \ref GRAS_ex_timer_decl
570      - \ref GRAS_ex_timer_delay
571      - \ref GRAS_ex_timer_repeat
572      - \ref GRAS_ex_timer_main
573
574     \dontinclude timer.c
575
576     \subsubsection GRAS_ex_timer_decl   1. Declarations and headers
577     \skip include
578     \until my_globals
579
580     \subsubsection GRAS_ex_timer_delay  2. Source code of the delayed action
581     \skip repetitive_action
582     \until end_of_repetitive_action
583
584     \subsubsection GRAS_ex_timer_repeat 3. Source code of the repetitive action
585     \skip delayed_action
586     \until end_of_delayed_action
587
588     \subsubsection GRAS_ex_timer_main   4. Source code of main function
589     \skip client
590     \until end_of_client
591
592
593 */
594
595