Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
70f427713f9e5e6fc07f94ce130d63d71393f5dd
[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 messages_count, size_t payload_size)
22 {
23   std::vector<sg4::CommPtr> pending_comms;
24   sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver-0");
25
26   /* Start dispatching all messages to the receiver */
27   for (int i = 0; i < messages_count; i++) {
28     std::string message = std::string("Message ") + std::to_string(i);
29     auto* payload       = new std::string(message); // copy the data we send:
30
31     // 'msgName' is not a stable storage location
32     XBT_INFO("Send '%s' to '%s'", message.c_str(), mbox->get_cname());
33     /* Create a communication representing the ongoing communication */
34     sg4::CommPtr comm = mbox->put_async(payload, payload_size);
35     /* Add this comm to the vector of all known comms */
36     pending_comms.push_back(comm);
37   }
38
39   /* Start the finalize signal to the receiver*/
40   auto* payload     = new std::string("finalize"); // Make a copy of the data we will send
41   sg4::CommPtr comm = mbox->put_async(payload, 0);
42   pending_comms.push_back(comm);
43   XBT_INFO("Send 'finalize' to 'receiver-0'");
44
45   XBT_INFO("Done dispatching all messages");
46
47   /* Now that all message exchanges were initiated, wait for their completion, in order of creation. */
48   while (not pending_comms.empty()) {
49     sg4::CommPtr comm = pending_comms.back();
50     comm->wait_for(1);
51     pending_comms.pop_back(); // remove it from the list
52   }
53
54   XBT_INFO("Goodbye now!");
55 }
56
57 static void receiver()
58 {
59   sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver-0");
60
61   XBT_INFO("Wait for my first message");
62   for (bool cont = true; cont;) {
63     auto received = mbox->get_unique<std::string>();
64     XBT_INFO("I got a '%s'.", received->c_str());
65     if (*received == "finalize")
66       cont = false; // If it's a finalize message, we're done.
67   }
68 }
69
70 int main(int argc, char* argv[])
71 {
72   sg4::Engine e(&argc, argv);
73
74   e.load_platform(argv[1]);
75
76   sg4::Actor::create("sender", e.host_by_name("Tremblay"), sender, 3, 5e7);
77   sg4::Actor::create("receiver", e.host_by_name("Ruby"), receiver);
78
79   e.run();
80
81   return 0;
82 }