Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make single argument constructor LinkInRoute::LinkInRoute(const Link*) explicit.
[simgrid.git] / examples / cpp / plugin-prodcons / s4u-plugin-prodcons.cpp
1 /* Copyright (c) 2007-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 #include <simgrid/plugins/ProducerConsumer.hpp>
7 #include <simgrid/s4u/Actor.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <simgrid/s4u/Host.hpp>
10 #include <xbt/random.hpp>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
13
14 namespace sg4 = simgrid::s4u;
15
16 static void ingester(int id, simgrid::plugin::ProducerConsumerPtr<int> pc)
17 {
18   sg4::this_actor::sleep_for(simgrid::xbt::random::uniform_real(0, 1));
19   for (int i = 0; i < 3; i++) {
20     auto* data = new int(10 * id + i);
21     pc->put(data, 1.2125e6); // last for 0.01s
22     XBT_INFO("data sucessfully put: %d", *data);
23     sg4::this_actor::sleep_for((3 - i) * simgrid::xbt::random::uniform_real(0, 1));
24   }
25
26   for (int i = 0; i < 3; i++) {
27     auto* data = new int(10 * id + i);
28     pc->put_async(data, 1.2125e6); // last for 0.01s
29     XBT_INFO("data sucessfully put: %d", *data);
30     sg4::this_actor::sleep_for((i + 3) * simgrid::xbt::random::uniform_real(0, 1));
31   }
32 }
33
34 static void retriever(simgrid::plugin::ProducerConsumerPtr<int> pc)
35 {
36   sg4::this_actor::sleep_for(simgrid::xbt::random::uniform_real(0, 1));
37   for (int i = 0; i < 3; i++) {
38     int* data;
39     sg4::CommPtr comm = pc->get_async(&data);
40     comm->wait();
41     XBT_INFO("data sucessfully get: %d", *data);
42     delete data;
43     sg4::this_actor::sleep_for((i + 3) * simgrid::xbt::random::uniform_real(0, 1));
44   }
45
46   for (int i = 0; i < 3; i++) {
47     int* data = pc->get();
48     XBT_INFO("data sucessfully get: %d", *data);
49     delete data;
50     sg4::this_actor::sleep_for((3 - i) * simgrid::xbt::random::uniform_real(0, 1));
51   }
52 }
53
54 int main(int argc, char* argv[])
55 {
56   sg4::Engine e(&argc, argv);
57
58   // Platform creation
59   auto* cluster = sg4::create_star_zone("cluster");
60   for (int i = 0; i < 8; i++) {
61     std::string hostname = std::string("node-") + std::to_string(i) + ".simgrid.org";
62
63     const auto* host = cluster->create_host(hostname, "1Gf");
64
65     std::string linkname = std::string("cluster") + "_link_" + std::to_string(i);
66     const auto* link     = cluster->create_split_duplex_link(linkname, "1Gbps");
67
68     cluster->add_route(host->get_netpoint(), nullptr, nullptr, nullptr, {{link, sg4::LinkInRoute::Direction::UP}},
69                        true);
70   }
71
72   auto* router = cluster->create_router("cluster_router");
73   cluster->add_route(router, nullptr, nullptr, nullptr, {});
74
75   simgrid::plugin::ProducerConsumerPtr<int> pc = simgrid::plugin::ProducerConsumer<int>::create(2);
76
77   XBT_INFO("Maximum number of queued data is %u", pc->get_max_queue_size());
78   XBT_INFO("Transfers are done in %s mode", pc->get_transfer_mode().c_str());
79
80   for (int i = 0; i < 3; i++) {
81     std::string hostname = std::string("node-") + std::to_string(i) + ".simgrid.org";
82     sg4::Actor::create("ingester-" + std::to_string(i), sg4::Host::by_name(hostname), &ingester, i, pc);
83
84     hostname = std::string("node-") + std::to_string(i + 3) + ".simgrid.org";
85     sg4::Actor::create("retriever-" + std::to_string(i), sg4::Host::by_name(hostname), &retriever, pc);
86   }
87
88   e.run();
89
90   return 0;
91 }