Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
514c69edb8ec6a7b11363db99da5f63d5f82a144
[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
50       std::string msg_content = std::string("Message ") + std::to_string(i);
51       // Copy the data we send: 'msg_content' is not a stable storage location.
52       // It will be destroyed when this actor leaves the loop, ie before the receiver gets it
53       std::string* payload = new std::string(msg_content);
54
55       XBT_INFO("Send '%s' to '%s'", msg_content.c_str(), mboxes[i % receivers_count]->get_cname());
56
57       /* Create a communication representing the ongoing communication, and store it in pending_comms */
58       simgrid::s4u::CommPtr comm = mboxes[i % receivers_count]->put_async(payload, msg_size);
59       pending_comms.push_back(comm);
60     }
61
62     /* Start sending messages to let the workers know that they should stop */ // sphinx-doc: send-begin
63     for (int i = 0; i < receivers_count; i++) {
64       XBT_INFO("Send 'finalize' to 'receiver-%d'", i);
65       simgrid::s4u::CommPtr comm = mboxes[i]->put_async(new std::string("finalize"), 0);
66       pending_comms.push_back(comm);
67     }
68     XBT_INFO("Done dispatching all messages");
69
70     /* Now that all message exchanges were initiated, wait for their completion in one single call */
71     simgrid::s4u::Comm::wait_all(&pending_comms);
72     // sphinx-doc: send-end
73
74     XBT_INFO("Goodbye now!");
75   }
76 };
77
78 /* Receiver actor expects 1 argument: its ID */
79 class Receiver {
80   simgrid::s4u::MailboxPtr mbox;
81
82 public:
83   explicit Receiver(std::vector<std::string> args)
84   {
85     xbt_assert(args.size() == 2, "Expecting one parameter from the XML deployment file but got %zu", args.size());
86     std::string mboxName = std::string("receiver-") + args[1];
87     mbox                 = simgrid::s4u::Mailbox::by_name(mboxName);
88   }
89   void operator()()
90   {
91     XBT_INFO("Wait for my first message");
92     for (bool cont = true; cont;) {
93       std::string* received = static_cast<std::string*>(mbox->get());
94       XBT_INFO("I got a '%s'.", received->c_str());
95       cont = (*received != "finalize"); // If it's a finalize message, we're done
96       // Receiving the message was all we were supposed to do
97       delete received;
98     }
99   }
100 };
101
102 int main(int argc, char *argv[])
103 {
104   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
105
106   simgrid::s4u::Engine e(&argc, argv);
107   e.register_actor<Sender>("sender");
108   e.register_actor<Receiver>("receiver");
109
110   e.load_platform(argv[1]);
111   e.load_deployment(argv[2]);
112   e.run();
113
114   return 0;
115 }