Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[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 }
30
31 static void ponger(std::vector<std::string> args)
32 {
33   xbt_assert(args.size() == 1, "The ponger function one argument from the XML deployment file");
34   XBT_INFO("Pong -> %s", args[0].c_str());
35   xbt_assert(simgrid::s4u::Host::by_name_or_null(args[0]) != nullptr, "Unknown host %s. Stopping Now! ",
36              args[0].c_str());
37
38   /* - Receive the (small) ping first ....*/
39   double* sender_time =
40       (static_cast<double*>(simgrid::s4u::Mailbox::byName(simgrid::s4u::this_actor::getHost()->getName())->get()));
41   double communication_time = simgrid::s4u::Engine::getClock() - *sender_time;
42   XBT_INFO("Task received : small communication (latency bound)");
43   XBT_INFO(" Ping time (latency bound) %f", communication_time);
44
45   /*  - ... Then send a 1GB pong back (bandwidth bound) */
46   double* payload = new double();
47   *payload        = simgrid::s4u::Engine::getClock();
48   XBT_INFO("task_bw->data = %.3f", *payload);
49
50   simgrid::s4u::Mailbox::byName(args[0])->put(payload, 1e9);
51 }
52
53 int main(int argc, char* argv[])
54 {
55   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
56
57   e->loadPlatform(argv[1]);
58   std::vector<std::string> args;
59   args.push_back("Jupiter");
60   simgrid::s4u::Actor::createActor("pinger", simgrid::s4u::Host::by_name("Tremblay"), pinger, args);
61
62   args.pop_back();
63   args.push_back("Tremblay");
64
65   simgrid::s4u::Actor::createActor("ponger", simgrid::s4u::Host::by_name("Jupiter"), ponger, args);
66
67   e->run();
68
69   XBT_INFO("Total simulation time: %.3f", e->getClock());
70   delete e;
71
72   return 0;
73 }