Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement simgrid::s4u::Comm::wait_all() and use it in example
[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 "xbt/str.h"
17 #include <cstdlib>
18 #include <iostream>
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this msg example");
21
22 class sender {
23   long messages_count;
24   long receivers_count;
25   double msg_size; /* in bytes */
26
27 public:
28   explicit sender(std::vector<std::string> args)
29 {
30   xbt_assert(args.size() == 4, "This function expects 4 parameters from the XML deployment file but got %zu",
31              args.size());
32   messages_count  = std::stol(args[1]);
33   msg_size        = std::stod(args[2]);
34   receivers_count = std::stol(args[3]);
35
36 }
37 void operator()()
38 {
39   std::vector<simgrid::s4u::CommPtr>* pending_comms = new std::vector<simgrid::s4u::CommPtr>();
40
41   /* Start dispatching all messages to receivers, in a round robin fashion */
42   for (int i = 0; i < messages_count; i++) {
43
44     std::string mboxName          = std::string("receiver-") + std::to_string(i % receivers_count);
45     simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
46     std::string msgName           = std::string("Message ") + std::to_string(i);
47     char* payload = xbt_strdup(msgName.c_str()); // copy the data we send: 'msgName' is not a stable storage location
48
49     XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
50     /* Create a communication representing the ongoing communication */
51     simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, msg_size);
52     /* Add this comm to the vector of all known comms */
53     pending_comms->push_back(comm);
54   }
55   /* Start sending messages to let the workers know that they should stop */
56   for (int i = 0; i < receivers_count; i++) {
57     std::string mbox_name         = std::string("receiver-") + std::to_string(i % receivers_count);
58     simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mbox_name);
59     char* payload                 = xbt_strdup("finalize"); // Make a copy of the data we will send
60
61     simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, 0);
62     pending_comms->push_back(comm);
63     XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
64   }
65   XBT_INFO("Done dispatching all messages");
66
67   /* Now that all message exchanges were initiated, wait for their completion in one single call */
68   simgrid::s4u::Comm::wait_all(pending_comms);
69
70   XBT_INFO("Goodbye now!");
71   delete pending_comms;
72 }
73 };
74
75 class receiver {
76   simgrid::s4u::MailboxPtr mbox;
77
78 public:
79   explicit receiver(std::vector<std::string> args)
80 {
81   xbt_assert(args.size() == 2, "This function expects 2 parameters from the XML deployment file but got %zu",
82              args.size());
83   int id = xbt_str_parse_int(args[1].c_str(), "Any process of this example must have a numerical name, not %s");
84   std::string mbox_name = std::string("receiver-") + std::to_string(id);
85   mbox                  = simgrid::s4u::Mailbox::byName(mbox_name);
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   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
106
107   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
108
109   e->registerFunction<sender>("sender");
110   e->registerFunction<receiver>("receiver");
111
112   e->loadPlatform(argv[1]);
113   e->loadDeployment(argv[2]); 
114   e->run();
115
116   return 0;
117 }