1 #####################################################################
2 ########################### CORE ###################################
3 #####################################################################
5 /** \addtogroup GRAS_API
9 <div class="tocTitle">Table of content</div>
11 <li> <a href="#GRAS_funct">API documentation</a>
12 <li> <a href="#GRAS_example">Examples</a>
13 <li> <a href="#GRAS_tut_presentation">Tutorial</a>
14 <li> <a href="#GRAS_howto_presentation">HOWTOs</a>
18 \section GRAS_funct API documentation
19 GRAS offers the following functionnalities
20 - <b>\ref GRAS_comm</b>: Exchanging messages between peers
21 - \ref GRAS_dd : any data which may transit on the network must be
22 described beforehand so that GRAS can handle the platform
23 heterogeneity and convert them if needed.
24 - \ref GRAS_sock : this is how to open a communication channel to
25 other processes, and retrive information about them.
26 - \ref GRAS_msg : communications are message oriented. You have to
27 describe all possible messages and their payload beforehand, and
28 can then attach callbacks to the arrival of a given kind of message.
29 - \ref GRAS_timer : this is how to program repetitive and delayed
30 tasks, not unlike cron(8) and at(1). This cannot be used to timeout
31 a function (like setitimer(2) or signal(2) games could do).
32 - <b>\ref GRAS_run</b>: Running both on top of the simulator and on
33 top of real platforms, and portability support.
34 - \ref GRAS_virtu : You naturally don't want to call the
35 gettimeofday(2) function in simulation mode since it would give
36 you the time on the host running the simulation, not the time in
37 the simulated world (you are belonging to).\n
38 This a system call virtualization layer, which also acts as a
40 - \ref GRAS_globals : The use of globals is forbidden since the
41 "processes" are threads in simulation mode. \n
42 This is how to let GRAS handle your globals properly.
43 - \ref GRAS_emul : Support to emulate code excution (ie, reporting
44 execution time into the simulator and having code sections specific
45 to simulation or to real mode).
47 \section GRAS_example Examples
49 There is for now rather few examples of GRAS, but it's better than
57 The initiatic tour of the tutorial also contains several examples. The
58 most proeminent one is:
60 - \ref GRAS_tut_tour_explicitwait_use
62 \section GRAS_tut_presentation Tutorial
64 We even have a tutorial for the GRAS framework. It details in a
65 hopefully pedagogic order all the points of the API, along with example
66 of use for each of them. Unfortunately, it is not finished yet (the main
67 part missing is the one on how to describe data). Here is the table of
71 - \ref GRAS_tut_intro_what
72 - \ref GRAS_tut_intro_model
74 - \ref GRAS_tut_tour_install
75 - \ref GRAS_tut_tour_setup
76 - \ref GRAS_tut_tour_simpleexchange
77 - \ref GRAS_tut_tour_args
78 - \ref GRAS_tut_tour_callbacks
79 - \ref GRAS_tut_tour_globals
80 - \ref GRAS_tut_tour_logs
81 - \ref GRAS_tut_tour_timers
82 - \ref GRAS_tut_tour_exceptions
83 - \ref GRAS_tut_tour_rpc
84 - \ref GRAS_tut_tour_explicitwait
85 - \ref GRAS_tut_tour_message_recaping
87 \section GRAS_howto_presentation HOWTOs
89 The tutorial and the API documentation present the framework little
90 piece by little piece and provide a lot of information on each of them.
91 Quite orthogonally to this, the HOWTOs try to present transversal
92 aspects of the framework to give you some broader point of view on it.
93 How infortunate it is that only one such HOWTO exist for now...
96 - \ref GRAS_howto_design
99 /** @defgroup GRAS_comm Communication facilities */
100 /** @defgroup GRAS_run Virtualization */
101 /** @defgroup GRAS_ex Examples */
102 /** @defgroup GRAS_tut GRAS Tutorial */
104 #####################################################################
105 /** @addtogroup GRAS_comm
107 Here are the communication facilities. GRAS allows you to exchange
108 <i>messages</i> on <i>sockets</i> (which can be seen as pipes between
109 processes). On reception, messages start <i>callbacks</i> (that's the
110 default communication mode, not the only one). All messages of a given
111 type convey the same kind of data, and you have to describe it
114 Timers are also seen as a mean of communication (with yourself). It
115 allows you to run a repetitive task ("do this every N second until I tell
116 you to stop"), or to deffer a treatment ("do this in 3 sec").
119 /** @defgroup GRAS_dd Data description */
120 /** @defgroup GRAS_sock Sockets */
121 /** @defgroup GRAS_msg Messages */
122 /** @defgroup GRAS_timer Timers */
125 #####################################################################
126 /** @addtogroup GRAS_run
128 Virtualization facilities allow your code to run both on top of the simulator or in real setting.
132 /** @defgroup GRAS_globals Globals */
133 /** @defgroup GRAS_emul Emulation support */
134 /** @defgroup GRAS_virtu Syscalls */
138 #####################################################################
139 /** @addtogroup GRAS_ex
141 There is for now rather few examples of GRAS, but it's better than
149 The initiatic tour of the tutorial also contains several examples. The
150 most proeminent one is:
152 - \ref GRAS_tut_tour_explicitwait_use
155 DOXYGEN_NAVBAR_CHILD "Ping-Pong"=GRAS_ex_ping.html
156 DOXYGEN_NAVBAR_CHILD "RPC"=GRAS_ex_mmrpc.html
157 DOXYGEN_NAVBAR_CHILD "Token Ring"=GRAS_ex_token.html
158 DOXYGEN_NAVBAR_CHILD "Timers"=GRAS_ex_timer.html
161 There is some more examples in the distribution, under the directory
162 <tt>examples/gras</tt>.
165 #####################################################################
166 ######################### EXAMPLES #################################
167 #####################################################################
169 ---------------------------------------------------------------------
170 ------------------------- Ping Pong ---------------------------------
171 ---------------------------------------------------------------------
173 /** \page GRAS_ex_ping The classical Ping-Pong in GRAS
175 This example implements the very classical ping-pong in GRAS. It
176 involves a client (initiating the ping-pong) and a server (answering to
179 It works the following way:
180 - Both the client and the server register all needed messages
181 - The server registers a callback to the ping message, which sends pong
183 - The client sends the ping message to the server, and waits for the
184 pong message as an answer.
186 This example resides in the <b>examples/gras/ping/ping.c</b> file. Yes, both
187 the code of the client and of the server is placed in the same file. See
188 the \ref GRAS_tut_tour_setup of the tutorial if wondering.
190 \section GRAS_ex_ping_toc Table of contents of the ping example
191 - \ref GRAS_ex_ping_common
192 - \ref GRAS_ex_ping_initial
193 - \ref GRAS_ex_ping_register
194 - \ref GRAS_ex_ping_server
195 - \ref GRAS_ex_ping_serdata
196 - \ref GRAS_ex_ping_sercb
197 - \ref GRAS_ex_ping_sermain
198 - \ref GRAS_ex_ping_client
199 - \ref GRAS_ex_ping_climain
203 \dontinclude gras/ping/ping_common.c
205 \section GRAS_ex_ping_common 1) Common code to the client and the server
207 \subsection GRAS_ex_ping_initial 1.a) Initial settings
209 Let's first load the module header and declare a logging category (see
210 \ref XBT_log for more info on logging).
215 The module header <tt>ping.h</tt> reads:
217 \dontinclude gras/ping/ping.h
222 \subsection GRAS_ex_ping_register 1.b) Register the messages
224 This function, called by both the client and the server is in charge of
225 declaring the existing messages to GRAS. Since the payload does not
226 involve any newly created types but only int, this is quite easy.
227 (to exchange more complicated types, see \ref GRAS_dd or
228 \ref GRAS_ex_mmrpc for an example).
230 \dontinclude gras/ping/ping_common.c
231 \skip register_messages
234 [Back to \ref GRAS_ex_ping_toc]
236 \section GRAS_ex_ping_server 2) Server's code
238 \subsection GRAS_ex_ping_serdata 2.a) The server's globals
240 In order to ensure the communication between the "main" and the callback
241 of the server, we need to declare some globals. We have to put them in a
242 struct definition so that they can be handled properly in GRAS (see the
243 \ref GRAS_tut_tour_globals for more info).
245 \dontinclude gras/ping/ping_server.c
249 \subsection GRAS_ex_ping_sercb 2.b) The callback to the ping message
251 Here is the callback run when the server receives any ping message (this
252 will be registered later by the server).
254 \skip server_cb_ping_handler
255 \until end_of_server_cb_ping_handler
257 \subsection GRAS_ex_ping_sermain 2.c) The "main" of the server
259 This is the "main" of the server. As explained in the tutorial, \ref
260 GRAS_tut_tour_setup, you must not write any main()
261 function yourself. Instead, you just have to write a regular function
262 like this one which will act as a main.
267 [Back to \ref GRAS_ex_ping_toc]
269 \section GRAS_ex_ping_client 3) Client's code
271 \subsection GRAS_ex_ping_climain 3.a) Client's "main" function
273 This function is quite straightforward, and the inlined comments should
274 be enough to understand it.
276 \dontinclude gras/ping/ping_client.c
280 [Back to \ref GRAS_ex_ping_toc]
283 ---------------------------------------------------------------------
284 --------------------- Simple Token Ring -----------------------------
285 ---------------------------------------------------------------------
287 /** \page GRAS_ex_token Token Ring example
289 This example implements the token ring algorithm. It involves several
290 nodes arranged in a ring (each of them have a left and a right neighbour)
291 and exchanging a "token". This algorithm is one of the solution to ensure
292 the mutual exclusion between distributed processes. There is only one
293 token at any time, so the process in its possession is ensured to be the
294 only one having it. So, if there is an action you want all processes to
295 do alternativly, but you cannot afford to have two processes doing it at
296 the same time, let the process having the token doing it.
298 Actually, there is a lot of different token ring algorithms in the
299 litterature, so this example implements one of them: the simplest one.
300 The ring is static (no new node can join it, and you'll get trouble if
301 one node dies or leaves), and nothing is done for the case in which the
304 - \ref GRAS_ex_stoken_deploy
305 - \ref GRAS_ex_stoken_global
306 - \ref GRAS_ex_stoken_callback
307 - \ref GRAS_ex_stoken_main
309 \section GRAS_ex_stoken_deploy 1) Deployment file
311 Here is the deployment file:
312 \include examples/gras/mutual_exclusion/simple_token/simple_token.xml
314 The neighbour of each node is given at startup as command line argument.
315 Moreover, one of the nodes is instructed by a specific argument (the one
316 on Tremblay here) to create the token at the begining of the algorithm.
318 \section GRAS_ex_stoken_global 2) Global definition
320 The token is incarned by a specific message, which circulates from node
321 to node (the payload is an integer incremented at each hop). So, the most
322 important part of the code is the message callback, which forwards the
323 message to the next node. That is why we have to store all variable in a
324 global, as explained in the \ref GRAS_globals section.
326 \dontinclude examples/gras/mutual_exclusion/simple_token/simple_token.c
330 \section GRAS_ex_stoken_callback 3) The callback
332 Even if this is the core of this algorithm, this function is quite
335 \skip node_cb_stoken_handler
336 \until end_of_node_cb_stoken_handler
338 \section GRAS_ex_stoken_main 4) The main function
340 This function is splited in two parts: The first one performs all the
341 needed initialisations (points 1-7) while the end (point 8. below) calls
342 gras_msg_handle() as long as the planned amount of ring loops are not
350 ---------------------------------------------------------------------
351 -------------------------- MM RPC -----------------------------------
352 ---------------------------------------------------------------------
354 /** \page GRAS_ex_mmrpc A simple RPC for matrix multiplication
356 This example implements a remote matrix multiplication. It involves a client
357 (creating the matrices and sending the multiplications requests) and a server
358 (computing the multiplication on client's behalf).
360 This example also constitutes a more advanced example of data description
361 mechanisms, since the message payload type is a bit more complicated than in
362 other examples such as the ping one (\ref GRAS_ex_ping).
364 It works the following way (not very different from the ping example):
365 - Both the client and the server register all needed messages and datatypes
366 - The server registers a callback to the "request" message, which computes
367 what needs to be and returns the result to the expeditor.
368 - The client creates two matrices, ask for their multiplication and check
371 This example resides in the <b>examples/gras/mmrpc/mmrpc.c</b> file. (See
372 the \ref GRAS_tut_tour_setup of the tutorial if wondering why both the server
373 and the client live in the same source file)
375 \section GRAS_ex_mmrpc_toc Table of contents of the mmrpc example
376 - \ref GRAS_ex_mmrpc_common
377 - \ref GRAS_ex_mmrpc_header
378 - \ref GRAS_ex_mmrpc_dataregister
379 - \ref GRAS_ex_mmrpc_logdef
380 - \ref GRAS_ex_mmrpc_msgregister
381 - \ref GRAS_ex_mmrpc_matdump
382 - \ref GRAS_ex_mmrpc_server
383 - \ref GRAS_ex_mmrpc_serinc
384 - \ref GRAS_ex_mmrpc_sercb
385 - \ref GRAS_ex_mmrpc_sermain
386 - \ref GRAS_ex_mmrpc_client
387 - \ref GRAS_ex_mmrpc_cliinc
388 - \ref GRAS_ex_mmrpc_climain
393 \section GRAS_ex_mmrpc_common 1) Common code to the client and the server (mmrpc_common.c and mmrpc.h)
396 \subsection GRAS_ex_mmrpc_header 1.a) Module header (mmrpc.h)
398 This loads the gras header and declare the function's prototypes as well
401 \dontinclude gras/mmrpc/mmrpc.h
406 \subsection GRAS_ex_mmrpc_dataregister 1.b) Register the data types (mmrpc.h)
408 The messages involved in this example do use structures as payload,
409 so we have to declare it to GRAS. Hopefully, this can be done easily by enclosing
410 the structure declaration within a \ref GRAS_DEFINE_TYPE macro call. It will then copy this
411 declaration into an hidden string variable, which can be automatically parsed at
412 run time. Of course, the declaration is also copied unmodified by this macro, so that it
413 gets parsed by the compiler also.
415 There is some semantic that GRAS cannot guess alone and you need to <i>annotate</i>
416 your declaration to add some. For example, the ctn pointer can be a reference to an
417 object or a whole array (in which case you also has to specify its size). This is done
418 with the GRAS_ANNOTE call. It is removed from the text passed to the compiler, but it helps
419 GRAS getting some information about the semantic of your data. Here, it says that \a ctn is an
420 array, which size is the result of the operation \a rows * \a cols (with \a rows and \a cols
421 being the other fields of the structure).
423 Please note that this annotation mechanism is not as robust and cool as this example seems to
424 imply. If you want to use it yourself, you'd better use the exact right syntax, which is
425 detailed in the \ref GRAS_dd section.
427 \skip GRAS_DEFINE_TYPE
430 \subsection GRAS_ex_mmrpc_logdef 1.c) Logging category definition (mmrpc_common.c)
432 Let's first load the module header and declare a logging category (see
433 \ref XBT_log for more info on logging). This logging category does live
434 in this file (ie the required symbols are defined here and declared as
435 "extern" in any other file using them). That is why we use
436 \ref XBT_LOG_NEW_DEFAULT_CATEGORY here and
437 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY in mmrpc_client.c and mmrpc_server.c.
439 \dontinclude gras/mmrpc/mmrpc_common.c
443 \subsection GRAS_ex_mmrpc_msgregister 1.d) Register the messages (mmrpc_common.c)
445 This function, called by both the client and the server is in charge of
446 declaring the existing messages to GRAS. Note the use of the \ref gras_datadesc_by_symbol
447 function to parse and retrieve the structure declaration which were passed to \ref GRAS_DEFINE_TYPE
450 The datatype description builded that way can then be used to build an array datatype or
453 \skip register_messages
456 \subsection GRAS_ex_mmrpc_matdump 1.e) Helper debugging function (mmrpc_common.c)
458 This function dumps a matrix to screen for debugging.
464 [Back to \ref GRAS_ex_mmrpc_toc]
466 \section GRAS_ex_mmrpc_server 2) Server's code (mmrpc_server.c)
468 \subsection GRAS_ex_mmrpc_serinc 2.a) Server intial settings
470 All module symbols live in the mmrpc_common.c file. We thus have to
471 define \ref GRAS_DEFINE_TYPE_EXTERN to the preprocessor so that the
472 \ref GRAS_DEFINE_TYPE symbols don't get included here. Likewise, we use
473 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY to get the log category in here.
475 \dontinclude gras/mmrpc/mmrpc_server.c
479 \subsection GRAS_ex_mmrpc_sercb 2.b) The callback to the mmrpc message
481 Here is the callback run when the server receives any mmrpc message (this
482 will be registered later by the server). Note the way we get the message
483 payload. In the ping example, there was one additional level of pointer
484 indirection (see \ref GRAS_ex_ping_sercb). This is because the payload is
485 an array here (ie a pointer) whereas it is a scalar in the ping example.
487 \skip server_cb_request_handler
488 \until end_of_server_cb_request_handler
490 \subsection GRAS_ex_mmrpc_sermain 2.c) The "main" of the server
492 This is the "main" of the server. As explained in the tutorial, \ref
493 GRAS_tut_tour_setup, you must not write any main()
494 function yourself. Instead, you just have to write a regular function
495 like this one which will act as a main.
500 [Back to \ref GRAS_ex_mmrpc_toc]
502 \section GRAS_ex_mmrpc_client 3) Client's code (mmrpc_client.c)
504 \subsection GRAS_ex_mmrpc_cliinc 2.a) Server intial settings
506 As for the server, some extra love is needed to make sure that automatic
507 datatype parsing and log categories do work even if we are using several
510 \dontinclude gras/mmrpc/mmrpc_client.c
514 \subsection GRAS_ex_mmrpc_climain 3.b) Client's "main" function
516 This function is quite straightforward, and the inlined comments should
517 be enough to understand it.
519 \dontinclude gras/mmrpc/mmrpc_client.c
523 [Back to \ref GRAS_ex_mmrpc_toc]
526 ---------------------------------------------------------------------
527 ---------------------------- Timers ---------------------------------
528 ---------------------------------------------------------------------
530 /** \page GRAS_ex_timer Some timer games
532 This example fools around with the GRAS timers (\ref GRAS_timer). It is
533 mainly a regression test, since it uses almost all timer features.
535 The main program registers a repetititive task and a delayed one, and
536 then loops until the <tt>still_to_do</tt> variables of its globals reach
537 0. The delayed task set it to 5, and the repetititive one decrease it
538 each time. Here is an example of output:
539 \verbatim Initialize GRAS
541 [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
542 [1108335471] Programming the delayed_action for after 2.000000 sec
543 [1108335471] Have a rest
544 [1108335472] Canceling the delayed_action.
545 [1108335472] Re-programming the delayed_action for after 2.000000 sec
546 [1108335472] Repetitive_action has nothing to do yet
547 [1108335473] Repetitive_action has nothing to do yet
548 [1108335473] delayed_action setting globals->still_to_do to 5
549 [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
550 [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
551 [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
552 [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
553 [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
554 Exiting GRAS\endverbatim
557 - \ref GRAS_ex_timer_decl
558 - \ref GRAS_ex_timer_delay
559 - \ref GRAS_ex_timer_repeat
560 - \ref GRAS_ex_timer_main
564 \section GRAS_ex_timer_decl 1. Declarations and headers
568 \section GRAS_ex_timer_delay 2. Source code of the delayed action
569 \skip repetitive_action
570 \until end_of_repetitive_action
572 \section GRAS_ex_timer_repeat 3. Source code of the repetitive action
574 \until end_of_delayed_action
576 \section GRAS_ex_timer_main 4. Source code of main function