Logo AND Algorithmique Numérique Distribuée

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