1 /* Copyright (c) 2007-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 simple example demonstrates the Comm::sento_init() Comm::sento_async() functions,
7 that can be used to create a direct communication from one host to another without
8 relying on the mailbox mechanism.
10 There is not much to say, actually: The _init variant creates the communication and
11 leaves it unstarted (in case you want to modify this communication before it starts),
12 while the _async variant creates and start it. In both cases, you need to wait() it.
14 It is mostly useful when you want to have a centralized simulation of your settings,
15 with a central actor declaring all communications occurring on your distributed system.
18 #include <simgrid/s4u.hpp>
19 namespace sg4 = simgrid::s4u;
21 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_host2host, "Messages specific for this s4u example");
23 static void sender(sg4::Host* h1, sg4::Host* h2, sg4::Host* h3, sg4::Host* h4)
25 XBT_INFO("Send c12 with sendto_async(%s -> %s), and c34 with sendto_init(%s -> %s)", h1->get_cname(), h2->get_cname(),
26 h3->get_cname(), h4->get_cname());
28 auto c12 = sg4::Comm::sendto_async(h1, h2, 1.5e7); // Creates and start a direct communication
30 auto c34 = sg4::Comm::sendto_init(h3, h4); // Creates but do not start another direct communication
31 c34->set_remaining(1e7); // Specify the amount of bytes to exchange in this comm
33 // You can also detach() communications that you never plan to test() or wait().
34 // Here we create a communication that only slows down the other ones
35 auto noise = sg4::Comm::sendto_init(h1, h2);
36 noise->set_remaining(10000);
39 XBT_INFO("After creation, c12 is %s (remaining: %.2e bytes); c34 is %s (remaining: %.2e bytes)",
40 c12->get_state_str(), c12->get_remaining(), c34->get_state_str(), c34->get_remaining());
41 sg4::this_actor::sleep_for(1);
42 XBT_INFO("One sec later, c12 is %s (remaining: %.2e bytes); c34 is %s (remaining: %.2e bytes)",
43 c12->get_state_str(), c12->get_remaining(), c34->get_state_str(), c34->get_remaining());
45 XBT_INFO("After c34->start,c12 is %s (remaining: %.2e bytes); c34 is %s (remaining: %.2e bytes)",
46 c12->get_state_str(), c12->get_remaining(), c34->get_state_str(), c34->get_remaining());
48 XBT_INFO("After c12->wait, c12 is %s (remaining: %.2e bytes); c34 is %s (remaining: %.2e bytes)",
49 c12->get_state_str(), c12->get_remaining(), c34->get_state_str(), c34->get_remaining());
51 XBT_INFO("After c34->wait, c12 is %s (remaining: %.2e bytes); c34 is %s (remaining: %.2e bytes)",
52 c12->get_state_str(), c12->get_remaining(), c34->get_state_str(), c34->get_remaining());
54 /* As usual, you don't have to explicitly start communications that were just init()ed.
55 The wait() will start it automatically. */
56 auto c14 = sg4::Comm::sendto_init(h1, h4);
57 c14->set_remaining(100)->wait(); // Chaining 2 operations on this new communication
60 int main(int argc, char* argv[])
62 sg4::Engine e(&argc, argv);
63 e.load_platform(argv[1]);
65 sg4::Actor::create("sender", e.host_by_name("Boivin"), sender, e.host_by_name("Tremblay"), e.host_by_name("Jupiter"),
66 e.host_by_name("Fafard"), e.host_by_name("Ginette"));
70 XBT_INFO("Total simulation time: %.3f", sg4::Engine::get_clock());