Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove a bunch of dumb deployment files + simplify some examples
[simgrid.git] / examples / cpp / comm-ready / s4u-comm-ready.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 use simgrid::s4u::Mailbox::ready() to check for completed communications.
7  *
8  * We have a number of peers which send and receive messages in two phases:
9  * -> sending phase:   each one of them sends a number of messages to the others followed
10  *                     by a single "finalize" message.
11  * -> receiving phase: each one of them receives all the available messages that reached
12  *                     their corresponding mailbox until it has all the needed "finalize"
13  *                     messages to know that no more work needs to be done.
14  *
15  * To avoid doing a wait() over the ongoing communications, each peer makes use of the
16  * simgrid::s4u::Mailbox::ready() method. If it returns true then a following get() will fetch the
17  * message immediately, if not the peer will sleep for a fixed amount of time before checking again.
18  *
19  */
20
21 #include "simgrid/s4u.hpp"
22 #include <cstdlib>
23 #include <iostream>
24 #include <string>
25 namespace sg4 = simgrid::s4u;
26
27 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_async_ready, "Messages specific for this s4u example");
28
29 static void peer(int my_id, int messages_count, size_t payload_size, int peers_count)
30 {
31   /* Set myself as the persistent receiver of my mailbox so that messages start flowing to me as soon as they are put
32    * into it */
33   sg4::Mailbox* my_mbox = sg4::Mailbox::by_name(std::string("peer-") + std::to_string(my_id));
34   my_mbox->set_receiver(sg4::Actor::self());
35
36   std::vector<sg4::CommPtr> pending_comms;
37
38   /* Start dispatching all messages to peers others that myself */
39   for (int i = 0; i < messages_count; i++) {
40     for (int peer_id = 0; peer_id < peers_count; peer_id++) {
41       if (peer_id != my_id) {
42         sg4::Mailbox* mbox  = sg4::Mailbox::by_name(std::string("peer-") + std::to_string(peer_id));
43         std::string message = std::string("Message ") + std::to_string(i) + " from peer " + std::to_string(my_id);
44         auto* payload       = new std::string(message); // copy the data we send:
45         // 'msgName' is not a stable storage location
46         XBT_INFO("Send '%s' to '%s'", message.c_str(), mbox->get_cname());
47         /* Create a communication representing the ongoing communication */
48         pending_comms.push_back(mbox->put_async(payload, payload_size));
49       }
50     }
51   }
52
53   /* Start sending messages to let peers know that they should stop */
54   for (int peer_id = 0; peer_id < peers_count; peer_id++) {
55     if (peer_id != my_id) {
56       sg4::Mailbox* mbox = sg4::Mailbox::by_name(std::string("peer-") + std::to_string(peer_id));
57       auto* payload      = new std::string("finalize"); // Make a copy of the data we will send
58       pending_comms.push_back(mbox->put_async(payload, payload_size));
59       XBT_INFO("Send 'finalize' to 'peer-%d'", peer_id);
60     }
61   }
62   XBT_INFO("Done dispatching all messages");
63
64   /* Retrieve all the messages other peers have been sending to me until I receive all the corresponding "Finalize"
65    * messages */
66   long pending_finalize_messages = peers_count - 1;
67   while (pending_finalize_messages > 0) {
68     if (my_mbox->ready()) {
69       double start        = sg4::Engine::get_clock();
70       auto received       = my_mbox->get_unique<std::string>();
71       double waiting_time = sg4::Engine::get_clock() - start;
72       xbt_assert(
73           waiting_time == 0,
74           "Expecting the waiting time to be 0 because the communication was supposedly ready, but got %f instead",
75           waiting_time);
76       XBT_INFO("I got a '%s'.", received->c_str());
77       if (*received == "finalize") {
78         pending_finalize_messages--;
79       }
80     } else {
81       XBT_INFO("Nothing ready to consume yet, I better sleep for a while");
82       sg4::this_actor::sleep_for(.01);
83     }
84   }
85
86   XBT_INFO("I'm done, just waiting for my peers to receive the messages before exiting");
87   sg4::Comm::wait_all(pending_comms);
88
89   XBT_INFO("Goodbye now!");
90 }
91
92 int main(int argc, char* argv[])
93 {
94   sg4::Engine e(&argc, argv);
95   e.load_platform(argv[1]);
96
97   sg4::Actor::create("peer", e.host_by_name("Tremblay"), peer, 0, 2, 5e7, 3);
98   sg4::Actor::create("peer", e.host_by_name("Ruby"), peer, 1, 6, 2.5e5, 3);
99   sg4::Actor::create("peer", e.host_by_name("Perl"), peer, 2, 0, 5e7, 3);
100
101   e.run();
102
103   return 0;
104 }