Logo AND Algorithmique Numérique Distribuée

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