From: Navarrop Date: Wed, 30 Nov 2011 15:52:09 +0000 (+0100) Subject: Add a masterslave example with arguments X-Git-Tag: exp_20120216~241^2~29 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/c1cb2509d6783e56d7334bb4872a1101318816f5 Add a masterslave example with arguments number_of_jobs number_of_slaves --- diff --git a/examples/msg/masterslave/CMakeLists.txt b/examples/msg/masterslave/CMakeLists.txt index dc90fe30c9..b6b352f84d 100644 --- a/examples/msg/masterslave/CMakeLists.txt +++ b/examples/msg/masterslave/CMakeLists.txt @@ -9,6 +9,7 @@ add_executable(masterslave_bypass "masterslave_bypass.c") add_executable(masterslave_console "masterslave_console.c") add_executable(masterslave_cluster "masterslave_cluster.c") add_executable(masterslave_kill "masterslave_kill.c") +add_executable(masterslave_arg "masterslave_arg.c") ### Add definitions for compile if(WIN32) @@ -18,6 +19,7 @@ target_link_libraries(masterslave_mailbox simgrid ) target_link_libraries(masterslave_bypass simgrid ) target_link_libraries(masterslave_console simgrid ) target_link_libraries(masterslave_kill simgrid ) +target_link_libraries(masterslave_arg simgrid ) else(WIN32) target_link_libraries(masterslave_forwarder simgrid m ) target_link_libraries(masterslave_failure simgrid m ) @@ -25,5 +27,6 @@ target_link_libraries(masterslave_mailbox simgrid m ) target_link_libraries(masterslave_bypass simgrid m ) target_link_libraries(masterslave_console simgrid m ) target_link_libraries(masterslave_kill simgrid m ) +target_link_libraries(masterslave_arg simgrid m ) endif(WIN32) target_link_libraries(masterslave_cluster simgrid) \ No newline at end of file diff --git a/examples/msg/masterslave/masterslave_arg.c b/examples/msg/masterslave/masterslave_arg.c new file mode 100644 index 0000000000..7ad5d0b624 --- /dev/null +++ b/examples/msg/masterslave/masterslave_arg.c @@ -0,0 +1,143 @@ +/* Copyright (c) 2010. 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. */ + +#include +#include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */ +#include "xbt/sysdep.h" /* calloc, printf */ + +/* Create a log channel to have nice outputs. */ +#include "xbt/log.h" +#include "xbt/asserts.h" +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, + "Messages specific for this msg example"); + +int master(int argc, char *argv[]); +int slave(int argc, char *argv[]); + +#define task_comp_size 50000000 +#define task_comm_size 1000000 + +long number_of_jobs; +long number_of_slaves; + +long my_random(long n) { + return n*(rand()/(RAND_MAX+1)); +} + +/** Emitter function */ +int master(int argc, char *argv[]) +{ + int i; + + for (i = 1; i <= number_of_jobs; i++) { + char mailbox[256]; + char sprintf_buffer[256]; + m_task_t task = NULL; + + sprintf(mailbox, "slave-%ld", i % number_of_slaves); + sprintf(sprintf_buffer, "Task_%d", i); + task = + MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, + NULL); + XBT_DEBUG("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, + number_of_jobs, mailbox); + + MSG_task_send(task, mailbox); + } + + XBT_DEBUG + ("All tasks have been dispatched. Let's tell everybody the computation is over."); + for (i = 0; i < number_of_slaves; i++) { + char mailbox[80]; + + sprintf(mailbox, "slave-%ld", i % number_of_slaves); + m_task_t finalize = MSG_task_create("finalize", 0, 0, 0); + MSG_task_send(finalize, mailbox); + } + + XBT_DEBUG("Goodbye now!"); + return 0; +} /* end_of_master */ + +/** Receiver function */ +int slave(int argc, char *argv[]) +{ + m_task_t task = NULL; + _XBT_GNUC_UNUSED int res; + int id = -1; + + XBT_DEBUG("mailbox: %s",MSG_process_get_name(MSG_process_self())); + while (1) { + res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self())); + xbt_assert(res == MSG_OK, "MSG_task_get failed"); + + 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)); + MSG_task_execute(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_slave */ /* end_of_test_all */ + +/** Main function */ +int main(int argc, char *argv[]) +{ + MSG_error_t res = MSG_OK; + long i; + + MSG_global_init(&argc, argv); + if (argc < 4) { + printf("Usage: %s platform_file number_of_jobs number_of_slaves\n", argv[0]); + printf("example: %s msg_platform.xml 10 5\n", argv[0]); + exit(1); + } + + MSG_set_channel_number(0); + MSG_function_register("master", master); + MSG_function_register("slave", slave); + + MSG_create_environment(argv[1]); + + number_of_jobs = atol(argv[2]); + number_of_slaves = atol(argv[3]); + + XBT_INFO("Got %ld slaves, %ld tasks to process, and %d hosts", number_of_slaves, number_of_jobs,MSG_get_host_number()); + + m_host_t *host_table = MSG_get_host_table(); + + MSG_process_create( "master", + master, + NULL, + host_table[my_random(number_of_slaves)] + ); + + for(i = 0 ; i