Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
790b9cb7abf420369a8af25d10997090e002c66a
[simgrid.git] / examples / s4u / comm-host2host / s4u-comm-host2host.cpp
1 /* Copyright (c) 2007-2021. 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 occuring 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   XBT_INFO("After creation,  c12 is %s (remaining: %.2e bytes); c34 is %s (remaining: %.2e bytes)",
34            c12->get_state_str(), c12->get_remaining(), c34->get_state_str(), c34->get_remaining());
35   sg4::this_actor::sleep_for(1);
36   XBT_INFO("One sec later,   c12 is %s (remaining: %.2e bytes); c34 is %s (remaining: %.2e bytes)",
37            c12->get_state_str(), c12->get_remaining(), c34->get_state_str(), c34->get_remaining());
38   c34->start();
39   XBT_INFO("After c34->start,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   c12->wait();
42   XBT_INFO("After c12->wait, 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->wait();
45   XBT_INFO("After c34->wait, 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
48   /* As usual, you don't have to explicitly start communications that were just init()ed. The wait() will start it
49    * automatically. */
50   auto c14 = sg4::Comm::sendto_init(h1, h4);
51   c14->set_remaining(100)->wait(); // Chaining 2 operations on this new communication
52 }
53
54 int main(int argc, char* argv[])
55 {
56   sg4::Engine e(&argc, argv);
57   e.load_platform(argv[1]);
58
59   sg4::Actor::create("sender", sg4::Host::by_name("Tremblay"), sender, sg4::Host::by_name("Tremblay"),
60                      sg4::Host::by_name("Jupiter"), sg4::Host::by_name("Fafard"), sg4::Host::by_name("Ginette"));
61
62   e.run();
63
64   XBT_INFO("Total simulation time: %.3f", e.get_clock());
65
66   return 0;
67 }