1 /* Copyright (c) 2021-2022. 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_exec_failure, "Messages specific for this s4u example");
21 namespace sg4 = simgrid::s4u;
23 static void dispatcher(sg4::Host* host1, sg4::Host* host2)
25 std::vector<sg4::ExecPtr> pending_execs;
26 XBT_INFO("Initiating asynchronous exec on %s", host1->get_cname());
27 auto exec1 = sg4::this_actor::exec_init(20)->set_host(host1);
28 pending_execs.push_back(exec1);
30 XBT_INFO("Initiating asynchronous exec on %s", host2->get_cname());
31 auto exec2 = sg4::this_actor::exec_init(20)->set_host(host2);
32 pending_execs.push_back(exec2);
35 XBT_INFO("Calling wait_any..");
37 long index = sg4::Exec::wait_any(pending_execs);
38 XBT_INFO("Wait any returned index %ld (exec on %s)", index, pending_execs.at(index)->get_host()->get_cname());
39 } catch (const simgrid::HostFailureException&) {
40 XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong");
41 XBT_INFO("Now it needs to figure out which of the two execs failed by looking at their state");
44 XBT_INFO("Exec on %s has state: %s", pending_execs[0]->get_host()->get_cname(), pending_execs[0]->get_state_str());
45 XBT_INFO("Exec on %s has state: %s", pending_execs[1]->get_host()->get_cname(), pending_execs[1]->get_state_str());
48 pending_execs[1]->wait();
49 } catch (const simgrid::HostFailureException& e) {
50 XBT_INFO("Waiting on a FAILED exec raises an exception: '%s'", e.what());
52 pending_execs.pop_back();
53 XBT_INFO("Wait for remaining exec, just to be nice");
54 sg4::Exec::wait_any(pending_execs);
55 XBT_INFO("Dispatcher ends");
58 static void host_killer(sg4::Host* to_kill)
60 XBT_INFO("HostKiller sleeping 10 seconds...");
61 sg4::this_actor::sleep_for(10.0);
62 XBT_INFO("HostKiller turning off host %s", to_kill->get_cname());
64 XBT_INFO("HostKiller ends");
67 int main(int argc, char** argv)
69 sg4::Engine engine(&argc, argv);
71 auto* zone = sg4::create_full_zone("AS0");
72 auto* host1 = zone->create_host("Host1", "1f");
73 auto* host2 = zone->create_host("Host2", "1f");
76 sg4::Actor::create("Dispatcher", host1, dispatcher, host1, host2);
77 sg4::Actor::create("HostKiller", host1, host_killer, host2)->daemonize();