Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
S4U is a true API, it has to have its own master-worker ;)
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 10 Mar 2017 09:32:07 +0000 (10:32 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 10 Mar 2017 09:32:07 +0000 (10:32 +0100)
examples/s4u/CMakeLists.txt
examples/s4u/app-masterworker/s4u_app-masterworker.cpp [new file with mode: 0644]
examples/s4u/app-masterworker/s4u_app-masterworker.tesh [new file with mode: 0644]
examples/s4u/app-masterworker/s4u_app-masterworker_d.xml [new file with mode: 0644]
examples/s4u/app-token-ring/s4u_app-token-ring.cpp

index c4817c7..00d01fd 100644 (file)
@@ -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})
   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
 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)
 
 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()
   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 (file)
index 0000000..68342bd
--- /dev/null
@@ -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 <simgrid/s4u.hpp>
+#include <string>
+
+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<std::string> 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<std::string> 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<char*>(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>("master");
+  e->registerFunction<Worker>("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 (file)
index 0000000..f9b20df
--- /dev/null
@@ -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 (file)
index 0000000..869db93
--- /dev/null
@@ -0,0 +1,27 @@
+<?xml version='1.0'?>
+<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
+<platform version="4">
+  <!-- The master process (with some arguments) -->
+  <process 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 -->
+  </process>
+  <!-- The worker processes (with mailbox to listen on as argument) -->
+  <process host="Tremblay" function="worker" on_failure="RESTART">
+    <argument value="0"/> 
+  </process>
+  <process host="Jupiter" function="worker" on_failure="RESTART">
+    <argument value="1"/> 
+  </process>
+  <process host="Fafard" function="worker" on_failure="RESTART">
+    <argument value="2"/> 
+  </process>
+  <process host="Ginette" function="worker" on_failure="RESTART">
+    <argument value="3"/> 
+  </process>
+  <process host="Bourassa" function="worker" on_failure="RESTART">
+    <argument value="4"/> 
+  </process>
+</platform>
index 4a9efdb..303529b 100644 (file)
@@ -15,11 +15,12 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_token_ring, "Messages specific for this s4u
 extern std::map<std::string, simgrid::s4u::Host*> host_list;
 
 class RelayRunner {
 extern std::map<std::string, simgrid::s4u::Host*> 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;
   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()()
   explicit RelayRunner() = default;
 
   void operator()()