Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Not this one
[simgrid.git] / doc / gtut-tour.doc
1
2 /** 
3 @page GRAS_tut_tour GRAS initiatic tour
4
5
6 \section GRAS_tut_tour_what What will you find here
7
8 On this page, you will learn all you need to write your own GRAS
9 applications, from the installation of the framework to the use of all
10 features available in GRAS.
11
12  - \ref GRAS_tut_tour_install
13  - \ref GRAS_tut_tour_setup\n It explains the code layout you should setup to
14    build a GRAS application as well as the role and content of the several
15    files needed.
16    - \ref GRAS_tut_tour_setup_C
17    - \ref GRAS_tut_tour_setup_plat
18    - \ref GRAS_tut_tour_setup_deploy
19    - \ref GRAS_tut_tour_setup_glue
20    - \ref GRAS_tut_tour_setup_make
21    - \ref GRAS_tut_tour_setup_start
22  - \ref GRAS_tut_tour_simpleexchange
23    - \ref GRAS_tut_tour_simpleexchange_msgtype
24    - \ref GRAS_tut_tour_simpleexchange_socks
25    - \ref GRAS_tut_tour_simpleexchange_exchange
26    - \ref GRAS_tut_tour_simpleexchange_recaping
27  - \ref GRAS_tut_tour_args
28    
29 <hr>
30 \section GRAS_tut_tour_install Lesson 0: Installing GRAS
31
32 Since GRAS is technically part of the SimGrid project, you have to install
33 SimGrid to install GRAS. Doing so is explained in the relevant FAQ section
34 (\ref faq_installation). 
35
36 Newcommers should install the stable release from the tarball, since the cvs
37 snapshots may suffer from (additionnal;) stability issues. Only go for the CVS if you
38 really need features not present in the stable releases yet.
39
40 <hr>
41 \section GRAS_tut_tour_setup Lesson 1: Setting up your own project
42
43 Any GRAS project should be constituted of at least 3 files, and possibly
44 much more.
45
46   - <tt>&lt;project&gt;.c</tt>: A source file providing the source code of your
47     processes.
48     
49   - <tt>&lt;platform&gt;.xml</tt>: A platform description file. It describes
50     the virtual platform you want to run your application onto following the
51     SurfXML formatting so that the simulator can parse it. This file is only
52     needed in SG, and you don't need any to run on real platforms (of
53     course). The simplest is to use one of the pre-existing one.
54     
55   - <tt>&lt;project&gt;.xml</tt>: A deployment file. It describes which of
56     your processes to start, on which machine and with which arguments.
57     
58   - A makefile is often needed, too, even if it's not mandatory.
59
60 If we start a project called <tt>test</tt>, we have to write 3 files:
61 <tt>test.c</tt>, <tt>platform.xml</tt> and <tt>test.xml</tt>
62
63 \subsection GRAS_tut_tour_setup_C The C source file
64
65 Let's look at the C source file first. It should contain one main function
66 for each type of processes in your overlay. Let's assume that you want to
67 code a simple client/server communication. For this, the source file should
68 read as:
69
70 \verbatim #include <gras.h>
71
72 int client(int argc, char *argv[]) {
73   ...
74 }
75
76 int server(int argc, char *argv[]) {
77   ...
78 }
79 \endverbatim
80
81 Note that each of the processes's main function have the exact same
82 prototype of the classical <tt>main()</tt> function in C. 
83
84 This is on purpose, each of them can assume this role when running in RL.
85 But you shouldn't write a main() function yourself since all processes will
86 run as threads within the same regular process in simulation mode. That is
87 why the real <tt>main</tt> function of GRAS programs are generated
88 automatically. This will be detailled in time (section \ref
89 GRAS_tut_tour_setup_glue), but for now just note the similarity between the
90 "main" functions you have to write for each processes and a "real main"
91 function.
92
93 Then, each process must initialize the GRAS framework at the beginning (with
94 \ref gras_init) and should finalize it at the end (with \ref gras_exit). 
95
96 You should pass to \ref gras_init the <tt>argc</tt> and <tt>argv</tt> you
97 received in your "main" function so that the users of your application can
98 pass some configuration flags to the framework.
99
100 It is not enough to have one of the processes initializing the framework
101 since in RL, each of them will run on a different host. If you use some AMOK
102 modules, you have to initialize them in each process too.
103
104 The source file then reads: \include 1-bones.c
105
106 That's it. You have a working GRAS application with two processes. They
107 don't do anything useful, but that's a beginning. Let's see how to bring
108 them to life.
109
110 \subsection GRAS_tut_tour_setup_plat The platform file
111
112 The platform file is used by the simulator to know about the existing hosts
113 and their interactions. Its exact syntax is at the same time very simple and
114 a bit beyond the topic of this document. Here is a very simple example
115 describin two hosts named <i>Jacquelin</i> and <i>Boivin</i> and how they
116 are interconnected.
117
118 \include gtut-platform.xml
119
120 At this point, you should not try to write your own platform file, but use
121 one of the existing ones. There is a few of them in the examples/msg
122 directory of the project. The only information we need from those files are
123 the names of the existing hosts. It will be mandatory to write the
124 deployment file.
125
126 \subsection GRAS_tut_tour_setup_deploy The deployment file
127
128 This file explains which of your processes should be started on the
129 different hosts. It is mainly used in simulation. In real life, you will
130 have to start your processes manually (see below). We we dream of a system
131 able to apply a deployment file in real life and TakTuk may be the right
132 tool for this, but this is still to be done.
133
134 Here is an example of such file, describing that a <tt>server</tt> process
135 must be started onto the <tt>Jacquelin</tt> host and a <tt>client</tt>
136 process must be started on the <tt>Boivin</tt> host.
137
138 \include test.xml
139
140 Actually, you should write such a file also if you only plan to use GRAS in
141 RL since this file is also used to glue your code to GRAS, as explained in
142 the next section.
143
144 \subsection GRAS_tut_tour_setup_glue Glueing things together
145
146 As explained above, you shouldn't write any real <tt>main</tt> function
147 since its content depends on whether you run in RL ou in SG. Instead, you
148 use a tool <tt>gras_stub_generator</tt> to get the proper glue around your
149 code generated. If you installed SimGrid in a regular place, this program is
150 now in your path. Its source resides in the tools/gras/ directory of the
151 archive, if you wonder.
152
153 Here is the calling syntax:     
154 \verbatim gras_stub_generator <project_name> <deployment_file.xml>\endverbatim
155
156 It parses the deployment file (called <tt>test.xml</tt> in our example),
157 searching for all the kind of processes you have in your project. It
158 then generates the following C files:
159
160  - a <tt>_&lt;project_name&gt;_&lt;process_kind&gt;.c</tt> file for each process kind you
161    have.\n
162    They are used to launch your project in real life. They
163    contain a main() in charge of initializing the GRAS infrastructure and
164    launching your code afterward.
165  - a <tt>_&lt;project_name&gt;_simulator.c</tt> file.\n
166    This file is suited to the simulation mode. It contains a main()
167    function initializing the simulator and launching your project within.
168  - a <tt>&lt;project_name&gt;.mk</tt> file.\n
169    This is a makefile to regenerate any files on need. See next section.
170
171 In our example, we will thus obtain <tt>_test_server.c</tt>,
172 <tt>_test_client.c</tt>, <tt>_test_simulator.c</tt> and <tt>test.mk</tt>.
173
174 There is a pitfall: no verification is made on your actual source code, so
175 if you have a typo on the process name in the deployment file, the generated
176 code will be wrong, and the linker will spit error messages at you. Also
177 remember that those names are in fact C function names, so they are
178 case-sensitive.
179
180 \subsection GRAS_tut_tour_setup_make A typical Makefile
181
182 Now, we want to compile all the source files to build the actual binaries.
183 It can be done manually, but it is much more convenient to use a makefile.
184 Fortunately, gras_stub_generator generates a makefile for you under the name
185 <tt>&lt;project&gt;.mk</tt>. This file is sufficient for now. To compile our test
186 application, just type:
187 \verbatim make -f test.mk \endverbatim
188
189 You may want to rename this file to Makefile so that typing <tt>make</tt>
190 without argument becomes sufficient. In any case, do not edit this file
191 without renaming it, or your changes will get overwritten at the next glue
192 generation.
193
194 If you already have a Makefile (or a Makefile.am for automake users), you
195 can also add the following chunk at the end of your file:
196 \verbatim NAME=your_project_name
197  PROCESSES=list of processes type in your project
198
199  $(foreach proc, $(PROCESSES), _$(NAME)_$(proc).c) _$(NAME)_simulator.c: $(NAME).c $(NAME)_deployment.xml
200         path/to/gras_stub_generator $(NAME) $(NAME)_deployment.xml >/dev/null
201 \endverbatim
202
203 A simpler solution in our example would be to add:
204 \verbatim _test_client.c _test_server.c _test_simulator.c: test.c test.xml
205         path/to/gras_stub_generator test test.xml >/dev/null
206 \endverbatim
207
208
209
210 \subsection GRAS_tut_tour_setup_start Actually running the processes
211
212 There is nothing to know to start your processes in RL. Simply call the
213 generated binaries, and that's it. To start the simulation, simply call:
214 \verbatim ./<project>_simulator platform.xml deployment.xml\endverbatim
215
216 Here is an example of execution: \include 1-bones.output
217
218 That's it. You are done with this lesson and can now write, build and
219 execute GRAS applications as long as they don't do anything ;) Simply read on
220 to add some flesh on these bones.
221
222 (back to the top of the \ref GRAS_tut_tour)
223 <hr>
224
225 \section GRAS_tut_tour_simpleexchange Lesson 2: Exchanging simple messages
226
227 \subsection GRAS_tut_tour_simpleexchange_msgtype Declaring the messages to be exchanged
228
229 We will now see how to exchange messages between hosts. As explained in
230 section \ref GRAS_tut_intro_model, every GRAS message is (strongly) typed. A
231 message type is described by its name and the datatype of the data it can
232 convey. Each process which may exchange a given type of message should
233 declare it before sending or receiving it. If the description used by the
234 sender doesn't match the one used by the receiver, you'll get into trouble.
235 Fortunately, such discrepency will be detected in SG.
236
237 We won't convey any payload in this lesson, so we just have to give the name
238 of message to declare them:
239 \dontinclude 2-simple.c
240 \skip gras_msgtype_declare
241 \until gras_msgtype_declare
242
243 Remember that all processes should declare the message types they use.
244
245 \subsection GRAS_tut_tour_simpleexchange_socks Identifying peers you want to communicate with
246
247 Then, you need to specify with who you want to communicate. This is done
248 by opening sockets. GRAS sockets are loosely inspirated by the regular BSD
249 sockets, but with several simplifications.
250
251 If you want to eventually receive messages, you have to open a so-called
252 <i>server socket</i>. Actually, any GRAS process should open a server socket
253 since it will allows to identify it uniquely in the system. A socket is
254 defined by an host name and a port number (just like with BSD sockets).
255
256 Since server socket are on the current host, opening a socket to receive
257 messages on the port 12345 is as easy as:
258 \skip gras_socket_server
259 \until gras_socket_server
260
261 Hardcoding port numbers that way may lead to difficulties on RL (at least)
262 since at most one process can listen on a given port. So, if you can, prefer
263 the \ref gras_socket_server_range, which picks a working port from a range
264 of value. Of course, if you want your processes to find each others, at
265 least one port must be hardcoded in the system. Then, any other processes
266 contact the one listening on that port, which acts as a coordinator.
267
268 Our client should also open a server socket, but the picked port don't
269 matter, so we use:
270 \skip gras_socket_server
271 \until gras_socket_server
272
273 It will select a port between 1024 (ports below 1024 are reserved under
274 UNIX) and 10000. You can safely ignore the two last arguments for now and
275 pass 0.
276
277 So, you now know how to create sockets allowing to receive messages. To send
278 messages, you have to create a so-called <i>client socket</i>. For this, use
279 \ref gras_socket_client with the hostname and the port of the process you
280 want to contact as arguments. Our client should simply do:
281
282 \dontinclude 2-simple.c
283 \skip socket_client
284 \until socket_client
285
286 The corresponding server socket must be opened before any client socket can
287 connect to it. It is thus safe to add a little delay before creating the
288 client socket. But you cannot use the classical sleep() function for this,
289 or you will delay the simulator in SG, not your processes. Use \ref
290 gras_os_sleep instead.
291
292 \subsection GRAS_tut_tour_simpleexchange_exchange Actually exchanging messages
293
294 GRAS offers a plenty of ways to communicate. The simple one is to use \ref
295 gras_msg_send on the sender side, and \ref gras_msg_wait on the receiver side.
296
297 \ref gras_msg_send expects 3 arguments: the socket on which to send the
298 message, the message type, and a pointer to the actual content of the
299 message. The simplest way to retrive a message type from its name is to use
300 \ref gras_msgtype_by_name. Since we don't have any payload, this becomes:
301
302 \dontinclude 2-simple.c
303 \skip msg_send
304 \until msg_send
305
306 \ref gras_msg_wait accepts 4 arguments. The first one is the delay you are
307 disposed to wait for messages, while the the type of message you are
308 expecting. Then come output arguments. The third argument should be the
309 address of a gras_socket_t variable which will indicate who wrote the
310 message you received while the last argument is where to put the payload.
311
312 Since our server is willing to wait up to 60 seconds for a message, the
313 following will do it:
314 \dontinclude 2-simple.c
315 \skip msg_wait
316 \until msg_wait
317
318 \subsection GRAS_tut_tour_simpleexchange_recaping Recaping everything together
319
320 Here is the complete code of this example. Note the use of the functions
321 \ref gras_socket_my_port, \ref gras_socket_peer_name and \ref
322 gras_socket_peer_port to retrieve information about who you are connected to.
323
324 \include 2-simple.c
325
326 Here is the output of the simulator. Note that \ref gras_socket_peer_port
327 actually returns the port number of the <i>server</i> of the peer. This may
328 sound a bit strange to BSD experts, but it is actually really useful: you
329 can store this value, and contact your peer afterward passing this number to
330 \ref gras_socket_client .
331 \include 2-simple.output
332
333 Here we are, you now know how to exchange messages between peers. There is
334 still a large room for improvement, such as adding payload to messages.
335 Moreover the most problematic issue is that this code does not work in RL
336 since we hardcoded the server hostname in the client code. Next lesson will
337 learn you how to pass arguments to your processes to overcome this situation.
338
339 <hr>
340 \section GRAS_tut_tour_args Lesson 3: Passing arguments to the processes (in SG)
341
342 \section GRAS_tut_tour_todo TODO
343
344 - Lesson 4: Passing arguments to processes in the deployment file
345 - Lesson 5: Callback ("I got a message from %s")
346 - Lesson 6: Globals (for a kill message)
347 - Lesson  : Timers
348 - Lesson 7: Using logs
349
350 - Lesson 8: Exchanging simple data through ping-pong
351 - Lesson 9: More complex data description (automatic parsing, manual description) and example
352
353 - Lesson 10: Splitting in several files
354
355 - Lesson 11: RPC mecanism and dealing with exceptions
356
357 - Lesson 12: Debuging GRAS programs
358
359 */