Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4a5069a4e9c5f28fab62235730558c8ee38af445
[simgrid.git] / examples / cpp / comm-throttling / s4u-comm-throttling.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 #include <simgrid/s4u.hpp>
7 namespace sg4 = simgrid::s4u;
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_throttling, "Messages specific for this s4u example");
10
11 static void sender(sg4::Mailbox* mailbox)
12 {
13   XBT_INFO("Send at full bandwidth");
14
15   /* - First send a 2.5e8 Bytes payload at full bandwidth (1.25e8 Bps) */
16   auto* payload = new double(sg4::Engine::get_clock());
17   mailbox->put(payload, 2.5e8);
18
19   XBT_INFO("Throttle the bandwidth at the Comm level");
20   /* - ... then send it again but throttle the Comm */
21   payload = new double(sg4::Engine::get_clock());
22   /* get a handler on the comm first */
23   sg4::CommPtr comm = mailbox->put_init(payload, 2.5e8);
24
25   /* let throttle the communication. It amounts to set the rate of the comm to half the nominal bandwidth of the link,
26    * i.e., 1.25e8 / 2. This second communication will thus take approximately twice as long as the first one*/
27   comm->set_rate(1.25e8 / 2)->wait();
28 }
29
30 static void receiver(sg4::Mailbox* mailbox)
31 {
32   /* - Receive the first payload sent at full bandwidth */
33   auto sender_time          = mailbox->get_unique<double>();
34   double communication_time = sg4::Engine::get_clock() - *sender_time;
35   XBT_INFO("Payload received (full bandwidth) in %f seconds", communication_time);
36
37   /*  - ... Then receive the second payload sent with a throttled Comm */
38   sender_time        = mailbox->get_unique<double>();
39   communication_time = sg4::Engine::get_clock() - *sender_time;
40   XBT_INFO("Payload received (throttled) in %f seconds", communication_time);
41 }
42
43 int main(int argc, char* argv[])
44 {
45   sg4::Engine e(&argc, argv);
46   e.load_platform(argv[1]);
47
48   sg4::Mailbox* mbox = e.mailbox_by_name_or_create("Mailbox");
49
50   sg4::Actor::create("sender", e.host_by_name("node-0.simgrid.org"), sender, mbox);
51   sg4::Actor::create("receiver", e.host_by_name("node-1.simgrid.org"), receiver, mbox);
52
53   e.run();
54
55   return 0;
56 }