Logo AND Algorithmique Numérique Distribuée

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