Logo AND Algorithmique Numérique Distribuée

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