Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into jbod
[simgrid.git] / examples / cpp / platform-comm-serialize / s4u-platform-comm-serialize.cpp
1 /* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* This example shows how to serialize a set of communications going through a link using Link::set_concurrency_limit()
7  *
8  * This example is very similar to the other asynchronous communication examples, but messages get serialized by the platform.
9  * Without this call to Link::set_concurrency_limit(2) in main, all messages would be received at the exact same timestamp since
10  * they are initiated at the same instant and are of the same size. But with this extra configuration to the link, at most 2
11  * messages can travel through the link at the same time.
12  */
13
14 #include "simgrid/s4u.hpp"
15 #include <string>
16 namespace sg4 = simgrid::s4u;
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_async_serialize, "Messages specific for this s4u example");
19
20 class Sender {
21   int messages_count; /* - number of messages */
22   int msg_size;       /* - message size in bytes */
23
24 public:
25   explicit Sender(int size, int count) : messages_count(count), msg_size(size) {}
26   void operator()() const
27   {
28     // sphinx-doc: init-begin (this line helps the doc to build; ignore it)
29     /* ActivitySet in which we store all ongoing communications */
30     sg4::ActivitySet pending_comms;
31
32     /* Mailbox to use */
33     sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver");
34     // sphinx-doc: init-end
35
36     /* Start dispatching all messages to receiver */
37     for (int i = 0; i < messages_count; i++) {
38       std::string msg_content = "Message " + std::to_string(i);
39       // Copy the data we send: the 'msg_content' variable is not a stable storage location.
40       // It will be destroyed when this actor leaves the loop, ie before the receiver gets it
41       auto* payload = new std::string(msg_content);
42
43       XBT_INFO("Send '%s' to '%s'", msg_content.c_str(), mbox->get_cname());
44
45       /* Create a communication representing the ongoing communication, and store it in pending_comms */
46       sg4::CommPtr comm = mbox->put_async(payload, msg_size);
47       pending_comms.push(comm);
48     }
49
50     XBT_INFO("Done dispatching all messages");
51
52     /* Now that all message exchanges were initiated, wait for their completion in one single call */
53     pending_comms.wait_all();
54     // sphinx-doc: put-end
55
56     XBT_INFO("Goodbye now!");
57   }
58 };
59
60 /* Receiver actor expects 1 argument: number of messages to be received */
61 class Receiver {
62   sg4::Mailbox* mbox;
63   int messages_count = 10; /* - number of messages */
64
65 public:
66   explicit Receiver(int count) : messages_count(count) { mbox = sg4::Mailbox::by_name("receiver"); }
67   void operator()()
68   {
69     /* Where we store all incoming msgs */
70     std::unordered_map<sg4::CommPtr, std::shared_ptr<std::string*>> pending_msgs;
71     sg4::ActivitySet pending_comms;
72
73     XBT_INFO("Wait for %d messages asynchronously", messages_count);
74     for (int i = 0; i < messages_count; i++) {
75       std::shared_ptr<std::string*> msg =std::make_shared<std::string*>();
76       auto comm = mbox->get_async<std::string>(msg.get());
77       pending_comms.push(comm);
78       pending_msgs.insert({comm, msg});
79     }
80
81     while (not pending_comms.empty()) {
82       auto completed_one = pending_comms.wait_any();
83       if (completed_one != nullptr){
84         auto comm = boost::dynamic_pointer_cast<sg4::Comm>(completed_one);
85         auto msg = *pending_msgs[comm];
86         XBT_INFO("I got '%s'.", msg->c_str());
87         /* cleanup memory and remove from map */
88         delete msg;
89         pending_msgs.erase(comm);
90       }
91     }
92   }
93 };
94
95 int main(int argc, char* argv[])
96 {
97   sg4::Engine e(&argc, argv);
98   /* Creates the platform
99    *  ________                 __________
100    * | Sender |===============| Receiver |
101    * |________|    Link1      |__________|
102    */
103   auto* zone     = sg4::create_full_zone("Zone1");
104   auto* sender   = zone->create_host("sender", 1)->seal();
105   auto* receiver = zone->create_host("receiver", 1)->seal();
106
107   /* create split-duplex link1 (UP/DOWN), limiting the number of concurrent flows in it for 2 */
108   const auto* link =
109       zone->create_split_duplex_link("link1", 10e9)->set_latency(10e-6)->set_concurrency_limit(2)->seal();
110
111   /* create routes between nodes */
112   zone->add_route(sender, receiver, {link});
113   zone->seal();
114
115   /* create actors Sender/Receiver */
116   sg4::Actor::create("receiver", receiver, Receiver(10));
117   sg4::Actor::create("sender", sender, Sender(1e6, 10));
118
119   e.run();
120
121   return 0;
122 }