Logo AND Algorithmique Numérique Distribuée

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