Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rework the GRAS tree and kill some of the old navbar tries
[simgrid.git] / doc / module-gras.doc
1 #####################################################################
2 ###########################  CORE ###################################
3 #####################################################################
4
5 /** \addtogroup GRAS_API
6   
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
27           portability layer.
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.
44        - \ref GRAS_compile
45      
46           
47     \section GRAS_example Examples
48       
49     There is for now rather few examples of GRAS, but it's better than
50     nothing, isn't it?
51     
52        - \ref GRAS_ex_ping
53        - \ref GRAS_ex_mmrpc
54        - \ref GRAS_ex_timer
55
56     @{ */
57        /** @defgroup GRAS_comm    Communication facilities */
58        /** @defgroup GRAS_run     Virtualization */
59        /** @defgroup GRAS_code    Project and code management */
60        /** @defgroup GRAS_ex      Examples */
61 /** @} */
62 #####################################################################
63 /** @addtogroup GRAS_comm
64
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
70    beforehand.
71
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").
75
76     @{ */     
77        /** @defgroup GRAS_dd      Data description      */       
78        /** @defgroup GRAS_sock    Sockets               */           
79        /** @defgroup GRAS_msg     Messages              */               
80        /** @defgroup GRAS_timer   Timers                */               
81      
82 /** @} */
83 #####################################################################
84 /** @addtogroup GRAS_run
85
86     @{ */     
87          
88        /** @defgroup GRAS_globals Globals               */ 
89        /** @defgroup GRAS_emul    Emulation support */ 
90        /** @defgroup GRAS_virtu   Syscalls              */ 
91
92 /** @} */
93
94 #####################################################################
95 /** @addtogroup GRAS_code
96
97     \htmlonly <!-- 
98       DOXYGEN_NAVBAR_LABEL="Project management"
99       DOXYGEN_NAVBAR_CHILD "main() and GRAS"=GRAS_main_generation.html
100       DOXYGEN_NAVBAR_CHILD "Compiling your GRAS project"=GRAS_compile.html
101     --> \endhtmlonly
102 */
103
104 #####################################################################
105 /** @addtogroup GRAS_ex
106
107     \htmlonly <!-- 
108       DOXYGEN_NAVBAR_CHILD "Ping-Pong"=GRAS_ex_ping.html
109       DOXYGEN_NAVBAR_CHILD "RPC"=GRAS_ex_mmrpc.html
110       DOXYGEN_NAVBAR_CHILD "Timers"=GRAS_ex_timer.html
111     --> \endhtmlonly
112 */
113
114 #####################################################################
115 #########################  EXTRA PAGES ##############################
116 #####################################################################
117
118 ---------------------------------------------------------------------
119 --------------------- main() generation -----------------------------
120 ---------------------------------------------------------------------
121
122 /** \page GRAS_main_generation main function
123
124     \section GRAS_maingen_toc Table of content
125      
126      - \ref GRAS_maingen_intro
127      - \ref GRAS_maingen_script
128      - \ref GRAS_maingen_make
129     
130     <hr>
131
132     \section GRAS_maingen_intro What's the matter with main() functions in GRAS?
133
134     In simulation mode, all processes are run as thread of the same process
135     while they are real processes in the real life. Unfortunately, the main
136     function of a real process must be called <tt>main</tt> while this
137     function must not use this name for threads.
138     
139     To deal with this, you should call the main function of your processes
140     with another name (usually, the process function such as client, server,
141     or such). Then GRAS can generate the wrapper functions adapted to the
142     real and simulated modes.
143
144     \section GRAS_maingen_script Generating the main()s automatically
145     
146     This is done by the gras_stub_generator program, which gets installed on
147     <tt>make install</tt> (the source resides in the tools/gras/ directory).
148     Here is the calling syntax: 
149     \verbatim gras_stub_generator <project_name> <deployment_file.xml>\endverbatim
150     
151     It parses the deployment file, searching for all the kind of processes
152     you have in your project. It then generates the following C files:
153      - a <tt>_<project_name>_<process_kind>.c</tt> file for each process kind you
154        have\n
155        They are used to launch your project in real life. They
156        contain a main() in charge of initializing the GRAS infrastructure and
157        launching your code afterward.
158      - a <tt>_<project_name>_simulator.c</tt> file.\n
159        This file is suited to the simulation mode. It contains a main()
160        function initializing the simulator and launching your project within.
161     
162     For this to work, the name of process described in your deployment file
163     should match the name of a function in your code, which prototype is for
164     example: \verbatim int client(int argc,char *argv[]);\endverbatim
165     
166     Unfortunately, all this is still partially documented. I guess I ought
167     to improve this situation somehow. In the meanwhile, check the generated 
168     code and maybe also the GRAS \ref GRAS_example, sorry. 
169         
170     \section GRAS_maingen_make Integration within an hand-made Makefile 
171     
172     The easiest to set it up is to add the following chunk at the end of
173     your Makefile (or Makefile.am), putting the right values into NAME and
174     PROCESSES.
175 \verbatim NAME=your_project_name
176  PROCESSES=list of processes type in your project
177
178  $(foreach proc, $(PROCESSES), _$(NAME)_$(proc).c) _$(NAME)_simulator.c: $(NAME).c $(NAME)_deployment.xml
179         path/to/gras_stub_generator $(NAME) $(NAME)_deployment.xml >/dev/null
180 \endverbatim
181
182     Of course, your personal millage may vary. For the \ref GRAS_ex_ping, may read:
183 \verbatim _ping_client.c _ping_server.c _ping_simulator.c: ping.c ping_deployment.xml 
184         $(top_srcdir)/tools/gras/gras_stub_generator ping ping_deployment.xml >/dev/null
185 \endverbatim
186
187    \warning 
188    Actually, gras_stub_generator also generates some makefiles both for
189    local compilation and remote code distribution and installation. See the
190    section \ref GRAS_compile for more details.
191
192 */
193
194 ---------------------------------------------------------------------
195 ------------------------- Compiling ---------------------------------
196 ---------------------------------------------------------------------
197
198 /** \page GRAS_compile Compiling your project
199
200     As explained in section \ref GRAS_main_generation, the
201     gras_stub_generator tool can be used to generate the system
202     initialization code in your projet. While we were at this, this tool
203     also generates the makefiles you will need to compile your project
204     properly.
205     
206     Code source deployment and remote compilation also constitutes a
207     challenging area in distributed applications development. The GRASPE
208     (GRAS Platform Expender) tool was designed to make this less painful.
209
210     \section GRAS_compile_toc Table of content
211     
212       - \ref GRAS_compile_local
213         - \ref GRAS_compile_local_install
214         - \ref GRAS_compile_local_helpfiles
215         - \ref GRAS_compile_local_makefile
216       - \ref GRAS_compile_remote
217       
218     <hr>
219     
220     \section GRAS_compile_local Local compilation of GRAS projects
221     
222     \subsection GRAS_compile_local_install Installing SimGrid and GRAS
223     
224     To compile locally a GRAS project, you first need to install SimGrid on
225     your machine. Use the --prefix flag to the configure script to specify
226     where you want to install the toolkit (refere to section \ref
227     faq_compiling for more information)
228     
229     \subsection GRAS_compile_local_helpfiles Simulation description files
230     
231     Then, you will probably need to write a platform description file and
232     application deployment description file to feed the simulator with. This
233     part is unfortunatelly not documented enough. Files examples can be
234     found along with the MSG \ref MSG_ex_master_slave example. 
235
236     \note yes, both platform and application description files are portable
237     between MSG and GRAS. Actually, there depend on the SURF, not on the
238     programming environment you use.
239     
240     For the first try, you could probably reuse the provided platform file
241     as is while you will need to adapt the application file to fit your
242     needs. 
243     
244     To generate new platform files, we usually use the Tiers Topology
245     Generator (ask google about it) and annotate the generated graph with
246     home-made scripts to let them fit the SURF. Those scripts live in the
247     tools/platform_generation/ directory of the distribution.
248     
249     \subsection GRAS_compile_local_makefile Generating a Makefile usable for your project
250     
251     From the information contained in the application description file, the
252     gras_stub_generator tool can create a Makefile which can be used to
253     seamlessly compile your project. Just go to the directory containing all
254     your project files, and type:
255     
256 \verbatim path/to/gras_stub_generator [project_name] [application_deployment.file] >/dev/null
257 \endverbatim
258
259     The first argument is the name of your project, such as
260     "MyLovelyApplication" while the second one is the application deployment
261     file. 
262     
263     Several files get generated by this command. One C file per kind of
264     process in your project (such as "master" and "slave") plus one C file
265     for simulating your project. All those files are (or should ;) described
266     in section \ref GRAS_main_generation.
267     
268     The most intersting file in this context is
269     [project_name].Makefile.local (you can safely ignore the others for
270     now). To use it, simply type (from your project main directory):
271     
272 \verbatim GRAS_ROOT=/path/to/simgrid/installation make -f [project_name].Makefile.local
273 \endverbatim
274     
275     And that's it, all the binaries are built and linked against the correct
276     libraries.
277     
278     \section GRAS_compile_remote Distribution and remote compilation of GRAS projects
279     
280     Actually, there is two somehow parallel ways to do so since both Arnaud
281     and Martin gave it a try. Merging both approaches is underway. As usual,
282     if you want to help, you're welcome ;)
283     
284 */
285
286 #####################################################################
287 #########################  EXAMPLES #################################
288 #####################################################################
289
290 ---------------------------------------------------------------------
291 ------------------------- Ping Pong ---------------------------------
292 ---------------------------------------------------------------------
293
294 /** \page GRAS_ex_ping The classical Ping-Pong in GRAS
295
296     This example implements the very classical ping-pong in GRAS. It
297     involves a client (initiating the ping-pong) and a server (answering to 
298     client's requests).
299
300     It works the following way:
301      - Both the client and the server register all needed messages
302      - The server registers a callback to the ping message, which sends pong
303        to the expeditor
304      - The client sends the ping message to the server, and waits for the
305        pong message as an answer.
306  
307     This example resides in the <b>examples/gras/ping/ping.c</b> file. Yes, both
308     the code of the client and of the server is placed in the same file. See
309     the \ref GRAS_main_generation section if wondering.
310
311     \section GRAS_ex_ping_toc Table of contents of the ping example
312       - \ref GRAS_ex_ping_common
313         - \ref GRAS_ex_ping_initial
314         - \ref GRAS_ex_ping_register
315       - \ref GRAS_ex_ping_server
316         - \ref GRAS_ex_ping_serdata
317         - \ref GRAS_ex_ping_sercb
318         - \ref GRAS_ex_ping_sermain
319       - \ref GRAS_ex_ping_client
320         - \ref GRAS_ex_ping_climain
321         
322     <hr>
323
324     \dontinclude gras/ping/ping.c
325     
326     \section GRAS_ex_ping_common 1) Common code to the client and the server 
327     
328     \subsection GRAS_ex_ping_initial 1.a) Initial settings
329     
330     Let's first load the gras header and declare a logging category (see
331     \ref XBT_log for more info on logging).
332     
333     \skip include
334     \until XBT_LOG
335
336     \subsection GRAS_ex_ping_register 1.b) Register the messages
337     
338     This function, called by both the client and the server is in charge of
339     declaring the existing messages to GRAS. Since the payload does not
340     involve any newly created types but only int, this is quite easy. 
341     (to exchange more complicated types, see \ref GRAS_dd or 
342     \ref GRAS_ex_mmrpc for an example).
343     
344     \skip register_messages
345     \until }
346
347     [Back to \ref GRAS_ex_ping_toc]
348
349     \section GRAS_ex_ping_server 2) Server's code
350     
351     \subsection GRAS_ex_ping_serdata 2.a) The server's globals
352
353     In order to ensure the communication between the "main" and the callback
354     of the server, we need to declare some globals. We have to put them in a
355     struct definition so that they can be handled properly in GRAS (see the
356     \ref GRAS_globals for more info).
357
358     \skip typedef struct
359     \until }
360     
361     \subsection GRAS_ex_ping_sercb 2.b) The callback to the ping message
362
363     Here is the callback run when the server receives any ping message (this
364     will be registered later by the server).
365     
366     \skip server_cb_ping_handler
367     \until end_of_server_cb_ping_handler
368
369     \subsection GRAS_ex_ping_sermain 2.c) The "main" of the server
370     
371     This is the "main" of the server. As explained in the \ref
372     GRAS_main_generation, you must not write any main()
373     function yourself. Instead, you just have to write a regular function
374     like this one which will act as a main.
375     
376     \skip server
377     \until end_of_server
378
379     [Back to \ref GRAS_ex_ping_toc]
380     
381     \section GRAS_ex_ping_client 3) Client's code
382     
383     \subsection GRAS_ex_ping_climain 3.a) Client's "main" function
384     
385     This function is quite straightforward, and the inlined comments should
386     be enough to understand it.
387
388     \skip client
389     \until end_of_client
390
391     [Back to \ref GRAS_ex_ping_toc]
392  */
393
394 ---------------------------------------------------------------------
395 -------------------------- MM RPC -----------------------------------
396 ---------------------------------------------------------------------
397
398 /** \page GRAS_ex_mmrpc A simple RPC for matrix multiplication
399
400     This example implements a remote matrix multiplication. It involves a client 
401     (creating the matrices and sending the multiplications requests) and a server 
402     (computing the multiplication on client's behalf).
403
404     This example also constitutes a more advanced example of data description 
405     mechanisms, since the message payload type is a bit more complicated than in 
406     other examples such as the ping one (\ref GRAS_ex_ping).
407
408     It works the following way (not very different from the ping example):
409      - Both the client and the server register all needed messages and datatypes
410      - The server registers a callback to the "request" message, which computes
411        what needs to be and returns the result to the expeditor.
412      - The client creates two matrices, ask for their multiplication and check 
413        the server's answer.
414  
415     This example resides in the <b>examples/gras/mmrpc/mmrpc.c</b> file. (See
416     the \ref GRAS_main_generation section if wondering why both the server
417     and the client live in the same source file)
418
419     \section GRAS_ex_mmrpc_toc Table of contents of the mmrpc example
420       - \ref GRAS_ex_mmrpc_common
421         - \ref GRAS_ex_mmrpc_initial
422         - \ref GRAS_ex_mmrpc_dataregister
423         - \ref GRAS_ex_mmrpc_msgregister
424       - \ref GRAS_ex_mmrpc_server
425         - \ref GRAS_ex_mmrpc_sercb
426         - \ref GRAS_ex_mmrpc_sermain
427       - \ref GRAS_ex_mmrpc_client
428         - \ref GRAS_ex_mmrpc_climain
429         
430     <hr>
431
432     \dontinclude gras/mmrpc/mmrpc.c
433     
434     \section GRAS_ex_mmrpc_common 1) Common code to the client and the server 
435     
436     \subsection GRAS_ex_mmrpc_initial 1.a) Initial settings
437     
438     Let's first load the gras header, specify the matrix size and declare a 
439     logging category (see \ref XBT_log for more info on logging).
440     
441     \skip include
442     \until XBT_LOG
443
444     \subsection GRAS_ex_mmrpc_dataregister 1.b) Register the data types
445
446     The messages involved in this example do use structures as payload, 
447     so we have to declare it to GRAS. Hopefully, this can be done easily by enclosing 
448     the structure declaration within a \ref GRAS_DEFINE_TYPE macro call. It will then copy this 
449     declaration into an hidden string variable, which can be automatically parsed at 
450     run time. Of course, the declaration is also copied unmodified by this macro, so that it
451     gets parsed by the compiler also. 
452
453     There is some semantic that GRAS cannot guess alone and you need to  <i>annotate</i>
454     your declaration to add some. For example, the ctn pointer can be a reference to an 
455     object or a whole array (in which case you also has to specify its size). This is done 
456     with the GRAS_ANNOTE call. It is removed from the text passed to the compiler, but it helps
457     GRAS getting some information about the semantic of your data. Here, it says that \a ctn is an 
458     array, which size is the result of the operation \a rows * \a cols (with \a rows and \a cols 
459     being the other fields of the structure). 
460
461     Please note that this annotation mechanism is not as robust and cool as this example seems to 
462     imply. If you want to use it yourself, you'd better use the exact right syntax, which is 
463     detailed in the \ref GRAS_dd section.
464
465     \skip GRAS_DEFINE_TYPE
466     \until matrix_t
467
468     \subsection GRAS_ex_mmrpc_msgregister 1.c) Register the messages
469     
470     This function, called by both the client and the server is in charge of
471     declaring the existing messages to GRAS. Note the use of the \ref gras_datadesc_by_symbol 
472     function to parse and retrieve the structure declaration which were passed to \ref GRAS_DEFINE_TYPE 
473     above. 
474
475     The datatype description builded that way can then be used to build an array datatype or 
476     to declare messages.
477     
478     \skip register_messages
479     \until }
480
481     [Back to \ref GRAS_ex_mmrpc_toc]
482
483     \section GRAS_ex_mmrpc_server 2) Server's code
484     
485     \subsection GRAS_ex_mmrpc_sercb 2.a) The callback to the mmrpc message
486
487     Here is the callback run when the server receives any mmrpc message (this
488     will be registered later by the server). Note the way we get the message 
489     payload. In the ping example, there was one additional level of pointer 
490     indirection (see \ref GRAS_ex_ping_sercb). This is because the payload is
491     an array here (ie a pointer) whereas it is a scalar in the ping example.
492     
493     \skip server_cb_request_handler
494     \until end_of_server_cb_request_handler
495
496     \subsection GRAS_ex_mmrpc_sermain 2.b) The "main" of the server
497     
498     This is the "main" of the server. As explained in the \ref
499     GRAS_main_generation, you must not write any main()
500     function yourself. Instead, you just have to write a regular function
501     like this one which will act as a main.
502     
503     \skip server
504     \until end_of_server
505     
506     [Back to \ref GRAS_ex_mmrpc_toc]
507
508     \section GRAS_ex_mmrpc_client 3) Client's code
509     
510     \subsection GRAS_ex_mmrpc_climain 3.a) Client's "main" function
511     
512     This function is quite straightforward, and the inlined comments should
513     be enough to understand it.
514
515     \skip client
516     \until end_of_client
517
518     [Back to \ref GRAS_ex_mmrpc_toc]
519   */
520
521 ---------------------------------------------------------------------
522 ---------------------------- Timers ---------------------------------
523 ---------------------------------------------------------------------
524
525 /** \page GRAS_ex_timer Some timer games
526
527     This example fools around with the GRAS timers (\ref GRAS_timer). It is
528     mainly a regression test, since it uses almost all timer features.
529     
530     The main program registers a repetititive task and a delayed one, and
531     then loops until the <tt>still_to_do</tt> variables of its globals reach
532     0. The delayed task set it to 5, and the repetititive one decrease it
533     each time. Here is an example of output:
534 \verbatim Initialize GRAS
535  Initialize XBT
536  [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
537  [1108335471] Programming the delayed_action for after 2.000000 sec
538  [1108335471] Have a rest
539  [1108335472] Canceling the delayed_action.
540  [1108335472] Re-programming the delayed_action for after 2.000000 sec
541  [1108335472] Repetitive_action has nothing to do yet
542  [1108335473] Repetitive_action has nothing to do yet
543  [1108335473] delayed_action setting globals->still_to_do to 5
544  [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
545  [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
546  [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
547  [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
548  [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
549  Exiting GRAS\endverbatim
550
551     Source code:
552      - \ref GRAS_ex_timer_decl
553      - \ref GRAS_ex_timer_delay
554      - \ref GRAS_ex_timer_repeat
555      - \ref GRAS_ex_timer_main
556
557     \dontinclude timer.c
558     
559     \section GRAS_ex_timer_decl   1. Declarations and headers
560     \skip include
561     \until my_globals
562     
563     \section GRAS_ex_timer_delay  2. Source code of the delayed action
564     \skip repetitive_action
565     \until end_of_repetitive_action
566     
567     \section GRAS_ex_timer_repeat 3. Source code of the repetitive action
568     \skip delayed_action
569     \until end_of_delayed_action
570     
571     \section GRAS_ex_timer_main   4. Source code of main function
572     \skip client
573     \until end_of_client
574 */