Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6745ee3b0bddbd68c2537d13e13c0fa164cc0d11
[simgrid.git] / examples / s4u / async-ready / s4u-async-ready.cpp
1 /* Copyright (c) 2010-2020. 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
26 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_async_ready, "Messages specific for this s4u example");
27
28 static void peer(int argc, char** argv)
29 {
30   xbt_assert(argc == 5, "Expecting 4 parameters from the XML deployment file but got %d", argc);
31   int my_id           = std::stoi(argv[1]); /* - my id */
32   long messages_count = std::stol(argv[2]); /* - number of tasks */
33   double msg_size     = std::stol(argv[3]); /* - communication cost in bytes */
34   long peers_count    = std::stod(argv[4]); /* - number of peers */
35
36   /* Set myself as the persistent receiver of my mailbox so that messages start flowing to me as soon as they are put into it */
37   simgrid::s4u::Mailbox* my_mbox = simgrid::s4u::Mailbox::by_name(std::string("peer-") + std::to_string(my_id));
38   my_mbox->set_receiver(simgrid::s4u::Actor::self());
39
40   std::vector<simgrid::s4u::CommPtr> pending_comms;
41
42   /* Start dispatching all messages to peers others that myself */
43   for (int i = 0; i < messages_count; i++) {
44     for (int peer_id = 0; peer_id < peers_count; peer_id++) {
45       if (peer_id != my_id) {
46         std::string mboxName          = std::string("peer-") + std::to_string(peer_id);
47         simgrid::s4u::Mailbox* mbox   = simgrid::s4u::Mailbox::by_name(mboxName);
48         std::string msgName           = std::string("Message ") + std::to_string(i) + std::string(" from peer ") + std::to_string(my_id);
49         std::string* payload          = new std::string(msgName); // copy the data we send:
50         // 'msgName' is not a stable storage location
51         XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
52         /* Create a communication representing the ongoing communication */
53         pending_comms.push_back(mbox->put_async(payload, msg_size));
54       }
55     }
56   }
57
58   /* Start sending messages to let peers know that they should stop */
59   for (int peer_id = 0; peer_id < peers_count; peer_id++) {
60     if (peer_id != my_id) {
61       std::string mboxName          = std::string("peer-") + std::to_string(peer_id);
62       simgrid::s4u::Mailbox* mbox   = simgrid::s4u::Mailbox::by_name(mboxName);
63       std::string* payload          = new std::string("finalize"); // Make a copy of the data we will send
64       pending_comms.push_back(mbox->put_async(payload, msg_size));
65       XBT_INFO("Send 'finalize' to 'peer-%d'", peer_id);
66     }
67   }
68   XBT_INFO("Done dispatching all messages");
69
70   /* Retrieve all the messages other peers have been sending to me until I receive all the corresponding "Finalize" messages */
71   int pending_finalize_messages = peers_count - 1;
72   while (pending_finalize_messages > 0) {
73     if (my_mbox->ready()) {
74       double start          = simgrid::s4u::Engine::get_clock();
75       const std::string* received = static_cast<std::string*>(my_mbox->get());
76       double waiting_time   = simgrid::s4u::Engine::get_clock() - start;
77       xbt_assert(waiting_time == 0, "Expecting the waiting time to be 0 because the communication was supposedly ready, but got %f instead", waiting_time);
78       XBT_INFO("I got a '%s'.", received->c_str());
79       if (*received == "finalize") {
80         pending_finalize_messages--;
81       }
82       delete received;
83     } else {
84       XBT_INFO("Nothing ready to consume yet, I better sleep for a while");
85       simgrid::s4u::this_actor::sleep_for(.01);
86     }
87   }
88
89   XBT_INFO("I'm done, just waiting for my peers to receive the messages before exiting");
90   simgrid::s4u::Comm::wait_all(&pending_comms);
91
92   XBT_INFO("Goodbye now!");
93 }
94
95
96 int main(int argc, char *argv[])
97 {
98   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
99
100   simgrid::s4u::Engine e(&argc, argv);
101   e.register_function("peer", &peer);
102
103   e.load_platform(argv[1]);
104   e.load_deployment(argv[2]);
105
106   e.run();
107
108   return 0;
109 }