1 #####################################################################
2 ########################### CORE ###################################
3 #####################################################################
5 /** \addtogroup GRAS_API
7 \section GRAS_funct Offered functionnalities
8 - <b>\ref GRAS_comm</b>: Exchanging messages between peers
9 - \ref GRAS_dd : any data which may transit on the network must be
10 described beforehand so that GRAS can handle the platform
11 heterogeneity and convert them if needed.
12 - \ref GRAS_sock : this is how to open a communication channel to
13 other processes, and retrive information about them.
14 - \ref GRAS_msg : communications are message oriented. You have to
15 describe all possible messages and their payload beforehand, and
16 can then attach callbacks to the arrival of a given kind of message.
17 - \ref GRAS_timer : this is how to program repetitive and delayed
18 tasks, not unlike cron(8) and at(1). This cannot be used to timeout
19 a function (like setitimer(2) or signal(2) games could do).
20 - <b>\ref GRAS_run</b>: Running both on top of the simulator and on
21 top of real platforms, and portability support.
22 - \ref GRAS_virtu : You naturally don't want to call the
23 gettimeofday(2) function in simulation mode since it would give
24 you the time on the host running the simulation, not the time in
25 the simulated world (you are belonging to).\n
26 This a system call virtualization layer, which also acts as a
28 - \ref GRAS_globals : The use of globals is forbidden since the
29 "processes" are threads in simulation mode. \n
30 This is how to let GRAS handle your globals properly.
31 - \ref GRAS_emul : Support to emulate code excution (ie, reporting
32 execution time into the simulator and having code sections specific
33 to simulation or to real mode).
34 - <b>\ref GRAS_code</b>: Here are some tools which may help
35 you setting up a GRAS project.\n
36 Setting up and building a GRAS application is complicated by the
37 library schizoid. The code to setup the environment differs
38 depending on whether you run on the simulator on a real platform.
39 And then, you'll have to deal with the usual distributed
40 application development difficulties.
41 - \ref GRAS_main_generation : Since processes are threads in
42 simulation mode and regular processes in the real world, GRAS does
43 generate your main functions for you.
47 \section GRAS_example Examples
49 There is for now rather few examples of GRAS, but it's better than
57 /** @defgroup GRAS_comm Communication facilities */
58 /** @defgroup GRAS_run Virtualization */
59 /** @defgroup GRAS_code Project and code management */
60 /** @defgroup GRAS_ex Examples */
62 #####################################################################
63 /** @addtogroup GRAS_comm
65 Here are the communication facilities. GRAS allows you to exchange
66 <i>messages</i> on <i>sockets</i> (which can be seen as pipes between
67 processes). On reception, messages start <i>callbacks</i> (that's the
68 default communication mode, not the only one). All messages of a given
69 type convey the same kind of data, and you have to describe it
72 Timers are also seen as a mean of communication (with yourself). It
73 allows you to run a repetitive task ("do this every N second until I tell
74 you to stop"), or to deffer a treatment ("do this in 3 sec").
77 /** @defgroup GRAS_dd Data description */
78 /** @defgroup GRAS_sock Sockets */
79 /** @defgroup GRAS_msg Messages */
80 /** @defgroup GRAS_timer Timers */
83 #####################################################################
84 /** @addtogroup GRAS_run
86 Virtualization facilities allow your code to run both on top of the simulator or in real setting.
90 /** @defgroup GRAS_globals Globals */
91 /** @defgroup GRAS_emul Emulation support */
92 /** @defgroup GRAS_virtu Syscalls */
96 #####################################################################
97 /** @addtogroup GRAS_code
99 Here is how to setup your code when you want to use GRAS. You will also
100 learn how to get the most repetitive parts of your code generated
103 (use the tabs on top of the page to navigate)
106 DOXYGEN_NAVBAR_LABEL="Project management"
107 DOXYGEN_NAVBAR_CHILD "main() and GRAS"=GRAS_main_generation.html
108 DOXYGEN_NAVBAR_CHILD "Compiling your GRAS project"=GRAS_compile.html
112 #####################################################################
113 /** @addtogroup GRAS_ex
115 There is for now rather few examples of GRAS, but it's better than
123 DOXYGEN_NAVBAR_CHILD "Ping-Pong"=GRAS_ex_ping.html
124 DOXYGEN_NAVBAR_CHILD "RPC"=GRAS_ex_mmrpc.html
125 DOXYGEN_NAVBAR_CHILD "Timers"=GRAS_ex_timer.html
128 There is some more examples in the distribution, under the directory
129 <tt>examples/gras</tt>.
132 #####################################################################
133 ######################### EXTRA PAGES ##############################
134 #####################################################################
136 ---------------------------------------------------------------------
137 --------------------- main() generation -----------------------------
138 ---------------------------------------------------------------------
140 /** \page GRAS_main_generation main function
142 \section GRAS_maingen_toc Table of content
144 - \ref GRAS_maingen_intro
145 - \ref GRAS_maingen_script
146 - \ref GRAS_maingen_make
150 \section GRAS_maingen_intro What's the matter with main() functions in GRAS?
152 In simulation mode, all processes are run as thread of the same process
153 while they are real processes in the real life. Unfortunately, the main
154 function of a real process must be called <tt>main</tt> while this
155 function must not use this name for threads.
157 To deal with this, you should call the main function of your processes
158 with another name (usually, the process function such as client, server,
159 or such). Then GRAS can generate the wrapper functions adapted to the
160 real and simulated modes.
162 \section GRAS_maingen_script Generating the main()s automatically
164 This is done by the gras_stub_generator program, which gets installed on
165 <tt>make install</tt> (the source resides in the tools/gras/ directory).
166 Here is the calling syntax:
167 \verbatim gras_stub_generator <project_name> <deployment_file.xml>\endverbatim
169 It parses the deployment file, searching for all the kind of processes
170 you have in your project. It then generates the following C files:
171 - a <tt>_<project_name>_<process_kind>.c</tt> file for each process kind you
173 They are used to launch your project in real life. They
174 contain a main() in charge of initializing the GRAS infrastructure and
175 launching your code afterward.
176 - a <tt>_<project_name>_simulator.c</tt> file.\n
177 This file is suited to the simulation mode. It contains a main()
178 function initializing the simulator and launching your project within.
180 For this to work, the name of process described in your deployment file
181 should match the name of a function in your code, which prototype is for
182 example: \verbatim int client(int argc,char *argv[]);\endverbatim
184 Unfortunately, all this is still partially documented. I guess I ought
185 to improve this situation somehow. In the meanwhile, check the generated
186 code and maybe also the GRAS \ref GRAS_example, sorry.
188 \section GRAS_maingen_make Integration within an hand-made Makefile
190 The easiest to set it up is to add the following chunk at the end of
191 your Makefile (or Makefile.am), putting the right values into NAME and
193 \verbatim NAME=your_project_name
194 PROCESSES=list of processes type in your project
196 $(foreach proc, $(PROCESSES), _$(NAME)_$(proc).c) _$(NAME)_simulator.c: $(NAME).c $(NAME)_deployment.xml
197 path/to/gras_stub_generator $(NAME) $(NAME)_deployment.xml >/dev/null
200 Of course, your personal millage may vary. For the \ref GRAS_ex_ping, may read:
201 \verbatim _ping_client.c _ping_server.c _ping_simulator.c: ping.c ping_deployment.xml
202 $(top_srcdir)/tools/gras/gras_stub_generator ping ping_deployment.xml >/dev/null
206 Actually, gras_stub_generator also generates some makefiles both for
207 local compilation and remote code distribution and installation. See the
208 section \ref GRAS_compile for more details.
212 ---------------------------------------------------------------------
213 ------------------------- Compiling ---------------------------------
214 ---------------------------------------------------------------------
216 /** \page GRAS_compile Compiling your project
218 As explained in section \ref GRAS_main_generation, the
219 gras_stub_generator tool can be used to generate the system
220 initialization code in your projet. While we were at this, this tool
221 also generates the makefiles you will need to compile your project
224 Code source deployment and remote compilation also constitutes a
225 challenging area in distributed applications development. The GRASPE
226 (GRAS Platform Expender) tool was designed to make this less painful.
228 \section GRAS_compile_toc Table of content
230 - \ref GRAS_compile_local
231 - \ref GRAS_compile_local_install
232 - \ref GRAS_compile_local_helpfiles
233 - \ref GRAS_compile_local_makefile
234 - \ref GRAS_compile_remote
238 \section GRAS_compile_local Local compilation of GRAS projects
240 \subsection GRAS_compile_local_install Installing SimGrid and GRAS
242 To compile locally a GRAS project, you first need to install SimGrid on
243 your machine. Use the --prefix flag to the configure script to specify
244 where you want to install the toolkit (refere to section \ref
245 faq_compiling for more information)
247 \subsection GRAS_compile_local_helpfiles Simulation description files
249 Then, you will probably need to write a platform description file and
250 application deployment description file to feed the simulator with. This
251 part is unfortunatelly not documented enough. Files examples can be
252 found along with the MSG \ref MSG_ex_master_slave example.
254 \note yes, both platform and application description files are portable
255 between MSG and GRAS. Actually, there depend on the SURF, not on the
256 programming environment you use.
258 For the first try, you could probably reuse the provided platform file
259 as is while you will need to adapt the application file to fit your
262 To generate new platform files, we usually use the Tiers Topology
263 Generator (ask google about it) and annotate the generated graph with
264 home-made scripts to let them fit the SURF. Those scripts live in the
265 tools/platform_generation/ directory of the distribution.
267 \subsection GRAS_compile_local_makefile Generating a Makefile usable for your project
269 From the information contained in the application description file, the
270 gras_stub_generator tool can create a Makefile which can be used to
271 seamlessly compile your project. Just go to the directory containing all
272 your project files, and type:
274 \verbatim path/to/gras_stub_generator [project_name] [application_deployment.file] >/dev/null
277 The first argument is the name of your project, such as
278 "MyLovelyApplication" while the second one is the application deployment
281 Several files get generated by this command. One C file per kind of
282 process in your project (such as "master" and "slave") plus one C file
283 for simulating your project. All those files are (or should ;) described
284 in section \ref GRAS_main_generation.
286 The most intersting file in this context is
287 [project_name].Makefile.local (you can safely ignore the others for
288 now). To use it, simply type (from your project main directory):
290 \verbatim GRAS_ROOT=/path/to/simgrid/installation make -f [project_name].Makefile.local
293 And that's it, all the binaries are built and linked against the correct
296 \section GRAS_compile_remote Distribution and remote compilation of GRAS projects
298 Actually, there is two somehow parallel ways to do so since both Arnaud
299 and Martin gave it a try. Merging both approaches is underway. As usual,
300 if you want to help, you're welcome ;)
304 #####################################################################
305 ######################### EXAMPLES #################################
306 #####################################################################
308 ---------------------------------------------------------------------
309 ------------------------- Ping Pong ---------------------------------
310 ---------------------------------------------------------------------
312 /** \page GRAS_ex_ping The classical Ping-Pong in GRAS
314 This example implements the very classical ping-pong in GRAS. It
315 involves a client (initiating the ping-pong) and a server (answering to
318 It works the following way:
319 - Both the client and the server register all needed messages
320 - The server registers a callback to the ping message, which sends pong
322 - The client sends the ping message to the server, and waits for the
323 pong message as an answer.
325 This example resides in the <b>examples/gras/ping/ping.c</b> file. Yes, both
326 the code of the client and of the server is placed in the same file. See
327 the \ref GRAS_main_generation section if wondering.
329 \section GRAS_ex_ping_toc Table of contents of the ping example
330 - \ref GRAS_ex_ping_common
331 - \ref GRAS_ex_ping_initial
332 - \ref GRAS_ex_ping_register
333 - \ref GRAS_ex_ping_server
334 - \ref GRAS_ex_ping_serdata
335 - \ref GRAS_ex_ping_sercb
336 - \ref GRAS_ex_ping_sermain
337 - \ref GRAS_ex_ping_client
338 - \ref GRAS_ex_ping_climain
342 \dontinclude gras/ping/ping_common.c
344 \section GRAS_ex_ping_common 1) Common code to the client and the server
346 \subsection GRAS_ex_ping_initial 1.a) Initial settings
348 Let's first load the module header and declare a logging category (see
349 \ref XBT_log for more info on logging).
354 The module header <tt>ping.h</tt> reads:
356 \dontinclude gras/ping/ping.h
361 \subsection GRAS_ex_ping_register 1.b) Register the messages
363 This function, called by both the client and the server is in charge of
364 declaring the existing messages to GRAS. Since the payload does not
365 involve any newly created types but only int, this is quite easy.
366 (to exchange more complicated types, see \ref GRAS_dd or
367 \ref GRAS_ex_mmrpc for an example).
369 \dontinclude gras/ping/ping_common.c
370 \skip register_messages
373 [Back to \ref GRAS_ex_ping_toc]
375 \section GRAS_ex_ping_server 2) Server's code
377 \subsection GRAS_ex_ping_serdata 2.a) The server's globals
379 In order to ensure the communication between the "main" and the callback
380 of the server, we need to declare some globals. We have to put them in a
381 struct definition so that they can be handled properly in GRAS (see the
382 \ref GRAS_globals for more info).
384 \dontinclude gras/ping/ping_server.c
388 \subsection GRAS_ex_ping_sercb 2.b) The callback to the ping message
390 Here is the callback run when the server receives any ping message (this
391 will be registered later by the server).
393 \skip server_cb_ping_handler
394 \until end_of_server_cb_ping_handler
396 \subsection GRAS_ex_ping_sermain 2.c) The "main" of the server
398 This is the "main" of the server. As explained in the \ref
399 GRAS_main_generation, you must not write any main()
400 function yourself. Instead, you just have to write a regular function
401 like this one which will act as a main.
406 [Back to \ref GRAS_ex_ping_toc]
408 \section GRAS_ex_ping_client 3) Client's code
410 \subsection GRAS_ex_ping_climain 3.a) Client's "main" function
412 This function is quite straightforward, and the inlined comments should
413 be enough to understand it.
415 \dontinclude gras/ping/ping_client.c
419 [Back to \ref GRAS_ex_ping_toc]
422 ---------------------------------------------------------------------
423 -------------------------- MM RPC -----------------------------------
424 ---------------------------------------------------------------------
426 /** \page GRAS_ex_mmrpc A simple RPC for matrix multiplication
428 This example implements a remote matrix multiplication. It involves a client
429 (creating the matrices and sending the multiplications requests) and a server
430 (computing the multiplication on client's behalf).
432 This example also constitutes a more advanced example of data description
433 mechanisms, since the message payload type is a bit more complicated than in
434 other examples such as the ping one (\ref GRAS_ex_ping).
436 It works the following way (not very different from the ping example):
437 - Both the client and the server register all needed messages and datatypes
438 - The server registers a callback to the "request" message, which computes
439 what needs to be and returns the result to the expeditor.
440 - The client creates two matrices, ask for their multiplication and check
443 This example resides in the <b>examples/gras/mmrpc/mmrpc.c</b> file. (See
444 the \ref GRAS_main_generation section if wondering why both the server
445 and the client live in the same source file)
447 \section GRAS_ex_mmrpc_toc Table of contents of the mmrpc example
448 - \ref GRAS_ex_mmrpc_common
449 - \ref GRAS_ex_mmrpc_header
450 - \ref GRAS_ex_mmrpc_dataregister
451 - \ref GRAS_ex_mmrpc_logdef
452 - \ref GRAS_ex_mmrpc_msgregister
453 - \ref GRAS_ex_mmrpc_matdump
454 - \ref GRAS_ex_mmrpc_server
455 - \ref GRAS_ex_mmrpc_serinc
456 - \ref GRAS_ex_mmrpc_sercb
457 - \ref GRAS_ex_mmrpc_sermain
458 - \ref GRAS_ex_mmrpc_client
459 - \ref GRAS_ex_mmrpc_cliinc
460 - \ref GRAS_ex_mmrpc_climain
465 \section GRAS_ex_mmrpc_common 1) Common code to the client and the server (mmrpc_common.c and mmrpc.h)
468 \subsection GRAS_ex_mmrpc_header 1.a) Module header (mmrpc.h)
470 This loads the gras header and declare the function's prototypes as well
473 \dontinclude gras/mmrpc/mmrpc.h
478 \subsection GRAS_ex_mmrpc_dataregister 1.b) Register the data types (mmrpc.h)
480 The messages involved in this example do use structures as payload,
481 so we have to declare it to GRAS. Hopefully, this can be done easily by enclosing
482 the structure declaration within a \ref GRAS_DEFINE_TYPE macro call. It will then copy this
483 declaration into an hidden string variable, which can be automatically parsed at
484 run time. Of course, the declaration is also copied unmodified by this macro, so that it
485 gets parsed by the compiler also.
487 There is some semantic that GRAS cannot guess alone and you need to <i>annotate</i>
488 your declaration to add some. For example, the ctn pointer can be a reference to an
489 object or a whole array (in which case you also has to specify its size). This is done
490 with the GRAS_ANNOTE call. It is removed from the text passed to the compiler, but it helps
491 GRAS getting some information about the semantic of your data. Here, it says that \a ctn is an
492 array, which size is the result of the operation \a rows * \a cols (with \a rows and \a cols
493 being the other fields of the structure).
495 Please note that this annotation mechanism is not as robust and cool as this example seems to
496 imply. If you want to use it yourself, you'd better use the exact right syntax, which is
497 detailed in the \ref GRAS_dd section.
499 \skip GRAS_DEFINE_TYPE
502 \subsection GRAS_ex_mmrpc_logdef 1.c) Logging category definition (mmrpc_common.c)
504 Let's first load the module header and declare a logging category (see
505 \ref XBT_log for more info on logging). This logging category does live
506 in this file (ie the required symbols are defined here and declared as
507 "extern" in any other file using them). That is why we use
508 \ref XBT_LOG_NEW_DEFAULT_CATEGORY here and
509 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY in mmrpc_client.c and mmrpc_server.c.
511 \dontinclude gras/mmrpc/mmrpc_common.c
515 \subsection GRAS_ex_mmrpc_msgregister 1.d) Register the messages (mmrpc_common.c)
517 This function, called by both the client and the server is in charge of
518 declaring the existing messages to GRAS. Note the use of the \ref gras_datadesc_by_symbol
519 function to parse and retrieve the structure declaration which were passed to \ref GRAS_DEFINE_TYPE
522 The datatype description builded that way can then be used to build an array datatype or
525 \skip register_messages
528 \subsection GRAS_ex_mmrpc_matdump 1.e) Helper debugging function (mmrpc_common.c)
530 This function dumps a matrix to screen for debugging.
536 [Back to \ref GRAS_ex_mmrpc_toc]
538 \section GRAS_ex_mmrpc_server 2) Server's code (mmrpc_server.c)
540 \subsection GRAS_ex_mmrpc_serinc 2.a) Server intial settings
542 All module symbols live in the mmrpc_common.c file. We thus have to
543 define \ref GRAS_DEFINE_TYPE_EXTERN to the preprocessor so that the
544 \ref GRAS_DEFINE_TYPE symbols don't get included here. Likewise, we use
545 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY to get the log category in here.
547 \dontinclude gras/mmrpc/mmrpc_server.c
551 \subsection GRAS_ex_mmrpc_sercb 2.b) The callback to the mmrpc message
553 Here is the callback run when the server receives any mmrpc message (this
554 will be registered later by the server). Note the way we get the message
555 payload. In the ping example, there was one additional level of pointer
556 indirection (see \ref GRAS_ex_ping_sercb). This is because the payload is
557 an array here (ie a pointer) whereas it is a scalar in the ping example.
559 \skip server_cb_request_handler
560 \until end_of_server_cb_request_handler
562 \subsection GRAS_ex_mmrpc_sermain 2.c) The "main" of the server
564 This is the "main" of the server. As explained in the \ref
565 GRAS_main_generation, you must not write any main()
566 function yourself. Instead, you just have to write a regular function
567 like this one which will act as a main.
572 [Back to \ref GRAS_ex_mmrpc_toc]
574 \section GRAS_ex_mmrpc_client 3) Client's code (mmrpc_client.c)
576 \subsection GRAS_ex_mmrpc_cliinc 2.a) Server intial settings
578 As for the server, some extra love is needed to make sure that automatic
579 datatype parsing and log categories do work even if we are using several
582 \dontinclude gras/mmrpc/mmrpc_client.c
586 \subsection GRAS_ex_mmrpc_climain 3.b) Client's "main" function
588 This function is quite straightforward, and the inlined comments should
589 be enough to understand it.
591 \dontinclude gras/mmrpc/mmrpc_client.c
595 [Back to \ref GRAS_ex_mmrpc_toc]
598 ---------------------------------------------------------------------
599 ---------------------------- Timers ---------------------------------
600 ---------------------------------------------------------------------
602 /** \page GRAS_ex_timer Some timer games
604 This example fools around with the GRAS timers (\ref GRAS_timer). It is
605 mainly a regression test, since it uses almost all timer features.
607 The main program registers a repetititive task and a delayed one, and
608 then loops until the <tt>still_to_do</tt> variables of its globals reach
609 0. The delayed task set it to 5, and the repetititive one decrease it
610 each time. Here is an example of output:
611 \verbatim Initialize GRAS
613 [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
614 [1108335471] Programming the delayed_action for after 2.000000 sec
615 [1108335471] Have a rest
616 [1108335472] Canceling the delayed_action.
617 [1108335472] Re-programming the delayed_action for after 2.000000 sec
618 [1108335472] Repetitive_action has nothing to do yet
619 [1108335473] Repetitive_action has nothing to do yet
620 [1108335473] delayed_action setting globals->still_to_do to 5
621 [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
622 [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
623 [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
624 [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
625 [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
626 Exiting GRAS\endverbatim
629 - \ref GRAS_ex_timer_decl
630 - \ref GRAS_ex_timer_delay
631 - \ref GRAS_ex_timer_repeat
632 - \ref GRAS_ex_timer_main
636 \section GRAS_ex_timer_decl 1. Declarations and headers
640 \section GRAS_ex_timer_delay 2. Source code of the delayed action
641 \skip repetitive_action
642 \until end_of_repetitive_action
644 \section GRAS_ex_timer_repeat 3. Source code of the repetitive action
646 \until end_of_delayed_action
648 \section GRAS_ex_timer_main 4. Source code of main function