Logo AND Algorithmique Numérique Distribuée

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