Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
massive cleanups in the s4u-async-waitall example
authorMartin Quinson <martin.quinson@loria.fr>
Sun, 10 Sep 2017 14:55:46 +0000 (16:55 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Sun, 10 Sep 2017 14:55:46 +0000 (16:55 +0200)
examples/s4u/async-waitall/s4u_async-waitall.cpp
examples/s4u/async-waitall/s4u_async-waitall.tesh
examples/s4u/async-waitall/s4u_async-waitall_d.xml

index 492bc94..612aabf 100644 (file)
@@ -1,51 +1,68 @@
-/* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-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. */
 
- #include "simgrid/s4u.hpp"
- #include "xbt/str.h"  
- #include <cstdlib>
- #include <iostream>
+/* This example shows how to use asynchronous communications.
+ *
+ * The sender initiate all the messages it wants to send, and then block for their completion.
+ * All messages thus occurs concurrently.
+ *
+ * On the receiver side, the reception is synchronous.
+ *
+ * TODO: this example is supposed to test the waitall function, but this is not ported to s4u yet.
+ *
+ */
+
+#include "simgrid/s4u.hpp"
+#include "xbt/str.h"
+#include <cstdlib>
+#include <iostream>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this msg example");
 
 class sender {
-  long number_of_tasks             = 0; /* - Number of tasks      */
-  long receivers_count             = 0; /* - Number of workers    */
+  long messages_count;
+  long receivers_count;
+  double msg_size; /* in bytes */
 
 public:
   explicit sender(std::vector<std::string> args)
 {
   xbt_assert(args.size()== 4, "This function expects 5 parameters from the XML deployment file");
-  number_of_tasks = std::stol(args[0]);
-  double task_comp_size = std::stod(args[1]);
-  double task_comm_size = std::stod(args[2]);
+  messages_count  = std::stol(args[1]);
+  msg_size        = std::stod(args[2]);
   receivers_count = std::stol(args[3]);
 
 }
 void operator()()
 {
-  simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("sender_mailbox");
-  simgrid::s4u::CommPtr* comms = new simgrid::s4u::CommPtr[number_of_tasks + receivers_count] ;
-
-  for (int i = 0; i < number_of_tasks; i++) {
-    char mailbox[80];
-    char taskname[80];
-    snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
-    snprintf(taskname,79, "Task_%d", i);
-    comms[i] = mbox->put_async((void*)taskname, 42);
-    XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
+  simgrid::s4u::CommPtr* comms = new simgrid::s4u::CommPtr[messages_count + receivers_count];
+
+  /* Start dispatching all messages to receivers, in a round robin fashion */
+  for (int i = 0; i < messages_count; i++) {
+
+    std::string mboxName          = std::string("receiver-") + std::to_string(i % receivers_count);
+    simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
+    std::string msgName           = std::string("Message ") + std::to_string(i);
+    char* payload = xbt_strdup(msgName.c_str()); // copy the data we send: 'msgName' is not a stable storage location
+
+    XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
+    comms[i] = mbox->put_async((void*)payload, msg_size);
   }
+  /* Start sending messages to let the workers know that they should stop */
   for (int i = 0; i < receivers_count; i++) {
-    char mailbox[80];
-    snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
-    comms[i + number_of_tasks] = mbox->put_async((void*)"finalize", 42);
-    XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
+    std::string mbox_name         = std::string("receiver-") + std::to_string(i % receivers_count);
+    simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mbox_name);
+    char* payload                 = xbt_strdup("finalize"); // Make a copy of the data we will send
+
+    comms[i + messages_count] = mbox->put_async((void*)payload, 0);
+    XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
   }
+  XBT_INFO("Done dispatching all messages");
 
-  /* Here we are waiting for the completion of all communications */
-  for (int i = 0; i < number_of_tasks + receivers_count; i++)
+  /* Now that all message exchanges were initiated, this loop waits for the termination of them all */
+  for (int i = 0; i < messages_count + receivers_count; i++)
     comms[i]->wait();
 
   delete [] comms;
@@ -54,49 +71,43 @@ void operator()()
 };
 
 class receiver {
+  simgrid::s4u::MailboxPtr mbox;
+
 public:
   explicit receiver(std::vector<std::string> args)
 {
-  xbt_assert(args.size() == 1,"This function expects 1 parameter from the XML deployment file");
-  int id = xbt_str_parse_int(args[0].c_str(), "Any process of this example must have a numerical name, not %s");
-  char mailbox[80];
-  snprintf(mailbox,79, "receiver-%d", id);
-
-  simgrid::s4u::this_actor::sleep_for(10.0);
-
-  XBT_INFO("I'm done. See you!");
+  xbt_assert(args.size() == 2, "This function expects 2 parameters from the XML deployment file but got %zu",
+             args.size());
+  int id = xbt_str_parse_int(args[1].c_str(), "Any process of this example must have a numerical name, not %s");
+  std::string mbox_name = std::string("receiver-") + std::to_string(id);
+  mbox                  = simgrid::s4u::Mailbox::byName(mbox_name);
 }
 void operator()()
 {
-  simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName("receiver_mailbox");
+  XBT_INFO("Wait for my first message");
   while (1) {
-    XBT_INFO("Wait to receive a task");
-    void *received = NULL;
-    simgrid::s4u::CommPtr comm = mbox->get_async(&received);
-    comm->wait();
-    std::string* receivedStr = static_cast<std::string*>(received);
-    if (receivedStr->compare("finalize") == 0) {
-      delete receivedStr;
+    char* received = static_cast<char*>(mbox->get());
+    XBT_INFO("I got a '%s'.", received);
+    if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */
+      xbt_free(received);
       break;
     }
-    double *comp_size = static_cast<double*>(received);
-    /*  - Otherwise, process the task */
-    simgrid::s4u::this_actor::execute(*comp_size);
+    /*  - Otherwise receiving the message was all we were supposed to do */
+    xbt_free(received);
   }
-  XBT_INFO("I'm done. See you!");
 }
 };
 
 int main(int argc, char *argv[])
 {
-  simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,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]);
+  xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
 
-  e->registerFunction<sender>("sender");   
-  e->registerFunction<receiver>("receiver"); 
+  e->registerFunction<sender>("sender");
+  e->registerFunction<receiver>("receiver");
 
+  e->loadPlatform(argv[1]);
   e->loadDeployment(argv[2]); 
   e->run();
 
index e6d3b38..402131a 100644 (file)
@@ -1,26 +1,21 @@
 #! ./tesh
-p Test1 MSG_comm_waitall() for sender
 
-! output sort 19
-$ $SG_TEST_EXENV ${bindir:=.}/async-waitall ${srcdir:=.}/small_platform_fatpipe.xml ${srcdir:=.}/../msg/async-waitall/async-waitall_d.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (1:sender@Tremblay) Send to receiver-0 Task_0
-> [  0.000000] (1:sender@Tremblay) Send to receiver-0 Task_1
-> [  0.000000] (1:sender@Tremblay) Send to receiver-0 Task_2
-> [  0.000000] (1:sender@Tremblay) Send to receiver-0 finalize
-> [ 10.000000] (2:receiver@Ruby) Wait to receive a task
-> [ 10.004022] (2:receiver@Ruby) Received "Task_0"
-> [ 10.004022] (2:receiver@Ruby) Processing "Task_0"
-> [ 10.513732] (2:receiver@Ruby) "Task_0" done
-> [ 10.513732] (2:receiver@Ruby) Wait to receive a task
-> [ 10.517753] (2:receiver@Ruby) Received "Task_1"
-> [ 10.517753] (2:receiver@Ruby) Processing "Task_1"
-> [ 11.027463] (2:receiver@Ruby) "Task_1" done
-> [ 11.027463] (2:receiver@Ruby) Wait to receive a task
-> [ 11.031485] (2:receiver@Ruby) Received "Task_2"
-> [ 11.031485] (2:receiver@Ruby) Processing "Task_2"
-> [ 11.541195] (2:receiver@Ruby) "Task_2" done
-> [ 11.541195] (2:receiver@Ruby) Wait to receive a task
-> [ 11.543146] (0:maestro@) Simulation time 11.5431
-> [ 11.543146] (1:sender@Tremblay) Goodbye now!
-> [ 11.543146] (2:receiver@Ruby) Received "finalize"
-> [ 11.543146] (2:receiver@Ruby) I'm done. See you!
+$ $SG_TEST_EXENV ${bindir:=.}/s4u_async-waitall ${srcdir:=.}/small_platform_fatpipe.xml ${srcdir:=.}/../s4u/async-waitall/s4u_async-waitall_d.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
+> [  0.000000] (1:sender@Tremblay) Send 'Message 0' to 'receiver-0'
+> [  0.000000] (2:receiver@Ruby) Wait for my first message
+> [  0.000000] (3:receiver@Perl) Wait for my first message
+> [  0.000000] (1:sender@Tremblay) Send 'Message 1' to 'receiver-1'
+> [  0.000000] (1:sender@Tremblay) Send 'Message 2' to 'receiver-0'
+> [  0.000000] (1:sender@Tremblay) Send 'Message 3' to 'receiver-1'
+> [  0.000000] (1:sender@Tremblay) Send 'Message 4' to 'receiver-0'
+> [  0.000000] (1:sender@Tremblay) Send 'finalize' to 'receiver-0'
+> [  0.000000] (1:sender@Tremblay) Send 'finalize' to 'receiver-1'
+> [  0.000000] (1:sender@Tremblay) Done dispatching all messages
+> [  0.004022] (2:receiver@Ruby) I got a 'Message 0'.
+> [  0.004022] (3:receiver@Perl) I got a 'Message 1'.
+> [  0.008043] (2:receiver@Ruby) I got a 'Message 2'.
+> [  0.008043] (3:receiver@Perl) I got a 'Message 3'.
+> [  0.009995] (3:receiver@Perl) I got a 'finalize'.
+> [  0.012065] (2:receiver@Ruby) I got a 'Message 4'.
+> [  0.014016] (2:receiver@Ruby) I got a 'finalize'.
+> [  0.014016] (1:sender@Tremblay) Goodbye now!
index 6f122c2..122a030 100644 (file)
@@ -3,13 +3,15 @@
 <platform version="4.1">
   <!-- The master actor (with some arguments) -->
   <actor host="Tremblay" function="sender">
-    <argument value="3"/>       <!-- Number of tasks -->
-    <argument value="50000000"/>  <!-- Computation size of tasks -->
-    <argument value="1000000"/>   <!-- Communication size of tasks -->
-    <argument value="1"/>         <!-- Number of receivers -->
+    <argument value="5"/>         <!-- Number of messages -->
+    <argument value="1000000"/>   <!-- Size of messages -->
+    <argument value="2"/>         <!-- Number of receivers -->
   </actor>
-  <!-- The receiver processes -->
+  <!-- The receiver processes. -->
   <actor host="Ruby" function="receiver">
     <argument value="0"/>
   </actor>
+  <actor host="Perl" function="receiver">
+    <argument value="1"/>
+  </actor>
 </platform>