Logo AND Algorithmique Numérique Distribuée

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