From: Frederic Suter Date: Fri, 10 Mar 2017 09:32:07 +0000 (+0100) Subject: S4U is a true API, it has to have its own master-worker ;) X-Git-Tag: v3_15~162 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/1311b96eb936192957d85c894f5d2fae1c04446d?ds=sidebyside S4U is a true API, it has to have its own master-worker ;) --- diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index c4817c7d1e..00d01fda4a 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach (example app-token-ring io launching mutex actions-comm) +foreach (example app-masterworker app-token-ring io launching mutex actions-comm) add_executable (s4u_${example} ${example}/s4u_${example}.cpp) target_link_libraries(s4u_${example} simgrid) set_target_properties(s4u_${example} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${example}) @@ -11,12 +11,13 @@ set(examples_src ${examples_src} PARENT_SCO set(tesh_files ${tesh_files} PARENT_SCOPE) set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/launching/deployment.xml ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm_split_d.xml - ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm_d.xml PARENT_SCOPE) + ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm_d.xml + ${CMAKE_CURRENT_SOURCE_DIR}/app-masterworker/app-masterworker_d.xml PARENT_SCOPE) set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm_split_p0.txt ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm_split_p1.txt ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm.txt ${CMAKE_CURRENT_SOURCE_DIR}/README.doc PARENT_SCOPE) -foreach(example app-token-ring io launching mutex actions-comm) +foreach(example app-masterworker app-token-ring io launching mutex actions-comm) ADD_TESH_FACTORIES(s4u-${example} "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_CURRENT_BINARY_DIR}/${example} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/${example} s4u_${example}.tesh) endforeach() diff --git a/examples/s4u/app-masterworker/s4u_app-masterworker.cpp b/examples/s4u/app-masterworker/s4u_app-masterworker.cpp new file mode 100644 index 0000000000..68342bd3c7 --- /dev/null +++ b/examples/s4u/app-masterworker/s4u_app-masterworker.cpp @@ -0,0 +1,106 @@ +/* Copyright (c) 2010-2016. 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 "xbt/str.h" +#include "xbt/sysdep.h" +#include +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this s4u example"); + +class Master { + long number_of_tasks = 0; /* - Number of tasks */ + double comp_size = 0; /* - Task compute cost */ + double comm_size = 0; /* - Task communication size */ + long workers_count = 0; /* - Number of workers */ + simgrid::s4u::MailboxPtr mailbox = nullptr; + +public: + explicit Master(std::vector args) + { + xbt_assert(args.size() == 5, "The master function expects 4 arguments from the XML deployment file"); + + number_of_tasks = xbt_str_parse_int(args[1].c_str(), "Invalid amount of tasks: %s"); /* - Number of tasks */ + comp_size = xbt_str_parse_double(args[2].c_str(), "Invalid computational size: %s"); /* - Task compute cost */ + comm_size = xbt_str_parse_double(args[3].c_str(), "Invalid communication size: %s"); /* - Communication size */ + workers_count = xbt_str_parse_int(args[4 ].c_str(), "Invalid amount of workers: %s"); /* - Number of workers */ + + XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks); + } + + void operator()() + { + for (int i = 0; i < number_of_tasks; i++) { /* For each task to be executed: */ + /* - Select a @ref worker in a round-robin way */ + mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count)); + + if (number_of_tasks < 10000 || i % 10000 == 0) + XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", (std::string("Task_") + std::to_string(i)).c_str(), + number_of_tasks, mailbox->name()); + + /* - Send the task to the @ref worker */ + char* payload = bprintf("%f", comp_size); + simgrid::s4u::this_actor::send(mailbox, (void*)(payload), comm_size); + } + + XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over."); + for (int i = 0; i < workers_count; i++) { + /* - Eventually tell all the workers to stop by sending a "finalize" task */ + mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count)); + simgrid::s4u::this_actor::send(mailbox, xbt_strdup("finalize"), 0); + } + } +}; + +class Worker { + long id = -1; + simgrid::s4u::MailboxPtr mailbox = nullptr; + +public: + explicit Worker(std::vector args) + { + xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: " + "its worker ID (its numerical rank)"); + id = xbt_str_parse_int(args[1].c_str(), "Invalid argument %s"); + mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(id)); + } + + void operator()() + { + while (1) { /* The worker waits in an infinite loop for tasks sent by the \ref master */ + char* res = static_cast(simgrid::s4u::this_actor::recv(mailbox)); + xbt_assert(res != nullptr, "MSG_task_get failed"); + + if (strcmp(res, "finalize") == 0) { /* - Exit if 'finalize' is received */ + xbt_free(res); + break; + } + /* - Otherwise, process the task */ + double comp_size = xbt_str_parse_double(res, nullptr); + xbt_free(res); + simgrid::s4u::this_actor::execute(comp_size); + } + XBT_INFO("I'm done. See you!"); + } +}; + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&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]); + + e->loadPlatform(argv[1]); /** - Load the platform description */ + e->registerFunction("master"); + e->registerFunction("worker"); /** - Register the function to be executed by the processes */ + e->loadDeployment(argv[2]); /** - Deploy the application */ + + e->run(); /** - Run the simulation */ + + XBT_INFO("Simulation time %g", e->getClock()); + + return 0; +} diff --git a/examples/s4u/app-masterworker/s4u_app-masterworker.tesh b/examples/s4u/app-masterworker/s4u_app-masterworker.tesh new file mode 100644 index 0000000000..f9b20df3fa --- /dev/null +++ b/examples/s4u/app-masterworker/s4u_app-masterworker.tesh @@ -0,0 +1,36 @@ +#! ./tesh + +p Testing a simple master/worker example application (mailbox version) + +! output sort 19 +$ $SG_TEST_EXENV ${bindir:=.}/s4u_app-masterworker$EXEEXT ${srcdir:=.}/small_platform_with_routers.xml ${srcdir:=.}/../s4u/app-masterworker/s4u_app-masterworker_d.xml --cfg=network/crosstraffic:0 --trace "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n" +> [ 0.000000] (maestro@) Configuration change: Set 'network/crosstraffic' to '0' +> [ 0.000000] (master@Tremblay) Got 5 workers and 20 tasks to process +> [ 0.000000] (master@Tremblay) Sending "Task_0" (of 20) to mailbox "worker-0" +> [ 0.002265] (master@Tremblay) Sending "Task_1" (of 20) to mailbox "worker-1" +> [ 0.164270] (master@Tremblay) Sending "Task_2" (of 20) to mailbox "worker-2" +> [ 0.316349] (master@Tremblay) Sending "Task_3" (of 20) to mailbox "worker-3" +> [ 0.434977] (master@Tremblay) Sending "Task_4" (of 20) to mailbox "worker-4" +> [ 0.562492] (master@Tremblay) Sending "Task_5" (of 20) to mailbox "worker-0" +> [ 0.564757] (master@Tremblay) Sending "Task_6" (of 20) to mailbox "worker-1" +> [ 0.981618] (master@Tremblay) Sending "Task_7" (of 20) to mailbox "worker-2" +> [ 1.133696] (master@Tremblay) Sending "Task_8" (of 20) to mailbox "worker-3" +> [ 1.584703] (master@Tremblay) Sending "Task_9" (of 20) to mailbox "worker-4" +> [ 1.721105] (master@Tremblay) Sending "Task_10" (of 20) to mailbox "worker-0" +> [ 1.723370] (master@Tremblay) Sending "Task_11" (of 20) to mailbox "worker-1" +> [ 1.885375] (master@Tremblay) Sending "Task_12" (of 20) to mailbox "worker-2" +> [ 2.037454] (master@Tremblay) Sending "Task_13" (of 20) to mailbox "worker-3" +> [ 2.734429] (master@Tremblay) Sending "Task_14" (of 20) to mailbox "worker-4" +> [ 2.879718] (master@Tremblay) Sending "Task_15" (of 20) to mailbox "worker-0" +> [ 2.881983] (master@Tremblay) Sending "Task_16" (of 20) to mailbox "worker-1" +> [ 3.043989] (master@Tremblay) Sending "Task_17" (of 20) to mailbox "worker-2" +> [ 3.196067] (master@Tremblay) Sending "Task_18" (of 20) to mailbox "worker-3" +> [ 3.884155] (master@Tremblay) Sending "Task_19" (of 20) to mailbox "worker-4" +> [ 4.038331] (master@Tremblay) All tasks have been dispatched. Let's tell everybody the computation is over. +> [ 4.038526] (worker@Tremblay) I'm done. See you! +> [ 4.057541] (worker@Jupiter) I'm done. See you! +> [ 4.083249] (worker@Fafard) I'm done. See you! +> [ 4.931805] (worker@Ginette) I'm done. See you! +> [ 5.094868] (maestro@) Simulation time 5.09487 +> [ 5.094868] (worker@Bourassa) I'm done. See you! + diff --git a/examples/s4u/app-masterworker/s4u_app-masterworker_d.xml b/examples/s4u/app-masterworker/s4u_app-masterworker_d.xml new file mode 100644 index 0000000000..869db93152 --- /dev/null +++ b/examples/s4u/app-masterworker/s4u_app-masterworker_d.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/s4u/app-token-ring/s4u_app-token-ring.cpp b/examples/s4u/app-token-ring/s4u_app-token-ring.cpp index 4a9efdb4e3..303529be92 100644 --- a/examples/s4u/app-token-ring/s4u_app-token-ring.cpp +++ b/examples/s4u/app-token-ring/s4u_app-token-ring.cpp @@ -15,11 +15,12 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_token_ring, "Messages specific for this s4u extern std::map host_list; class RelayRunner { -public: size_t task_comm_size = 1000000; /* The token is 1MB long*/ simgrid::s4u::MailboxPtr my_mailbox; simgrid::s4u::MailboxPtr neighbor_mailbox; unsigned int rank = 0; + +public: explicit RelayRunner() = default; void operator()()