Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
97e9a0fcecd80bb1f0f637476cd6aced16e7a76e
[simgrid.git] / doc / doxygen / tutorial.doc
1 /*! @page tutorial SimGrid First Tutorial
2
3 SimGrid is a toolkit providing the core functionalities for the
4 simulation of distributed applications in heterogeneous distributed
5 environments.
6
7 The project goal is both to facilitate research and to help improving
8 real applications in the area of distributed and parallel systems,
9 ranging from simple network of workstations to Computational Grids to
10 Clouds and to supercomputers.
11
12 \tableofcontents
13
14 \section  Scenario
15 The goal of this practical session is to illustrate various usage of
16 the MSG interface. To this end we will use the following simple setting:
17
18 > Assume we have a (possibly large) bunch of (possibly large) data to
19 > process and which originally reside on a server (a.k.a. master). For
20 > sake of simplicity, we assume all input file require the same amount
21 > of computation. We assume the server can be helped by a (possibly
22 > large) set of worker machines. What is the best way to organize the
23 > computations ?
24
25 Although this looks like a very simple setting it raises several
26 interesting questions:
27
28 - Which algorithm should the master use to send workload?
29
30     The most obvious algorithm would be to send tasks to workers in a
31     round-robin fashion. This is the initial code we provide you.
32
33     A less obvious but probably more efficient approach would be to set up
34     a request mechanism where a client first ask for tasks, which allows
35     the server to decide which request to answer and possibly to send
36     the tasks to the fastest machines. Maybe you can think of a
37     smarter mechanism...
38
39 - How many tasks should the client ask for?
40
41     Indeed, if we set up a request mechanism so that workers only
42     send request whenever they have no more task to process, they are
43     likely to be poorly exploited since they will have to wait for the
44     master to consider their request and for the input data to be
45     transferred. A client should thus probably request a pool of tasks
46     but if it requests too many tasks, it is likely to lead to a poor
47     load-balancing...
48
49 - How is the quality of such algorithm dependent on the platform
50     characteristics and on the task characteristics?
51
52     Whenever the input communication time is very small compared to
53     processing time and workers are homogeneous, it is likely that the
54     round-robin algorithm performs very well. Would it still hold true
55     when transfer time is not negligible and the platform is, say,
56     a volunteer computing system ?
57
58 - The network topology interconnecting the master and the workers
59   may be quite complicated. How does such a topology impact the
60   previous result?
61
62     When data transfers are the bottleneck, it is likely that a good
63     modeling of the platform becomes essential. In this case, you may
64     want to be able to account for complex platform topologies.
65
66 - Do the algorithms depend on a perfect knowledge of this
67   topology?
68
69     Should we still use a flat master worker deployment or should we
70     use a
71
72 - How is such an algorithm sensitive to external workload variation?
73
74     What if bandwidth, latency and power can vary with no warning?
75     Shouldn't you study whether your algorithm is sensitive to such
76     load variations?
77
78 - Although an algorithm may be more efficient than another, how
79   does it interfere with other applications?
80
81     As you can see, this very simple setting may need to evolve way
82     beyond what you initially imagined.
83
84     <blockquote> Premature optimization is  the root of all evil. -- D.E.Knuth</blockquote>
85
86     Furthermore, writing your own simulator is much harder than you
87     may imagine. This is why you should rely on an established and flexible
88     one.
89
90 The following figure is a screenshot of [triva][fn:1] visualizing a [SimGrid
91 simulation][fn:2] of two master worker applications (one in light gray and
92 the other in dark gray) running in concurrence and showing resource
93 usage over a long period of time.
94
95 ![Test](./sc3-description.png)
96
97 \section Prerequisites
98
99 Of course, you need to install SimGrid before taking this tutorial.
100 Please refer to the relevant Section: \ref install.
101
102 ## Tutorials
103
104 A lot of information on how to install and use Simgrid are
105 provided by the [online documentation][fn:4] and by several tutorials:
106
107 - http://simgrid.gforge.inria.fr/tutorials/simgrid-use-101.pdf
108 - http://simgrid.gforge.inria.fr/tutorials/simgrid-tracing-101.pdf
109 - http://simgrid.gforge.inria.fr/tutorials/simgrid-platf-101.pdf
110
111 ## Installing the visualization softwares
112
113 Several tools can be used to visualize the result of SimGrid
114 simulations and get a better understanding of simulations.
115
116 - [viva][fn:1] will be useful to make fancy graph or treemap visualizations.
117 - [pajeng][fn:5] provides a Gantt-chart visualization.
118 - [Vite][fn:6] also provides a Gantt-chart visualization.
119
120 Under Debian or Ubuntu, this is really easy with apt-get, while you
121 may have to install from the source on other systems. Check the
122 documentation of each software for more details.
123
124 ~~~~{.sh}
125 sudo apt-get install viva pajeng vite
126 ~~~~
127
128 \section intro_start Let's get started
129
130 \anchor intro_setup
131 ## Setting up and Compiling
132
133 The corresponding archive with all source files can be obtained
134 [here](http://simgrid.gforge.inria.fr/tutorials/msg-tuto/msg-tuto.tgz),
135 while the simgrid archive contains
136 [several platform files](https://github.com/simgrid/simgrid/tree/master/examples/platforms)
137 (click on the "Raw" button of files you want to download from GitHub).
138
139 ~~~~{.sh}
140 tar zxf msg-tuto.tgz
141 cd msg-tuto/src
142 make
143 ~~~~
144
145 As you can see, there is already a nice Makefile that compiles
146 everything for you. Now the tiny example has been compiled and it
147 can be easily run as follows:
148
149 ~~~~{.sh}
150 ./masterworker0 platforms/platform.xml deployment0.xml 2>&1
151 ~~~~
152
153 If you create a single self-content C-file named foo.c, the
154 corresponding program will be simply compiled and linked with
155 SimGrid by typing:
156
157 ~~~~{.sh}
158 make foo
159 ~~~~
160
161 For a more "fancy" output, you can try:
162
163 ~~~~{.sh}
164 ./masterworker0 platforms/platform.xml deployment0.xml 2>&1 | simgrid-colorizer
165 ~~~~
166
167 For a really fancy output, you should use [viva/triva][fn:1]:
168
169 ~~~~{.sh}
170 ./masterworker0 platforms/platform.xml deployment0.xml --cfg=tracing:yes \
171     --cfg=tracing/uncategorized:yes --cfg=viva/uncategorized:uncat.plist
172 LANG=C ; viva simgrid.trace uncat.plist
173 ~~~~
174
175 For a more classical Gantt-Chart visualization, you can produce a
176 [Paje][fn:5] trace:
177
178 ~~~~{.sh}
179 ./masterworker0 platforms/platform.xml deployment0.xml --cfg=tracing:yes \
180     --cfg=tracing/msg/process:yes
181 pajeng simgrid.trace
182 ~~~~
183
184 Alternatively, you can use [vite][fn:6].
185
186 ~~~~{.sh}
187 ./masterworker0 platforms/platform.xml deployment0.xml --cfg=tracing:yes \
188     --cfg=tracing/msg/process:yes --cfg=tracing/basic:yes
189 vite simgrid.trace
190 ~~~~
191
192 ## Getting Rid of Workers in the Deployment File
193
194 In the previous example, the deployment file `deployment0.xml`
195 is tightly connected to the platform file `platform.xml` and a
196 worker process is launched on each host:
197
198 ~~~~{.xml}
199 <?xml version='1.0'?>
200 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
201 <platform version="3">
202   <!-- The master process (with some arguments) -->
203   <process host="Tremblay" function="master">
204      <argument value="20"/>       <!-- Number of tasks -->
205      <argument value="50000000"/>  <!-- Computation size of tasks -->
206      <argument value="1000000"/>   <!-- Communication size of tasks -->
207      <argument value="Jupiter"/>  <!-- First worker -->
208      <argument value="Fafard"/>   <!-- Second worker -->
209      <argument value="Ginette"/>  <!-- Third worker -->
210      <argument value="Bourassa"/> <!-- Last worker -->
211      <argument value="Tremblay"/> <!-- Me! I can work too! -->
212   </process>
213   <!-- The worker process (with no argument) -->
214   <process host="Tremblay" function="worker" on_failure="RESTART"/>
215   <process host="Jupiter" function="worker" on_failure="RESTART"/>
216   <process host="Fafard" function="worker" on_failure="RESTART"/>
217   <process host="Ginette" function="worker" on_failure="RESTART"/>
218   <process host="Bourassa" function="worker" on_failure="RESTART"/>
219 </platform>
220 ~~~~
221
222 This is ok as the platform is rather small but will be painful when
223 using larger platforms. Instead, modify the simulator
224 `masterworker0.c` into `masterworker1.c` so that the master
225 launches a worker process on all the other machines at startup. The
226 new deployment file `deployment1.xml` should thus now simply be:
227
228 ~~~~{.xml}
229 <?xml version='1.0'?>
230 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
231 <platform version="3">
232   <!-- The master process (with some arguments) -->
233   <process host="Tremblay" function="master">
234      <argument value="20"/>       <!-- Number of tasks -->
235      <argument value="50000000"/>  <!-- Computation size of tasks -->
236      <argument value="1000000"/>   <!-- Communication size of tasks -->
237   </process>
238 </platform>
239 ~~~~
240
241 To this end you may need the following MSG functions (click on the links
242 to see their descriptions):
243
244 ~~~~{.c}
245 int MSG_get_host_number(void);
246 xbt_dynar_t MSG_hosts_as_dynar(void);
247 void * xbt_dynar_to_array (xbt_dynar_t dynar);
248 msg_process_t MSG_process_create(const char *name, xbt_main_func_t code,
249                                  void *data, msg_host_t host);
250 ~~~~
251
252 \note
253     It may avoid bugs later to avoid launching a worker on
254     the master host so you probably want to remove it from the host
255     list.
256
257 The `data` field of the @ref MSG_process_create can be used to pass
258 a channel name that will be private between master
259 and workers (e.g., `master_name:worker_name`). Adding the
260 `master_name` in the channel name will allow to easily have several
261 masters and a worker per master on each machine. To this end, you
262 may need to use the following functions:
263
264 ~~~~{.c}
265 msg_host_t MSG_host_self(void);
266 const char * MSG_host_get_name(msg_host_t host);
267 msg_process_t MSG_process_self(void);
268 void * MSG_process_get_data(msg_process_t process);
269 ~~~~
270
271 If you are not too familiar with string
272 manipulation in C, you may want to use the following functions
273 (see the C reference for details):
274
275 ~~~~{.c}
276 char *strcpy(char *dest, const char *src);
277 char *strcat(char *dest, const char *src);
278 ~~~~
279
280 ## Setting up a Time Limit Mechanism
281
282 In the current version, the number of tasks is defined through the
283 worker arguments. Hence, tasks are created at the very beginning of
284 the simulation. Instead, create tasks as needed and provide a time
285 limit indicating when it stops sending tasks. To this end, you will
286 obviously need to know what time it is:
287
288 ~~~~{.c}
289 double MSG_get_clock(void);
290 ~~~~
291
292 Otherwise, a quite effective way of terminating the simulation
293 would be to use some of the following functions:
294
295 ~~~~{.c}
296 void MSG_process_kill(msg_process_t process);
297 int MSG_process_killall(int reset_PIDs);
298 ~~~~
299
300 Anyway, the new deployment `deployment2.xml` file should thus look
301 like this:
302
303 ~~~~{.xml}
304 <?xml version='1.0'?>
305 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
306 <platform version="3">
307   <process host="Tremblay" function="master">
308      <argument value="3600"/>      <!-- Simulation timeout -->
309      <argument value="50000000"/>  <!-- Computation size of tasks -->
310      <argument value="1000000"/>   <!-- Communication size of tasks -->
311   </process>
312 </platform>
313 ~~~~
314
315 It may also be a good idea to transform most of the `XBT_INFO` into
316 `XBT_DEBUG` (e.g., keep the information on the total number of
317 tasks processed). These debug messages can be activated as follows:
318
319 ~~~~{.sh}
320 ./masterworker2 platforms/platform.xml deployment2.xml --log=msg_test.thres:debug
321 ~~~~
322
323 ## Using the Tracing Mechanism
324
325 SimGrid can trace all resource consumption and the outcome can be
326 displayed with viva as illustrated in the section \ref intro_setup. However, when several
327 masters are deployed, it is hard to understand what happens.
328
329 ~~~~{.xml}
330 <?xml version='1.0'?>
331 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
332 <platform version="3">
333   <process host="Tremblay" function="master">
334      <argument value="3600"/>      <!-- Simulation timeout -->
335      <argument value="50000000"/>  <!-- Computation size of tasks -->
336      <argument value="10"/>   <!-- Communication size of tasks -->
337   </process>
338   <process host="Fafard" function="master">
339      <argument value="3600"/>      <!-- Simulation timeout -->
340      <argument value="50000000"/>  <!-- Computation size of tasks -->
341      <argument value="10"/>   <!-- Communication size of tasks -->
342   </process>
343   <process host="Jupiter" function="master">
344      <argument value="3600"/>      <!-- Simulation timeout -->
345      <argument value="50000000"/>  <!-- Computation size of tasks -->
346      <argument value="10"/>   <!-- Communication size of tasks -->
347   </process>
348 </platform>
349 ~~~~
350
351 So let's use categories to track more precisely who does what and when:
352
353 ~~~~{.c}
354 void TRACE_category(const char *category);
355 void MSG_task_set_category (msg_task_t task, const char *category);
356 ~~~~
357
358 The outcome can then be visualized as follows:
359
360 ~~~~{.sh}
361 ./masterworker3 platforms/platform.xml deployment3.xml --cfg=tracing:yes\
362     --cfg=tracing/categorized:yes --cfg=viva/categorized:viva_cat.plist
363 LANG=C; viva simgrid.trace viva_cat.plist
364 ~~~~
365
366 Right now, you should realize that nothing is behaving like you
367 expect. Most workers are idle even though input data are ridiculous
368 and there are several masters deployed on the platform. Using a
369 Gantt-chart visualization may help:
370
371 ~~~~{.sh}
372 ./masterworker3 platforms/platform.xml deployment3.xml --cfg=tracing:yes \
373     --cfg=tracing/msg/process:yes
374 pajeng simgrid.trace
375 ~~~~
376
377 OK, so it should now be obvious that round robin is actually
378 very bad.
379
380 ## Improving the Scheduling
381
382 Instead of a round-robin scheduling, let's implement a first-come
383 first-served mechanism. To this end, workers need to send a tiny
384 request first. A possible way to implement such a request with MSG
385 is to send on a specific channel (e.g., the name of the master
386 name) a task with payload 0 and whose attached data is the worker
387 name. This way, the master can keep track of which workers are idle
388 and willing to work.
389
390 To know whether it has pending requests, the master can use the
391 following [function][fn:7]:
392
393 ~~~~{.c}
394 int MSG_task_listen(const char *alias);
395 ~~~~
396
397 If so, it should get the request and push the corresponding host
398 into a dynar so that they can later be retrieved when sending a
399 real [task][fn:7].
400
401 ~~~~{.c}
402 xbt_dynar_t xbt_dynar_new(const unsigned long elm_size,
403                           void_f_pvoid_t const free_f);
404 void xbt_dynar_push(xbt_dynar_t const dynar, const void *src);
405 void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
406 unsigned long xbt_dynar_length(const xbt_dynar_t dynar);
407 ~~~~
408
409 As you will soon realize, with such simple mechanisms, simple
410 deadlocks will soon appear. They can easily be removed with a
411 simple polling mechanism, hence the need for the following
412 [function][fn:7]:
413
414 ~~~~{.c}
415 msg_error_t MSG_process_sleep(double nb_sec);
416 ~~~~
417
418 As you should quickly realize, on the simple previous example, it
419 will double the throughput of the platform but will be quite
420 ineffective when input size of the tasks is not negligible anymore.
421
422 From this, many things can easily be added. For example, you could:
423 - add a performance measurement mechanism;
424 - enable the master to make smart scheduling choices using
425   measurement information;
426 - allow workers to have several pending requests so as to overlap
427   communication and computations as much as possible;
428 - ...
429
430 ## Using More Elaborate Platforms
431
432 SimGrid offers a rather powerful platform modeling mechanism. The
433 `src/examples/platforms/` repository comprises a variety of platforms ranging
434 from simple to elaborate. Associated to a good
435 visualization tool to ensure your simulation is meaningful, they
436 can allow you to study to which extent your algorithm scales...
437
438 What is the largest number of tasks requiring 50e6 flops and 1e5
439 bytes that you manage to distribute and process in one hour on
440 `g5k.xml` (you should use `deployment_general.xml`)?
441
442 \section intro_todo TODO: Points to improve for the next time
443
444 - Propose equivalent exercises and skeleton in java.
445 - Propose a virtualbox image with everything (simgrid, pajeng, viva,
446   ...) already set up.
447 - Ease the installation on mac OS X (binary installer) and
448   windows.
449 - Explain that programming in C or java and having a working
450   development environment is a prerequisite.
451
452 [fn:1]: http://triva.gforge.inria.fr/index.html
453 [fn:2]: http://hal.inria.fr/inria-00529569
454 [fn:3]: http://hal.inria.fr/hal-00738321
455 [fn:4]: http://simgrid.gforge.inria.fr/simgrid/latest/doc/
456 [fn:5]: https://github.com/schnorr/pajeng/
457 [fn:6]: http://vite.gforge.inria.fr/
458
459
460
461
462
463 */