1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved. */
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. */
6 /* This example shows how to simulate a non-linear resource sharing for
10 #include <simgrid/s4u.hpp>
12 namespace sg4 = simgrid::s4u;
14 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_network_nonlinear, "Messages specific for this s4u example");
16 /*************************************************************************************************/
18 int messages_count; /* - number of messages */
19 int msg_size = 1e6; /* - message size in bytes */
22 explicit Sender(int count) : messages_count(count) {}
23 void operator()() const
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;
29 /* Make a vector of the mailboxes to use */
30 sg4::Mailbox* mbox = sg4::Mailbox::by_name("receiver");
31 // sphinx-doc: init-end
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);
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);
47 XBT_INFO("Done dispatching all messages");
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
53 XBT_INFO("Goodbye now!");
57 /* Receiver actor expects 1 argument: number of messages to be received */
60 int messages_count = 10; /* - number of messages */
63 explicit Receiver(int count) : messages_count(count) { mbox = sg4::Mailbox::by_name("receiver"); }
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;
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()));
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 */
81 pending_comms.erase(pending_comms.begin() + index);
82 pending_msgs.erase(pending_msgs.begin() + index);
87 /*************************************************************************************************/
88 /** @brief Non-linear resource sharing for links
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.
95 static double link_nonlinear(const sg4::Link* link, double capacity, int n)
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
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);
105 /** @brief Create a simple 2-hosts platform */
106 static void load_platform()
108 /* Creates the platform
109 * ________ __________
110 * | Sender |===============| Receiver |
111 * |________| Link1 |__________|
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();
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();
127 /* create routes between nodes */
128 zone->add_route(sender->get_netpoint(), receiver->get_netpoint(), nullptr, nullptr,
129 {{link, sg4::LinkInRoute::Direction::UP}}, true);
132 /* create actors Sender/Receiver */
133 sg4::Actor::create("receiver", receiver, Receiver(9));
134 sg4::Actor::create("sender", sender, Sender(9));
137 /*************************************************************************************************/
138 int main(int argc, char* argv[])
140 sg4::Engine e(&argc, argv);
142 /* create platform */
145 /* runs the simulation */