Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f675ce041ef8d4713ed684d771dcfec71c130b75
[simgrid.git] / examples / s4u / async-wait / s4u-async-wait.cpp
1 /* Copyright (c) 2010-2018. 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::this_actor::wait() to wait for a given communication.
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 occurs concurrently.
10  *
11  * The sender then loops until there is no ongoing communication.
12  */
13
14 #include "simgrid/s4u.hpp"
15 #include <cstdlib>
16 #include <iostream>
17 #include <string>
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this s4u example");
20
21 class Sender {
22   long messages_count;  /* - number of tasks */
23   long receivers_count; /* - number of receivers */
24   double msg_size;      /* - communication cost in bytes */
25
26 public:
27   explicit Sender(std::vector<std::string> args)
28   {
29     xbt_assert(args.size() == 4, "Expecting 3 parameters from the XML deployment file but got %zu", args.size());
30     messages_count  = std::stol(args[1]);
31     msg_size        = std::stod(args[2]);
32     receivers_count = std::stol(args[3]);
33   }
34   void operator()()
35   {
36     std::vector<simgrid::s4u::CommPtr> pending_comms;
37
38     /* Start dispatching all messages to receivers, in a round robin fashion */
39     for (int i = 0; i < messages_count; i++) {
40
41       std::string mboxName          = std::string("receiver-") + std::to_string(i % receivers_count);
42       simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
43       std::string msgName = std::string("Message ") + std::to_string(i);
44       std::string* payload          = new std::string(msgName); // copy the data we send:
45                                                                 // 'msgName' is not a stable storage location
46       XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
47       /* Create a communication representing the ongoing communication */
48       simgrid::s4u::CommPtr comm = mbox->put_async(payload, msg_size);
49       /* Add this comm to the vector of all known comms */
50       pending_comms.push_back(comm);
51     }
52
53     /* Start sending messages to let the workers know that they should stop */
54     for (int i = 0; i < receivers_count; i++) {
55       std::string mboxName          = std::string("receiver-") + std::to_string(i % receivers_count);
56       simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
57       std::string* payload          = new std::string("finalize"); // Make a copy of the data we will send
58
59       simgrid::s4u::CommPtr comm = mbox->put_async(payload, 0);
60       pending_comms.push_back(comm);
61       XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
62     }
63     XBT_INFO("Done dispatching all messages");
64
65     /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
66     while (not pending_comms.empty()) {
67       simgrid::s4u::CommPtr comm = pending_comms.back();
68       comm->wait();             // we could provide a timeout as a parameter
69       pending_comms.pop_back(); // remove it from the list
70     }
71
72     XBT_INFO("Goodbye now!");
73   }
74 };
75
76 /* Receiver actor expects 1 argument: its ID */
77 class Receiver {
78   simgrid::s4u::MailboxPtr mbox;
79
80 public:
81   explicit Receiver(std::vector<std::string> args)
82   {
83     xbt_assert(args.size() == 2, "Expecting one parameter from the XML deployment file but got %zu", args.size());
84     std::string mboxName = std::string("receiver-") + args[1];
85     mbox                 = simgrid::s4u::Mailbox::byName(mboxName);
86   }
87
88   void operator()()
89   {
90     XBT_INFO("Wait for my first message");
91     for (bool cont = true; cont;) {
92       std::string* received = static_cast<std::string*>(mbox->get());
93       XBT_INFO("I got a '%s'.", received->c_str());
94       cont = (*received != "finalize"); // If it's a finalize message, we're done
95       // Receiving the message was all we were supposed to do
96       delete received;
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   simgrid::s4u::Engine e(&argc, argv);
106   e.registerFunction<Sender>("sender");
107   e.registerFunction<Receiver>("receiver");
108
109   e.loadPlatform(argv[1]);
110   e.loadDeployment(argv[2]);
111   e.run();
112
113   return 0;
114 }