Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use shared pointers in these examples
authorFred Suter <suterf@ornl.gov>
Mon, 17 Jul 2023 20:36:39 +0000 (16:36 -0400)
committerFred Suter <suterf@ornl.gov>
Mon, 17 Jul 2023 20:36:39 +0000 (16:36 -0400)
examples/cpp/network-nonlinear/s4u-network-nonlinear.cpp
examples/cpp/platform-comm-serialize/s4u-platform-comm-serialize.cpp

index 9bfce5b..c403d2b 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include <simgrid/s4u.hpp>
+#include <string>
 
 namespace sg4 = simgrid::s4u;
 
@@ -64,22 +65,22 @@ public:
   void operator()()
   {
     /* Where we store all incoming msgs */
-    std::unordered_map<sg4::CommPtr, std::string**> pending_msgs;
+    std::unordered_map<sg4::CommPtr, std::shared_ptr<std::string*>> pending_msgs;
     sg4::ActivitySet pending_comms;
 
     XBT_INFO("Wait for %d messages asynchronously", messages_count);
     for (int i = 0; i < messages_count; i++) {
-      std::string* msg;
-      auto comm = mbox->get_async<std::string>(&msg);
+      std::shared_ptr<std::string*> msg =std::make_shared<std::string*>();
+      auto comm = mbox->get_async<std::string>(msg.get());
       pending_comms.push(comm);
-      pending_msgs.insert({comm, &msg});
+      pending_msgs.insert({comm, msg});
     }
 
     while (not pending_comms.empty()) {
       auto completed_one = pending_comms.wait_any();
       if (completed_one != nullptr){
         auto comm = boost::dynamic_pointer_cast<sg4::Comm>(completed_one);
-        std::string *msg = std::move(*pending_msgs[comm]);
+        auto msg = *pending_msgs[comm];
         XBT_INFO("I got '%s'.", msg->c_str());
         /* cleanup memory and remove from map */
         delete msg;
index cbff56d..3335631 100644 (file)
@@ -66,23 +66,28 @@ public:
   explicit Receiver(int count) : messages_count(count) { mbox = sg4::Mailbox::by_name("receiver"); }
   void operator()()
   {
-    /* Vector in which we store all incoming msgs */
-    std::vector<std::unique_ptr<std::string*>> pending_msgs;
-    std::vector<sg4::CommPtr> pending_comms;
+    /* Where we store all incoming msgs */
+    std::unordered_map<sg4::CommPtr, std::shared_ptr<std::string*>> pending_msgs;
+    sg4::ActivitySet pending_comms;
 
     XBT_INFO("Wait for %d messages asynchronously", messages_count);
     for (int i = 0; i < messages_count; i++) {
-      pending_msgs.push_back(std::make_unique<std::string*>());
-      pending_comms.emplace_back(mbox->get_async<std::string>(pending_msgs[i].get()));
+      std::shared_ptr<std::string*> msg =std::make_shared<std::string*>();
+      auto comm = mbox->get_async<std::string>(msg.get());
+      pending_comms.push(comm);
+      pending_msgs.insert({comm, msg});
     }
+
     while (not pending_comms.empty()) {
-      ssize_t index    = sg4::Comm::wait_any(pending_comms);
-      std::string* msg = *pending_msgs[index];
-      XBT_INFO("I got '%s'.", msg->c_str());
-      /* cleanup memory and remove from vectors */
-      delete msg;
-      pending_comms.erase(pending_comms.begin() + index);
-      pending_msgs.erase(pending_msgs.begin() + index);
+      auto completed_one = pending_comms.wait_any();
+      if (completed_one != nullptr){
+        auto comm = boost::dynamic_pointer_cast<sg4::Comm>(completed_one);
+        auto msg = *pending_msgs[comm];
+        XBT_INFO("I got '%s'.", msg->c_str());
+        /* cleanup memory and remove from map */
+        delete msg;
+        pending_msgs.erase(comm);
+      }
     }
   }
 };