Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / comm-host2host / s4u-comm-host2host.cpp
1 /* Copyright (c) 2007-2022. 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 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.
9
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.
13
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.
16   */
17
18 #include <simgrid/s4u.hpp>
19 namespace sg4 = simgrid::s4u;
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_host2host, "Messages specific for this s4u example");
22
23 static void sender(sg4::Host* h1, sg4::Host* h2, sg4::Host* h3, sg4::Host* h4)
24 {
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());
27
28   auto c12 = sg4::Comm::sendto_async(h1, h2, 1.5e7); // Creates and start a direct communication
29
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
32
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);
37   noise->detach();
38
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());
44   c34->start();
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());
47   c12->wait();
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());
50   c34->wait();
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());
53
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
58 }
59
60 int main(int argc, char* argv[])
61 {
62   sg4::Engine e(&argc, argv);
63   e.load_platform(argv[1]);
64
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"));
67
68   e.run();
69
70   XBT_INFO("Total simulation time: %.3f", sg4::Engine::get_clock());
71
72   return 0;
73 }