1 /* Copyright (c) 2010-2023. 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 /* ActivitySet in which we store all ongoing communications */
27 sg4::ActivitySet pending_comms;
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 = "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(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 pending_comms.wait_all();
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 /* Where we store all incoming msgs */
67 std::unordered_map<sg4::CommPtr, std::string**> pending_msgs;
68 sg4::ActivitySet pending_comms;
70 XBT_INFO("Wait for %d messages asynchronously", messages_count);
71 for (int i = 0; i < messages_count; i++) {
73 auto comm = mbox->get_async<std::string>(&msg);
74 pending_comms.push(comm);
75 pending_msgs.insert({comm, &msg});
78 while (not pending_comms.empty()) {
79 auto completed_one = pending_comms.wait_any();
80 if (completed_one != nullptr){
81 auto comm = boost::dynamic_pointer_cast<sg4::Comm>(completed_one);
82 std::string *msg = std::move(*pending_msgs[comm]);
83 XBT_INFO("I got '%s'.", msg->c_str());
84 /* cleanup memory and remove from map */
86 pending_msgs.erase(comm);
92 /*************************************************************************************************/
93 /** @brief Non-linear resource sharing for links
95 * Note that the callback is called twice in this example:
96 * 1) link UP: with the number of active flows (from 9 to 1)
97 * 2) link DOWN: with 0 active flows. A crosstraffic communication is happing
98 * in the down link, but it's not considered as an active flow.
100 static double link_nonlinear(const sg4::Link* link, double capacity, int n)
102 /* emulates a degradation in link according to the number of flows
103 * you probably want something more complex than that and based on real
105 capacity = std::min(capacity, capacity * (1.0 - (n - 1) / 10.0));
106 XBT_INFO("Link %s, %d active communications, new capacity %lf", link->get_cname(), n, capacity);
110 /** @brief Create a simple 2-hosts platform */
111 static void load_platform()
113 /* Creates the platform
114 * ________ __________
115 * | Sender |===============| Receiver |
116 * |________| Link1 |__________|
118 auto* zone = sg4::create_full_zone("Zone1");
119 auto* sender = zone->create_host("sender", 1)->seal();
120 auto* receiver = zone->create_host("receiver", 1)->seal();
122 auto* link = zone->create_split_duplex_link("link1", 1e6);
123 /* setting same callbacks (could be different) for link UP/DOWN in split-duplex link */
124 link->get_link_up()->set_sharing_policy(
125 sg4::Link::SharingPolicy::NONLINEAR,
126 std::bind(&link_nonlinear, link->get_link_up(), std::placeholders::_1, std::placeholders::_2));
127 link->get_link_down()->set_sharing_policy(
128 sg4::Link::SharingPolicy::NONLINEAR,
129 std::bind(&link_nonlinear, link->get_link_down(), std::placeholders::_1, std::placeholders::_2));
130 link->set_latency(10e-6)->seal();
132 /* create routes between nodes */
133 zone->add_route(sender->get_netpoint(), receiver->get_netpoint(), nullptr, nullptr,
134 {{link, sg4::LinkInRoute::Direction::UP}}, true);
137 /* create actors Sender/Receiver */
138 sg4::Actor::create("receiver", receiver, Receiver(9));
139 sg4::Actor::create("sender", sender, Sender(9));
142 /*************************************************************************************************/
143 int main(int argc, char* argv[])
145 sg4::Engine e(&argc, argv);
147 /* create platform */
150 /* runs the simulation */