Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / examples / cpp / network-nonlinear / s4u-network-nonlinear.cpp
1 /* Copyright (c) 2010-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 /* This example shows how to simulate a non-linear resource sharing for
7  * network links.
8  */
9
10 #include <simgrid/s4u.hpp>
11
12 namespace sg4 = simgrid::s4u;
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_network_nonlinear, "Messages specific for this s4u example");
15
16 /*************************************************************************************************/
17 class Sender {
18   int messages_count; /* - number of messages */
19   int msg_size = 1e6; /* - message size in bytes */
20
21 public:
22   explicit Sender(int count) : messages_count(count) {}
23   void operator()() const
24   {
25     // sphinx-doc: init-begin (this line helps the doc to build; ignore it)
26     /* Vector in which we store all ongoing communications */
27     std::vector<sg4::CommPtr> pending_comms;
28
29     /* Make a vector of the mailboxes to use */
30     sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver");
31     // sphinx-doc: init-end
32
33     /* Start dispatching all messages to receiver */
34     for (int i = 0; i < messages_count; i++) {
35       std::string msg_content = std::string("Message ") + std::to_string(i);
36       // Copy the data we send: the 'msg_content' variable is not a stable storage location.
37       // It will be destroyed when this actor leaves the loop, ie before the receiver gets it
38       auto* payload           = new std::string(msg_content);
39       unsigned long long size = msg_size * (i + 1);
40       XBT_INFO("Send '%s' to '%s, msg size: %llu'", msg_content.c_str(), mbox->get_cname(), size);
41
42       /* Create a communication representing the ongoing communication, and store it in pending_comms */
43       sg4::CommPtr comm = mbox->put_async(payload, size);
44       pending_comms.push_back(comm);
45     }
46
47     XBT_INFO("Done dispatching all messages");
48
49     /* Now that all message exchanges were initiated, wait for their completion in one single call */
50     sg4::Comm::wait_all(pending_comms);
51     // sphinx-doc: put-end
52
53     XBT_INFO("Goodbye now!");
54   }
55 };
56
57 /* Receiver actor expects 1 argument: number of messages to be received */
58 class Receiver {
59   sg4::Mailbox* mbox;
60   int messages_count = 10; /* - number of messages */
61
62 public:
63   explicit Receiver(int count) : messages_count(count) { mbox = sg4::Mailbox::by_name("receiver"); }
64   void operator()()
65   {
66     /* Vector in which we store all incoming msgs */
67     std::vector<std::unique_ptr<std::string*>> pending_msgs;
68     std::vector<sg4::CommPtr> pending_comms;
69
70     XBT_INFO("Wait for %d messages asynchronously", messages_count);
71     for (int i = 0; i < messages_count; i++) {
72       pending_msgs.push_back(std::make_unique<std::string*>());
73       pending_comms.emplace_back(mbox->get_async<std::string>(pending_msgs[i].get()));
74     }
75     while (not pending_comms.empty()) {
76       ssize_t index    = sg4::Comm::wait_any(pending_comms);
77       std::string* msg = *pending_msgs[index];
78       XBT_INFO("I got '%s'.", msg->c_str());
79       /* cleanup memory and remove from vectors */
80       delete msg;
81       pending_comms.erase(pending_comms.begin() + index);
82       pending_msgs.erase(pending_msgs.begin() + index);
83     }
84   }
85 };
86
87 /*************************************************************************************************/
88 /** @brief Non-linear resource sharing for links
89  *
90  * Note that the callback is called twice in this example:
91  * 1) link UP: with the number of active flows (from 9 to 1)
92  * 2) link DOWN: with 0 active flows. A crosstraffic communication is happing
93  * in the down link, but it's not considered as an active flow.
94  */
95 static double link_nonlinear(const sg4::Link* link, double capacity, int n)
96 {
97   /* emulates a degradation in link according to the number of flows
98    * you probably want something more complex than that and based on real
99    * experiments */
100   capacity = std::min(capacity, capacity * (1.0 - (n - 1) / 10.0));
101   XBT_INFO("Link %s, %d active communications, new capacity %lf", link->get_cname(), n, capacity);
102   return capacity;
103 }
104
105 /** @brief Create a simple 2-hosts platform */
106 static void load_platform()
107 {
108   /* Creates the platform
109    *  ________                 __________
110    * | Sender |===============| Receiver |
111    * |________|    Link1      |__________|
112    */
113   auto* zone     = sg4::create_full_zone("Zone1");
114   auto* sender   = zone->create_host("sender", 1)->seal();
115   auto* receiver = zone->create_host("receiver", 1)->seal();
116
117   auto* link = zone->create_split_duplex_link("link1", 1e6);
118   /* setting same callbacks (could be different) for link UP/DOWN in split-duplex link */
119   link->get_link_up()->set_sharing_policy(
120       sg4::Link::SharingPolicy::NONLINEAR,
121       std::bind(&link_nonlinear, link->get_link_up(), std::placeholders::_1, std::placeholders::_2));
122   link->get_link_down()->set_sharing_policy(
123       sg4::Link::SharingPolicy::NONLINEAR,
124       std::bind(&link_nonlinear, link->get_link_down(), std::placeholders::_1, std::placeholders::_2));
125   link->set_latency(10e-6)->seal();
126
127   /* create routes between nodes */
128   zone->add_route(sender->get_netpoint(), receiver->get_netpoint(), nullptr, nullptr,
129                   {{link, sg4::LinkInRoute::Direction::UP}}, true);
130   zone->seal();
131
132   /* create actors Sender/Receiver */
133   sg4::Actor::create("receiver", receiver, Receiver(9));
134   sg4::Actor::create("sender", sender, Sender(9));
135 }
136
137 /*************************************************************************************************/
138 int main(int argc, char* argv[])
139 {
140   sg4::Engine e(&argc, argv);
141
142   /* create platform */
143   load_platform();
144
145   /* runs the simulation */
146   e.run();
147
148   return 0;
149 }