Logo AND Algorithmique Numérique Distribuée

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