1 /*! @page tutorial SimGrid First Tutorial
3 SimGrid is a toolkit providing the core functionalities for the
4 simulation of distributed applications in heterogeneous distributed
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.
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:
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
25 Although this looks like a very simple setting it raises several
26 interesting questions:
28 - Which algorithm should the master use to send workload?
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.
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
39 - How many tasks should the client ask for?
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
49 - How is the quality of such algorithm dependent on the platform
50 characteristics and on the task characteristics?
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 ?
58 - The network topology interconnecting the master and the workers
59 may be quite complicated. How does such a topology impact the
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.
66 - Do the algorithms depend on a perfect knowledge of this
69 Should we still use a flat master worker deployment or should we
72 - How is such an algorithm sensitive to external workload variation?
74 What if bandwidth, latency and power can vary with no warning?
75 Shouldn't you study whether your algorithm is sensitive to such
78 - Although an algorithm may be more efficient than another, how
79 does it interfere with other applications?
81 As you can see, this very simple setting may need to evolve way
82 beyond what you initially imagined.
84 <blockquote> Premature optimization is the root of all evil. -- D.E.Knuth</blockquote>
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
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.
95 ![Test](./sc3-description.png)
97 \section Prerequisites
99 Of course, you need to install SimGrid before taking this tutorial.
100 Please refer to the relevant Section: \ref install.
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:
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
111 ## Installing the visualization softwares
113 Several tools can be used to visualize the result of SimGrid
114 simulations and get a better understanding of simulations.
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.
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.
125 sudo apt-get install viva pajeng vite
128 \section intro_start Let's get started
131 ## Setting up and Compiling
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).
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:
150 ./masterworker0 platforms/platform.xml deployment0.xml 2>&1
153 If you create a single self-content C-file named foo.c, the
154 corresponding program will be simply compiled and linked with
161 For a more "fancy" output, you can try:
164 ./masterworker0 platforms/platform.xml deployment0.xml 2>&1 | simgrid-colorizer
167 For a really fancy output, you should use [viva/triva][fn:1]:
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
175 For a more classical Gantt-Chart visualization, you can produce a
179 ./masterworker0 platforms/platform.xml deployment0.xml --cfg=tracing:yes \
180 --cfg=tracing/msg/process:yes
184 Alternatively, you can use [vite][fn:6].
187 ./masterworker0 platforms/platform.xml deployment0.xml --cfg=tracing:yes \
188 --cfg=tracing/msg/process:yes --cfg=tracing/basic:yes
192 ## Getting Rid of Workers in the Deployment File
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:
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! -->
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"/>
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:
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 -->
241 To this end you may need the following MSG functions (click on the links
242 to see their descriptions):
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);
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
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:
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);
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):
276 char *strcpy(char *dest, const char *src);
277 char *strcat(char *dest, const char *src);
280 ## Setting up a Time Limit Mechanism
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:
289 double MSG_get_clock(void);
292 Otherwise, a quite effective way of terminating the simulation
293 would be to use some of the following functions:
296 void MSG_process_kill(msg_process_t process);
297 int MSG_process_killall(int reset_PIDs);
300 Anyway, the new deployment `deployment2.xml` file should thus look
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 -->
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:
320 ./masterworker2 platforms/platform.xml deployment2.xml --log=msg_test.thres:debug
323 ## Using the Tracing Mechanism
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.
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 -->
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 -->
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 -->
351 So let's use categories to track more precisely who does what and when:
354 void TRACE_category(const char *category);
355 void MSG_task_set_category (msg_task_t task, const char *category);
358 The outcome can then be visualized as follows:
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
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:
372 ./masterworker3 platforms/platform.xml deployment3.xml --cfg=tracing:yes \
373 --cfg=tracing/msg/process:yes
377 OK, so it should now be obvious that round robin is actually
380 ## Improving the Scheduling
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
390 To know whether it has pending requests, the master can use the
391 following [function][fn:7]:
394 int MSG_task_listen(const char *alias);
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
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);
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
415 msg_error_t MSG_process_sleep(double nb_sec);
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.
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;
430 ## Using More Elaborate Platforms
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...
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`)?
442 \section intro_todo TODO: Points to improve for the next time
444 - Propose equivalent exercises and skeleton in java.
445 - Propose a virtualbox image with everything (simgrid, pajeng, viva,
447 - Ease the installation on mac OS X (binary installer) and
449 - Explain that programming in C or java and having a working
450 development environment is a prerequisite.
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/