1 /* Copyright (c) 2017-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/plugins/energy.h"
8 #include "xbt/random.hpp"
9 #include <simgrid/s4u.hpp>
11 /* Parameters of the random generation of the flow size */
12 static const int min_size = 1e6;
13 static const int max_size = 1e9;
15 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_energyconsumption, "Messages specific for this s4u example");
16 namespace sg4 = simgrid::s4u;
18 static void sender(std::vector<std::string> args)
20 xbt_assert(args.size() == 2, "The master function expects 2 arguments.");
21 int flow_amount = std::stoi(args.at(0));
22 long comm_size = std::stol(args.at(1));
23 XBT_INFO("Send %ld bytes, in %d flows", comm_size, flow_amount);
25 sg4::Mailbox* mailbox = sg4::Mailbox::by_name("message");
27 /* Sleep a while before starting the example */
28 sg4::this_actor::sleep_for(10);
30 if (flow_amount == 1) {
31 /* - Send the task to the @ref worker */
32 char* payload = bprintf("%ld", comm_size);
33 mailbox->put(payload, comm_size);
35 // Start all comms in parallel, and wait for all completions in one shot
36 std::vector<sg4::CommPtr> comms;
37 for (int i = 0; i < flow_amount; i++)
38 comms.push_back(mailbox->put_async(bprintf("%d", i), comm_size));
39 sg4::Comm::wait_all(comms);
41 XBT_INFO("sender done.");
44 static void receiver(std::vector<std::string> args)
46 int flow_amount = std::stoi(args.at(0));
48 XBT_INFO("Receiving %d flows ...", flow_amount);
50 sg4::Mailbox* mailbox = sg4::Mailbox::by_name("message");
52 if (flow_amount == 1) {
53 char* res = mailbox->get<char>();
56 std::vector<char*> data(flow_amount);
58 // Start all comms in parallel, and wait for their completion in one shot
59 std::vector<sg4::CommPtr> comms;
60 for (int i = 0; i < flow_amount; i++)
61 comms.push_back(mailbox->get_async<char>(&data[i]));
63 sg4::Comm::wait_all(comms);
64 for (int i = 0; i < flow_amount; i++)
67 XBT_INFO("receiver done.");
70 int main(int argc, char* argv[])
72 sg4::Engine e(&argc, argv);
74 XBT_INFO("Activating the SimGrid link energy plugin");
75 sg_link_energy_plugin_init();
77 xbt_assert(argc > 1, "\nUsage: %s platform_file [flowCount [datasize]]\n"
78 "\tExample: %s s4uplatform.xml \n",
80 e.load_platform(argv[1]);
82 /* prepare to launch the actors */
83 std::vector<std::string> argSender;
84 std::vector<std::string> argReceiver;
86 argSender.emplace_back(argv[2]); // Take the amount of flows from the command line
87 argReceiver.emplace_back(argv[2]);
89 argSender.emplace_back("1"); // Default value
90 argReceiver.emplace_back("1");
94 if (strcmp(argv[3], "random") == 0) { // We're asked to get a random size
95 std::string size = std::to_string(simgrid::xbt::random::uniform_int(min_size, max_size));
96 argSender.push_back(size);
97 } else { // Not "random" ? Then it should be the size to use
98 argSender.emplace_back(argv[3]); // Take the datasize from the command line
100 } else { // No parameter at all? Then use the default value
101 argSender.emplace_back("25000");
103 sg4::Actor::create("sender", e.host_by_name("MyHost1"), sender, argSender);
104 sg4::Actor::create("receiver", e.host_by_name("MyHost2"), receiver, argReceiver);
106 /* And now, launch the simulation */