1 /*! \page use Using SimGrid
3 SimGrid comes with many examples provided in the examples/ directory. Those examples are described in section \ref MSG_examples . Those examples are commented and should be easy to understand. for a first step into SimGird we also provide some more detailed examples in the sections below.
5 \section using_msg Using MSG
7 Here are some examples on how to use MSG, the most used API.
10 MSG comes with an extensive set of examples. It is sometimes difficult
11 to find the one you need. This list aims at helping you finding the
12 example from which you can learn what you want to.
14 \subsection MSG_ex_basics Basic examples and features
16 \subsubsection MSG_ex_asynchronous_communications Asynchronous communications
19 Simulation of asynchronous communications between a sender and a receiver using a realistic platform and
20 an external description of the deployment.
22 - \ref MSG_ext_icomms_code
23 - \ref MSG_ext_icomms_preliminary
24 - \ref MSG_ext_icomms_Sender
25 - \ref MSG_ext_icomms_Receiver
26 - \ref MSG_ext_icomms_core
27 - \ref MSG_ext_icomms_Main
28 - \ref MSG_ext_icomms_fct_Waitall
29 - \ref MSG_ext_icomms_fct_Waitany
33 \dontinclude msg/icomms/peer.c
35 \paragraph MSG_ext_icomms_code Code of the application
37 \paragraph MSG_ext_icomms_preliminary Preliminary declarations
39 \until Sender function
41 \paragraph MSG_ext_icomms_Sender Sender function
43 The sender send to a receiver an asynchronous message with the function "MSG_task_isend()". Cause this function is non-blocking
44 we have to make "MSG_comm_test()" to know if the communication is finished for finally destroy it with function "MSG_comm_destroy()".
45 It also available to "make MSG_comm_wait()" which make both of them.
47 C style arguments (argc/argv) are interpreted as:
48 - the number of tasks to distribute
49 - the computation size of each task
50 - the size of the files associated to each task
51 - a list of host that will accept those tasks.
52 - the time to sleep at the beginning of the function
53 - This time defined the process sleep time
54 if time = 0 use of MSG_comm_wait()
55 if time > 0 use of MSG_comm_test()
58 \until Receiver function
60 \paragraph MSG_ext_icomms_Receiver Receiver function
62 This function executes tasks when it receives them. As the receiving is asynchronous we have to test the communication to know
63 if it is completed or not with "MSG_comm_test()" or wait for the completion "MSG_comm_wait()".
65 C style arguments (argc/argv) are interpreted as:
66 - the id to use for received the communication.
67 - the time to sleep at the beginning of the function
68 - This time defined the process sleep time
69 if time = 0 use of MSG_comm_wait()
70 if time > 0 use of MSG_comm_test()
74 \paragraph MSG_ext_icomms_core Simulation core
76 This function is the core of the simulation and is divided only into 3 parts
77 thanks to MSG_create_environment() and MSG_launch_application().
78 -# Simulation settings : MSG_create_environment() creates a realistic
80 -# Application deployment : create the processes on the right locations with
81 MSG_launch_application()
82 -# The simulation is run with #MSG_main()
85 - <i>platform_file</i>: the name of a file containing an valid surfxml platform description.
86 - <i>application_file</i>: the name of a file containing a valid surfxml application description
90 \paragraph MSG_ext_icomms_Main Main function
92 This initializes MSG, runs a simulation, and free all data-structures created by MSG.
96 \dontinclude msg/icomms/peer2.c
98 \paragraph MSG_ext_icomms_fct_Waitall Waitall function for sender
100 The use of this function permit to send all messages and wait for the completion of all in one time.
102 \skipline Sender function
105 \paragraph MSG_ext_icomms_fct_Waitany Waitany function
107 The MSG_comm_waitany() function return the place of the first message send or receive from a xbt_dynar_t table.
109 \paragraph MSG_ext_icomms_fct_Waitany_sender From a sender
110 We can use this function to wait all sent messages.
111 \dontinclude msg/icomms/peer3.c
112 \skipline Sender function
115 \paragraph MSG_ext_icomms_fct_Waitany_receiver From a receiver
116 We can also wait for the arrival of all messages.
117 \dontinclude msg/icomms/peer3.c
118 \skipline Receiver function
119 \until end_of_receiver
121 \subsubsection MSG_ex_master_slave Basic Master/Slaves
123 Simulation of a master-slave application using a realistic platform
124 and an external description of the deployment.
126 \paragraph MSG_ex_ms_TOC Table of contents:
128 - \ref MSG_ext_ms_preliminary
129 - \ref MSG_ext_ms_master
130 - \ref MSG_ext_ms_slave
131 - \ref MSG_ext_ms_forwarder
132 - \ref MSG_ext_ms_core
133 - \ref MSG_ext_ms_main
134 - \ref MSG_ext_ms_helping
135 - \ref MSG_ext_ms_application
136 - \ref MSG_ext_ms_platform
140 \dontinclude msg/masterslave/masterslave_forwarder.c
143 \paragraph MSG_ext_ms_preliminary Preliminary declarations
149 \paragraph MSG_ext_ms_master Master code
151 This function has to be assigned to a m_process_t that will behave as
152 the master. It should not be called directly but either given as a
153 parameter to #MSG_process_create() or registered as a public function
154 through #MSG_function_register() and then automatically assigned to a
155 process through #MSG_launch_application().
157 C style arguments (argc/argv) are interpreted as:
158 - the number of tasks to distribute
159 - the computation size of each task
160 - the size of the files associated to each task
161 - a list of host that will accept those tasks.
163 Tasks are dumbly sent in a round-robin style.
167 \paragraph MSG_ext_ms_slave Slave code
169 This function has to be assigned to a #msg_process_t that has to behave
170 as a slave. Just like the master fuction (described in \ref
171 MSG_ext_ms_master), it should not be called directly.
173 This function keeps waiting for tasks and executes them as it receives them.
177 \paragraph MSG_ext_ms_forwarder Forwarder code
179 This function has to be assigned to a #msg_process_t that has to behave
180 as a forwarder. Just like the master function (described in \ref
181 MSG_ext_ms_master), it should not be called directly.
183 C style arguments (argc/argv) are interpreted as a list of host that
184 will accept those tasks.
186 This function keeps waiting for tasks and dispathes them to its slaves.
188 \until end_of_forwarder
190 \paragraph MSG_ext_ms_core Simulation core
192 This function is the core of the simulation and is divided only into 3 parts
193 thanks to MSG_create_environment() and MSG_launch_application().
194 -# Simulation settings : MSG_create_environment() creates a realistic
196 -# Application deployment : create the processes on the right locations with
197 MSG_launch_application()
198 -# The simulation is run with #MSG_main()
201 - <i>platform_file</i>: the name of a file containing an valid surfxml platform description.
202 - <i>application_file</i>: the name of a file containing a valid surfxml application description
204 \until end_of_test_all
206 \paragraph MSG_ext_ms_main Main() function
208 This initializes MSG, runs a simulation, and free all data-structures created by MSG.
212 \subsubsection MSG_ext_ms_helping Helping files
214 \paragraph MSG_ext_ms_application Example of application file
216 \include msg/masterslave/deployment_masterslave.xml
218 \paragraph MSG_ext_ms_platform Example of platform file
220 \include msg/small_platform.xml
222 \section using_gras Using GRAS
224 Here are some examples on how to use GRAS.
227 There is for now rather few examples of GRAS, but it's better than
236 \subsection GRAS_ex_ping Ping-Pong
238 This example implements the very classical ping-pong in GRAS. It
239 involves a client (initiating the ping-pong) and a server (answering to
242 It works the following way:
243 - Both the client and the server register all needed messages
244 - The server registers a callback to the ping message, which sends pong
246 - The client sends the ping message to the server, and waits for the
247 pong message as an answer.
249 This example resides in the <b>examples/gras/ping/ping.c</b> file. Yes, both
250 the code of the client and of the server is placed in the same file.
252 \subsubsection GRAS_ex_ping_toc Table of contents of the ping example
253 - \ref GRAS_ex_ping_common
254 - \ref GRAS_ex_ping_initial
255 - \ref GRAS_ex_ping_register
256 - \ref GRAS_ex_ping_server
257 - \ref GRAS_ex_ping_serdata
258 - \ref GRAS_ex_ping_sercb
259 - \ref GRAS_ex_ping_sermain
260 - \ref GRAS_ex_ping_client
261 - \ref GRAS_ex_ping_climain
265 \dontinclude gras/ping/ping_common.c
267 \subsubsection GRAS_ex_ping_common 1) Common code to the client and the server
269 \paragraph GRAS_ex_ping_initial 1.a) Initial settings
271 Let's first load the module header and declare a logging category (see
272 \ref XBT_log for more info on logging).
277 The module header <tt>ping.h</tt> reads:
279 \dontinclude gras/ping/ping.h
284 \paragraph GRAS_ex_ping_register 1.b) Register the messages
286 This function, called by both the client and the server is in charge of
287 declaring the existing messages to GRAS. Since the payload does not
288 involve any newly created types but only int, this is quite easy.
289 (to exchange more complicated types, see \ref GRAS_dd or
290 \ref GRAS_ex_mmrpc for an example).
292 \dontinclude gras/ping/ping_common.c
293 \skip register_messages
296 [Back to \ref GRAS_ex_ping_toc]
298 \subsubsection GRAS_ex_ping_server 2) Server's code
300 \paragraph GRAS_ex_ping_serdata 2.a) The server's globals
302 In order to ensure the communication between the "main" and the callback
303 of the server, we need to declare some globals. We have to put them in a
304 struct definition so that they can be handled properly in GRAS.
306 \dontinclude gras/ping/ping_server.c
310 \paragraph GRAS_ex_ping_sercb 2.b) The callback to the ping message
312 Here is the callback run when the server receives any ping message (this
313 will be registered later by the server).
315 \skip server_cb_ping_handler
316 \until end_of_server_cb_ping_handler
318 \paragraph GRAS_ex_ping_sermain 2.c) The "main" of the server
320 This is the "main" of the server. You must not write any main()
321 function yourself. Instead, you just have to write a regular function
322 like this one which will act as a main.
327 [Back to \ref GRAS_ex_ping_toc]
329 \subsubsection GRAS_ex_ping_client 3) Client's code
331 \paragraph GRAS_ex_ping_climain 3.a) Client's "main" function
333 This function is quite straightforward, and the inlined comments should
334 be enough to understand it.
336 \dontinclude gras/ping/ping_client.c
340 [Back to \ref GRAS_ex_ping_toc]
342 \subsection GRAS_ex_token Token Ring example
344 This example implements the token ring algorithm. It involves several
345 nodes arranged in a ring (each of them have a left and a right neighbour)
346 and exchanging a "token". This algorithm is one of the solution to ensure
347 the mutual exclusion between distributed processes. There is only one
348 token at any time, so the process in its possession is ensured to be the
349 only one having it. So, if there is an action you want all processes to
350 do alternativly, but you cannot afford to have two processes doing it at
351 the same time, let the process having the token doing it.
353 Actually, there is a lot of different token ring algorithms in the
354 litterature, so this example implements one of them: the simplest one.
355 The ring is static (no new node can join it, and you'll get trouble if
356 one node dies or leaves), and nothing is done for the case in which the
359 - \ref GRAS_ex_stoken_deploy
360 - \ref GRAS_ex_stoken_global
361 - \ref GRAS_ex_stoken_callback
362 - \ref GRAS_ex_stoken_main
364 \subsection GRAS_ex_stoken_deploy 1) Deployment file
366 Here is the deployment file:
367 \include examples/gras/mutual_exclusion/simple_token/simple_token.xml
369 The neighbour of each node is given at startup as command line argument.
370 Moreover, one of the nodes is instructed by a specific argument (the one
371 on Tremblay here) to create the token at the begining of the algorithm.
373 \subsection GRAS_ex_stoken_global 2) Global definition
375 The token is incarned by a specific message, which circulates from node
376 to node (the payload is an integer incremented at each hop). So, the most
377 important part of the code is the message callback, which forwards the
378 message to the next node. That is why we have to store all variable in a
379 global, as explained in the \ref GRAS_globals section.
381 \dontinclude examples/gras/mutual_exclusion/simple_token/simple_token.c
385 \subsection GRAS_ex_stoken_callback 3) The callback
387 Even if this is the core of this algorithm, this function is quite
390 \skip node_cb_stoken_handler
391 \until end_of_node_cb_stoken_handler
393 \subsection GRAS_ex_stoken_main 4) The main function
395 This function is splited in two parts: The first one performs all the
396 needed initialisations (points 1-7) while the end (point 8. below) calls
397 gras_msg_handle() as long as the planned amount of ring loops are not
403 \subsection GRAS_ex_mmrpc A simple RPC for matrix multiplication
405 This example implements a remote matrix multiplication. It involves a client
406 (creating the matrices and sending the multiplications requests) and a server
407 (computing the multiplication on client's behalf).
409 This example also constitutes a more advanced example of data description
410 mechanisms, since the message payload type is a bit more complicated than in
411 other examples such as the ping one (\ref GRAS_ex_ping).
413 It works the following way (not very different from the ping example):
414 - Both the client and the server register all needed messages and datatypes
415 - The server registers a callback to the "request" message, which computes
416 what needs to be and returns the result to the expeditor.
417 - The client creates two matrices, ask for their multiplication and check
420 This example resides in the <b>examples/gras/mmrpc/mmrpc.c</b> file.
422 \subsubsection GRAS_ex_mmrpc_toc Table of contents of the mmrpc example
423 - \ref GRAS_ex_mmrpc_common
424 - \ref GRAS_ex_mmrpc_header
425 - \ref GRAS_ex_mmrpc_dataregister
426 - \ref GRAS_ex_mmrpc_logdef
427 - \ref GRAS_ex_mmrpc_msgregister
428 - \ref GRAS_ex_mmrpc_server
429 - \ref GRAS_ex_mmrpc_serinc
430 - \ref GRAS_ex_mmrpc_sercb
431 - \ref GRAS_ex_mmrpc_sermain
432 - \ref GRAS_ex_mmrpc_client
433 - \ref GRAS_ex_mmrpc_cliinc
434 - \ref GRAS_ex_mmrpc_climain
439 \subsubsection GRAS_ex_mmrpc_common 1) Common code to the client and the server (mmrpc_common.c and mmrpc.h)
442 \paragraph GRAS_ex_mmrpc_header 1.a) Module header (mmrpc.h)
444 This loads the gras header and declare the function's prototypes as well
447 \dontinclude gras/mmrpc/mmrpc.h
452 \paragraph GRAS_ex_mmrpc_dataregister 1.b) Register the data types (mmrpc.h)
454 The messages involved in a matrix of double. This type is automatically
455 known by the GRAS mecanism, using the gras_datadesc_matrix() function of the
458 \paragraph GRAS_ex_mmrpc_logdef 1.c) Logging category definition (mmrpc_common.c)
460 Let's first load the module header and declare a logging category (see
461 \ref XBT_log for more info on logging). This logging category does live
462 in this file (ie the required symbols are defined here and declared as
463 "extern" in any other file using them). That is why we use
464 \ref XBT_LOG_NEW_DEFAULT_CATEGORY here and
465 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY in mmrpc_client.c and mmrpc_server.c.
467 \dontinclude gras/mmrpc/mmrpc_common.c
471 \paragraph GRAS_ex_mmrpc_msgregister 1.d) Register the messages (mmrpc_common.c)
473 This function, called by both the client and the server is in charge of
474 declaring the existing messages to GRAS.
476 The datatype description builded that way can then be used to build an array datatype or
479 \skip register_messages
482 [Back to \ref GRAS_ex_mmrpc_toc]
484 \subsubsection GRAS_ex_mmrpc_server 2) Server's code (mmrpc_server.c)
486 \paragraph GRAS_ex_mmrpc_serinc 2.a) Server intial settings
488 All module symbols live in the mmrpc_common.c file. We thus have to
489 define \ref XBT_DEFINE_TYPE_EXTERN to the preprocessor so that the
490 \ref XBT_DEFINE_TYPE symbols don't get included here. Likewise, we use
491 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY to get the log category in here.
493 \dontinclude gras/mmrpc/mmrpc_server.c
497 \paragraph GRAS_ex_mmrpc_sercb 2.b) The callback to the mmrpc message
499 Here is the callback run when the server receives any mmrpc message (this
500 will be registered later by the server). Note the way we get the message
501 payload. In the ping example, there was one additional level of pointer
502 indirection (see \ref GRAS_ex_ping_sercb). This is because the payload is
503 an array here (ie a pointer) whereas it is a scalar in the ping example.
505 \skip server_cb_request_handler
506 \until end_of_server_cb_request_handler
508 \paragraph GRAS_ex_mmrpc_sermain 2.c) The "main" of the server
510 This is the "main" of the server. You must not write any main()
511 function yourself. Instead, you just have to write a regular function
512 like this one which will act as a main.
517 [Back to \ref GRAS_ex_mmrpc_toc]
519 \subsubsection GRAS_ex_mmrpc_client 3) Client's code (mmrpc_client.c)
521 \paragraph GRAS_ex_mmrpc_cliinc 2.a) Server intial settings
523 As for the server, some extra love is needed to make sure that automatic
524 datatype parsing and log categories do work even if we are using several
527 \dontinclude gras/mmrpc/mmrpc_client.c
531 \paragraph GRAS_ex_mmrpc_climain 3.b) Client's "main" function
533 This function is quite straightforward, and the inlined comments should
534 be enough to understand it.
536 \dontinclude gras/mmrpc/mmrpc_client.c
540 [Back to \ref GRAS_ex_mmrpc_toc]
542 \subsection GRAS_ex_timer Some timer games
544 This example fools around with the GRAS timers (\ref GRAS_timer). It is
545 mainly a regression test, since it uses almost all timer features.
547 The main program registers a repetititive task and a delayed one, and
548 then loops until the <tt>still_to_do</tt> variables of its globals reach
549 0. The delayed task set it to 5, and the repetititive one decrease it
550 each time. Here is an example of output:
551 \verbatim Initialize GRAS
553 [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
554 [1108335471] Programming the delayed_action for after 2.000000 sec
555 [1108335471] Have a rest
556 [1108335472] Canceling the delayed_action.
557 [1108335472] Re-programming the delayed_action for after 2.000000 sec
558 [1108335472] Repetitive_action has nothing to do yet
559 [1108335473] Repetitive_action has nothing to do yet
560 [1108335473] delayed_action setting globals->still_to_do to 5
561 [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
562 [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
563 [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
564 [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
565 [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
566 Exiting GRAS\endverbatim
569 - \ref GRAS_ex_timer_decl
570 - \ref GRAS_ex_timer_delay
571 - \ref GRAS_ex_timer_repeat
572 - \ref GRAS_ex_timer_main
576 \subsubsection GRAS_ex_timer_decl 1. Declarations and headers
580 \subsubsection GRAS_ex_timer_delay 2. Source code of the delayed action
581 \skip repetitive_action
582 \until end_of_repetitive_action
584 \subsubsection GRAS_ex_timer_repeat 3. Source code of the repetitive action
586 \until end_of_delayed_action
588 \subsubsection GRAS_ex_timer_main 4. Source code of main function