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 #include <simgrid/s4u.hpp>
7 namespace sg4 = simgrid::s4u;
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_comm_throttling, "Messages specific for this s4u example");
11 static void sender(sg4::Mailbox* mailbox)
13 XBT_INFO("Send at full bandwidth");
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);
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);
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();
30 static void receiver(sg4::Mailbox* mailbox)
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);
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);
43 int main(int argc, char* argv[])
45 sg4::Engine e(&argc, argv);
46 e.load_platform(argv[1]);
48 sg4::Mailbox* mbox = e.mailbox_by_name_or_create("Mailbox");
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);