Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initial commit for split doc (in order to let Pierre play with it). Please do not...
[simgrid.git] / doc / shared / doxygen / gras-examples.doc
1 /**
2 @defgroup GRAS_ex GRAS Examples
3 @ingroup GRAS_User_Guide
4     
5
6 */
7
8 /**
9 \section GRAS_examples Examples
10
11     There is for now rather few examples of GRAS, but it's better than
12     nothing, isn't it?
13
14        - \ref GRAS_ex_ping
15        - \ref GRAS_ex_mmrpc
16        - \ref GRAS_ex_token
17        - \ref GRAS_ex_timer
18
19      The initiatic tour of the tutorial also contains several examples. The
20      most proeminent one is:
21
22        - \ref GRAS_tut_tour_explicitwait_use
23
24     \section GRAS_tut_presentation Tutorial
25
26     We even have a tutorial for the GRAS framework. It details in a
27     hopefully pedagogic order all the points of the API, along with example
28     of use for each of them. Unfortunately, it is not finished yet (the main
29     part missing is the one on how to describe data). Here is the table of
30     content:
31
32        - \ref GRAS_tut_intro
33          - \ref GRAS_tut_intro_what
34          - \ref GRAS_tut_intro_model
35        - \ref GRAS_tut_tour
36          - \ref GRAS_tut_tour_install
37          - \ref GRAS_tut_tour_setup
38          - \ref GRAS_tut_tour_simpleexchange
39          - \ref GRAS_tut_tour_args
40          - \ref GRAS_tut_tour_callbacks
41          - \ref GRAS_tut_tour_globals
42          - \ref GRAS_tut_tour_logs
43          - \ref GRAS_tut_tour_timers
44          - \ref GRAS_tut_tour_exceptions
45          - \ref GRAS_tut_tour_rpc
46          - \ref GRAS_tut_tour_explicitwait
47          - \ref GRAS_tut_tour_message_recaping
48
49     \section GRAS_howto_presentation HOWTOsbis
50
51     The tutorial and the API documentation present the framework little
52     piece by little piece and provide a lot of information on each of them.
53     Quite orthogonally to this, the HOWTOs try to present transversal
54     aspects of the framework to give you some broader point of view on it.
55     How infortunate it is that only one such HOWTO exist for now...
56
57        - \ref GRAS_howto
58          - \ref GRAS_howto_design
59
60    @{ */ 
61        /** @defgroup GRAS_ex      Examples */
62        /** @defgroup GRAS_tut     Tutorial */
63 /** @} */
64
65
66 #####################################################################
67 /** @addtogroup GRAS_ex
68
69     There is for now rather few examples of GRAS, but it's better than
70     nothing, isn't it?
71
72        - \ref GRAS_ex_ping
73        - \ref GRAS_ex_mmrpc
74        - \ref GRAS_ex_token
75        - \ref GRAS_ex_timer
76
77      The initiatic tour of the tutorial also contains several examples. The
78      most proeminent one is:
79
80        - \ref GRAS_tut_tour_explicitwait_use
81
82   There is some more examples in the distribution, under the directory
83   <tt>examples/gras</tt>.
84 */
85
86 #####################################################################
87 #########################  EXAMPLES #################################
88 #####################################################################
89 ---------------------------------------------------------------------
90 ------------------------- Ping Pong ---------------------------------
91 ---------------------------------------------------------------------
92 /** @defgroup GRAS_ex_ping Ping-Pong
93     @ingroup GRAS_ex
94
95     This example implements the very classical ping-pong in GRAS. It
96     involves a client (initiating the ping-pong) and a server (answering to
97     client's requests).
98
99     It works the following way:
100      - Both the client and the server register all needed messages
101      - The server registers a callback to the ping message, which sends pong
102        to the expeditor
103      - The client sends the ping message to the server, and waits for the
104        pong message as an answer.
105
106     This example resides in the <b>examples/gras/ping/ping.c</b> file. Yes, both
107     the code of the client and of the server is placed in the same file. See
108     the \ref GRAS_tut_tour_setup of the tutorial if wondering.
109
110     \section GRAS_ex_ping_toc Table of contents of the ping example
111       - \ref GRAS_ex_ping_common
112         - \ref GRAS_ex_ping_initial
113         - \ref GRAS_ex_ping_register
114       - \ref GRAS_ex_ping_server
115         - \ref GRAS_ex_ping_serdata
116         - \ref GRAS_ex_ping_sercb
117         - \ref GRAS_ex_ping_sermain
118       - \ref GRAS_ex_ping_client
119         - \ref GRAS_ex_ping_climain
120
121     <hr>
122
123     \dontinclude gras/ping/ping_common.c
124
125     \section GRAS_ex_ping_common 1) Common code to the client and the server
126
127     \subsection GRAS_ex_ping_initial 1.a) Initial settings
128
129     Let's first load the module header and declare a logging category (see
130     \ref XBT_log for more info on logging).
131
132     \skip include
133     \until XBT_LOG
134
135     The module header <tt>ping.h</tt> reads:
136
137     \dontinclude gras/ping/ping.h
138     \skip include
139     \until argv
140     \until argv
141
142     \subsection GRAS_ex_ping_register 1.b) Register the messages
143
144     This function, called by both the client and the server is in charge of
145     declaring the existing messages to GRAS. Since the payload does not
146     involve any newly created types but only int, this is quite easy.
147     (to exchange more complicated types, see \ref GRAS_dd or
148     \ref GRAS_ex_mmrpc for an example).
149
150     \dontinclude gras/ping/ping_common.c
151     \skip register_messages
152     \until }
153
154     [Back to \ref GRAS_ex_ping_toc]
155
156     \section GRAS_ex_ping_server 2) Server's code
157
158     \subsection GRAS_ex_ping_serdata 2.a) The server's globals
159
160     In order to ensure the communication between the "main" and the callback
161     of the server, we need to declare some globals. We have to put them in a
162     struct definition so that they can be handled properly in GRAS (see the
163     \ref GRAS_tut_tour_globals for more info).
164
165     \dontinclude gras/ping/ping_server.c
166     \skip typedef struct
167     \until }
168
169     \subsection GRAS_ex_ping_sercb 2.b) The callback to the ping message
170
171     Here is the callback run when the server receives any ping message (this
172     will be registered later by the server).
173
174     \skip server_cb_ping_handler
175     \until end_of_server_cb_ping_handler
176
177     \subsection GRAS_ex_ping_sermain 2.c) The "main" of the server
178
179     This is the "main" of the server. As explained in the tutorial, \ref
180     GRAS_tut_tour_setup, you must not write any main()
181     function yourself. Instead, you just have to write a regular function
182     like this one which will act as a main.
183
184     \skip server
185     \until end_of_server
186
187     [Back to \ref GRAS_ex_ping_toc]
188
189     \section GRAS_ex_ping_client 3) Client's code
190
191     \subsection GRAS_ex_ping_climain 3.a) Client's "main" function
192
193     This function is quite straightforward, and the inlined comments should
194     be enough to understand it.
195
196     \dontinclude gras/ping/ping_client.c
197     \skip client
198     \until end_of_client
199
200     [Back to \ref GRAS_ex_ping_toc]
201  */
202
203 ---------------------------------------------------------------------
204 --------------------- Simple Token Ring -----------------------------
205 ---------------------------------------------------------------------
206
207 /** @defgroup GRAS_ex_token Token Ring example
208     @ingroup GRAS_ex
209
210    This example implements the token ring algorithm. It involves several
211    nodes arranged in a ring (each of them have a left and a right neighbour)
212    and exchanging a "token". This algorithm is one of the solution to ensure
213    the mutual exclusion between distributed processes. There is only one
214    token at any time, so the process in its possession is ensured to be the
215    only one having it. So, if there is an action you want all processes to
216    do alternativly, but you cannot afford to have two processes doing it at
217    the same time, let the process having the token doing it.
218
219    Actually, there is a lot of different token ring algorithms in the
220    litterature, so this example implements one of them: the simplest one.
221    The ring is static (no new node can join it, and you'll get trouble if
222    one node dies or leaves), and nothing is done for the case in which the
223    token is lost.
224
225    - \ref GRAS_ex_stoken_deploy
226    - \ref GRAS_ex_stoken_global
227    - \ref GRAS_ex_stoken_callback
228    - \ref GRAS_ex_stoken_main
229
230    \section GRAS_ex_stoken_deploy 1) Deployment file
231
232    Here is the deployment file:
233    \include examples/gras/mutual_exclusion/simple_token/simple_token.xml
234
235    The neighbour of each node is given at startup as command line argument.
236    Moreover, one of the nodes is instructed by a specific argument (the one
237    on Tremblay here) to create the token at the begining of the algorithm.
238
239    \section GRAS_ex_stoken_global 2) Global definition
240
241    The token is incarned by a specific message, which circulates from node
242    to node (the payload is an integer incremented at each hop). So, the most
243    important part of the code is the message callback, which forwards the
244    message to the next node. That is why we have to store all variable in a
245    global, as explained in the \ref GRAS_globals section.
246
247    \dontinclude examples/gras/mutual_exclusion/simple_token/simple_token.c
248    \skip typedef
249    \until }
250
251    \section GRAS_ex_stoken_callback 3) The callback
252
253    Even if this is the core of this algorithm, this function is quite
254    straightforward.
255
256    \skip node_cb_stoken_handler
257    \until end_of_node_cb_stoken_handler
258
259    \section GRAS_ex_stoken_main 4) The main function
260
261    This function is splited in two parts: The first one performs all the
262    needed initialisations (points 1-7) while the end (point 8. below) calls
263    gras_msg_handle() as long as the planned amount of ring loops are not
264    performed.
265
266    \skip node
267    \until end_of_node
268
269 */
270
271 ---------------------------------------------------------------------
272 -------------------------- MM RPC -----------------------------------
273 ---------------------------------------------------------------------
274
275 /** @defgroup GRAS_ex_mmrpc A simple RPC for matrix multiplication
276     @ingroup GRAS_ex
277
278     This example implements a remote matrix multiplication. It involves a client
279     (creating the matrices and sending the multiplications requests) and a server
280     (computing the multiplication on client's behalf).
281
282     This example also constitutes a more advanced example of data description
283     mechanisms, since the message payload type is a bit more complicated than in
284     other examples such as the ping one (\ref GRAS_ex_ping).
285
286     It works the following way (not very different from the ping example):
287      - Both the client and the server register all needed messages and datatypes
288      - The server registers a callback to the "request" message, which computes
289        what needs to be and returns the result to the expeditor.
290      - The client creates two matrices, ask for their multiplication and check
291        the server's answer.
292
293     This example resides in the <b>examples/gras/mmrpc/mmrpc.c</b> file. (See
294     the \ref GRAS_tut_tour_setup of the tutorial if wondering why both the server
295     and the client live in the same source file)
296
297     \section GRAS_ex_mmrpc_toc Table of contents of the mmrpc example
298       - \ref GRAS_ex_mmrpc_common
299         - \ref GRAS_ex_mmrpc_header
300         - \ref GRAS_ex_mmrpc_dataregister
301         - \ref GRAS_ex_mmrpc_logdef
302         - \ref GRAS_ex_mmrpc_msgregister
303       - \ref GRAS_ex_mmrpc_server
304         - \ref GRAS_ex_mmrpc_serinc
305         - \ref GRAS_ex_mmrpc_sercb
306         - \ref GRAS_ex_mmrpc_sermain
307       - \ref GRAS_ex_mmrpc_client
308         - \ref GRAS_ex_mmrpc_cliinc
309         - \ref GRAS_ex_mmrpc_climain
310
311     <hr>
312
313
314     \section GRAS_ex_mmrpc_common 1) Common code to the client and the server (mmrpc_common.c and mmrpc.h)
315
316
317     \subsection GRAS_ex_mmrpc_header 1.a) Module header (mmrpc.h)
318
319     This loads the gras header and declare the function's prototypes as well
320     as the matrix size.
321
322     \dontinclude gras/mmrpc/mmrpc.h
323     \skip include
324     \until argv
325     \until argv
326
327     \subsection GRAS_ex_mmrpc_dataregister 1.b) Register the data types (mmrpc.h)
328
329     The messages involved in a matrix of double. This type is automatically
330     known by the GRAS mecanism, using the gras_datadesc_matrix() function of the
331     xbt/matrix module.
332
333     \subsection GRAS_ex_mmrpc_logdef 1.c) Logging category definition (mmrpc_common.c)
334
335     Let's first load the module header and declare a logging category (see
336     \ref XBT_log for more info on logging). This logging category does live
337     in this file (ie the required symbols are defined here and declared as
338     "extern" in any other file using them). That is why we use
339     \ref XBT_LOG_NEW_DEFAULT_CATEGORY here and
340     \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY in mmrpc_client.c and mmrpc_server.c.
341
342     \dontinclude gras/mmrpc/mmrpc_common.c
343     \skip include
344     \until XBT_LOG
345
346     \subsection GRAS_ex_mmrpc_msgregister 1.d) Register the messages (mmrpc_common.c)
347
348     This function, called by both the client and the server is in charge of
349     declaring the existing messages to GRAS.
350
351     The datatype description builded that way can then be used to build an array datatype or
352     to declare messages.
353
354     \skip register_messages
355     \until }
356
357     [Back to \ref GRAS_ex_mmrpc_toc]
358
359     \section GRAS_ex_mmrpc_server 2) Server's code (mmrpc_server.c)
360
361     \subsection GRAS_ex_mmrpc_serinc 2.a) Server intial settings
362
363     All module symbols live in the mmrpc_common.c file. We thus have to
364     define \ref XBT_DEFINE_TYPE_EXTERN to the preprocessor so that the
365     \ref XBT_DEFINE_TYPE symbols don't get included here. Likewise, we use
366     \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY to get the log category in here.
367
368     \dontinclude gras/mmrpc/mmrpc_server.c
369     \skip define
370     \until XBT_LOG
371
372     \subsection GRAS_ex_mmrpc_sercb 2.b) The callback to the mmrpc message
373
374     Here is the callback run when the server receives any mmrpc message (this
375     will be registered later by the server). Note the way we get the message
376     payload. In the ping example, there was one additional level of pointer
377     indirection (see \ref GRAS_ex_ping_sercb). This is because the payload is
378     an array here (ie a pointer) whereas it is a scalar in the ping example.
379
380     \skip server_cb_request_handler
381     \until end_of_server_cb_request_handler
382
383     \subsection GRAS_ex_mmrpc_sermain 2.c) The "main" of the server
384
385     This is the "main" of the server. As explained in the tutorial, \ref
386     GRAS_tut_tour_setup, you must not write any main()
387     function yourself. Instead, you just have to write a regular function
388     like this one which will act as a main.
389
390     \skip server
391     \until end_of_server
392
393     [Back to \ref GRAS_ex_mmrpc_toc]
394
395     \section GRAS_ex_mmrpc_client 3) Client's code (mmrpc_client.c)
396
397     \subsection GRAS_ex_mmrpc_cliinc 2.a) Server intial settings
398
399     As for the server, some extra love is needed to make sure that automatic
400     datatype parsing and log categories do work even if we are using several
401     files.
402
403     \dontinclude gras/mmrpc/mmrpc_client.c
404     \skip define
405     \until XBT_LOG
406
407     \subsection GRAS_ex_mmrpc_climain 3.b) Client's "main" function
408
409     This function is quite straightforward, and the inlined comments should
410     be enough to understand it.
411
412     \dontinclude gras/mmrpc/mmrpc_client.c
413     \skip argv
414     \until end_of_client
415
416     [Back to \ref GRAS_ex_mmrpc_toc]
417   */
418
419 ---------------------------------------------------------------------
420 ---------------------------- Timers ---------------------------------
421 ---------------------------------------------------------------------
422
423 /** @defgroup GRAS_ex_timer Some timer games
424     @ingroup GRAS_ex
425
426     This example fools around with the GRAS timers (\ref GRAS_timer). It is
427     mainly a regression test, since it uses almost all timer features.
428
429     The main program registers a repetititive task and a delayed one, and
430     then loops until the <tt>still_to_do</tt> variables of its globals reach
431     0. The delayed task set it to 5, and the repetititive one decrease it
432     each time. Here is an example of output:
433 \verbatim Initialize GRAS
434  Initialize XBT
435  [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
436  [1108335471] Programming the delayed_action for after 2.000000 sec
437  [1108335471] Have a rest
438  [1108335472] Canceling the delayed_action.
439  [1108335472] Re-programming the delayed_action for after 2.000000 sec
440  [1108335472] Repetitive_action has nothing to do yet
441  [1108335473] Repetitive_action has nothing to do yet
442  [1108335473] delayed_action setting globals->still_to_do to 5
443  [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
444  [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
445  [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
446  [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
447  [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
448  Exiting GRAS\endverbatim
449
450     Source code:
451      - \ref GRAS_ex_timer_decl
452      - \ref GRAS_ex_timer_delay
453      - \ref GRAS_ex_timer_repeat
454      - \ref GRAS_ex_timer_main
455
456     \dontinclude timer.c
457
458     \section GRAS_ex_timer_decl   1. Declarations and headers
459     \skip include
460     \until my_globals
461
462     \section GRAS_ex_timer_delay  2. Source code of the delayed action
463     \skip repetitive_action
464     \until end_of_repetitive_action
465
466     \section GRAS_ex_timer_repeat 3. Source code of the repetitive action
467     \skip delayed_action
468     \until end_of_delayed_action
469
470     \section GRAS_ex_timer_main   4. Source code of main function
471     \skip client
472     \until end_of_client
473 */