Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b61246159ebf405cf968737a07c1911de71950a2
[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
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