1 /* Copyright (c) 2021. The SimGrid Team. All rights reserved. */
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. */
6 /* This example shows how to serialize a set of communications going through a link
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.
13 * However, as they will be serialized in a link of the platform, the messages arrive 2 by 2.
15 * The sender then blocks until all ongoing communication terminate, using simgrid::s4u::Comm::wait_all()
18 #include <simgrid/s4u.hpp>
20 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_failure, "Messages specific for this s4u example");
21 namespace sg4 = simgrid::s4u;
24 std::string mailbox1_name;
25 std::string mailbox2_name;
28 Sender(const std::string& mailbox1_name, const std::string& mailbox2_name)
29 : mailbox1_name(mailbox1_name), mailbox2_name(mailbox2_name)
33 void operator()() const
35 auto mailbox1 = sg4::Mailbox::by_name(mailbox1_name);
36 auto mailbox2 = sg4::Mailbox::by_name(mailbox2_name);
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);
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);
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");
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());
60 } catch (const simgrid::NetworkFailureException& e) {
61 XBT_INFO("Waiting on a FAILED comm raises an exception: '%s'", e.what());
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);
70 std::string mailbox_name;
73 explicit Receiver(const std::string& mailbox_name) : mailbox_name(mailbox_name) {}
75 void operator()() const
77 auto mailbox = sg4::Mailbox::by_name(mailbox_name);
78 XBT_INFO("Receiver posting a receive...");
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");
89 std::string link_name;
92 explicit LinkKiller(const std::string& link_name) : link_name(link_name) {}
94 void operator()() const
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");
105 int main(int argc, char** argv)
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");
113 sg4::LinkInRoute linkto2{zone->create_link("linkto2", "1bps")->seal()};
114 sg4::LinkInRoute linkto3{zone->create_link("linkto3", "1bps")->seal()};
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);
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();