Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e64eccf1b96560dd0cef55a299e0e3a1f12d61b8
[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   /* - Send the task to the @ref worker */
31   char* payload = bprintf("%f", comm_size);
32
33   if (flow_amount == 1) {
34     mailbox->put(payload, comm_size);
35   } else {
36     // Start all comms in parallel
37     std::vector<simgrid::s4u::CommPtr> comms;
38     for (int i = 0; i < flow_amount; i++)
39       comms.push_back(mailbox->put_async(const_cast<char*>("message"), comm_size));
40
41     // And now, wait for all comms. Manually since wait_all is not part of this_actor yet
42     for (int i = 0; i < flow_amount; i++) {
43       simgrid::s4u::CommPtr comm = comms.at(i);
44       comm->wait();
45     }
46     comms.clear();
47   }
48   XBT_INFO("sender done.");
49 }
50
51 static void receiver(std::vector<std::string> args)
52 {
53   int flow_amount = std::stoi(args.at(0));
54
55   XBT_INFO("Receiving %d flows ...", flow_amount);
56
57   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(std::string("message"));
58
59   if (flow_amount == 1) {
60     void* res = mailbox->get();
61     xbt_free(res);
62   } else {
63     void* ignored;
64
65     // Start all comms in parallel
66     std::vector<simgrid::s4u::CommPtr> comms;
67     for (int i = 0; i < flow_amount; i++)
68       comms.push_back(mailbox->get_async(&ignored));
69
70     // And now, wait for all comms. Manually since wait_all is not part of this_actor yet
71     for (int i = 0; i < flow_amount; i++)
72       comms.at(i)->wait();
73     comms.clear();
74   }
75   XBT_INFO("receiver done.");
76 }
77
78 int main(int argc, char* argv[])
79 {
80
81   simgrid::s4u::Engine e(&argc, argv);
82
83   /* Check if we got --NS3 on the command line, and activate ecofen if so */
84   bool NS3 = false;
85   for (int i = 0; i < argc; i++) {
86     if (strcmp(argv[i], "--NS3") == 0)
87       NS3 = true;
88     if (NS3) // Found the --NS3 parameter previously; shift the rest of the line
89       argv[i] = argv[i + 1];
90   }
91   if (NS3) {
92     xbt_die("No Ecofen in this build");
93     //    XBT_INFO("Activating the Ecofen energy plugin");
94     //    ns3_link_energy_plugin_init();
95     //    xbt_cfg_set_parse("network/model:NS3");
96     //    argc -= 1; // We removed it from the parameters
97   } else {
98     XBT_INFO("Activating the SimGrid link energy plugin");
99     sg_link_energy_plugin_init();
100   }
101
102   xbt_assert(argc > 1, "\nUsage: %s platform_file [flowCount [datasize]] [--NS3]\n"
103                        "\tExample: %s s4uplatform.xml \n"
104                        "\tIf you add NS3 as last parameter, this will try to activate the ecofen plugin.\n"
105                        "\tWithout it, it will use the SimGrid link energy plugin.\n",
106              argv[0], argv[0]);
107   e.loadPlatform(argv[1]);
108
109   /* prepare to launch the actors */
110   std::vector<std::string> argSender;
111   std::vector<std::string> argReceiver;
112   if (argc > 2) {
113     argSender.push_back(argv[2]); // Take the amount of flows from the command line
114     argReceiver.push_back(argv[2]);
115   } else {
116     argSender.push_back("1"); // Default value
117     argReceiver.push_back("1");
118   }
119   if (argc > 3) {
120     if (strcmp(argv[3], "random") == 0) { // We're asked to get a random size
121       /* Initialize the random number generator */
122       std::random_device rd;
123       std::default_random_engine generator(rd());
124
125       /* Distribution on which to apply the generator */
126       std::uniform_int_distribution<unsigned long int> distribution(min_size, max_size);
127
128       char* size = bprintf("%lu", distribution(generator));
129       argSender.push_back(std::string(size));
130       xbt_free(size);
131     } else {                        // Not "random" ? Then it should be the size to use
132       argSender.push_back(argv[3]); // Take the datasize from the command line
133     }
134   } else { // No parameter at all? Then use the default value
135     argSender.push_back("25000");
136   }
137   simgrid::s4u::Actor::createActor("sender", simgrid::s4u::Host::by_name("MyHost1"), sender, argSender);
138   simgrid::s4u::Actor::createActor("receiver", simgrid::s4u::Host::by_name("MyHost2"), receiver, argReceiver);
139
140   /* And now, launch the simulation */
141   e.run();
142
143   return 0;
144 }