Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a FAILED state to activities. tested on comm and exec
[simgrid.git] / examples / cpp / comm-failure / s4u-comm-failure.cpp
1 /* Copyright (c) 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 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(std::string mailbox1_name, std::string mailbox2_name)
29       : mailbox1_name(mailbox1_name), mailbox2_name(mailbox2_name)
30   {
31   }
32
33   void operator()()
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     long index;
48     try {
49       index = sg4::Comm::wait_any(pending_comms);
50       XBT_INFO("Wait any returned index %ld (comm to %s)", index, pending_comms.at(index)->get_mailbox()->get_cname());
51     } catch (simgrid::NetworkFailureException& e) {
52       XBT_INFO("Sender has experienced a network failure exception, so it knows that something went wrong");
53       XBT_INFO("Now it needs to figure out which of the two comms failed by looking at their state");
54     }
55
56     XBT_INFO("Comm to %s has state: %s", comm1->get_mailbox()->get_cname(), comm1->get_state_str());
57     XBT_INFO("Comm to %s has state: %s", comm2->get_mailbox()->get_cname(), comm2->get_state_str());
58
59     try {
60       comm1->wait();
61     } catch (simgrid::NetworkFailureException& e) {
62       XBT_INFO("Waiting on a FAILED comm raises an exception: '%s'", e.what());
63     }
64     XBT_INFO("Wait for remaining comm, just to be nice");
65     pending_comms.erase(pending_comms.begin());
66     index = simgrid::s4u::Comm::wait_any(pending_comms);
67   }
68 };
69
70 class Receiver {
71   std::string mailbox_name;
72
73 public:
74   explicit Receiver(std::string mailbox_name) : mailbox_name(mailbox_name) {}
75
76   void operator()()
77   {
78     auto mailbox = sg4::Mailbox::by_name(mailbox_name);
79     XBT_INFO("Receiver posting a receive...");
80     try {
81       mailbox->get<void*>();
82       XBT_INFO("Receiver has received successfully!");
83     } catch (simgrid::NetworkFailureException& e) {
84       XBT_INFO("Receiver has experience a network failure exception");
85     }
86   }
87 };
88
89 class LinkKiller {
90   std::string link_name;
91
92 public:
93   explicit LinkKiller(std::string link_name) : link_name(link_name) {}
94
95   void operator()()
96   {
97     auto link_to_kill = sg4::Link::by_name(link_name);
98     XBT_INFO("LinkKiller  sleeping 10 seconds...");
99     sg4::this_actor::sleep_for(10.0);
100     XBT_INFO("LinkKiller turning off link %s", link_to_kill->get_cname());
101     link_to_kill->turn_off();
102     XBT_INFO("LinkKiller killed. exiting");
103   }
104 };
105
106 int main(int argc, char** argv)
107 {
108
109   sg4::Engine engine(&argc, argv);
110   auto* zone  = sg4::create_full_zone("AS0");
111   auto* host1 = zone->create_host("Host1", "1f");
112   auto* host2 = zone->create_host("Host2", "1f");
113   auto* host3 = zone->create_host("Host3", "1f");
114
115   sg4::LinkInRoute linkto2{zone->create_link("linkto2", "1bps")->seal()};
116   sg4::LinkInRoute linkto3{zone->create_link("linkto3", "1bps")->seal()};
117
118   zone->add_route(host1->get_netpoint(), host2->get_netpoint(), nullptr, nullptr, {linkto2}, false);
119   zone->add_route(host1->get_netpoint(), host3->get_netpoint(), nullptr, nullptr, {linkto3}, false);
120   zone->seal();
121
122   sg4::Actor::create("Sender", host1, Sender("mailbox2", "mailbox3"));
123   sg4::Actor::create("Receiver", host2, Receiver("mailbox2"))->daemonize();
124   sg4::Actor::create("Receiver", host3, Receiver("mailbox3"))->daemonize();
125   sg4::Actor::create("LinkKiller", host1, LinkKiller("linkto2"))->daemonize();
126
127   engine.run();
128
129   return 0;
130 }