1 /*! \page use Using SimGrid
3 \section using_msg Using MSG
5 Here are some examples on how to use MSG, the most used API.
8 MSG comes with an extensive set of examples. It is sometimes difficult
9 to find the one you need. This list aims at helping you finding the
10 example from which you can learn what you want to.
12 \subsection MSG_ex_basics Basic examples and features
14 \subsubsection MSG_ex_asynchronous_communications Asynchronous communications
17 Simulation of asynchronous communications between a sender and a receiver using a realistic platform and
18 an external description of the deployment.
20 - \ref MSG_ext_icomms_code
21 - \ref MSG_ext_icomms_preliminary
22 - \ref MSG_ext_icomms_Sender
23 - \ref MSG_ext_icomms_Receiver
24 - \ref MSG_ext_icomms_core
25 - \ref MSG_ext_icomms_Main
26 - \ref MSG_ext_icomms_fct_Waitall
27 - \ref MSG_ext_icomms_fct_Waitany
31 \dontinclude msg/icomms/peer.c
33 \paragraph MSG_ext_icomms_code Code of the application
35 \paragraph MSG_ext_icomms_preliminary Preliminary declarations
37 \until Sender function
39 \paragraph MSG_ext_icomms_Sender Sender function
41 The sender send to a receiver an asynchronous message with the function "MSG_task_isend()". Cause this function is non-blocking
42 we have to make "MSG_comm_test()" to know if the communication is finished for finally destroy it with function "MSG_comm_destroy()".
43 It also available to "make MSG_comm_wait()" which make both of them.
45 C style arguments (argc/argv) are interpreted as:
46 - the number of tasks to distribute
47 - the computation size of each task
48 - the size of the files associated to each task
49 - a list of host that will accept those tasks.
50 - the time to sleep at the beginning of the function
51 - This time defined the process sleep time
52 if time = 0 use of MSG_comm_wait()
53 if time > 0 use of MSG_comm_test()
56 \until Receiver function
58 \paragraph MSG_ext_icomms_Receiver Receiver function
60 This function executes tasks when it receives them. As the receiving is asynchronous we have to test the communication to know
61 if it is completed or not with "MSG_comm_test()" or wait for the completion "MSG_comm_wait()".
63 C style arguments (argc/argv) are interpreted as:
64 - the id to use for received the communication.
65 - the time to sleep at the beginning of the function
66 - This time defined the process sleep time
67 if time = 0 use of MSG_comm_wait()
68 if time > 0 use of MSG_comm_test()
72 \paragraph MSG_ext_icomms_core Simulation core
74 This function is the core of the simulation and is divided only into 3 parts
75 thanks to MSG_create_environment() and MSG_launch_application().
76 -# Simulation settings : MSG_create_environment() creates a realistic
78 -# Application deployment : create the processes on the right locations with
79 MSG_launch_application()
80 -# The simulation is run with #MSG_main()
83 - <i>platform_file</i>: the name of a file containing an valid surfxml platform description.
84 - <i>application_file</i>: the name of a file containing a valid surfxml application description
88 \paragraph MSG_ext_icomms_Main Main function
90 This initializes MSG, runs a simulation, and free all data-structures created by MSG.
94 \dontinclude msg/icomms/peer2.c
96 \paragraph MSG_ext_icomms_fct_Waitall Waitall function for sender
98 The use of this function permit to send all messages and wait for the completion of all in one time.
100 \skipline Sender function
103 \paragraph MSG_ext_icomms_fct_Waitany Waitany function
105 The MSG_comm_waitany() function return the place of the first message send or receive from a xbt_dynar_t table.
107 \paragraph MSG_ext_icomms_fct_Waitany_sender From a sender
108 We can use this function to wait all sent messages.
109 \dontinclude msg/icomms/peer3.c
110 \skipline Sender function
113 \paragraph MSG_ext_icomms_fct_Waitany_receiver From a receiver
114 We can also wait for the arrival of all messages.
115 \dontinclude msg/icomms/peer3.c
116 \skipline Receiver function
117 \until end_of_receiver
119 \subsubsection MSG_ex_master_slave Basic Master/Slaves
121 Simulation of a master-slave application using a realistic platform
122 and an external description of the deployment.
124 \paragraph MSG_ex_ms_TOC Table of contents:
126 - \ref MSG_ext_ms_preliminary
127 - \ref MSG_ext_ms_master
128 - \ref MSG_ext_ms_slave
129 - \ref MSG_ext_ms_forwarder
130 - \ref MSG_ext_ms_core
131 - \ref MSG_ext_ms_main
132 - \ref MSG_ext_ms_helping
133 - \ref MSG_ext_ms_application
134 - \ref MSG_ext_ms_platform
138 \dontinclude msg/masterslave/masterslave_forwarder.c
141 \paragraph MSG_ext_ms_preliminary Preliminary declarations
147 \paragraph MSG_ext_ms_master Master code
149 This function has to be assigned to a m_process_t that will behave as
150 the master. It should not be called directly but either given as a
151 parameter to #MSG_process_create() or registered as a public function
152 through #MSG_function_register() and then automatically assigned to a
153 process through #MSG_launch_application().
155 C style arguments (argc/argv) are interpreted as:
156 - the number of tasks to distribute
157 - the computation size of each task
158 - the size of the files associated to each task
159 - a list of host that will accept those tasks.
161 Tasks are dumbly sent in a round-robin style.
165 \paragraph MSG_ext_ms_slave Slave code
167 This function has to be assigned to a #msg_process_t that has to behave
168 as a slave. Just like the master fuction (described in \ref
169 MSG_ext_ms_master), it should not be called directly.
171 This function keeps waiting for tasks and executes them as it receives them.
175 \paragraph MSG_ext_ms_forwarder Forwarder code
177 This function has to be assigned to a #msg_process_t that has to behave
178 as a forwarder. Just like the master function (described in \ref
179 MSG_ext_ms_master), it should not be called directly.
181 C style arguments (argc/argv) are interpreted as a list of host that
182 will accept those tasks.
184 This function keeps waiting for tasks and dispathes them to its slaves.
186 \until end_of_forwarder
188 \paragraph MSG_ext_ms_core Simulation core
190 This function is the core of the simulation and is divided only into 3 parts
191 thanks to MSG_create_environment() and MSG_launch_application().
192 -# Simulation settings : MSG_create_environment() creates a realistic
194 -# Application deployment : create the processes on the right locations with
195 MSG_launch_application()
196 -# The simulation is run with #MSG_main()
199 - <i>platform_file</i>: the name of a file containing an valid surfxml platform description.
200 - <i>application_file</i>: the name of a file containing a valid surfxml application description
202 \until end_of_test_all
204 \paragraph MSG_ext_ms_main Main() function
206 This initializes MSG, runs a simulation, and free all data-structures created by MSG.
210 \subsubsection MSG_ext_ms_helping Helping files
212 \paragraph MSG_ext_ms_application Example of application file
214 \include msg/masterslave/deployment_masterslave.xml
216 \paragraph MSG_ext_ms_platform Example of platform file
218 \include msg/small_platform.xml
220 \section using_gras Using GRAS
222 Here are some examples on how to use GRAS.
225 There is for now rather few examples of GRAS, but it's better than
234 \subsection GRAS_ex_ping Ping-Pong
236 This example implements the very classical ping-pong in GRAS. It
237 involves a client (initiating the ping-pong) and a server (answering to
240 It works the following way:
241 - Both the client and the server register all needed messages
242 - The server registers a callback to the ping message, which sends pong
244 - The client sends the ping message to the server, and waits for the
245 pong message as an answer.
247 This example resides in the <b>examples/gras/ping/ping.c</b> file. Yes, both
248 the code of the client and of the server is placed in the same file.
250 \subsubsection GRAS_ex_ping_toc Table of contents of the ping example
251 - \ref GRAS_ex_ping_common
252 - \ref GRAS_ex_ping_initial
253 - \ref GRAS_ex_ping_register
254 - \ref GRAS_ex_ping_server
255 - \ref GRAS_ex_ping_serdata
256 - \ref GRAS_ex_ping_sercb
257 - \ref GRAS_ex_ping_sermain
258 - \ref GRAS_ex_ping_client
259 - \ref GRAS_ex_ping_climain
263 \dontinclude gras/ping/ping_common.c
265 \subsubsection GRAS_ex_ping_common 1) Common code to the client and the server
267 \paragraph GRAS_ex_ping_initial 1.a) Initial settings
269 Let's first load the module header and declare a logging category (see
270 \ref XBT_log for more info on logging).
275 The module header <tt>ping.h</tt> reads:
277 \dontinclude gras/ping/ping.h
282 \paragraph GRAS_ex_ping_register 1.b) Register the messages
284 This function, called by both the client and the server is in charge of
285 declaring the existing messages to GRAS. Since the payload does not
286 involve any newly created types but only int, this is quite easy.
287 (to exchange more complicated types, see \ref GRAS_dd or
288 \ref GRAS_ex_mmrpc for an example).
290 \dontinclude gras/ping/ping_common.c
291 \skip register_messages
294 [Back to \ref GRAS_ex_ping_toc]
296 \subsubsection GRAS_ex_ping_server 2) Server's code
298 \paragraph GRAS_ex_ping_serdata 2.a) The server's globals
300 In order to ensure the communication between the "main" and the callback
301 of the server, we need to declare some globals. We have to put them in a
302 struct definition so that they can be handled properly in GRAS.
304 \dontinclude gras/ping/ping_server.c
308 \paragraph GRAS_ex_ping_sercb 2.b) The callback to the ping message
310 Here is the callback run when the server receives any ping message (this
311 will be registered later by the server).
313 \skip server_cb_ping_handler
314 \until end_of_server_cb_ping_handler
316 \paragraph GRAS_ex_ping_sermain 2.c) The "main" of the server
318 This is the "main" of the server. You must not write any main()
319 function yourself. Instead, you just have to write a regular function
320 like this one which will act as a main.
325 [Back to \ref GRAS_ex_ping_toc]
327 \subsubsection GRAS_ex_ping_client 3) Client's code
329 \paragraph GRAS_ex_ping_climain 3.a) Client's "main" function
331 This function is quite straightforward, and the inlined comments should
332 be enough to understand it.
334 \dontinclude gras/ping/ping_client.c
338 [Back to \ref GRAS_ex_ping_toc]
340 \subsection GRAS_ex_token Token Ring example
342 This example implements the token ring algorithm. It involves several
343 nodes arranged in a ring (each of them have a left and a right neighbour)
344 and exchanging a "token". This algorithm is one of the solution to ensure
345 the mutual exclusion between distributed processes. There is only one
346 token at any time, so the process in its possession is ensured to be the
347 only one having it. So, if there is an action you want all processes to
348 do alternativly, but you cannot afford to have two processes doing it at
349 the same time, let the process having the token doing it.
351 Actually, there is a lot of different token ring algorithms in the
352 litterature, so this example implements one of them: the simplest one.
353 The ring is static (no new node can join it, and you'll get trouble if
354 one node dies or leaves), and nothing is done for the case in which the
357 - \ref GRAS_ex_stoken_deploy
358 - \ref GRAS_ex_stoken_global
359 - \ref GRAS_ex_stoken_callback
360 - \ref GRAS_ex_stoken_main
362 \subsection GRAS_ex_stoken_deploy 1) Deployment file
364 Here is the deployment file:
365 \include examples/gras/mutual_exclusion/simple_token/simple_token.xml
367 The neighbour of each node is given at startup as command line argument.
368 Moreover, one of the nodes is instructed by a specific argument (the one
369 on Tremblay here) to create the token at the begining of the algorithm.
371 \subsection GRAS_ex_stoken_global 2) Global definition
373 The token is incarned by a specific message, which circulates from node
374 to node (the payload is an integer incremented at each hop). So, the most
375 important part of the code is the message callback, which forwards the
376 message to the next node. That is why we have to store all variable in a
377 global, as explained in the \ref GRAS_globals section.
379 \dontinclude examples/gras/mutual_exclusion/simple_token/simple_token.c
383 \subsection GRAS_ex_stoken_callback 3) The callback
385 Even if this is the core of this algorithm, this function is quite
388 \skip node_cb_stoken_handler
389 \until end_of_node_cb_stoken_handler
391 \subsection GRAS_ex_stoken_main 4) The main function
393 This function is splited in two parts: The first one performs all the
394 needed initialisations (points 1-7) while the end (point 8. below) calls
395 gras_msg_handle() as long as the planned amount of ring loops are not
401 \subsection GRAS_ex_mmrpc A simple RPC for matrix multiplication
403 This example implements a remote matrix multiplication. It involves a client
404 (creating the matrices and sending the multiplications requests) and a server
405 (computing the multiplication on client's behalf).
407 This example also constitutes a more advanced example of data description
408 mechanisms, since the message payload type is a bit more complicated than in
409 other examples such as the ping one (\ref GRAS_ex_ping).
411 It works the following way (not very different from the ping example):
412 - Both the client and the server register all needed messages and datatypes
413 - The server registers a callback to the "request" message, which computes
414 what needs to be and returns the result to the expeditor.
415 - The client creates two matrices, ask for their multiplication and check
418 This example resides in the <b>examples/gras/mmrpc/mmrpc.c</b> file.
420 \subsubsection GRAS_ex_mmrpc_toc Table of contents of the mmrpc example
421 - \ref GRAS_ex_mmrpc_common
422 - \ref GRAS_ex_mmrpc_header
423 - \ref GRAS_ex_mmrpc_dataregister
424 - \ref GRAS_ex_mmrpc_logdef
425 - \ref GRAS_ex_mmrpc_msgregister
426 - \ref GRAS_ex_mmrpc_server
427 - \ref GRAS_ex_mmrpc_serinc
428 - \ref GRAS_ex_mmrpc_sercb
429 - \ref GRAS_ex_mmrpc_sermain
430 - \ref GRAS_ex_mmrpc_client
431 - \ref GRAS_ex_mmrpc_cliinc
432 - \ref GRAS_ex_mmrpc_climain
437 \subsubsection GRAS_ex_mmrpc_common 1) Common code to the client and the server (mmrpc_common.c and mmrpc.h)
440 \paragraph GRAS_ex_mmrpc_header 1.a) Module header (mmrpc.h)
442 This loads the gras header and declare the function's prototypes as well
445 \dontinclude gras/mmrpc/mmrpc.h
450 \paragraph GRAS_ex_mmrpc_dataregister 1.b) Register the data types (mmrpc.h)
452 The messages involved in a matrix of double. This type is automatically
453 known by the GRAS mecanism, using the gras_datadesc_matrix() function of the
456 \paragraph GRAS_ex_mmrpc_logdef 1.c) Logging category definition (mmrpc_common.c)
458 Let's first load the module header and declare a logging category (see
459 \ref XBT_log for more info on logging). This logging category does live
460 in this file (ie the required symbols are defined here and declared as
461 "extern" in any other file using them). That is why we use
462 \ref XBT_LOG_NEW_DEFAULT_CATEGORY here and
463 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY in mmrpc_client.c and mmrpc_server.c.
465 \dontinclude gras/mmrpc/mmrpc_common.c
469 \paragraph GRAS_ex_mmrpc_msgregister 1.d) Register the messages (mmrpc_common.c)
471 This function, called by both the client and the server is in charge of
472 declaring the existing messages to GRAS.
474 The datatype description builded that way can then be used to build an array datatype or
477 \skip register_messages
480 [Back to \ref GRAS_ex_mmrpc_toc]
482 \subsubsection GRAS_ex_mmrpc_server 2) Server's code (mmrpc_server.c)
484 \paragraph GRAS_ex_mmrpc_serinc 2.a) Server intial settings
486 All module symbols live in the mmrpc_common.c file. We thus have to
487 define \ref XBT_DEFINE_TYPE_EXTERN to the preprocessor so that the
488 \ref XBT_DEFINE_TYPE symbols don't get included here. Likewise, we use
489 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY to get the log category in here.
491 \dontinclude gras/mmrpc/mmrpc_server.c
495 \paragraph GRAS_ex_mmrpc_sercb 2.b) The callback to the mmrpc message
497 Here is the callback run when the server receives any mmrpc message (this
498 will be registered later by the server). Note the way we get the message
499 payload. In the ping example, there was one additional level of pointer
500 indirection (see \ref GRAS_ex_ping_sercb). This is because the payload is
501 an array here (ie a pointer) whereas it is a scalar in the ping example.
503 \skip server_cb_request_handler
504 \until end_of_server_cb_request_handler
506 \paragraph GRAS_ex_mmrpc_sermain 2.c) The "main" of the server
508 This is the "main" of the server. You must not write any main()
509 function yourself. Instead, you just have to write a regular function
510 like this one which will act as a main.
515 [Back to \ref GRAS_ex_mmrpc_toc]
517 \subsubsection GRAS_ex_mmrpc_client 3) Client's code (mmrpc_client.c)
519 \paragraph GRAS_ex_mmrpc_cliinc 2.a) Server intial settings
521 As for the server, some extra love is needed to make sure that automatic
522 datatype parsing and log categories do work even if we are using several
525 \dontinclude gras/mmrpc/mmrpc_client.c
529 \paragraph GRAS_ex_mmrpc_climain 3.b) Client's "main" function
531 This function is quite straightforward, and the inlined comments should
532 be enough to understand it.
534 \dontinclude gras/mmrpc/mmrpc_client.c
538 [Back to \ref GRAS_ex_mmrpc_toc]
540 \subsection GRAS_ex_timer Some timer games
542 This example fools around with the GRAS timers (\ref GRAS_timer). It is
543 mainly a regression test, since it uses almost all timer features.
545 The main program registers a repetititive task and a delayed one, and
546 then loops until the <tt>still_to_do</tt> variables of its globals reach
547 0. The delayed task set it to 5, and the repetititive one decrease it
548 each time. Here is an example of output:
549 \verbatim Initialize GRAS
551 [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
552 [1108335471] Programming the delayed_action for after 2.000000 sec
553 [1108335471] Have a rest
554 [1108335472] Canceling the delayed_action.
555 [1108335472] Re-programming the delayed_action for after 2.000000 sec
556 [1108335472] Repetitive_action has nothing to do yet
557 [1108335473] Repetitive_action has nothing to do yet
558 [1108335473] delayed_action setting globals->still_to_do to 5
559 [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
560 [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
561 [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
562 [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
563 [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
564 Exiting GRAS\endverbatim
567 - \ref GRAS_ex_timer_decl
568 - \ref GRAS_ex_timer_delay
569 - \ref GRAS_ex_timer_repeat
570 - \ref GRAS_ex_timer_main
574 \subsubsection GRAS_ex_timer_decl 1. Declarations and headers
578 \subsubsection GRAS_ex_timer_delay 2. Source code of the delayed action
579 \skip repetitive_action
580 \until end_of_repetitive_action
582 \subsubsection GRAS_ex_timer_repeat 3. Source code of the repetitive action
584 \until end_of_delayed_action
586 \subsubsection GRAS_ex_timer_main 4. Source code of main function