Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not talk about tasks anymore
[simgrid.git] / examples / s4u / async-waituntil / s4u-async-waituntil.cpp
1 /* Copyright (c) 2010-2020. 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 simgrid::s4u::Activity::wait_until() and
7  * simgrid::s4u::Activity::wait_for() on a given communication.
8  *
9  * It is very similar to the async-wait example, but the sender initially
10  * does some waits that are too short before doing an infinite wait.
11  */
12
13 #include "simgrid/s4u.hpp"
14 #include <cstdlib>
15 #include <iostream>
16 #include <string>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_async_waituntil, "Messages specific for this s4u example");
19
20 static void sender(int argc, char** argv)
21 {
22   xbt_assert(argc == 4, "Expecting 3 parameters from the XML deployment file but got %d", argc);
23   long messages_count  = std::stol(argv[1]); /* - number of messages */
24   double msg_size      = std::stol(argv[2]); /* - message size in bytes */
25   long receivers_count = std::stod(argv[3]); /* - number of receivers */
26
27   std::vector<simgrid::s4u::CommPtr> pending_comms;
28
29   /* Start dispatching all messages to receivers, in a round robin fashion */
30   for (int i = 0; i < messages_count; i++) {
31     std::string mboxName          = std::string("receiver-") + std::to_string(i % receivers_count);
32     simgrid::s4u::Mailbox* mbox   = simgrid::s4u::Mailbox::by_name(mboxName);
33     std::string msgName           = std::string("Message ") + std::to_string(i);
34     std::string* payload          = new std::string(msgName); // copy the data we send:
35
36     // 'msgName' is not a stable storage location
37     XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
38     /* Create a communication representing the ongoing communication */
39     simgrid::s4u::CommPtr comm = mbox->put_async(payload, msg_size);
40     /* Add this comm to the vector of all known comms */
41     pending_comms.push_back(comm);
42   }
43
44   /* Start sending messages to let the workers know that they should stop */
45   for (int i = 0; i < receivers_count; i++) {
46     std::string mboxName          = std::string("receiver-") + std::to_string(i % receivers_count);
47     simgrid::s4u::Mailbox* mbox   = simgrid::s4u::Mailbox::by_name(mboxName);
48     std::string* payload          = new std::string("finalize"); // Make a copy of the data we will send
49
50     simgrid::s4u::CommPtr comm = mbox->put_async(payload, 0);
51     pending_comms.push_back(comm);
52     XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
53   }
54   XBT_INFO("Done dispatching all messages");
55
56   /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
57   while (not pending_comms.empty()) {
58     simgrid::s4u::CommPtr comm = pending_comms.back();
59     comm->wait_for(1);
60     pending_comms.pop_back(); // remove it from the list
61   }
62
63   XBT_INFO("Goodbye now!");
64 }
65
66 /* Receiver actor expects 1 argument: its ID */
67 static void receiver(int argc, char** argv)
68 {
69   xbt_assert(argc == 2, "Expecting one parameter from the XML deployment file but got %d", argc);
70   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name(std::string("receiver-") + argv[1]);
71
72   XBT_INFO("Wait for my first message");
73   for (bool cont = true; cont;) {
74     const std::string* received = static_cast<std::string*>(mbox->get());
75     XBT_INFO("I got a '%s'.", received->c_str());
76     if (*received == "finalize")
77       cont = false; // If it's a finalize message, we're done.
78     delete received;
79   }
80 }
81
82 int main(int argc, char* argv[])
83 {
84   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
85
86   simgrid::s4u::Engine e(&argc, argv);
87   e.register_function("sender", &sender);
88   e.register_function("receiver", &receiver);
89
90   e.load_platform(argv[1]);
91   e.load_deployment(argv[2]);
92   e.run();
93
94   return 0;
95 }