Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a test to cover the Constant-time network model
[simgrid.git] / examples / s4u / app-pingpong / s4u-app-pingpong.cpp
1 /* Copyright (c) 2007-2017. 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(std::vector<std::string> args)
11 {
12   xbt_assert(args.size() == 1, "The pinger function one argument from the XML deployment file");
13   XBT_INFO("Ping -> %s", args[0].c_str());
14   xbt_assert(simgrid::s4u::Host::by_name_or_null(args[0]) != nullptr, "Unknown host %s. Stopping Now! ",
15              args[0].c_str());
16
17   /* - Do the ping with a 1-Byte task (latency bound) ... */
18   double* payload = new double();
19   *payload        = simgrid::s4u::Engine::getClock();
20
21   simgrid::s4u::Mailbox::byName(args[0])->put(payload, 1);
22   /* - ... then wait for the (large) pong */
23   double* sender_time =
24       static_cast<double*>(simgrid::s4u::Mailbox::byName(simgrid::s4u::this_actor::getHost()->getName())->get());
25
26   double communication_time = simgrid::s4u::Engine::getClock() - *sender_time;
27   XBT_INFO("Task received : large communication (bandwidth bound)");
28   XBT_INFO("Pong time (bandwidth bound): %.3f", communication_time);
29   delete sender_time;
30 }
31
32 static void ponger(std::vector<std::string> args)
33 {
34   xbt_assert(args.size() == 1, "The ponger function one argument from the XML deployment file");
35   XBT_INFO("Pong -> %s", args[0].c_str());
36   xbt_assert(simgrid::s4u::Host::by_name_or_null(args[0]) != nullptr, "Unknown host %s. Stopping Now! ",
37              args[0].c_str());
38
39   /* - Receive the (small) ping first ....*/
40   double* sender_time =
41       static_cast<double*>(simgrid::s4u::Mailbox::byName(simgrid::s4u::this_actor::getHost()->getName())->get());
42   double communication_time = simgrid::s4u::Engine::getClock() - *sender_time;
43   XBT_INFO("Task received : small communication (latency bound)");
44   XBT_INFO(" Ping time (latency bound) %f", communication_time);
45   delete sender_time;
46
47   /*  - ... Then send a 1GB pong back (bandwidth bound) */
48   double* payload = new double();
49   *payload        = simgrid::s4u::Engine::getClock();
50   XBT_INFO("task_bw->data = %.3f", *payload);
51
52   simgrid::s4u::Mailbox::byName(args[0])->put(payload, 1e9);
53 }
54
55 int main(int argc, char* argv[])
56 {
57   simgrid::s4u::Engine e(&argc, argv);
58
59   e.loadPlatform(argv[1]);
60   std::vector<std::string> args;
61   args.push_back("Jupiter");
62   simgrid::s4u::Actor::createActor("pinger", simgrid::s4u::Host::by_name("Tremblay"), pinger, args);
63
64   args.pop_back();
65   args.push_back("Tremblay");
66
67   simgrid::s4u::Actor::createActor("ponger", simgrid::s4u::Host::by_name("Jupiter"), ponger, args);
68
69   e.run();
70
71   XBT_INFO("Total simulation time: %.3f", e.getClock());
72
73   return 0;
74 }