Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove explicit conversion to std::string when it's not required.
[simgrid.git] / examples / cpp / comm-waitall / s4u-comm-waitall.cpp
1 /* Copyright (c) 2010-2022. 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 static void sender(unsigned int messages_count, unsigned int receivers_count, long msg_size)
24 {
25   if (messages_count == 0 || receivers_count == 0) {
26     XBT_WARN("Sender has nothing to do. Bail out!");
27     return;
28   }
29   // sphinx-doc: init-begin (this line helps the doc to build; ignore it)
30   /* Vector in which we store all ongoing communications */
31   std::vector<sg4::CommPtr> pending_comms;
32
33   /* Make a vector of the mailboxes to use */
34   std::vector<sg4::Mailbox*> mboxes;
35   for (unsigned int i = 0; i < receivers_count; i++)
36     mboxes.push_back(sg4::Mailbox::by_name("receiver-" + std::to_string(i)));
37   // sphinx-doc: init-end
38
39   /* Start dispatching all messages to receivers, in a round robin fashion */
40   for (unsigned int i = 0; i < messages_count; i++) {
41     std::string msg_content = "Message " + std::to_string(i);
42     // Copy the data we send: the 'msg_content' variable is not a stable storage location.
43     // It will be destroyed when this actor leaves the loop, ie before the receiver gets it
44     auto* payload = new std::string(msg_content);
45
46     XBT_INFO("Send '%s' to '%s'", msg_content.c_str(), mboxes[i % receivers_count]->get_cname());
47
48     /* Create a communication representing the ongoing communication, and store it in pending_comms */
49     sg4::CommPtr comm = mboxes[i % receivers_count]->put_async(payload, msg_size);
50     pending_comms.push_back(comm);
51   }
52
53   /* Start sending messages to let the workers know that they should stop */ // sphinx-doc: put-begin
54   for (unsigned int i = 0; i < receivers_count; i++) {
55     XBT_INFO("Send 'finalize' to 'receiver-%u'", i);
56     sg4::CommPtr comm = mboxes[i]->put_async(new std::string("finalize"), 0);
57     pending_comms.push_back(comm);
58   }
59   XBT_INFO("Done dispatching all messages");
60
61   /* Now that all message exchanges were initiated, wait for their completion in one single call */
62   sg4::Comm::wait_all(pending_comms);
63   // sphinx-doc: put-end
64
65   XBT_INFO("Goodbye now!");
66 }
67
68 /* Receiver actor expects 1 argument: its ID */
69 static void receiver(int id)
70 {
71   sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver-" + std::to_string(id));
72   XBT_INFO("Wait for my first message");
73   for (bool cont = true; cont;) {
74     auto received = mbox->get_unique<std::string>();
75     XBT_INFO("I got a '%s'.", received->c_str());
76     cont = (*received != "finalize"); // If it's a finalize message, we're done
77     // Receiving the message was all we were supposed to do
78   }
79 }
80
81 int main(int argc, char* argv[])
82 {
83   sg4::Engine e(&argc, argv);
84
85   e.load_platform(argv[1]);
86
87   sg4::Actor::create("sender", e.host_by_name("Tremblay"), sender, 5, 2, 1e6);
88   sg4::Actor::create("receiver", e.host_by_name("Ruby"), receiver, 0);
89   sg4::Actor::create("receiver", e.host_by_name("Perl"), receiver, 1);
90
91   e.run();
92
93   return 0;
94 }