From fcc6d3f51e021943c174f678becf0b9dd01d505e Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Fri, 27 Jan 2017 22:34:50 +0100 Subject: [PATCH 1/1] further cleanup the files provided with the tuto And update the tuto accordingly --- .gitignore | 1 + doc/doxygen/tutorial.doc | 32 +++------ doc/msg-tuto-src/Makefile | 83 ++++++++++++------------ doc/msg-tuto-src/masterworker0.c | 77 ++++++++++------------ doc/msg-tuto-src/masterworker1.c | 85 +++++++++++------------- doc/msg-tuto-src/masterworker2.c | 82 +++++++++++------------ doc/msg-tuto-src/masterworker3.c | 100 +++++++++++++---------------- doc/msg-tuto-src/masterworker4.c | 107 ++++++++++++++----------------- 8 files changed, 254 insertions(+), 313 deletions(-) diff --git a/.gitignore b/.gitignore index 24d8a14ae8..7fd94e20d4 100644 --- a/.gitignore +++ b/.gitignore @@ -115,6 +115,7 @@ tags 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 diff --git a/doc/doxygen/tutorial.doc b/doc/doxygen/tutorial.doc index 97e9a0fcec..af4bd36ecc 100644 --- a/doc/doxygen/tutorial.doc +++ b/doc/doxygen/tutorial.doc @@ -130,32 +130,20 @@ sudo apt-get install viva pajeng vite \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: diff --git a/doc/msg-tuto-src/Makefile b/doc/msg-tuto-src/Makefile index 76f60842fb..316c306b22 100644 --- a/doc/msg-tuto-src/Makefile +++ b/doc/msg-tuto-src/Makefile @@ -1,41 +1,42 @@ -# 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: masterworker1 masterworker2 masterworker3 masterworker4 -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 *~ -.PHONY: clean - +# 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 + diff --git a/doc/msg-tuto-src/masterworker0.c b/doc/msg-tuto-src/masterworker0.c index 6cf173fb80..e20b2eba06 100644 --- a/doc/msg-tuto-src/masterworker0.c +++ b/doc/msg-tuto-src/masterworker0.c @@ -1,5 +1,4 @@ -/* 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. */ @@ -13,41 +12,32 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example") /** 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 ! :)"); } @@ -57,7 +47,7 @@ static int master(int argc, char *argv[]) } 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])); } @@ -66,18 +56,17 @@ static int master(int argc, char *argv[]) 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; @@ -85,9 +74,8 @@ static int worker(int argc, char *argv[]) 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; @@ -96,20 +84,21 @@ static int worker(int argc, char *argv[]) /** 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); diff --git a/doc/msg-tuto-src/masterworker1.c b/doc/msg-tuto-src/masterworker1.c index 89ad92277b..35deb99254 100644 --- a/doc/msg-tuto-src/masterworker1.c +++ b/doc/msg-tuto-src/masterworker1.c @@ -1,5 +1,4 @@ -/* 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. */ @@ -24,49 +23,43 @@ static int worker(int argc, char* argv[]); 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"); @@ -87,28 +80,27 @@ static int master(int argc, char *argv[]) /** 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; @@ -117,20 +109,21 @@ static int worker(int argc, char *argv[]) /** 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 */ +} diff --git a/doc/msg-tuto-src/masterworker2.c b/doc/msg-tuto-src/masterworker2.c index 3a36097ea9..1974889d19 100644 --- a/doc/msg-tuto-src/masterworker2.c +++ b/doc/msg-tuto-src/masterworker2.c @@ -1,5 +1,4 @@ -/* 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. */ @@ -24,9 +23,6 @@ static int worker(int argc, char* argv[]); 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]; @@ -35,36 +31,34 @@ static int master(int argc, char *argv[]) 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++; @@ -80,39 +74,37 @@ static int master(int argc, char *argv[]) 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[]) @@ -120,16 +112,18 @@ 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 */ +} diff --git a/doc/msg-tuto-src/masterworker3.c b/doc/msg-tuto-src/masterworker3.c index 39d2cec69c..7919c3736f 100644 --- a/doc/msg-tuto-src/masterworker3.c +++ b/doc/msg-tuto-src/masterworker3.c @@ -1,5 +1,4 @@ -/* 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. */ @@ -24,118 +23,107 @@ static int worker(int argc, char* argv[]); 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 */ +} diff --git a/doc/msg-tuto-src/masterworker4.c b/doc/msg-tuto-src/masterworker4.c index 03a3668fd3..acec3257bd 100644 --- a/doc/msg-tuto-src/masterworker4.c +++ b/doc/msg-tuto-src/masterworker4.c @@ -1,5 +1,4 @@ -/* 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. */ @@ -24,53 +23,44 @@ static int worker(int argc, char* argv[]); 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) { @@ -87,39 +77,35 @@ static int master(int argc, char *argv[]) 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()); @@ -132,43 +118,44 @@ static int worker(int argc, char *argv[]) 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 */ +} -- 2.20.1