Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add test for issue105
[simgrid.git] / teshsuite / models / issue105 / issue105.cpp
1 /* Copyright (c) 2013-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 /**
7  * Test for issue105: https://framagit.org/simgrid/simgrid/-/issues/105
8  */
9
10 #include <simgrid/kernel/ProfileBuilder.hpp>
11 #include <simgrid/s4u.hpp>
12 #include <xbt/log.h>
13 namespace sg4 = simgrid::s4u;
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(issue105, "Issue105");
16 static void load_generator(sg4::Mailbox* mailbox)
17 {
18   std::vector<sg4::CommPtr> comms;
19
20   // Send the task messages
21   for (int i = 0; i < 100; i++) {
22     auto* payload     = new int(i);
23     sg4::CommPtr comm = mailbox->put_async(payload, 1024);
24     comms.push_back(comm);
25     sg4::this_actor::sleep_for(1.0);
26   }
27
28   // send shutdown messages
29   auto* payload     = new int(-1);
30   sg4::CommPtr comm = mailbox->put_async(payload, 1024);
31   XBT_INFO("Sent shutdown");
32   comms.push_back(comm);
33
34   // Wait for all messages to be consumed before ending the simulation
35   sg4::Comm::wait_all(comms);
36   XBT_INFO("Load generator finished");
37 }
38
39 static void receiver(sg4::Mailbox* mailbox)
40 {
41   bool shutdown = false;
42   while (not shutdown) {
43     auto msg = mailbox->get_unique<int>();
44     if (*msg >= 0) {
45       XBT_INFO("Started Task: %d", *msg);
46       sg4::this_actor::execute(1e9);
47     } else {
48       shutdown = true;
49     }
50   }
51   XBT_INFO("Receiver finished");
52 }
53
54 int main(int argc, char* argv[])
55 {
56   sg4::Engine e(&argc, argv);
57
58   sg4::NetZone* world = sg4::create_full_zone("world");
59   sg4::Host* hostGl01 = world->create_host("host-gl01", "98Mf")->seal();
60   sg4::Host* hostSa01 = world->create_host("host-sa01", "98Mf")->seal();
61
62   // create latency and bandwidth profiles
63   auto* linkSaLatencyProfile   = simgrid::kernel::profile::ProfileBuilder::from_string("link-sa-latency-profile",
64                                                                                      "0 0.100\n"
65                                                                                      "0.102 0.00002\n",
66                                                                                      60);
67   auto* linkSaBandwidthProfile = simgrid::kernel::profile::ProfileBuilder::from_string("link-sa-bandwidth-profile",
68                                                                                        "0 42000000\n"
69                                                                                        "1 200\n"
70                                                                                        "100 42000000\n",
71                                                                                        150);
72   const sg4::Link* linkSa      = world->create_link("link-front-sa", "42MBps")
73                                 ->set_latency("20ms")
74                                 ->set_latency_profile(linkSaLatencyProfile)
75                                 ->set_bandwidth_profile(linkSaBandwidthProfile)
76                                 ->seal();
77
78   world->add_route(hostGl01->get_netpoint(), hostSa01->get_netpoint(), nullptr, nullptr,
79                    {{linkSa, sg4::LinkInRoute::Direction::NONE}}, true);
80   world->seal();
81
82   sg4::Mailbox* mb1 = e.mailbox_by_name_or_create("Mailbox 1");
83   sg4::Actor::create("load-generator", hostGl01, load_generator, mb1);
84   sg4::Actor::create("cluster-node-sa01", hostSa01, receiver, mb1);
85
86   e.run();
87
88   XBT_INFO("Total simulation time: %.3f", e.get_clock());
89   return 0;
90 }