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"
8 #include <unordered_map>
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11 namespace sg4 = simgrid::s4u;
14 std::unordered_map<int, std::string> workernames;
15 std::unordered_map<int, std::string> masternames;
17 static void master(std::vector<std::string> args)
19 xbt_assert(args.size() == 4, "Strange number of arguments expected 3 got %zu", args.size() - 1);
21 XBT_DEBUG("Master started");
24 double msg_size = std::stod(args[1]);
25 int id = std::stoi(args[3]); // unique id to control statistics
28 workernames[id] = args[2];
30 sg4::Mailbox* mbox = sg4::Mailbox::by_name(args[3]);
32 masternames[id] = sg4::Host::current()->get_name();
34 auto* payload = new double(msg_size);
36 /* time measurement */
37 start_time = sg4::Engine::get_clock();
38 mbox->put(payload, static_cast<uint64_t>(msg_size));
40 XBT_DEBUG("Finished");
43 static void worker(std::vector<std::string> args)
45 xbt_assert(args.size() == 2, "Strange number of arguments expected 1 got %zu", args.size() - 1);
47 int id = std::stoi(args[1]);
48 sg4::Mailbox* mbox = sg4::Mailbox::by_name(args[1]);
50 XBT_DEBUG("Worker started");
52 auto payload = mbox->get_unique<double>();
54 double elapsed_time = sg4::Engine::get_clock() - start_time;
56 XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, *payload, masternames.at(id).c_str(),
57 workernames.at(id).c_str());
58 XBT_DEBUG("FLOW[%d] : transferred in %f seconds", id, elapsed_time);
60 XBT_DEBUG("Finished");
63 int main(int argc, char* argv[])
65 sg4::Engine e(&argc, argv);
67 "Usage: %s platform_file deployment_file\n"
68 "\tExample: %s platform.xml deployment.xml\n",
71 e.load_platform(argv[1]);
73 e.register_function("master", master);
74 e.register_function("worker", worker);
76 e.load_deployment(argv[2]);