SimGrid  3.19.1
Versatile Simulation of Distributed Systems
SimGrid Examples

SimGrid comes with many examples provided in the examples/ directory. Those examples are described in section MSG examples. Those examples are commented and should be easy to understand. for a first step into SimGrid we also provide some more detailed examples in the sections below.

You should also check our online tutorial section that contains a generic tutorial about using SimGrid.

Using MSG

You should also check our online tutorial section that contains a dedicated tutorial.

Here are some examples on how to use MSG, the most used API.

MSG comes with an extensive set of examples. It is sometimes difficult to find the one you need. This list aims at helping you finding the example from which you can learn what you want to.

Basic examples and features

Basic Master/Workers

Simulation of a master-worker application using a realistic platform and an external description of the deployment.

Table of contents:


Preliminary declarations

#include "simgrid/msg.h"
#include <stdio.h> /* snprintf */
XBT_LOG_NEW_DEFAULT_CATEGORY(msg_app_masterworker, "Messages specific for this msg example");

Master code

This function has to be assigned to a msg_process_t that will behave as the master. It should not be called directly but either given as a parameter to MSG_process_create() or registered as a public function through MSG_function_register() and then automatically assigned to a process through MSG_launch_application().

C style arguments (argc/argv) are interpreted as:

  • the number of tasks to distribute
  • the computational size of each task
  • the communication size of each task
  • the number of workers managed by the master.

Tasks are evenly sent in a round-robin style.

Worker code

This function has to be assigned to a msg_process_t that has to behave as a worker. Just like the master function (described in Master code), it should not be called directly.

C style arguments (argc/argv) are interpreted as:

  • a unique id used to build the mailbox name of the worker

This function keeps waiting for tasks and executes them as it receives them. When a special task named 'finalize' is received from the master, the process ends its execution.

Main function

This function is the core of the simulation and is divided only into 3 parts:

  1. Simulation settings : MSG_create_environment() creates a realistic environment
  2. Application deployment : create the processes on the right locations with MSG_launch_application()
  3. The simulation is run with MSG_main()

Its arguments are:

  • platform_file: the name of a file containing an valid platform description.
  • deployment_file: the name of a file containing a valid application description

Example of a platform file

The following platform description can be found in examples/msg/platforms/small_platform.xml

<?xml version='1.0'?>
<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
<platform version="4.1">
<zone id="zone0" routing="Full">
<host id="Tremblay" speed="98.095Mf"/>
<host id="Jupiter" speed="76.296Mf"/>
<host id="Fafard" speed="76.296Mf"/>
<host id="Ginette" speed="48.492Mf"/>
<host id="Bourassa" speed="48.492Mf"/>
<host id="Jacquelin" speed="137.333Mf"/>
<host id="Boivin" speed="98.095Mf"/>
<link id="6" bandwidth="41.279125MBps" latency="59.904us"/>
<link id="3" bandwidth="34.285625MBps" latency="514.433us"/>
<link id="7" bandwidth="11.618875MBps" latency="189.98us"/>
<link id="9" bandwidth="7.20975MBps" latency="1.461517ms"/>
<link id="2" bandwidth="118.6825MBps" latency="136.931us"/>
<link id="8" bandwidth="8.158MBps" latency="270.544us"/>
<link id="1" bandwidth="34.285625MBps" latency="514.433us"/>
<link id="4" bandwidth="10.099625MBps" latency="479.78us"/>
<link id="0" bandwidth="41.279125MBps" latency="59.904us"/>
<link id="5" bandwidth="27.94625MBps" latency="278.066us"/>
<link id="145" bandwidth="2.583375MBps" latency="410.463us"/>
<link id="10" bandwidth="34.285625MBps" latency="514.433us"/>
<link id="11" bandwidth="118.6825MBps" latency="136.931us"/>
<link id="16" bandwidth="34.285625MBps" latency="514.433us"/>
<link id="17" bandwidth="118.6825MBps" latency="136.931us"/>
<link id="44" bandwidth="10.314625MBps" latency="6.932556ms"/>
<link id="47" bandwidth="10.314625MBps" latency="6.932556ms"/>
<link id="54" bandwidth="15.376875MBps" latency="35.083019ms"/>
<link id="56" bandwidth="21.41475MBps" latency="29.5890617ms"/>
<link id="59" bandwidth="11.845375MBps" latency="370.788us"/>
<link id="78" bandwidth="27.94625MBps" latency="278.066us"/>
<link id="79" bandwidth="8.42725MBps" latency="156.056us"/>
<link id="80" bandwidth="15.376875MBps" latency="35.083019ms"/>
<link id="loopback" bandwidth="498MBps" latency="15us" sharing_policy="FATPIPE"/>
<route src="Tremblay" dst="Tremblay">
<link_ctn id="loopback"/>
</route>
<route src="Jupiter" dst="Jupiter">
<link_ctn id="loopback"/>
</route>
<route src="Fafard" dst="Fafard">
<link_ctn id="loopback"/>
</route>
<route src="Ginette" dst="Ginette">
<link_ctn id="loopback"/>
</route>
<route src="Bourassa" dst="Bourassa">
<link_ctn id="loopback"/>
</route>
<route src="Tremblay" dst="Jupiter">
<link_ctn id="9"/>
</route>
<route src="Tremblay" dst="Fafard">
<link_ctn id="4"/>
<link_ctn id="3"/>
<link_ctn id="2"/>
<link_ctn id="0"/>
<link_ctn id="1"/>
<link_ctn id="8"/>
</route>
<route src="Tremblay" dst="Ginette">
<link_ctn id="4"/>
<link_ctn id="3"/>
<link_ctn id="5"/>
</route>
<route src="Tremblay" dst="Bourassa">
<link_ctn id="4"/>
<link_ctn id="3"/>
<link_ctn id="2"/>
<link_ctn id="0"/>
<link_ctn id="1"/>
<link_ctn id="6"/>
<link_ctn id="7"/>
</route>
<route src="Jupiter" dst="Fafard">
<link_ctn id="9"/>
<link_ctn id="4"/>
<link_ctn id="3"/>
<link_ctn id="2"/>
<link_ctn id="0"/>
<link_ctn id="1"/>
<link_ctn id="8"/>
</route>
<route src="Jupiter" dst="Bourassa">
<link_ctn id="9"/>
<link_ctn id="4"/>
<link_ctn id="3"/>
<link_ctn id="2"/>
<link_ctn id="0"/>
<link_ctn id="1"/>
<link_ctn id="6"/>
<link_ctn id="7"/>
</route>
<route src="Fafard" dst="Ginette">
<link_ctn id="8"/>
<link_ctn id="1"/>
<link_ctn id="0"/>
<link_ctn id="2"/>
<link_ctn id="5"/>
</route>
<route src="Jupiter" dst="Jacquelin">
<link_ctn id="145"/>
</route>
<route src="Jupiter" dst="Boivin">
<link_ctn id="47"/>
</route>
<route src="Jupiter" dst="Ginette">
<link_ctn id="9"/>
<link_ctn id="4"/>
<link_ctn id="3"/>
<link_ctn id="5"/>
</route>
<route src="Fafard" dst="Bourassa">
<link_ctn id="8"/>
<link_ctn id="6"/>
<link_ctn id="7"/>
</route>
<route src="Ginette" dst="Bourassa">
<link_ctn id="5"/>
<link_ctn id="2"/>
<link_ctn id="0"/>
<link_ctn id="1"/>
<link_ctn id="6"/>
<link_ctn id="7"/>
</route>
<route src="Ginette" dst="Jacquelin">
<link_ctn id="145"/>
</route>
<route src="Ginette" dst="Boivin">
<link_ctn id="47"/>
</route>
<route src="Bourassa" dst="Jacquelin">
<link_ctn id="145"/>
</route>
<route src="Bourassa" dst="Boivin">
<link_ctn id="47"/>
</route>
<route src="Jacquelin" dst="Boivin">
<link_ctn id="145"/>
<link_ctn id="59"/>
<link_ctn id="56"/>
<link_ctn id="54"/>
<link_ctn id="17"/>
<link_ctn id="16"/>
<link_ctn id="10"/>
<link_ctn id="11"/>
<link_ctn id="44"/>
<link_ctn id="47"/>
</route>
<route src="Jacquelin" dst="Fafard">
<link_ctn id="145"/>
<link_ctn id="59"/>
<link_ctn id="56"/>
<link_ctn id="54"/>
<link_ctn id="17"/>
<link_ctn id="16"/>
<link_ctn id="10"/>
<link_ctn id="6"/>
<link_ctn id="9"/>
<link_ctn id="79"/>
<link_ctn id="78"/>
</route>
<route src="Jacquelin" dst="Tremblay">
<link_ctn id="145"/>
<link_ctn id="59"/>
<link_ctn id="56"/>
<link_ctn id="54"/>
<link_ctn id="2"/>
<link_ctn id="3"/>
</route>
<route src="Boivin" dst="Tremblay">
<link_ctn id="47"/>
<link_ctn id="44"/>
<link_ctn id="11"/>
<link_ctn id="10"/>
<link_ctn id="16"/>
<link_ctn id="0"/>
<link_ctn id="3"/>
</route>
<route src="Boivin" dst="Fafard">
<link_ctn id="47"/>
<link_ctn id="44"/>
<link_ctn id="11"/>
<link_ctn id="6"/>
<link_ctn id="9"/>
<link_ctn id="79"/>
<link_ctn id="78"/>
<link_ctn id="80"/>
</route>
</zone>
</platform>

Example of a deployment file

The following application description can be found in examples/msg/app-masterworker/app-masterworker_d.xml:

<?xml version='1.0'?>
<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
<platform version="4.1">
<!-- The master actor (with some arguments) -->
<actor host="Tremblay" function="master">
<argument value="20"/> <!-- Number of tasks -->
<argument value="50000000"/> <!-- Computation size of tasks -->
<argument value="1000000"/> <!-- Communication size of tasks -->
<argument value="5"/> <!-- Number of workers -->
</actor>
<!-- The worker processes (with mailbox to listen on as argument) -->
<actor host="Tremblay" function="worker" on_failure="RESTART">
<argument value="0"/>
</actor>
<actor host="Jupiter" function="worker" on_failure="RESTART">
<argument value="1"/>
</actor>
<actor host="Fafard" function="worker" on_failure="RESTART">
<argument value="2"/>
</actor>
<actor host="Ginette" function="worker" on_failure="RESTART">
<argument value="3"/>
</actor>
<actor host="Bourassa" function="worker" on_failure="RESTART">
<argument value="4"/>
</actor>
</platform>