Logo AND Algorithmique Numérique Distribuée

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