Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
these examples should not diverge
[simgrid.git] / examples / s4u / async-waitall / s4u-async-waitall.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 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     // sphinx-doc: init-begin (this line helps the doc to build; ignore it)
38     /* Vector in which we store all ongoing communications */
39     std::vector<simgrid::s4u::CommPtr> pending_comms;
40
41     /* Make a vector of the mailboxes to use */
42     std::vector<simgrid::s4u::MailboxPtr> mboxes;
43     for (int i = 0; i < receivers_count; i++)
44       mboxes.push_back(simgrid::s4u::Mailbox::by_name(std::string("receiver-") + std::to_string(i)));
45     // sphinx-doc: init-end
46
47     /* Start dispatching all messages to receivers, in a round robin fashion */
48     for (int i = 0; i < messages_count; i++) {
49       std::string msg_content = std::string("Message ") + std::to_string(i);
50       // Copy the data we send: 'msg_content' is not a stable storage location.
51       // It will be destroyed when this actor leaves the loop, ie before the receiver gets it
52       std::string* payload = new std::string(msg_content);
53
54       XBT_INFO("Send '%s' to '%s'", msg_content.c_str(), mboxes[i % receivers_count]->get_cname());
55
56       /* Create a communication representing the ongoing communication, and store it in pending_comms */
57       simgrid::s4u::CommPtr comm = mboxes[i % receivers_count]->put_async(payload, msg_size);
58       pending_comms.push_back(comm);
59     }
60
61     /* Start sending messages to let the workers know that they should stop */ // sphinx-doc: send-begin
62     for (int i = 0; i < receivers_count; i++) {
63       XBT_INFO("Send 'finalize' to 'receiver-%d'", i);
64       simgrid::s4u::CommPtr comm = mboxes[i]->put_async(new std::string("finalize"), 0);
65       pending_comms.push_back(comm);
66     }
67     XBT_INFO("Done dispatching all messages");
68
69     /* Now that all message exchanges were initiated, wait for their completion in one single call */
70     simgrid::s4u::Comm::wait_all(&pending_comms);
71     // sphinx-doc: send-end
72
73     XBT_INFO("Goodbye now!");
74   }
75 };
76
77 /* Receiver actor expects 1 argument: its ID */
78 class Receiver {
79   simgrid::s4u::MailboxPtr mbox;
80
81 public:
82   explicit Receiver(std::vector<std::string> args)
83   {
84     xbt_assert(args.size() == 2, "Expecting one parameter from the XML deployment file but got %zu", args.size());
85     std::string mboxName = std::string("receiver-") + args[1];
86     mbox                 = simgrid::s4u::Mailbox::by_name(mboxName);
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.register_actor<Sender>("sender");
107   e.register_actor<Receiver>("receiver");
108
109   e.load_platform(argv[1]);
110   e.load_deployment(argv[2]);
111   e.run();
112
113   return 0;
114 }