Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
af4bd36ecc48aeff0c2720a6f0cc58dec30e29b6
[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 source files can be obtained
134 [online on GitLab](https://gitlab.inria.fr/simgrid/simgrid/tree/master/doc/msg-tuto-src). 
135 If you find the right button on the top right of the interface, you can download the whole 
136 directory in one archive file. If you wish, you can find other platform file in 
137 [this GitLab directory](https://gitlab.inria.fr/simgrid/simgrid/tree/master/doc/examples/platforms).
138
139 As you can see, there is already a little Makefile that compiles
140 everything for you. If you struggle with the compilation, then you should double check 
141 your SimGrid installation. On need, please refer to the @ref install_yours_trouble section.
142
143 Once the tiny example has been compiled and it can be easily run as follows:
144
145 ~~~~{.sh}
146 ./masterworker0 platforms/platform.xml deployment0.xml
147 ~~~~
148
149 For a more "fancy" output, you can try:
150
151 ~~~~{.sh}
152 ./masterworker0 platforms/platform.xml deployment0.xml 2>&1 | simgrid-colorizer
153 ~~~~
154
155 For a really fancy output, you should use [viva/triva][fn:1]:
156
157 ~~~~{.sh}
158 ./masterworker0 platforms/platform.xml deployment0.xml --cfg=tracing:yes \
159     --cfg=tracing/uncategorized:yes --cfg=viva/uncategorized:uncat.plist
160 LANG=C ; viva simgrid.trace uncat.plist
161 ~~~~
162
163 For a more classical Gantt-Chart visualization, you can produce a
164 [Paje][fn:5] trace:
165
166 ~~~~{.sh}
167 ./masterworker0 platforms/platform.xml deployment0.xml --cfg=tracing:yes \
168     --cfg=tracing/msg/process:yes
169 pajeng simgrid.trace
170 ~~~~
171
172 Alternatively, you can use [vite][fn:6].
173
174 ~~~~{.sh}
175 ./masterworker0 platforms/platform.xml deployment0.xml --cfg=tracing:yes \
176     --cfg=tracing/msg/process:yes --cfg=tracing/basic:yes
177 vite simgrid.trace
178 ~~~~
179
180 ## Getting Rid of Workers in the Deployment File
181
182 In the previous example, the deployment file `deployment0.xml`
183 is tightly connected to the platform file `platform.xml` and a
184 worker process is launched on each host:
185
186 ~~~~{.xml}
187 <?xml version='1.0'?>
188 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
189 <platform version="3">
190   <!-- The master process (with some arguments) -->
191   <process host="Tremblay" function="master">
192      <argument value="20"/>       <!-- Number of tasks -->
193      <argument value="50000000"/>  <!-- Computation size of tasks -->
194      <argument value="1000000"/>   <!-- Communication size of tasks -->
195      <argument value="Jupiter"/>  <!-- First worker -->
196      <argument value="Fafard"/>   <!-- Second worker -->
197      <argument value="Ginette"/>  <!-- Third worker -->
198      <argument value="Bourassa"/> <!-- Last worker -->
199      <argument value="Tremblay"/> <!-- Me! I can work too! -->
200   </process>
201   <!-- The worker process (with no argument) -->
202   <process host="Tremblay" function="worker" on_failure="RESTART"/>
203   <process host="Jupiter" function="worker" on_failure="RESTART"/>
204   <process host="Fafard" function="worker" on_failure="RESTART"/>
205   <process host="Ginette" function="worker" on_failure="RESTART"/>
206   <process host="Bourassa" function="worker" on_failure="RESTART"/>
207 </platform>
208 ~~~~
209
210 This is ok as the platform is rather small but will be painful when
211 using larger platforms. Instead, modify the simulator
212 `masterworker0.c` into `masterworker1.c` so that the master
213 launches a worker process on all the other machines at startup. The
214 new deployment file `deployment1.xml` should thus now simply be:
215
216 ~~~~{.xml}
217 <?xml version='1.0'?>
218 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
219 <platform version="3">
220   <!-- The master process (with some arguments) -->
221   <process host="Tremblay" function="master">
222      <argument value="20"/>       <!-- Number of tasks -->
223      <argument value="50000000"/>  <!-- Computation size of tasks -->
224      <argument value="1000000"/>   <!-- Communication size of tasks -->
225   </process>
226 </platform>
227 ~~~~
228
229 To this end you may need the following MSG functions (click on the links
230 to see their descriptions):
231
232 ~~~~{.c}
233 int MSG_get_host_number(void);
234 xbt_dynar_t MSG_hosts_as_dynar(void);
235 void * xbt_dynar_to_array (xbt_dynar_t dynar);
236 msg_process_t MSG_process_create(const char *name, xbt_main_func_t code,
237                                  void *data, msg_host_t host);
238 ~~~~
239
240 \note
241     It may avoid bugs later to avoid launching a worker on
242     the master host so you probably want to remove it from the host
243     list.
244
245 The `data` field of the @ref MSG_process_create can be used to pass
246 a channel name that will be private between master
247 and workers (e.g., `master_name:worker_name`). Adding the
248 `master_name` in the channel name will allow to easily have several
249 masters and a worker per master on each machine. To this end, you
250 may need to use the following functions:
251
252 ~~~~{.c}
253 msg_host_t MSG_host_self(void);
254 const char * MSG_host_get_name(msg_host_t host);
255 msg_process_t MSG_process_self(void);
256 void * MSG_process_get_data(msg_process_t process);
257 ~~~~
258
259 If you are not too familiar with string
260 manipulation in C, you may want to use the following functions
261 (see the C reference for details):
262
263 ~~~~{.c}
264 char *strcpy(char *dest, const char *src);
265 char *strcat(char *dest, const char *src);
266 ~~~~
267
268 ## Setting up a Time Limit Mechanism
269
270 In the current version, the number of tasks is defined through the
271 worker arguments. Hence, tasks are created at the very beginning of
272 the simulation. Instead, create tasks as needed and provide a time
273 limit indicating when it stops sending tasks. To this end, you will
274 obviously need to know what time it is:
275
276 ~~~~{.c}
277 double MSG_get_clock(void);
278 ~~~~
279
280 Otherwise, a quite effective way of terminating the simulation
281 would be to use some of the following functions:
282
283 ~~~~{.c}
284 void MSG_process_kill(msg_process_t process);
285 int MSG_process_killall(int reset_PIDs);
286 ~~~~
287
288 Anyway, the new deployment `deployment2.xml` file should thus look
289 like this:
290
291 ~~~~{.xml}
292 <?xml version='1.0'?>
293 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
294 <platform version="3">
295   <process host="Tremblay" function="master">
296      <argument value="3600"/>      <!-- Simulation timeout -->
297      <argument value="50000000"/>  <!-- Computation size of tasks -->
298      <argument value="1000000"/>   <!-- Communication size of tasks -->
299   </process>
300 </platform>
301 ~~~~
302
303 It may also be a good idea to transform most of the `XBT_INFO` into
304 `XBT_DEBUG` (e.g., keep the information on the total number of
305 tasks processed). These debug messages can be activated as follows:
306
307 ~~~~{.sh}
308 ./masterworker2 platforms/platform.xml deployment2.xml --log=msg_test.thres:debug
309 ~~~~
310
311 ## Using the Tracing Mechanism
312
313 SimGrid can trace all resource consumption and the outcome can be
314 displayed with viva as illustrated in the section \ref intro_setup. However, when several
315 masters are deployed, it is hard to understand what happens.
316
317 ~~~~{.xml}
318 <?xml version='1.0'?>
319 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
320 <platform version="3">
321   <process host="Tremblay" function="master">
322      <argument value="3600"/>      <!-- Simulation timeout -->
323      <argument value="50000000"/>  <!-- Computation size of tasks -->
324      <argument value="10"/>   <!-- Communication size of tasks -->
325   </process>
326   <process host="Fafard" function="master">
327      <argument value="3600"/>      <!-- Simulation timeout -->
328      <argument value="50000000"/>  <!-- Computation size of tasks -->
329      <argument value="10"/>   <!-- Communication size of tasks -->
330   </process>
331   <process host="Jupiter" function="master">
332      <argument value="3600"/>      <!-- Simulation timeout -->
333      <argument value="50000000"/>  <!-- Computation size of tasks -->
334      <argument value="10"/>   <!-- Communication size of tasks -->
335   </process>
336 </platform>
337 ~~~~
338
339 So let's use categories to track more precisely who does what and when:
340
341 ~~~~{.c}
342 void TRACE_category(const char *category);
343 void MSG_task_set_category (msg_task_t task, const char *category);
344 ~~~~
345
346 The outcome can then be visualized as follows:
347
348 ~~~~{.sh}
349 ./masterworker3 platforms/platform.xml deployment3.xml --cfg=tracing:yes\
350     --cfg=tracing/categorized:yes --cfg=viva/categorized:viva_cat.plist
351 LANG=C; viva simgrid.trace viva_cat.plist
352 ~~~~
353
354 Right now, you should realize that nothing is behaving like you
355 expect. Most workers are idle even though input data are ridiculous
356 and there are several masters deployed on the platform. Using a
357 Gantt-chart visualization may help:
358
359 ~~~~{.sh}
360 ./masterworker3 platforms/platform.xml deployment3.xml --cfg=tracing:yes \
361     --cfg=tracing/msg/process:yes
362 pajeng simgrid.trace
363 ~~~~
364
365 OK, so it should now be obvious that round robin is actually
366 very bad.
367
368 ## Improving the Scheduling
369
370 Instead of a round-robin scheduling, let's implement a first-come
371 first-served mechanism. To this end, workers need to send a tiny
372 request first. A possible way to implement such a request with MSG
373 is to send on a specific channel (e.g., the name of the master
374 name) a task with payload 0 and whose attached data is the worker
375 name. This way, the master can keep track of which workers are idle
376 and willing to work.
377
378 To know whether it has pending requests, the master can use the
379 following [function][fn:7]:
380
381 ~~~~{.c}
382 int MSG_task_listen(const char *alias);
383 ~~~~
384
385 If so, it should get the request and push the corresponding host
386 into a dynar so that they can later be retrieved when sending a
387 real [task][fn:7].
388
389 ~~~~{.c}
390 xbt_dynar_t xbt_dynar_new(const unsigned long elm_size,
391                           void_f_pvoid_t const free_f);
392 void xbt_dynar_push(xbt_dynar_t const dynar, const void *src);
393 void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
394 unsigned long xbt_dynar_length(const xbt_dynar_t dynar);
395 ~~~~
396
397 As you will soon realize, with such simple mechanisms, simple
398 deadlocks will soon appear. They can easily be removed with a
399 simple polling mechanism, hence the need for the following
400 [function][fn:7]:
401
402 ~~~~{.c}
403 msg_error_t MSG_process_sleep(double nb_sec);
404 ~~~~
405
406 As you should quickly realize, on the simple previous example, it
407 will double the throughput of the platform but will be quite
408 ineffective when input size of the tasks is not negligible anymore.
409
410 From this, many things can easily be added. For example, you could:
411 - add a performance measurement mechanism;
412 - enable the master to make smart scheduling choices using
413   measurement information;
414 - allow workers to have several pending requests so as to overlap
415   communication and computations as much as possible;
416 - ...
417
418 ## Using More Elaborate Platforms
419
420 SimGrid offers a rather powerful platform modeling mechanism. The
421 `src/examples/platforms/` repository comprises a variety of platforms ranging
422 from simple to elaborate. Associated to a good
423 visualization tool to ensure your simulation is meaningful, they
424 can allow you to study to which extent your algorithm scales...
425
426 What is the largest number of tasks requiring 50e6 flops and 1e5
427 bytes that you manage to distribute and process in one hour on
428 `g5k.xml` (you should use `deployment_general.xml`)?
429
430 \section intro_todo TODO: Points to improve for the next time
431
432 - Propose equivalent exercises and skeleton in java.
433 - Propose a virtualbox image with everything (simgrid, pajeng, viva,
434   ...) already set up.
435 - Ease the installation on mac OS X (binary installer) and
436   windows.
437 - Explain that programming in C or java and having a working
438   development environment is a prerequisite.
439
440 [fn:1]: http://triva.gforge.inria.fr/index.html
441 [fn:2]: http://hal.inria.fr/inria-00529569
442 [fn:3]: http://hal.inria.fr/hal-00738321
443 [fn:4]: http://simgrid.gforge.inria.fr/simgrid/latest/doc/
444 [fn:5]: https://github.com/schnorr/pajeng/
445 [fn:6]: http://vite.gforge.inria.fr/
446
447
448
449
450
451 */