Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c7ca1c5e256d2e25bc3f1d94cdb5b2fdd28a3ca3
[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 use asynchronous communications.
7  *
8  * The sender initiate all the messages it wants to send, and then block for their completion.
9  * All messages thus occurs concurrently.
10  *
11  * On the receiver side, the reception is synchronous.
12  *
13  * TODO: this example is supposed to test the waitall function, but this is not ported to s4u yet.
14  *
15  */
16
17 #include "simgrid/s4u.hpp"
18 #include "xbt/str.h"
19 #include <cstdlib>
20 #include <iostream>
21
22 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this msg example");
23
24 class sender {
25   long messages_count;
26   long receivers_count;
27   double msg_size; /* in bytes */
28
29 public:
30   explicit sender(std::vector<std::string> args)
31 {
32   xbt_assert(args.size() == 4, "This function expects 4 parameters from the XML deployment file but got %zu",
33              args.size());
34   messages_count  = std::stol(args[1]);
35   msg_size        = std::stod(args[2]);
36   receivers_count = std::stol(args[3]);
37
38 }
39 void operator()()
40 {
41   std::vector<simgrid::s4u::CommPtr>* pending_comms = new std::vector<simgrid::s4u::CommPtr>();
42
43   /* Start dispatching all messages to receivers, in a round robin fashion */
44   for (int i = 0; i < messages_count; i++) {
45
46     std::string mboxName          = std::string("receiver-") + std::to_string(i % receivers_count);
47     simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mboxName);
48     std::string msgName           = std::string("Message ") + std::to_string(i);
49     char* payload = xbt_strdup(msgName.c_str()); // copy the data we send: 'msgName' is not a stable storage location
50
51     XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
52     /* Create a communication representing the ongoing communication */
53     simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, msg_size);
54     /* Add this comm to the vector of all known comms */
55     pending_comms->push_back(comm);
56   }
57   /* Start sending messages to let the workers know that they should stop */
58   for (int i = 0; i < receivers_count; i++) {
59     std::string mbox_name         = std::string("receiver-") + std::to_string(i % receivers_count);
60     simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::byName(mbox_name);
61     char* payload                 = xbt_strdup("finalize"); // Make a copy of the data we will send
62
63     simgrid::s4u::CommPtr comm = mbox->put_async((void*)payload, 0);
64     pending_comms->push_back(comm);
65     XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
66   }
67   XBT_INFO("Done dispatching all messages");
68
69   /* Now that all message exchanges were initiated, this loop waits for the termination of them all */
70   for (int i = 0; i < messages_count + receivers_count; i++)
71     pending_comms->at(i)->wait();
72
73   XBT_INFO("Goodbye now!");
74   delete pending_comms;
75 }
76 };
77
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, "This function expects 2 parameters from the XML deployment file but got %zu",
85              args.size());
86   int id = xbt_str_parse_int(args[1].c_str(), "Any process of this example must have a numerical name, not %s");
87   std::string mbox_name = std::string("receiver-") + std::to_string(id);
88   mbox                  = simgrid::s4u::Mailbox::byName(mbox_name);
89 }
90 void operator()()
91 {
92   XBT_INFO("Wait for my first message");
93   while (1) {
94     char* received = static_cast<char*>(mbox->get());
95     XBT_INFO("I got a '%s'.", received);
96     if (std::strcmp(received, "finalize") == 0) { /* If it's a finalize message, we're done */
97       xbt_free(received);
98       break;
99     }
100     /* Otherwise receiving the message was all we were supposed to do */
101     xbt_free(received);
102   }
103 }
104 };
105
106 int main(int argc, char *argv[])
107 {
108   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
109
110   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
111
112   e->registerFunction<sender>("sender");
113   e->registerFunction<receiver>("receiver");
114
115   e->loadPlatform(argv[1]);
116   e->loadDeployment(argv[2]); 
117   e->run();
118
119   return 0;
120 }