Logo AND Algorithmique Numérique Distribuée

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