Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / comm-pingpong / s4u-comm-pingpong.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 #include <simgrid/s4u.hpp>
7 namespace sg4 = simgrid::s4u;
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_pingpong, "Messages specific for this s4u example");
10
11 static void pinger(sg4::Mailbox* mailbox_in, sg4::Mailbox* mailbox_out)
12 {
13   XBT_INFO("Ping from mailbox %s to mailbox %s", mailbox_in->get_name().c_str(), mailbox_out->get_name().c_str());
14
15   /* - Do the ping with a 1-Byte payload (latency bound) ... */
16   auto* payload = new double(sg4::Engine::get_clock());
17
18   mailbox_out->put(payload, 1);
19   /* - ... then wait for the (large) pong */
20   auto sender_time = mailbox_in->get_unique<double>();
21
22   double communication_time = sg4::Engine::get_clock() - *sender_time;
23   XBT_INFO("Payload received : large communication (bandwidth bound)");
24   XBT_INFO("Pong time (bandwidth bound): %.3f", communication_time);
25 }
26
27 static void ponger(sg4::Mailbox* mailbox_in, sg4::Mailbox* mailbox_out)
28 {
29   XBT_INFO("Pong from mailbox %s to mailbox %s", mailbox_in->get_name().c_str(), mailbox_out->get_name().c_str());
30
31   /* - Receive the (small) ping first ....*/
32   auto sender_time          = mailbox_in->get_unique<double>();
33   double communication_time = sg4::Engine::get_clock() - *sender_time;
34   XBT_INFO("Payload received : small communication (latency bound)");
35   XBT_INFO("Ping time (latency bound) %f", communication_time);
36
37   /*  - ... Then send a 1GB pong back (bandwidth bound) */
38   auto* payload = new double(sg4::Engine::get_clock());
39   XBT_INFO("payload = %.3f", *payload);
40
41   mailbox_out->put(payload, 1e9);
42 }
43
44 int main(int argc, char* argv[])
45 {
46   sg4::Engine e(&argc, argv);
47   e.load_platform(argv[1]);
48
49   sg4::Mailbox* mb1 = e.mailbox_by_name_or_create("Mailbox 1");
50   sg4::Mailbox* mb2 = e.mailbox_by_name_or_create("Mailbox 2");
51
52   sg4::Actor::create("pinger", e.host_by_name("Tremblay"), pinger, mb1, mb2);
53   sg4::Actor::create("ponger", e.host_by_name("Jupiter"), ponger, mb2, mb1);
54
55   e.run();
56
57   XBT_INFO("Total simulation time: %.3f", sg4::Engine::get_clock());
58
59   return 0;
60 }