callgrind.out.*
### Examples and traces
*.exe
+doc/msg-tuto-src/masterworker0
doc/msg-tuto-src/masterworker1
doc/msg-tuto-src/masterworker2
doc/msg-tuto-src/masterworker3
\anchor intro_setup
## Setting up and Compiling
-The corresponding archive with all source files can be obtained
-[here](http://simgrid.gforge.inria.fr/tutorials/msg-tuto/msg-tuto.tgz),
-while the simgrid archive contains
-[several platform files](https://github.com/simgrid/simgrid/tree/master/examples/platforms)
-(click on the "Raw" button of files you want to download from GitHub).
+The corresponding source files can be obtained
+[online on GitLab](https://gitlab.inria.fr/simgrid/simgrid/tree/master/doc/msg-tuto-src).
+If you find the right button on the top right of the interface, you can download the whole
+directory in one archive file. If you wish, you can find other platform file in
+[this GitLab directory](https://gitlab.inria.fr/simgrid/simgrid/tree/master/doc/examples/platforms).
-~~~~{.sh}
-tar zxf msg-tuto.tgz
-cd msg-tuto/src
-make
-~~~~
-
-As you can see, there is already a nice Makefile that compiles
-everything for you. Now the tiny example has been compiled and it
-can be easily run as follows:
-
-~~~~{.sh}
-./masterworker0 platforms/platform.xml deployment0.xml 2>&1
-~~~~
+As you can see, there is already a little Makefile that compiles
+everything for you. If you struggle with the compilation, then you should double check
+your SimGrid installation. On need, please refer to the @ref install_yours_trouble section.
-If you create a single self-content C-file named foo.c, the
-corresponding program will be simply compiled and linked with
-SimGrid by typing:
+Once the tiny example has been compiled and it can be easily run as follows:
~~~~{.sh}
-make foo
+./masterworker0 platforms/platform.xml deployment0.xml
~~~~
For a more "fancy" output, you can try:
-# This Makefile is specifically tailored for the binaries of this tutorial.\r
-\r
-# For your own project, you should use the one provided at \r
-# http://simgrid.gforge.inria.fr/simgrid/latest/doc/install_yours.html\r
-\r
-# Some configuration\r
-SIMGRID_INSTALL_PATH = /opt/simgrid # Where you installed simgrid\r
-CC = gcc # Your compiler (on Mac, use clang instead)\r
-\r
-# No change needed bellow for this tutorial.\r
-############################################################################\r
-\r
-all: masterworker1 masterworker2 masterworker3 masterworker4\r
-masterworker1: masterworker1.o\r
-masterworker2: masterworker2.o\r
-masterworker3: masterworker3.o\r
-masterworker4: masterworker4.o\r
-\r
-WARNING = -Wshadow -Wcast-align -Waggregate-return -Wmissing-prototypes \\r
- -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes \\r
- -Wmissing-declarations -Wmissing-noreturn -Wredundant-decls \\r
- -Wnested-externs -Wpointer-arith -Wwrite-strings\r
-\r
-# CFLAGS = -g -O0 $(WARNINGS) # Use this line to make debugging easier\r
-CFLAGS = -g -O2 $(WARNINGS) # Use this line to get better performance\r
-\r
-# No change should be mandated past that line\r
-#############################################\r
-# The following are implicit rules, used by default to actually build\r
-# the targets for which you listed the dependencies above.\r
-\r
-# The blanks before the $(CC) must be a Tab char, not spaces\r
-%: %.o\r
- $(CC) -L$(strip $(SIMGRID_INSTALL_PATH))/lib/ $(CFLAGS) $^ -lsimgrid -o $@\r
-%.o: %.c\r
- $(CC) -I$(strip $(SIMGRID_INSTALL_PATH))/include $(CFLAGS) -c -o $@ $<\r
- \r
-clean:\r
- rm -f *.o *~\r
-.PHONY: clean\r
- \r
+# This Makefile is specifically tailored for the binaries of this tutorial.
+
+# For your own project, you should use the one provided at
+# http://simgrid.gforge.inria.fr/simgrid/latest/doc/install_yours.html
+
+# Some configuration
+SIMGRID_INSTALL_PATH = /opt/simgrid # Where you installed simgrid
+CC = gcc # Your compiler (on Mac, use clang instead)
+
+# No change needed bellow for this tutorial.
+############################################################################
+
+all: masterworker0 masterworker1 masterworker2 masterworker3 masterworker4
+masterworker0: masterworker0.o
+masterworker1: masterworker1.o
+masterworker2: masterworker2.o
+masterworker3: masterworker3.o
+masterworker4: masterworker4.o
+
+WARNING = -Wshadow -Wcast-align -Waggregate-return -Wmissing-prototypes \
+ -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes \
+ -Wmissing-declarations -Wmissing-noreturn -Wredundant-decls \
+ -Wnested-externs -Wpointer-arith -Wwrite-strings
+
+# CFLAGS = -g -O0 $(WARNINGS) # Use this line to make debugging easier
+CFLAGS = -g -O2 $(WARNINGS) # Use this line to get better performance
+
+# No change should be mandated past that line
+#############################################
+# The following are implicit rules, used by default to actually build
+# the targets for which you listed the dependencies above.
+
+# The blanks before the $(CC) must be a Tab char, not spaces
+%: %.o
+ $(CC) -L$(strip $(SIMGRID_INSTALL_PATH))/lib/ $(CFLAGS) $^ -lsimgrid -o $@
+%.o: %.c
+ $(CC) -I$(strip $(SIMGRID_INSTALL_PATH))/include $(CFLAGS) -c -o $@ $<
+
+clean:
+ rm -f *.o *~ masterworker0 masterworker1 masterworker2 masterworker3 masterworker4
+.PHONY: clean
+
-/* Copyright (c) 2007-2010, 2013-2015. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
/** Master expects 3+ arguments given in the XML deployment file: */
static int master(int argc, char *argv[])
{
- int workers_count = 0;
- msg_host_t *workers = NULL;
- msg_task_t *todo = NULL;
-
- int i;
-
long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
- { /* Task creation */
- char sprintf_buffer[64];
-
- todo = xbt_new0(msg_task_t, number_of_tasks);
+ /* Create the tasks in advance */
+ msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);
- for (i = 0; i < number_of_tasks; i++) {
- sprintf(sprintf_buffer, "Task_%d", i);
- todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
- }
+ for (int i = 0; i < number_of_tasks; i++) {
+ char sprintf_buffer[64];
+ sprintf(sprintf_buffer, "Task_%d", i);
+ todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
}
- { /* Process organization */
- workers_count = argc - 4;
- workers = xbt_new0(msg_host_t, workers_count);
+ /* Get the info about the worker processes from my parameters */
+ int workers_count = argc - 4;
+ msg_host_t* workers = xbt_new0(msg_host_t, workers_count);
- for (i = 4; i < argc; i++) {
- workers[i - 4] = MSG_get_host_by_name(argv[i]);
- xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
- }
+ for (int i = 4; i < argc; i++) {
+ workers[i - 4] = MSG_get_host_by_name(argv[i]);
+ xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
}
-
XBT_INFO("Got %d workers and %ld tasks to process", workers_count, number_of_tasks);
- for (i = 0; i < number_of_tasks; i++) {
- XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name, MSG_host_get_name(workers[i % workers_count]));
+ /* Dispatch the tasks */
+ for (int i = 0; i < number_of_tasks; i++) {
+ XBT_INFO("Sending '%s' to '%s'", todo[i]->name, MSG_host_get_name(workers[i % workers_count]));
if (MSG_host_self() == workers[i % workers_count]) {
XBT_INFO("Hey ! It's me ! :)");
}
}
XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
- for (i = 0; i < workers_count; i++) {
+ for (int i = 0; i < workers_count; i++) {
msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
MSG_task_send(finalize, MSG_host_get_name(workers[i]));
}
free(workers);
free(todo);
return 0;
-} /* end_of_master */
+}
/** Worker expects a single argument given in the XML deployment file: */
static int worker(int argc, char *argv[])
{
- msg_task_t task = NULL;
- XBT_ATTRIB_UNUSED int res;
while (1) {
- res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
+ msg_task_t task = NULL;
+ int res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
xbt_assert(res == MSG_OK, "MSG_task_receive failed");
- XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
+ XBT_INFO("Received '%s'", MSG_task_get_name(task));
if (!strcmp(MSG_task_get_name(task), "finalize")) {
MSG_task_destroy(task);
break;
XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
MSG_task_execute(task);
- XBT_INFO("\"%s\" done", MSG_task_get_name(task));
+ XBT_INFO("'%s' done", MSG_task_get_name(task));
MSG_task_destroy(task);
- task = NULL;
}
XBT_INFO("I'm done. See you!");
return 0;
/** Main function */
int main(int argc, char *argv[])
{
- msg_error_t res = MSG_OK;
MSG_init(&argc, argv);
xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
"\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
- { /* Simulation setting */
- MSG_create_environment(argv[1]);
- }
- { /* Application deployment */
- MSG_function_register("master", master);
- MSG_function_register("worker", worker);
- MSG_launch_application(argv[2]);
- }
- res = MSG_main();
+
+ /* Create a simulated platform */
+ MSG_create_environment(argv[1]);
+
+ /* Application deployment */
+ MSG_function_register("master", master);
+ MSG_function_register("worker", worker);
+ MSG_launch_application(argv[2]);
+
+ /* Run the simulation */
+ msg_error_t res = MSG_main();
XBT_INFO("Simulation time %g", MSG_get_clock());
return (res != MSG_OK);
-/* Copyright (c) 2007-2010, 2013-2015. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
static int master(int argc, char *argv[])
{
- int workers_count = 0;
- msg_host_t *workers = NULL;
- msg_task_t *todo = NULL;
msg_host_t host_self = MSG_host_self();
- char *master_name = (char *) MSG_host_get_name(host_self);
+ const char* master_name = MSG_host_get_name(host_self);
char channel[1024];
long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
- { /* Task creation */
- char sprintf_buffer[64];
-
- todo = xbt_new0(msg_task_t, number_of_tasks);
+ /* Create the tasks in advance */
+ msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);
- for (int i = 0; i < number_of_tasks; i++) {
- sprintf(sprintf_buffer, "Task_%d", i);
- todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
- }
+ for (int i = 0; i < number_of_tasks; i++) {
+ char sprintf_buffer[64];
+ sprintf(sprintf_buffer, "Task_%d", i);
+ todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
}
- { /* Process organization */
- workers_count = MSG_get_host_number();
- workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
+ /* Get the info about the worker processes (directly from SimGrid) */
+ int workers_count = argc - 4;
+ msg_host_t* workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
- for (int i = 0; i < workers_count; i++)
- if(host_self == workers[i]) {
- workers[i] = workers[workers_count-1];
- workers_count--;
- break;
- }
-
- for (int i = 0; i < workers_count; i++)
- MSG_process_create("worker", worker, master_name, workers[i]);
- }
+ for (int i = 0; i < workers_count; i++)
+ if (host_self == workers[i]) {
+ workers[i] = workers[workers_count - 1];
+ workers_count--;
+ break;
+ }
- XBT_INFO("Got %d workers and %Ld tasks to process", workers_count, number_of_tasks);
+ for (int i = 0; i < workers_count; i++)
+ MSG_process_create("worker", worker, (void*)master_name, workers[i]);
+ XBT_INFO("Got %d workers and %ld tasks to process", workers_count, number_of_tasks);
+ /* Dispatch the tasks */
for (int i = 0; i < number_of_tasks; i++) {
build_channel_name(channel, master_name, MSG_host_get_name(workers[i % workers_count]));
- XBT_INFO("Sending \"%s\" to channel \"%s\"", todo[i]->name, channel);
+ XBT_INFO("Sending '%s' to channel '%s'", todo[i]->name, channel);
MSG_task_send(todo[i], channel);
XBT_INFO("Sent");
/** Receiver function */
static int worker(int argc, char *argv[])
{
- msg_task_t task = NULL;
char channel[1024];
build_channel_name(channel, MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
- XBT_INFO("Receiving on channel \"%s\"", channel);
+ XBT_INFO("Receiving on channel '%s'", channel);
while (1) {
+ msg_task_t task = NULL;
int res = MSG_task_receive(&(task), channel);
xbt_assert(res == MSG_OK, "MSG_task_receive failed");
-
- XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
+
+ XBT_INFO("Received '%s'", MSG_task_get_name(task));
if (!strcmp(MSG_task_get_name(task), "finalize")) {
MSG_task_destroy(task);
break;
}
- XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
+ XBT_INFO("Processing '%s'", MSG_task_get_name(task));
MSG_task_execute(task);
- XBT_INFO("\"%s\" done", MSG_task_get_name(task));
+ XBT_INFO("'%s' done", MSG_task_get_name(task));
MSG_task_destroy(task);
- task = NULL;
}
XBT_INFO("I'm done. See you!");
return 0;
/** Main function */
int main(int argc, char *argv[])
{
-
MSG_init(&argc, argv);
xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
"\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
- { /* Simulation setting */
- MSG_create_environment(argv[1]);
- }
- { /* Application deployment */
- MSG_function_register("master", master);
- MSG_function_register("worker", worker);
- MSG_launch_application(argv[2]);
- }
+
+ /* Create a simulated platform */
+ MSG_create_environment(argv[1]);
+
+ /* Application deployment */
+ MSG_function_register("master", master);
+ MSG_function_register("worker", worker);
+ MSG_launch_application(argv[2]);
+
+ /* Run the simulation */
msg_error_t res = MSG_main();
XBT_INFO("Simulation time %g", MSG_get_clock());
return (res != MSG_OK);
-} /* end_of_main */
+}
-/* Copyright (c) 2007-2010, 2013-2015. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
static int master(int argc, char *argv[])
{
- int workers_count = 0;
- msg_host_t *workers = NULL;
- msg_task_t *todo = NULL;
msg_host_t host_self = MSG_host_self();
char *master_name = (char *) MSG_host_get_name(host_self);
char channel[1024];
double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
- { /* Process organization */
- workers_count = MSG_get_host_number();
- workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
+ /* Get the info about the worker processes (directly from SimGrid) */
+ int workers_count = argc - 4;
+ msg_host_t* workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
- for (int i = 0; i < workers_count; i++)
- if (host_self == workers[i]) {
- workers[i] = workers[workers_count-1];
- workers_count--;
- break;
- }
-
- for (int i = 0; i < workers_count; i++)
- MSG_process_create("worker", worker, master_name, workers[i]);
- }
+ for (int i = 0; i < workers_count; i++) // Remove my host from the list
+ if (host_self == workers[i]) {
+ workers[i] = workers[workers_count - 1];
+ workers_count--;
+ break;
+ }
- XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout);
+ for (int i = 0; i < workers_count; i++)
+ MSG_process_create("worker", worker, (void*)master_name, workers[i]);
+ XBT_INFO("Got %d workers and will send tasks for %g seconds", workers_count, timeout);
+ /* Dispatch the tasks */
int task_num = 0;
while (1) {
- char sprintf_buffer[64];
- msg_task_t task = NULL;
-
- if(MSG_get_clock()>timeout) break;
+ if (MSG_get_clock() > timeout)
+ break;
+ char sprintf_buffer[64];
sprintf(sprintf_buffer, "Task_%d", task_num);
- task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
+ msg_task_t task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
build_channel_name(channel, master_name, MSG_host_get_name(workers[task_num % workers_count]));
- XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
+ XBT_DEBUG("Sending '%s' to channel '%s'", task->name, channel);
MSG_task_send(task, channel);
XBT_DEBUG("Sent");
task_num++;
XBT_INFO("Sent %d tasks in total!", task_num);
free(workers);
- free(todo);
return 0;
-} /* end_of_master */
+}
/** Worker function */
static int worker(int argc, char *argv[])
{
- msg_task_t task = NULL;
char channel[1024];
build_channel_name(channel,MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
- XBT_DEBUG("Receiving on channel \"%s\"", channel);
+ XBT_DEBUG("Receiving on channel '%s'", channel);
while (1) {
+ msg_task_t task = NULL;
int res = MSG_task_receive(&(task), channel);
xbt_assert(res == MSG_OK, "MSG_task_receive failed");
-
- XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
+
+ XBT_DEBUG("Received '%s'", MSG_task_get_name(task));
if (!strcmp(MSG_task_get_name(task), "finalize")) {
MSG_task_destroy(task);
break;
}
- XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
+ XBT_DEBUG("Processing '%s'", MSG_task_get_name(task));
MSG_task_execute(task);
- XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
+ XBT_DEBUG("'%s' done", MSG_task_get_name(task));
MSG_task_destroy(task);
- task = NULL;
}
XBT_DEBUG("I'm done. See you!");
return 0;
-} /* end_of_worker */
+}
/** Main function */
int main(int argc, char *argv[])
MSG_init(&argc, argv);
xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
"\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
- { /* Simulation setting */
- MSG_create_environment(argv[1]);
- }
- { /* Application deployment */
- MSG_function_register("master", master);
- MSG_function_register("worker", worker);
- MSG_launch_application(argv[2]);
- }
+
+ /* Create a simulated platform */
+ MSG_create_environment(argv[1]);
+
+ /* Application deployment */
+ MSG_function_register("master", master);
+ MSG_function_register("worker", worker);
+ MSG_launch_application(argv[2]);
+
+ /* Run the simulation */
msg_error_t res = MSG_main();
XBT_INFO("Simulation time %g", MSG_get_clock());
return (res != MSG_OK);
-} /* end_of_main */
+}
-/* Copyright (c) 2007-2010, 2013-2015. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
static int master(int argc, char *argv[])
{
- int workers_count = 0;
- msg_host_t *workers = NULL;
- msg_task_t *todo = NULL;
msg_host_t host_self = MSG_host_self();
char *master_name = (char *) MSG_host_get_name(host_self);
char channel[1024];
- int i;
-
TRACE_category(master_name);
double timeout = xbt_str_parse_double(argv[1], "Invalid timeout: %s"); /** - timeout */
double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
- { /* Process organization */
- workers_count = MSG_get_host_number();
- workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
+ /* Get the info about the worker processes */
+ int workers_count = argc - 4;
+ msg_host_t* workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
- for (i = 0; i < workers_count; i++)
- if(host_self == workers[i]) {
- workers[i] = workers[workers_count-1];
- workers_count--;
- break;
- }
+ for (int i = 0; i < workers_count; i++) // Remove my host from the list
+ if (host_self == workers[i]) {
+ workers[i] = workers[workers_count - 1];
+ workers_count--;
+ break;
+ }
- for (i = 0; i < workers_count; i++)
- MSG_process_create("worker", worker, master_name, workers[i]);
- }
+ for (int i = 0; i < workers_count; i++)
+ MSG_process_create("worker", worker, (void*)master_name, workers[i]);
+ XBT_INFO("Got %d workers and will send tasks for %g seconds", workers_count, timeout);
- XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout);
+ /* Dispatch the tasks */
+ int task_num = 0;
+ while (1) {
+ if (MSG_get_clock() > timeout)
+ break;
- for (i = 0; 1; i++) {
char sprintf_buffer[64];
- msg_task_t task = NULL;
-
- if(MSG_get_clock()>timeout) break;
+ sprintf(sprintf_buffer, "Task_%d", task_num);
- sprintf(sprintf_buffer, "Task_%d", i);
- task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
+ msg_task_t task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
MSG_task_set_category(task, master_name);
- build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count]));
+ build_channel_name(channel, master_name, MSG_host_get_name(workers[task_num % workers_count]));
- XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
+ XBT_DEBUG("Sending '%s' to channel '%s'", task->name, channel);
MSG_task_send(task, channel);
XBT_DEBUG("Sent");
}
- int task_num = i;
-
XBT_DEBUG ("All tasks have been dispatched. Let's tell everybody the computation is over.");
- for (i = 0; i < workers_count; i++) {
+ for (int i = 0; i < workers_count; i++) {
msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
}
XBT_INFO("Sent %d tasks in total!", task_num);
free(workers);
- free(todo);
return 0;
-} /* end_of_master */
+}
/** Worker function */
static int worker(int argc, char *argv[])
{
- msg_task_t task = NULL;
- XBT_ATTRIB_UNUSED int res;
char channel[1024];
-
build_channel_name(channel,MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
- XBT_DEBUG("Receiving on channel \"%s\"", channel);
+ XBT_DEBUG("Receiving on channel '%s'", channel);
while (1) {
- res = MSG_task_receive(&(task),channel);
+ msg_task_t task = NULL;
+ int res = MSG_task_receive(&(task), channel);
xbt_assert(res == MSG_OK, "MSG_task_get failed");
-
- XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
+
+ XBT_DEBUG("Received '%s'", MSG_task_get_name(task));
if (!strcmp(MSG_task_get_name(task), "finalize")) {
MSG_task_destroy(task);
break;
}
- XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
+ XBT_DEBUG("Processing '%s'", MSG_task_get_name(task));
MSG_task_execute(task);
- XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
+ XBT_DEBUG("'%s' done", MSG_task_get_name(task));
MSG_task_destroy(task);
- task = NULL;
}
XBT_DEBUG("I'm done. See you!");
return 0;
-} /* end_of_worker */
+}
/** Main function */
int main(int argc, char *argv[])
{
- msg_error_t res = MSG_OK;
-
MSG_init(&argc, argv);
xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
"\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
- { /* Simulation setting */
- MSG_create_environment(argv[1]);
- }
- { /* Application deployment */
- MSG_function_register("master", master);
- MSG_function_register("worker", worker);
- MSG_launch_application(argv[2]);
- }
- res = MSG_main();
+
+ /* Create a simulated platform */
+ MSG_create_environment(argv[1]);
+
+ /* Application deployment */
+ MSG_function_register("master", master);
+ MSG_function_register("worker", worker);
+ MSG_launch_application(argv[2]);
+
+ /* Run the simulation */
+ msg_error_t res = MSG_main();
XBT_INFO("Simulation time %g", MSG_get_clock());
return (res != MSG_OK);
-} /* end_of_main */
+}
-/* Copyright (c) 2007-2010, 2013-2015. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
static int master(int argc, char *argv[])
{
- int workers_count = 0;
- XBT_ATTRIB_UNUSED int res;
- msg_host_t *workers = NULL;
- msg_task_t *todo = NULL;
msg_host_t host_self = MSG_host_self();
char *master_name = (char *) MSG_host_get_name(host_self);
char channel[1024];
- int i;
-
TRACE_category(master_name);
double timeout = xbt_str_parse_double(argv[1], "Invalid timeout: %s"); /** - timeout */
double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
- { /* Process organization */
- workers_count = MSG_get_host_number();
- workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
-
- for (i = 0; i < workers_count; i++)
- if(host_self == workers[i]) {
- workers[i] = workers[workers_count-1];
- workers_count--;
- break;
- }
+ /* Get the info about the worker processes */
+ int workers_count = argc - 4;
+ msg_host_t* workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
- for (i = 0; i < workers_count; i++)
- MSG_process_create("worker", worker, master_name, workers[i]);
- }
+ for (int i = 0; i < workers_count; i++) // Remove my host from the list
+ if (host_self == workers[i]) {
+ workers[i] = workers[workers_count - 1];
+ workers_count--;
+ break;
+ }
+
+ for (int i = 0; i < workers_count; i++)
+ MSG_process_create("worker", worker, (void*)master_name, workers[i]);
+ XBT_INFO("Got %d workers and will send tasks for %g seconds", workers_count, timeout);
- XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout);
+ /* Dispatch the tasks */
xbt_dynar_t idle_hosts = xbt_dynar_new(sizeof(msg_host_t), NULL);
msg_host_t request_host = NULL;
+ int task_num = 0;
+ while (1) {
- for (i = 0; 1;) {
- char sprintf_buffer[64];
- msg_task_t task = NULL;
-
- msg_task_t request = NULL;
- while(MSG_task_listen(master_name)) {
- res = MSG_task_receive(&(request),master_name);
+ while (MSG_task_listen(master_name)) {
+ msg_task_t request = NULL;
+ int res = MSG_task_receive(&(request), master_name);
xbt_assert(res == MSG_OK, "MSG_task_receive failed");
request_host = MSG_task_get_data(request);
xbt_dynar_push(idle_hosts, &request_host);
MSG_task_destroy(request);
- request = NULL;
}
if(MSG_get_clock()>timeout) {
continue;
}
- sprintf(sprintf_buffer, "Task_%d", i);
- task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
+ char sprintf_buffer[64];
+ sprintf(sprintf_buffer, "Task_%d", task_num);
+ msg_task_t task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
MSG_task_set_category(task, master_name);
xbt_dynar_shift(idle_hosts, &request_host);
build_channel_name(channel,master_name, MSG_host_get_name(request_host));
- XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
+ XBT_DEBUG("Sending '%s' to channel '%s'", task->name, channel);
MSG_task_send(task, channel);
XBT_DEBUG("Sent");
- i++;
+ task_num++;
}
- int task_num = i;
-
XBT_DEBUG ("All tasks have been dispatched. Let's tell everybody the computation is over.");
- for (i = 0; i < workers_count; i++) {
+ for (int i = 0; i < workers_count; i++) {
msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
}
XBT_INFO("Sent %d tasks in total!", task_num);
free(workers);
- free(todo);
return 0;
-} /* end_of_master */
+}
/** Worker function */
static int worker(int argc, char *argv[])
{
- msg_task_t task = NULL;
- XBT_ATTRIB_UNUSED int res;
char channel[1024];
const char *my_master = MSG_process_get_data(MSG_process_self());
msg_task_t request = MSG_task_create("request", 0, 0, MSG_host_self());
MSG_task_send(request, my_master);
- res = MSG_task_receive(&(task),channel);
+ /* Wait for the answer */
+ msg_task_t task = NULL;
+ int res = MSG_task_receive(&(task), channel);
xbt_assert(res == MSG_OK, "MSG_task_receive failed");
-
- XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
+
+ XBT_DEBUG("Received '%s'", MSG_task_get_name(task));
if (!strcmp(MSG_task_get_name(task), "finalize")) {
MSG_task_destroy(task);
break;
}
- XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
+ XBT_DEBUG("Processing '%s'", MSG_task_get_name(task));
MSG_task_execute(task);
- XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
+ XBT_DEBUG("'%s' done", MSG_task_get_name(task));
MSG_task_destroy(task);
- task = NULL;
}
XBT_DEBUG("I'm done. See you!");
return 0;
-} /* end_of_worker */
+}
/** Main function */
int main(int argc, char *argv[])
{
- msg_error_t res = MSG_OK;
-
MSG_init(&argc, argv);
xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
"\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
- { /* Simulation setting */
- MSG_create_environment(argv[1]);
- }
- { /* Application deployment */
- MSG_function_register("master", master);
- MSG_function_register("worker", worker);
- MSG_launch_application(argv[2]);
- }
- res = MSG_main();
+
+ /* Create a simulated platform */
+ MSG_create_environment(argv[1]);
+
+ /* Application deployment */
+ MSG_function_register("master", master);
+ MSG_function_register("worker", worker);
+ MSG_launch_application(argv[2]);
+
+ /* Run the simulation */
+ msg_error_t res = MSG_main();
XBT_INFO("Simulation time %g", MSG_get_clock());
return (res != MSG_OK);
-} /* end_of_main */
+}