Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / teshsuite / s4u / seal-platform / seal-platform.cpp
1 /* Copyright (c) 2010-2022. 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/s4u.hpp"
7 namespace sg4 = simgrid::s4u;
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_torus_multicpu, "Messages specific for this s4u example");
10
11 class Sender {
12   long msg_size = 1e6; /* message size in bytes */
13   std::vector<sg4::Host*> hosts_;
14
15 public:
16   explicit Sender(const std::vector<sg4::Host*>& hosts) : hosts_{hosts} {}
17   void operator()() const
18   {
19     /* Vector in which we store all ongoing communications */
20     std::vector<sg4::CommPtr> pending_comms;
21     /* Make a vector of the mailboxes to use */
22     std::vector<sg4::Mailbox*> mboxes;
23
24     std::string msg_content =
25         std::string("Hello, I'm alive and running on ") + std::string(sg4::this_actor::get_host()->get_name());
26     for (const auto* host : hosts_) {
27       auto* payload = new std::string(msg_content);
28       /* Create a communication representing the ongoing communication, and store it in pending_comms */
29       auto mbox = sg4::Mailbox::by_name(host->get_name());
30       mboxes.push_back(mbox);
31       sg4::CommPtr comm = mbox->put_async(payload, msg_size);
32       pending_comms.push_back(comm);
33     }
34
35     XBT_INFO("Done dispatching all messages");
36
37     /* Now that all message exchanges were initiated, wait for their completion in one single call */
38     sg4::Comm::wait_all(pending_comms);
39
40     XBT_INFO("Goodbye now!");
41   }
42 };
43
44 /* Receiver actor: wait for 1 message on the mailbox identified by the hostname */
45 class Receiver {
46 public:
47   void operator()() const
48   {
49     auto mbox     = sg4::Mailbox::by_name(sg4::this_actor::get_host()->get_name());
50     auto comm     = mbox->get_init();
51     auto received = mbox->get_unique<std::string>();
52     XBT_INFO("I got a '%s'.", received->c_str());
53
54     const sg4::Disk* disk = sg4::Host::current()->get_disks().front();
55     sg_size_t write_size  = disk->write(4e6);
56     XBT_INFO("Wrote %llu bytes on '%s'", write_size, disk->get_cname());
57   }
58 };
59
60 /*************************************************************************************************/
61 static sg4::NetZone* create_zone(const sg4::NetZone* root, const std::string& id)
62 {
63   auto* zone = sg4::create_floyd_zone(id);
64   zone->set_parent(root);
65   constexpr int n_host = 2;
66
67   auto* router = zone->create_router("router" + id);
68   for (int i = 0; i < n_host; i++) {
69     std::string hostname = id + "-cpu-" + std::to_string(i);
70     auto* host           = zone->create_host(hostname, 1e9);
71     host->create_disk("disk-" + hostname, 1e9, 1e6);
72     const auto* link = zone->create_link("link-" + hostname, 1e9);
73     zone->add_route(host->get_netpoint(), router, nullptr, nullptr, {sg4::LinkInRoute(link)});
74   }
75   return zone;
76 }
77
78 /*************************************************************************************************/
79
80 int main(int argc, char* argv[])
81 {
82   sg4::Engine e(&argc, argv);
83
84   /* create platform: intentionally do not do the seal of objects */
85   auto* root  = sg4::create_full_zone("root");
86   auto* zoneA = create_zone(root, "A");
87   auto* zoneB = create_zone(root, "B");
88   const auto* link = root->create_link("root-link", 1e10);
89   root->add_route(zoneA->get_netpoint(), zoneB->get_netpoint(), e.netpoint_by_name("routerA"),
90                   e.netpoint_by_name("routerB"), {sg4::LinkInRoute(link)});
91
92   std::vector<sg4::Host*> host_list = e.get_all_hosts();
93   /* create the sender actor running on first host */
94   sg4::Actor::create("sender", host_list[0], Sender(host_list));
95   /* create receiver in every host */
96   for (auto* host : host_list) {
97     sg4::Actor::create(std::string("receiver-") + std::string(host->get_name()), host, Receiver());
98   }
99
100   /* runs the simulation */
101   e.run();
102
103   return 0;
104 }