1 /* Copyright (c) 2007-2023. 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;
13 struct MasterWorkerNames {
17 using MasterWorkerNamesMap = std::unordered_map<int, MasterWorkerNames>;
24 static void master(MasterWorkerNamesMap& names, const std::vector<std::string>& args)
26 xbt_assert(args.size() == 4, "Strange number of arguments expected 3 got %zu", args.size() - 1);
28 XBT_DEBUG("Master started");
31 double msg_size = std::stod(args[1]);
32 int id = std::stoi(args[3]); // unique id to control statistics
34 /* master and worker names */
35 names.emplace(id, MasterWorkerNames{sg4::Host::current()->get_name(), args[2]});
37 sg4::Mailbox* mbox = sg4::Mailbox::by_name(args[3]);
39 auto* payload = new Payload{msg_size, sg4::Engine::get_clock()};
40 mbox->put(payload, static_cast<uint64_t>(msg_size));
42 XBT_DEBUG("Finished");
45 static void worker(const MasterWorkerNamesMap& names, const std::vector<std::string>& args)
47 xbt_assert(args.size() == 2, "Strange number of arguments expected 1 got %zu", args.size() - 1);
49 int id = std::stoi(args[1]);
50 sg4::Mailbox* mbox = sg4::Mailbox::by_name(args[1]);
52 XBT_DEBUG("Worker started");
54 auto payload = mbox->get_unique<Payload>();
56 double elapsed_time = sg4::Engine::get_clock() - payload->start_time;
58 XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, payload->msg_size, names.at(id).master.c_str(),
59 names.at(id).worker.c_str());
60 XBT_DEBUG("FLOW[%d] : transferred in %f seconds", id, elapsed_time);
62 XBT_DEBUG("Finished");
65 int main(int argc, char* argv[])
67 sg4::Engine e(&argc, argv);
69 "Usage: %s platform_file deployment_file\n"
70 "\tExample: %s platform.xml deployment.xml\n",
73 e.load_platform(argv[1]);
75 MasterWorkerNamesMap master_worker_names;
76 e.register_function("master", [&master_worker_names](auto args) { master(master_worker_names, args); });
77 e.register_function("worker", [&master_worker_names](auto args) { worker(master_worker_names, args); });
79 e.load_deployment(argv[2]);