Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless includes for xbt/str.h.
[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
19 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "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       char* payload = xbt_strdup(msgName.c_str()); // copy the data we send: 'msgName' is not a stable storage location
45
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((void*)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       char* payload                 = xbt_strdup("finalize"); // Make a copy of the data we will send
58
59       simgrid::s4u::CommPtr comm = mbox->put_async((void*)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 one single call */
66     simgrid::s4u::Comm::wait_all(&pending_comms);
67
68     XBT_INFO("Goodbye now!");
69   }
70 };
71
72 /* Receiver actor expects 1 argument: its ID */
73 class Receiver {
74   simgrid::s4u::MailboxPtr mbox;
75
76 public:
77   explicit Receiver(std::vector<std::string> args)
78   {
79     xbt_assert(args.size() == 2, "Expecting one parameter from the XML deployment file but got %zu", args.size());
80     std::string mboxName = std::string("receiver-") + args[1];
81     mbox                 = simgrid::s4u::Mailbox::byName(mboxName);
82   }
83   void operator()()
84   {
85     XBT_INFO("Wait for my first message");
86     while (1) {
87       char* received = static_cast<char*>(mbox->get());
88       XBT_INFO("I got a '%s'.", received);
89       if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */
90         xbt_free(received);
91         break;
92       }
93       /* Otherwise receiving the message was all we were supposed to do */
94       xbt_free(received);
95     }
96   }
97 };
98
99 int main(int argc, char *argv[])
100 {
101   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
102
103   simgrid::s4u::Engine e(&argc, argv);
104   e.registerFunction<Sender>("sender");
105   e.registerFunction<Receiver>("receiver");
106
107   e.loadPlatform(argv[1]);
108   e.loadDeployment(argv[2]);
109   e.run();
110
111   return 0;
112 }