Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify
[simgrid.git] / examples / cpp / comm-waituntil / s4u-comm-waituntil.cpp
1 /* Copyright (c) 2010-2021. 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 comm-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 namespace sg4 = simgrid::s4u;
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_waituntil, "Messages specific for this s4u example");
20
21 static void sender(int argc, char** argv)
22 {
23   xbt_assert(argc == 4, "Expecting 3 parameters from the XML deployment file but got %d", argc);
24   long messages_count  = std::stol(argv[1]); /* - number of messages */
25   long msg_size        = std::stol(argv[2]); /* - message size in bytes */
26   long receivers_count = std::stol(argv[3]); /* - number of receivers */
27
28   std::vector<sg4::CommPtr> pending_comms;
29
30   /* Start dispatching all messages to receivers, in a round robin fashion */
31   for (int i = 0; i < messages_count; i++) {
32     std::string mboxName        = std::string("receiver-") + std::to_string(i % receivers_count);
33     sg4::Mailbox* mbox          = sg4::Mailbox::by_name(mboxName);
34     std::string msgName         = std::string("Message ") + std::to_string(i);
35     auto* payload               = new std::string(msgName); // copy the data we send:
36
37     // 'msgName' is not a stable storage location
38     XBT_INFO("Send '%s' to '%s'", msgName.c_str(), mboxName.c_str());
39     /* Create a communication representing the ongoing communication */
40     sg4::CommPtr comm = mbox->put_async(payload, msg_size);
41     /* Add this comm to the vector of all known comms */
42     pending_comms.push_back(comm);
43   }
44
45   /* Start sending messages to let the workers know that they should stop */
46   for (int i = 0; i < receivers_count; i++) {
47     std::string mboxName        = std::string("receiver-") + std::to_string(i % receivers_count);
48     sg4::Mailbox* mbox          = sg4::Mailbox::by_name(mboxName);
49     auto* payload               = new std::string("finalize"); // Make a copy of the data we will send
50
51     sg4::CommPtr comm = mbox->put_async(payload, 0);
52     pending_comms.push_back(comm);
53     XBT_INFO("Send 'finalize' to 'receiver-%ld'", i % receivers_count);
54   }
55   XBT_INFO("Done dispatching all messages");
56
57   /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
58   while (not pending_comms.empty()) {
59     sg4::CommPtr comm = pending_comms.back();
60     comm->wait_for(1);
61     pending_comms.pop_back(); // remove it from the list
62   }
63
64   XBT_INFO("Goodbye now!");
65 }
66
67 /* Receiver actor expects 1 argument: its ID */
68 static void receiver(int argc, char** argv)
69 {
70   xbt_assert(argc == 2, "Expecting one parameter from the XML deployment file but got %d", argc);
71   sg4::Mailbox* mbox = sg4::Mailbox::by_name(std::string("receiver-") + argv[1]);
72
73   XBT_INFO("Wait for my first message");
74   for (bool cont = true; cont;) {
75     auto received = mbox->get_unique<std::string>();
76     XBT_INFO("I got a '%s'.", received->c_str());
77     if (*received == "finalize")
78       cont = false; // If it's a finalize message, we're done.
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   sg4::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 }