Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
13902a4532f44cfb9b93e7ec08ae4b53a3b61503
[simgrid.git] / examples / cpp / comm-waitall / s4u-comm-waitall.cpp
1 /* Copyright (c) 2010-2021. 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 block on the completion of a set of communications.
7  *
8  * As for the other asynchronous examples, the sender initiate all the messages it wants to send and
9  * pack the resulting simgrid::s4u::CommPtr objects in a vector. All messages thus occur concurrently.
10  *
11  * The sender then blocks until all ongoing communication terminate, using simgrid::s4u::Comm::wait_all()
12  *
13  */
14
15 #include "simgrid/s4u.hpp"
16 #include <cstdlib>
17 #include <iostream>
18 #include <string>
19 namespace sg4 = simgrid::s4u;
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_async_waitall, "Messages specific for this s4u example");
22
23 class Sender {
24   long messages_count;  /* - number of messages */
25   long receivers_count; /* - number of receivers */
26   long msg_size;        /* - message size in bytes */
27
28 public:
29   explicit Sender(std::vector<std::string> args)
30   {
31     xbt_assert(args.size() == 4, "Expecting 3 parameters from the XML deployment file but got %zu", args.size());
32     messages_count  = std::stol(args[1]);
33     msg_size        = std::stol(args[2]);
34     receivers_count = std::stol(args[3]);
35   }
36   void operator()() const
37   {
38     // sphinx-doc: init-begin (this line helps the doc to build; ignore it)
39     /* Vector in which we store all ongoing communications */
40     std::vector<sg4::CommPtr> pending_comms;
41
42     /* Make a vector of the mailboxes to use */
43     std::vector<sg4::Mailbox*> mboxes;
44     for (int i = 0; i < receivers_count; i++)
45       mboxes.push_back(sg4::Mailbox::by_name(std::string("receiver-") + std::to_string(i)));
46     // sphinx-doc: init-end
47
48     /* Start dispatching all messages to receivers, in a round robin fashion */
49     for (int i = 0; i < messages_count; i++) {
50       std::string msg_content = std::string("Message ") + std::to_string(i);
51       // Copy the data we send: the 'msg_content' variable is not a stable storage location.
52       // It will be destroyed when this actor leaves the loop, ie before the receiver gets it
53       auto* payload = new std::string(msg_content);
54
55       XBT_INFO("Send '%s' to '%s'", msg_content.c_str(), mboxes[i % receivers_count]->get_cname());
56
57       /* Create a communication representing the ongoing communication, and store it in pending_comms */
58       sg4::CommPtr comm = mboxes[i % receivers_count]->put_async(payload, msg_size);
59       pending_comms.push_back(comm);
60     }
61
62     /* Start sending messages to let the workers know that they should stop */ // sphinx-doc: put-begin
63     for (int i = 0; i < receivers_count; i++) {
64       XBT_INFO("Send 'finalize' to 'receiver-%d'", i);
65       sg4::CommPtr comm = mboxes[i]->put_async(new std::string("finalize"), 0);
66       pending_comms.push_back(comm);
67     }
68     XBT_INFO("Done dispatching all messages");
69
70     /* Now that all message exchanges were initiated, wait for their completion in one single call */
71     sg4::Comm::wait_all(pending_comms);
72     // sphinx-doc: put-end
73
74     XBT_INFO("Goodbye now!");
75   }
76 };
77
78 /* Receiver actor expects 1 argument: its ID */
79 class Receiver {
80   sg4::Mailbox* mbox;
81
82 public:
83   explicit Receiver(std::vector<std::string> args)
84   {
85     xbt_assert(args.size() == 2, "Expecting one parameter from the XML deployment file but got %zu", args.size());
86     std::string mboxName = std::string("receiver-") + args[1];
87     mbox                 = sg4::Mailbox::by_name(mboxName);
88   }
89   void operator()()
90   {
91     XBT_INFO("Wait for my first message");
92     for (bool cont = true; cont;) {
93       auto received = mbox->get_unique<std::string>();
94       XBT_INFO("I got a '%s'.", received->c_str());
95       cont = (*received != "finalize"); // If it's a finalize message, we're done
96       // Receiving the message was all we were supposed to do
97     }
98   }
99 };
100
101 int main(int argc, char* argv[])
102 {
103   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
104
105   sg4::Engine e(&argc, argv);
106   e.register_actor<Sender>("sender");
107   e.register_actor<Receiver>("receiver");
108
109   e.load_platform(argv[1]);
110   e.load_deployment(argv[2]);
111   e.run();
112
113   return 0;
114 }