1 /* Copyright (c) 2021-2023. 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>
19 #include "simgrid/kernel/ProfileBuilder.hpp"
21 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_exec_failure, "Messages specific for this s4u example");
22 namespace sg4 = simgrid::s4u;
24 static void dispatcher(std::vector<sg4::Host*> const& hosts)
26 std::vector<sg4::ExecPtr> pending_execs;
27 for (auto* host: hosts) {
28 XBT_INFO("Initiating asynchronous exec on %s", host->get_cname());
29 // Computing 20 flops on an host which speed is 1f takes 20 seconds (when it does not fail)
30 auto exec = sg4::this_actor::exec_init(20)->set_host(host);
31 pending_execs.push_back(exec);
35 XBT_INFO("---------------------------------");
36 XBT_INFO("Wait on the first exec, which host is turned off at t=10 by the another actor.");
38 pending_execs[0]->wait();
39 xbt_assert("This wait was not supposed to succeed.");
40 } catch (const simgrid::HostFailureException&) {
41 XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong.");
44 XBT_INFO("State of each exec:");
45 for (auto const& exec : pending_execs)
46 XBT_INFO(" Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
48 XBT_INFO("---------------------------------");
49 XBT_INFO("Wait on the second exec, which host is turned off at t=12 by the state profile.");
51 pending_execs[1]->wait();
52 xbt_assert("This wait was not supposed to succeed.");
53 } catch (const simgrid::HostFailureException&) {
54 XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong.");
56 XBT_INFO("State of each exec:");
57 for (auto const& exec : pending_execs)
58 XBT_INFO(" Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
60 XBT_INFO("---------------------------------");
61 XBT_INFO("Wait on the third exec, which should succeed.");
63 pending_execs[2]->wait();
64 XBT_INFO("No exception occured.");
65 } catch (const simgrid::HostFailureException&) {
66 xbt_assert("This wait was not supposed to fail.");
68 XBT_INFO("State of each exec:");
69 for (auto const& exec : pending_execs)
70 XBT_INFO(" Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
73 static void host_killer(sg4::Host* to_kill)
75 sg4::this_actor::sleep_for(10.0);
76 XBT_INFO("HostKiller turns off the host '%s'.", to_kill->get_cname());
80 int main(int argc, char** argv)
82 sg4::Engine engine(&argc, argv);
84 auto* zone = sg4::create_full_zone("world");
85 std::vector<sg4::Host*> hosts;
86 for (auto name : {"Host1", "Host2", "Host3"}) {
87 auto* host = zone->create_host(name, "1f");
88 hosts.push_back(host);
90 /* Attaching a state profile (ie a list of events changing the on/off state of the resource) to host3.
91 * The syntax of the profile (second parameter) is a list of: "date state\n"
92 * The R"( )" thing is the C++ way of writing multiline strings, including literals \n.
93 * You'd have the same behavior by using "12 0\n20 1\n" instead.
94 * So here, the link is turned off at t=12 and back on at t=20.
95 * The last parameter is the period of that profile, meaning that it loops after 30 seconds.
97 hosts[1]->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("profile name", R"(
104 sg4::Actor::create("Dispatcher", hosts[2], dispatcher, hosts);
105 sg4::Actor::create("HostKiller", hosts[2], host_killer, hosts[0]);