Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add an example for Comm::test_any (mimic that from umpire
authorSUTER Frederic <frederic.suter@cc.in2p3.fr>
Thu, 20 Jan 2022 09:05:46 +0000 (10:05 +0100)
committerSUTER Frederic <frederic.suter@cc.in2p3.fr>
Thu, 20 Jan 2022 09:06:56 +0000 (10:06 +0100)
MANIFEST.in
examples/cpp/CMakeLists.txt
examples/cpp/comm-testany/s4u-comm-testany.cpp [new file with mode: 0644]
examples/cpp/comm-testany/s4u-comm-testany.tesh [new file with mode: 0644]

index 9457517..efd55f6 100644 (file)
@@ -185,6 +185,8 @@ include examples/cpp/comm-serialize/s4u-comm-serialize.cpp
 include examples/cpp/comm-serialize/s4u-comm-serialize.tesh
 include examples/cpp/comm-suspend/s4u-comm-suspend.cpp
 include examples/cpp/comm-suspend/s4u-comm-suspend.tesh
+include examples/cpp/comm-testany/s4u-comm-testany.cpp
+include examples/cpp/comm-testany/s4u-comm-testany.tesh
 include examples/cpp/comm-throttling/s4u-comm-throttling.cpp
 include examples/cpp/comm-throttling/s4u-comm-throttling.tesh
 include examples/cpp/comm-wait/s4u-comm-wait.cpp
index 2cdca7d..beff957 100644 (file)
@@ -98,7 +98,7 @@ endif()
 foreach (example actor-create actor-daemon actor-exiting actor-join actor-kill
                  actor-lifetime actor-migrate actor-suspend actor-yield actor-stacksize
                  app-bittorrent app-chainsend app-token-ring
-                 comm-pingpong comm-ready comm-serialize comm-suspend comm-wait comm-waitany comm-waitall comm-waituntil
+                 comm-pingpong comm-ready comm-serialize comm-suspend comm-testany comm-wait comm-waitany comm-waitall comm-waituntil
                  comm-dependent comm-host2host comm-failure comm-throttling
                  cloud-capping cloud-migration cloud-simple
                  dag-comm dag-from-dax dag-from-dot dag-failure dag-io dag-scheduling dag-simple
diff --git a/examples/cpp/comm-testany/s4u-comm-testany.cpp b/examples/cpp/comm-testany/s4u-comm-testany.cpp
new file mode 100644 (file)
index 0000000..3c673c8
--- /dev/null
@@ -0,0 +1,73 @@
+/* Copyright (c) 2010-2022. 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 <cstdlib>
+#include <iostream>
+#include <string>
+namespace sg4 = simgrid::s4u;
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_testany, "Messages specific for this s4u example");
+
+static void rank0()
+{
+  sg4::Mailbox* mbox = sg4::Mailbox::by_name(std::string("rank0"));
+  std::string* msg1;
+  std::string* msg2;
+  std::string* msg3;
+
+  XBT_INFO("Post my asynchronous receives");
+  auto comm1                              = mbox->get_async(&msg1);
+  auto comm2                              = mbox->get_async(&msg2);
+  auto comm3                              = mbox->get_async(&msg3);
+  std::vector<sg4::CommPtr> pending_comms = {comm1, comm2, comm3};
+
+  XBT_INFO("Send some data to rank-1");
+  for (int i = 0; i < 3; i++)
+    sg4::Mailbox::by_name(std::string("rank1"))->put(new int(i), 1);
+
+  XBT_INFO("Test for completed comms");
+  while (not pending_comms.empty()) {
+    ssize_t flag = sg4::Comm::test_any(pending_comms);
+    if (flag != -1) {
+      pending_comms.erase(pending_comms.begin() + flag);
+      XBT_INFO("Remove a pending comm.");
+    } else // nothing matches, wait for a little bit
+      sg4::this_actor::sleep_for(0.1);
+  }
+  XBT_INFO("Last comm is complete");
+  delete msg1;
+  delete msg2;
+  delete msg3;
+}
+
+static void rank1()
+{
+  sg4::Mailbox* rank0_mbox = sg4::Mailbox::by_name(std::string("rank0"));
+  sg4::Mailbox* rank1_mbox = sg4::Mailbox::by_name(std::string("rank1"));
+
+  for (int i = 0; i < 3; i++) {
+    auto res = rank1_mbox->get_unique<int>();
+    XBT_INFO("Received %d", *res);
+    std::string msg_content = std::string("Message ") + std::to_string(i);
+    auto* payload           = new std::string(msg_content);
+    XBT_INFO("Send '%s'", msg_content.c_str());
+    rank0_mbox->put(payload, 1e6);
+  }
+}
+
+int main(int argc, char* argv[])
+{
+  sg4::Engine e(&argc, argv);
+
+  e.load_platform(argv[1]);
+
+  sg4::Actor::create("rank-0", e.host_by_name("Tremblay"), rank0);
+  sg4::Actor::create("rank-1", e.host_by_name("Fafard"), rank1);
+
+  e.run();
+
+  return 0;
+}
diff --git a/examples/cpp/comm-testany/s4u-comm-testany.tesh b/examples/cpp/comm-testany/s4u-comm-testany.tesh
new file mode 100644 (file)
index 0000000..0f19916
--- /dev/null
@@ -0,0 +1,16 @@
+#!/usr/bin/env tesh
+
+$ ${bindir:=.}/s4u-comm-testany ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e[%8h]%e[%a]%e%m%n"
+> [  0.000000] [Tremblay] [rank-0] Post my asynchronous receives
+> [  0.000000] [Tremblay] [rank-0] Send some data to rank-1
+> [  0.025708] [  Fafard] [rank-1] Received 0
+> [  0.025708] [  Fafard] [rank-1] Send 'Message 0'
+> [  0.209813] [  Fafard] [rank-1] Received 1
+> [  0.209813] [  Fafard] [rank-1] Send 'Message 1'
+> [  0.393918] [Tremblay] [rank-0] Test for completed comms
+> [  0.393918] [  Fafard] [rank-1] Received 2
+> [  0.393918] [  Fafard] [rank-1] Send 'Message 2'
+> [  0.393918] [Tremblay] [rank-0] Remove a pending comm.
+> [  0.393918] [Tremblay] [rank-0] Remove a pending comm.
+> [  0.593918] [Tremblay] [rank-0] Remove a pending comm.
+> [  0.593918] [Tremblay] [rank-0] Last comm is complete