Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / comm-failure / s4u-comm-failure.cpp
1 /* Copyright (c) 2021-2022. 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 serialize a set of communications going through a link
7  *
8  * As for the other asynchronous examples, the sender initiates all the messages it wants to send and
9  * pack the resulting simgrid::s4u::CommPtr objects in a vector.
10  * At the same time, the receiver starts receiving all messages asynchronously. Without serialization,
11  * all messages would be received at the same timestamp in the receiver.
12  *
13  * However, as they will be serialized in a link of the platform, the messages arrive 2 by 2.
14  *
15  * The sender then blocks until all ongoing communication terminate, using simgrid::s4u::Comm::wait_all()
16  */
17
18 #include <simgrid/s4u.hpp>
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_failure, "Messages specific for this s4u example");
21 namespace sg4 = simgrid::s4u;
22
23 class Sender {
24   std::string mailbox1_name;
25   std::string mailbox2_name;
26
27 public:
28   Sender(const std::string& mailbox1_name, const std::string& mailbox2_name)
29       : mailbox1_name(mailbox1_name), mailbox2_name(mailbox2_name)
30   {
31   }
32
33   void operator()() const
34   {
35     auto mailbox1 = sg4::Mailbox::by_name(mailbox1_name);
36     auto mailbox2 = sg4::Mailbox::by_name(mailbox2_name);
37
38     XBT_INFO("Initiating asynchronous send to %s", mailbox1->get_cname());
39     auto comm1 = mailbox1->put_async((void*)666, 5);
40     XBT_INFO("Initiating asynchronous send to %s", mailbox2->get_cname());
41     auto comm2 = mailbox2->put_async((void*)666, 2);
42
43     XBT_INFO("Calling wait_any..");
44     std::vector<sg4::CommPtr> pending_comms;
45     pending_comms.push_back(comm1);
46     pending_comms.push_back(comm2);
47     try {
48       long index = sg4::Comm::wait_any(pending_comms);
49       XBT_INFO("Wait any returned index %ld (comm to %s)", index, pending_comms.at(index)->get_mailbox()->get_cname());
50     } catch (const simgrid::NetworkFailureException&) {
51       XBT_INFO("Sender has experienced a network failure exception, so it knows that something went wrong");
52       XBT_INFO("Now it needs to figure out which of the two comms failed by looking at their state");
53     }
54
55     XBT_INFO("Comm to %s has state: %s", comm1->get_mailbox()->get_cname(), comm1->get_state_str());
56     XBT_INFO("Comm to %s has state: %s", comm2->get_mailbox()->get_cname(), comm2->get_state_str());
57
58     try {
59       comm1->wait();
60     } catch (const simgrid::NetworkFailureException& e) {
61       XBT_INFO("Waiting on a FAILED comm raises an exception: '%s'", e.what());
62     }
63     XBT_INFO("Wait for remaining comm, just to be nice");
64     pending_comms.erase(pending_comms.begin());
65     simgrid::s4u::Comm::wait_any(pending_comms);
66   }
67 };
68
69 class Receiver {
70   std::string mailbox_name;
71
72 public:
73   explicit Receiver(const std::string& mailbox_name) : mailbox_name(mailbox_name) {}
74
75   void operator()() const
76   {
77     auto mailbox = sg4::Mailbox::by_name(mailbox_name);
78     XBT_INFO("Receiver posting a receive...");
79     try {
80       mailbox->get<void*>();
81       XBT_INFO("Receiver has received successfully!");
82     } catch (const simgrid::NetworkFailureException&) {
83       XBT_INFO("Receiver has experience a network failure exception");
84     }
85   }
86 };
87
88 class LinkKiller {
89   std::string link_name;
90
91 public:
92   explicit LinkKiller(const std::string& link_name) : link_name(link_name) {}
93
94   void operator()() const
95   {
96     auto link_to_kill = sg4::Link::by_name(link_name);
97     XBT_INFO("LinkKiller  sleeping 10 seconds...");
98     sg4::this_actor::sleep_for(10.0);
99     XBT_INFO("LinkKiller turning off link %s", link_to_kill->get_cname());
100     link_to_kill->turn_off();
101     XBT_INFO("LinkKiller killed. exiting");
102   }
103 };
104
105 int main(int argc, char** argv)
106 {
107   sg4::Engine engine(&argc, argv);
108   auto* zone  = sg4::create_full_zone("AS0");
109   auto* host1 = zone->create_host("Host1", "1f");
110   auto* host2 = zone->create_host("Host2", "1f");
111   auto* host3 = zone->create_host("Host3", "1f");
112
113   sg4::LinkInRoute linkto2{zone->create_link("linkto2", "1bps")->seal()};
114   sg4::LinkInRoute linkto3{zone->create_link("linkto3", "1bps")->seal()};
115
116   zone->add_route(host1->get_netpoint(), host2->get_netpoint(), nullptr, nullptr, {linkto2}, false);
117   zone->add_route(host1->get_netpoint(), host3->get_netpoint(), nullptr, nullptr, {linkto3}, false);
118   zone->seal();
119
120   sg4::Actor::create("Sender", host1, Sender("mailbox2", "mailbox3"));
121   sg4::Actor::create("Receiver", host2, Receiver("mailbox2"))->daemonize();
122   sg4::Actor::create("Receiver", host3, Receiver("mailbox3"))->daemonize();
123   sg4::Actor::create("LinkKiller", host1, LinkKiller("linkto2"))->daemonize();
124
125   engine.run();
126
127   return 0;
128 }